Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Add this below any Tradingview indicator/strategy and fill in trigger variables.
- //Adds stop loss, trailing stops, direction selection, backtesting time selection.
- //This script snippet also performs position tracking for Autoview alerts.
- //Now alerts only fire if it is the first entry condition before an exit condition, or so on with pyramiding.
- // === Upgraded Conditions Framework ===
- ////////////////////////////////////////////////////////////////////////////
- long_entry = //Long Or Buy Condition Here
- short_entry = //Short Or Sell Condition Here
- long_exit = short_entry //Close Long Condition Here (Optional)
- short_exit = long_entry //Close Short Condition Here (Optional)
- ///////////////////////////////////////////////////////////////////////////
- // init these values here, they will get updated later as more decisions are made
- last_long_close = na
- last_short_close = na
- // === Long position detection ===
- // longs open
- longo = 0
- longo := nz(longo[1])
- // longs closed
- longc = 0
- longc := nz(longc[1])
- if long_entry
- longo := longo + 1
- longc := 0
- if long_exit
- longc := longc + 1
- longo := 0
- // === /END
- // === Short position detection ===
- shorto = 0
- shorto := nz(shorto[1])
- shortc = 0
- shortc := nz(shortc[1])
- if short_entry
- shorto := shorto + 1
- shortc := 0
- if short_exit
- shortc := shortc + 1
- shorto := 0
- // === /END
- // === Pyramiding Settings ===
- pyr = input(100, title="Pyramiding Setting")
- //pyr = 1
- longCondition = long_entry and longo <= pyr
- longX = long_exit and longc <= pyr
- shortCondition = short_entry and shorto <=pyr
- shortX = short_exit and shortc <=pyr
- // === /END
- // === Get Last Position Price ===
- last_open_longCondition = na
- last_open_shortCondition = na
- // last open prices
- last_open_longCondition := longCondition ? close : nz(last_open_longCondition[1])
- last_open_shortCondition := shortCondition ? close : nz(last_open_shortCondition[1])
- // === /END
- // === Check For Long/Short ===
- last_longCondition = na
- last_shortCondition = na
- // last open times
- last_longCondition := longCondition ? time : nz(last_longCondition[1])
- last_shortCondition := shortCondition ? time : nz(last_shortCondition[1])
- last_longClose = longX ? time : nz(last_long_close[1])
- last_shortClose = shortX ? time : nz(last_short_close[1])
- in_longCondition = last_longCondition > last_shortCondition and last_longCondition >= last_longClose
- in_shortCondition = last_shortCondition > last_longCondition and last_shortCondition >= last_shortClose
- // === /END
- // === Stop Loss (Long) ===
- isSLl = input(true, "Stop Loss (Long)")
- sll = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- long_call_sl = last_open_longCondition * (1 - sll)
- long_sl = isSLl and low <= long_call_sl and longCondition == 0
- // === /END
- // === Stop Loss (Short) ===
- isSLs = input(false, "Stop Loss (Short)")
- sls = input(6, "Stop Loss %", type=float, step=0.2, minval=0, maxval=100) / 100
- short_call_sl = last_open_shortCondition * (1 + sls)
- short_sl = isSLs and high >= short_call_sl and shortCondition == 0
- // === /END
- // === Trailing Stop ===
- last_high = na
- last_low = na
- last_high := in_longCondition ? (na(last_high[1]) or high > nz(last_high[1])) ? high : nz(last_high[1]) : na
- last_low := in_shortCondition ? (na(last_low[1]) or low < nz(last_low[1])) ? low : nz(last_low[1]) : na
- isTSl = input(true, "Trailing Stop Long")
- tsil = input(25, "Activate Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- tsl = input(8, "Trailing Stop % Long", type=float, step=1, minval=0, maxval=100) / 100
- long_call_ts = last_high * (1 - tsl)
- long_call_tsi = last_open_longCondition * (1 + tsil)
- long_ts = isTSl and not na(last_high) and low <= long_call_ts and longCondition == 0 and last_high >= long_call_tsi
- isTSs = input(true, "Trailing Stop Short")
- tsis = input(25, "Activate Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- tss = input(8, "Trailing Stop % Short", type=float, step=1, minval=0, maxval=100) / 100
- short_call_ts = last_low * (1 + tss)
- short_call_tsi = last_open_shortCondition * (1 - tsis)
- short_ts = isTSs and not na(last_low) and high >= short_call_ts and shortCondition == 0 and last_low <= short_call_tsi
- // === /END
- // === Create Single Close For All Closing Conditions ===
- closelong = long_sl or long_ts or longX
- closeshort = short_sl or short_ts or shortX
- // Get Last Close
- last_long_close := closelong ? time : nz(last_long_close[1])
- last_short_close := closeshort ? time : nz(last_short_close[1])
- // Check For Close Since Last Open
- if closelong and last_long_close[1] > last_longCondition
- closelong := 0
- if closeshort and last_short_close[1] > last_shortCondition
- closeshort := 0
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Alarm Settings ===
- alertcondition(longCondition==1, title='LONG', message='LONG')
- alertcondition(closelong==1, title='EXIT LONG', message='EXIT LONG')
- alertcondition(shortCondition==1, title='SHORT', message='SHORT')
- alertcondition(closeshort==1, title='EXIT SHORT', message='EXIT SHORT')
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // === Visuals & Debugs Here ===
- //Remove "//" To Check/Debug The Code Above
- // Signal Shapes
- //plotshape(longCondition[1]==1, title='LONG', style=shape.triangleup, size=size.large, color=#02CB80, location= location.belowbar)
- //plotshape(shortCondition[1]==1, title='SHORT', style=shape.triangledown, size=size.large, color=#DC143C, location=location.abovebar)
- //plotshape(shortCondition[1]==0 and closelong[1]==1, title='EXIT LONG', style=shape.xcross, color=#02CB80, location=location.belowbar, transp=0)
- //plotshape(longCondition[1]==0 and closeshort[1]==1, title='EXIT SHORT', style=shape.xcross, color=#DC143C, location=location.abovebar, transp=0)
- // SL Plot
- //slColor = (isSLl or isSLs) and (in_longCondition or in_shortCondition) ? red : white
- //plot(isSLl and in_longCondition ? long_call_sl : na, "Long SL", slColor, style=3, linewidth=2)
- //plot(isSLs and in_shortCondition ? short_call_sl : na, "Short SL", slColor, style=3, linewidth=2)
- // TP Plot
- //tpColor = isTP and (in_longCondition or in_shortCondition) ? purple : white
- //plot(isTP and in_longCondition ? long_call_tp : na, "Long TP", tpColor, style=3, linewidth=2)
- //plot(isTP and in_shortCondition ? short_call_tp : na, "Short TP", tpColor, style=3, linewidth=2)
- // TS Plot
- //tsColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? orange : white
- //tsiColor = (isTSl or isTSs) and (in_longCondition or in_shortCondition) ? white : orange
- //plot(isTSl and in_longCondition ? long_call_tsi : na, "Long Trailing", tsiColor, style=3, linewidth=2)
- //plot(isTSs and in_shortCondition ? short_call_tsi : na, "Short Trailing", tsiColor, style=3, linewidth=2)
- //plot(isTSl and in_longCondition and last_high > long_call_tsi ? long_call_ts : na, "Long Trailing", tsColor, style=2, linewidth=2)
- //plot(isTSs and in_shortCondition and last_low < short_call_tsi ? short_call_ts : na, "Short Trailing", tsColor, style=2, linewidth=2)
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // REMOVE THE CODE BELOW FOR STUDY CONVERSION //
- // //
- ////////////////////////////////////////////////////////////////////////////
- // === Strategy Direction Switch ===
- direction = input(title = "Strategy Direction", defval="Long", options=["Both", "Long", "Short"])
- // === /END
- // === Backtesting Dates ===
- testPeriodSwitch = input(false, "Custom Backtesting Dates")
- testStartYear = input(2017, "Backtest Start Year")
- testStartMonth = input(8, "Backtest Start Month")
- testStartDay = input(1, "Backtest Start Day")
- testPeriodStart = timestamp(testStartYear,testStartMonth,testStartDay,0,0)
- testStopYear = input(2018, "Backtest Stop Year")
- testStopMonth = input(1, "Backtest Stop Month")
- testStopDay = input(14, "Backtest Stop Day")
- testPeriodStop = timestamp(testStopYear,testStopMonth,testStopDay,0,0)
- testPeriod() =>
- time >= testPeriodStart and time <= testPeriodStop ? true : false
- isPeriod = testPeriodSwitch == true ? testPeriod() : true
- // === /END
- // === Strategy ===
- if isPeriod and direction=="Both"
- if (longCondition)
- strategy.entry("Long",strategy.long)
- if (closelong) and not shortCondition
- strategy.close("Long")
- if (shortCondition)
- strategy.entry("Short",strategy.short)
- if (closeshort) and not longCondition
- strategy.close("Short")
- if isPeriod and direction=="Long"
- if (longCondition)
- strategy.entry("Long",strategy.long)
- if (closelong)
- strategy.close("Long")
- if isPeriod and direction=="Short"
- if (shortCondition)
- strategy.entry("Short",strategy.short)
- if (closeshort)
- strategy.close("Short")
- // === /END
- ////////////////////////////////////////////////////////////////////////////
- // //
- // ULTIMATE PINE INJECTOR V1.2 //
- // //
- //////////////////////===ANION=CODE=END====/////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement