Pine Script v4 · Strategy
SNAP BACK 2.0 Strategy
A Pine Script v4 strategy built on EMA (close, 8) and SMA (close, 20). It has 46 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 5% of equity and pays no commission. 25 settings; defaults include Price Source close, Aggressive = 0, Conservative = 1 0 and Gap% between 8EMA & 20SMA 0.15.
Generated from the script's code by a template: every clause is a fact read from the source. Not written by a person or a model, and not taken from the author's description.
- Does SNAP BACK 2.0 Strategy repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 11 of 11 trades.
- How did SNAP BACK 2.0 Strategy do on BTCUSDT 1D with default settings?
- +0.09%
- From 26 Jan 2024 to 21 Oct 2026, over 32 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can SNAP BACK 2.0 Strategy be converted to Pine Script v5?
- Yes
- The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
- Does SNAP BACK 2.0 Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 140, 141, 143, 145, 146, 148, 149, 151, 153, 155, 156, 158, 159, 161, 163, 165, 166, 168, 170, 172, 174, 176, 180, 181, 183, 185, 186, 188, 189, 191, 193, 195, 196, 198, 199, 201, 203, 205, 206, 208, 210, 212, 214, 216.
- Does SNAP BACK 2.0 Strategy run in Zeal Terminal?
- Yes
- It compiles in 75 ms and runs 1,000 bars in 133 ms, in the browser.
BTCUSDT · 1D · default settings
- SH EMA
- MD EMA
- SH SMA
- MD SMA
- LG SMA
- HC-LB
- HC-SB
- Long Trail Stop
- Short Trail Stop
- Short Trail Stop
- Short Trail Stop
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +0.09%
- $17.06
- Buy and hold
- +102.5%
- same period
- Closed trades
- 32
- 16 won · 16 lost
- Profitable
- 50.0%
- of closed trades
- Profit factor
- 1.09
- gross profit ÷ gross loss
- Max drawdown
- 0.49%
- −$97.87
One run with the author's default settings, $20,000 starting capital, commission as the script sets it. A single backtest says little about future results and is not a recommendation. Backtests are simulations on historical data and are not investment advice.
Review
What a careful reader of this script would point out, found by checking the source and the backtest.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
Uses an argument v6 removed
when = on line 95, 99, 140, 141, 143, 145, 146, 148, 149, 151, 153, 155, 156, 158, 159, 161, 163, 165, 166, 168, 170, 172, 174, 176, 180, 181, 183, 185, 186, 188, 189, 191, 193, 195, 196, 198, 199, 201, 203, 205, 206, 208, 210, 212, 214, 216. The upgraded version below moves each call inside an if block.
-
Note
Uses iff(), which v5 removed line 70
iff() on line 70, 73, 75, 77, 80, 83, 83, 83, 85, 85, 85, 88, 90, 93, 97. v5 writes it as the ternary c ? a : b.
-
Note
Uses transp =, which v5 removed line 58
transp = on line 58, 66. v5 folds the transparency into the colour with color.new(c, transp).
Upgrade and export
Written in Pine Script v4. The upgrade to v5 takes 1 step, one per version, each applied to the result of the one before. A last clean-v5 pass then types the settings and moves when = into if blocks, which v6 requires. Every step runs before and after on the same candles in Zeal Terminal's engine; a step whose results differ is shown but not offered as a download. Upgrades preserve behaviour: bugs are fixed in the Review, never silently here.
- Upgrading to v5 renames ema → ta.ema, sma → ta.sma, abs → math.abs, crossover → ta.crossover, crossunder → ta.crossunder, max → math.max, min → math.min.
- 46 strategy calls use the when = argument (line 95, 99, 140, 141, 143, 145, 146, 148, 149, 151, 153, 155, 156, 158, 159, 161, 163, 165, 166, 168, 170, 172, 174, 176, 180, 181, 183, 185, 186, 188, 189, 191, 193, 195, 196, 198, 199, 201, 203, 205, 206, 208, 210, 212, 214, 216). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 32 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 39: ema → ta.ema
- Line 40: ema → ta.ema
- Line 41: sma → ta.sma
- Line 42: sma → ta.sma
- Line 43: sma → ta.sma
34 more edits
- Line 46: input(type = input.integer) → input.int()
- Line 47: input(type = input.integer) → input.int()
- Line 48: input(type = input.integer) → input.int()
- Line 49: input(type = input.integer) → input.int()
- Line 50: input(type = input.integer) → input.int()
- Line 51: input(type = input.integer) → input.int()
- Line 53: input(type = input.bool) → input.bool()
- Line 58: transp = 90 → color.new(showDate and window() ? color.gray : na, 90)
- Line 66: transp = 70 → color.new(color.red, 70)
- Line 70: iff(c, a, b) → (c ? a : b)
- Line 73: iff(c, a, b) → (c ? a : b)
- Line 75: iff(c, a, b) → (c ? a : b)
- Line 75: abs → math.abs
- Line 77: iff(c, a, b) → (c ? a : b)
- Line 80: iff(c, a, b) → (c ? a : b)
- Line 83: iff(c, a, b) → (c ? a : b)
- Line 83: iff(c, a, b) → (c ? a : b)
- Line 83: crossover → ta.crossover
- Line 83: iff(c, a, b) → (c ? a : b)
- Line 83: crossover → ta.crossover
- Line 85: iff(c, a, b) → (c ? a : b)
- Line 85: iff(c, a, b) → (c ? a : b)
- Line 85: crossunder → ta.crossunder
- Line 85: iff(c, a, b) → (c ? a : b)
- Line 85: crossunder → ta.crossunder
- Line 88: iff(c, a, b) → (c ? a : b)
- Line 90: iff(c, a, b) → (c ? a : b)
- Line 93: iff(c, a, b) → (c ? a : b)
- Line 97: iff(c, a, b) → (c ? a : b)
- Line 108: input(type = input.float) → input.float()
- Line 109: input(type = input.float) → input.float()
- Line 110: input(type = input.float) → input.float()
- Line 121: max → math.max
- Line 128: min → math.min
-//@version=4+//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯LG_SMA_length= input(title="LG SMA Length", defval=200) //long SMA length-SH_EMA=ema(price, SH_EMA_length) //short EMA +SH_EMA=ta.ema(price, SH_EMA_length) //short EMA -MD_EMA=ema(price, MD_EMA_length) //medium EMA+MD_EMA=ta.ema(price, MD_EMA_length) //medium EMA-SH_SMA=sma(price, SH_SMA_length) //short SMA +SH_SMA=ta.sma(price, SH_SMA_length) //short SMA -MD_SMA=sma(price, MD_SMA_length) //medium SMA+MD_SMA=ta.sma(price, MD_SMA_length) //medium SMA-LG_SMA=sma(price, LG_SMA_length) //long SMA+LG_SMA=ta.sma(price, LG_SMA_length) //long SMA// 2. Perform Calculations*************************************************************************************************************************************************************************// ************************************ INPUT BACKTEST RANGE ******************************************=== coutesy of alanaster-fromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)+fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-fromDay = input(defval = 1, title = "From Day", type = input.integer, minval = 1, maxval = 31)+fromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-fromYear = input(defval = 2022, title = "From Year", type = input.integer, minval = 1970)+fromYear = input.int(defval = 2022, title = "From Year", minval = 1970)-thruMonth = input(defval = 1, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)+thruMonth = input.int(defval = 1, title = "Thru Month", minval = 1, maxval = 12)-thruDay = input(defval = 1, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)+thruDay = input.int(defval = 1, title = "Thru Day", minval = 1, maxval = 31)-thruYear = input(defval = 2112, title = "Thru Year", type = input.integer, minval = 1970)+thruYear = input.int(defval = 2112, title = "Thru Year", minval = 1970)// === INPUT SHOW PLOT ===-showDate = input(defval = true, title = "Show Date Range", type = input.bool)+showDate = input.bool(defval = true, title = "Show Date Range")// === FUNCTION EXAMPLE ===⋯window() => time >= start and time <= finish ? true : false // create function "within window of time"-bgcolor(color = showDate and window() ? color.gray : na, transp = 90) +bgcolor(color = color.new(showDate and window() ? color.gray : na, 90)) // 3. Display Chart Information*************************************************************************************************************************************************************************⋯plot(MD_SMA, title = "MD SMA", color = color.orange)-plot(LG_SMA, title = "LG SMA", color = color.red, linewidth = 4, transp = 70)+plot(LG_SMA, title = "LG SMA", color = color.new(color.red, 70), linewidth = 4)// 4. Determine Entry Conditions*************************************************************************************************************************************************************************⋯SB_RB_Up= false //SB_RB_Up = Snap Back RainBow for an Uptrend Swing-SB_RB_Up:= iff(LG_SMA>MD_SMA and MD_SMA>MD_EMA and MD_EMA>SH_SMA and SH_SMA>SH_EMA, true, false) //is the 200SMA > 50 SMA > 34EMA > 20SMA > 8EMA +SB_RB_Up:= (LG_SMA>MD_SMA and MD_SMA>MD_EMA and MD_EMA>SH_SMA and SH_SMA>SH_EMA ? true : false) //is the 200SMA > 50 SMA > 34EMA > 20SMA > 8EMA // plotshape(SB_RB, title= "SB_RB", color=color.black, style=shape.cross, text="Rainbow") //for testing onlySB_RB_DWN= false //SB_RB_DWN = Snap Back RainBow for a Downtrend Swing-SB_RB_DWN:= iff(LG_SMA<MD_SMA and MD_SMA<MD_EMA and MD_EMA<SH_SMA and SH_SMA<SH_EMA, true, false) //is the 200SMA < 50 SMA < 34EMA < 20SMA < 8EMA +SB_RB_DWN:= (LG_SMA<MD_SMA and MD_SMA<MD_EMA and MD_EMA<SH_SMA and SH_SMA<SH_EMA ? true : false) //is the 200SMA < 50 SMA < 34EMA < 20SMA < 8EMA SB_Gap=false-SB_Gap:= iff(abs(SH_SMA-SH_EMA)>(price*GAP), true, false) //is there a greater than "GAP"% of the price gap between the 8EMA and 20SMA+SB_Gap:= (math.abs(SH_SMA-SH_EMA)>(price*GAP) ? true : false) //is there a greater than "GAP"% of the price gap between the 8EMA and 20SMASB_SetUp_Up=false-SB_SetUp_Up:= iff(SB_RB_Up and SB_Gap, true, false)//Uptrend Setup both conditions must be true+SB_SetUp_Up:= (SB_RB_Up and SB_Gap ? true : false)//Uptrend Setup both conditions must be true//plotshape(SB_SetUp, title= "SB_SetUp", color=color.white, style=shape.diamond, text="Set Up") //for testingSB_SetUp_DWN=false-SB_SetUp_DWN:= iff(SB_RB_DWN and SB_Gap, true, false)//Downtrend Setup both conditions must be true+SB_SetUp_DWN:= (SB_RB_DWN and SB_Gap ? true : false)//Downtrend Setup both conditions must be true//Determine trigger (TGR) for entrySB_TGR_Up=false-SB_TGR_Up:= iff(iff(aggro==0, crossover(price, SH_EMA), true) and iff(aggro==1, crossover(price[aggro],SH_EMA) and price>open[aggro], true), true, false) //if the price crosses over the 8EMA that is our entry signal, aggro determines how aggressively we enter the position (wait for a confirmaiton bar or not)+SB_TGR_Up:= ((aggro==0 ? ta.crossover(price, SH_EMA) : true) and (aggro==1 ? ta.crossover(price[aggro],SH_EMA) and price>open[aggro] : true) ? true : false) //if the price crosses over the 8EMA that is our entry signal, aggro determines how aggressively we enter the position (wait for a confirmaiton bar or not)SB_TGR_DWN=false-SB_TGR_DWN:= iff(iff(aggro==0, crossunder(price, SH_EMA), true) and iff(aggro==1, crossunder(price[aggro],SH_EMA) and price<open[aggro], true), true, false) //if the price crosses under the 8EMA that is our entry signal, aggro determines how aggressively we enter the position (wait for a confirmaiton bar or not)+SB_TGR_DWN:= ((aggro==0 ? ta.crossunder(price, SH_EMA) : true) and (aggro==1 ? ta.crossunder(price[aggro],SH_EMA) and price<open[aggro] : true) ? true : false) //if the price crosses under the 8EMA that is our entry signal, aggro determines how aggressively we enter the position (wait for a confirmaiton bar or not)//Determine when to run the strategy based on user input for uptrend or downtrendRunTrendUp=false //Varibile for running the Strategy in an UpTrend-RunTrendUp:= iff(RunTrend == "Up" or RunTrend == "Both", true, false)+RunTrendUp:= (RunTrend == "Up" or RunTrend == "Both" ? true : false)RunTrendDWN=false //Varibile for running the Strategy in a DownTrend-RunTrendDWN:= iff(RunTrend == "Down" or RunTrend == "Both", true, false)+RunTrendDWN:= (RunTrend == "Down" or RunTrend == "Both" ? true : false)//Determine full buy conditionsMAbuy=false//long entry variable-MAbuy := iff(SB_SetUp_Up and SB_TGR_Up and RunTrendUp, true, false) //when both the setup, the trigger, and RunTrend are true return true+MAbuy := (SB_SetUp_Up and SB_TGR_Up and RunTrendUp ? true : false) //when both the setup, the trigger, and RunTrend are true return trueplotshape(MAbuy, title= "HC-LB", color=color.lime, style=shape.circle, text="HC-LB")⋯MAsell=false//short entry variable-MAsell := iff(SB_SetUp_DWN and SB_TGR_DWN and RunTrendDWN, true, false) //when both the setup, the trigger, and RunTrend are true return true+MAsell := (SB_SetUp_DWN and SB_TGR_DWN and RunTrendDWN ? true : false) //when both the setup, the trigger, and RunTrend are true return trueplotshape(MAsell, title= "HC-SB", color=color.purple, style=shape.circle, text="HC-SB")⋯// User Options to Change Inputs (%)-TrailPerc = input(title="Trail Loss %", type=input.float, minval=0, step=0.01, defval=0.45) /100+TrailPerc = input.float(title="Trail Loss %", minval=0, step=0.01, defval=0.45) /100-stopPer = input(0.5, title='Stop Loss %', type=input.float) / 100+stopPer = input.float(0.5, title='Stop Loss %') / 100-takePer = input(0.5, title='Take Profit %', type=input.float) / 100+takePer = input.float(0.5, title='Take Profit %') / 100//Percent of SH_EMA to use for StopLoss⋯ stopValue = close * (1 - TrailPerc) - max(stopValue, longStopPrice[1])+ math.max(stopValue, longStopPrice[1])else⋯ shortStopValue = close * (1 + TrailPerc) - min(shortStopValue, shortStopPrice[1])+ math.min(shortStopValue, shortStopPrice[1])else⋯Same 32 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 26: input() → input.source()
- Line 28: input() → input.int()
- Line 30: input() → input.float()
- Line 32: input() → input.string()
- Line 34: input() → input.int()
- Line 35: input() → input.int()
55 more edits
- Line 36: input() → input.int()
- Line 37: input() → input.int()
- Line 38: input() → input.int()
- Line 95: when = MAbuy and window() → if block
- Line 99: when = MAsell and window() → if block
- Line 102: input() → input.string()
- Line 104: input() → input.string()
- Line 106: input() → input.string()
- Line 112: input() → input.int()
- Line 114: input() → input.int()
- Line 116: input() → input.int()
- Line 140: when = window() → if block
- Line 141: when = window() → if block
- Line 143: when = window() → if block
- Line 145: when = window() → if block
- Line 146: when = window() → if block
- Line 148: when = window() → if block
- Line 149: when = window() → if block
- Line 151: when = window() → if block
- Line 153: when = window() → if block
- Line 155: when = window() → if block
- Line 156: when = window() → if block
- Line 158: when = window() → if block
- Line 159: when = window() → if block
- Line 161: when = window() → if block
- Line 163: when = window() → if block
- Line 165: when = window() → if block
- Line 166: when = window() → if block
- Line 168: when = window() → if block
- Line 170: when = window() → if block
- Line 172: when = window() → if block
- Line 174: when = window() → if block
- Line 176: when = window() → if block
- Line 180: when = window() → if block
- Line 181: when = window() → if block
- Line 183: when = window() → if block
- Line 185: when = window() → if block
- Line 186: when = window() → if block
- Line 188: when = window() → if block
- Line 189: when = window() → if block
- Line 191: when = window() → if block
- Line 193: when = window() → if block
- Line 195: when = window() → if block
- Line 196: when = window() → if block
- Line 198: when = window() → if block
- Line 199: when = window() → if block
- Line 201: when = window() → if block
- Line 203: when = window() → if block
- Line 205: when = window() → if block
- Line 206: when = window() → if block
- Line 208: when = window() → if block
- Line 210: when = window() → if block
- Line 212: when = window() → if block
- Line 214: when = window() → if block
- Line 216: when = window() → if block
⋯//Define calculations price source-price = input(title="Price Source", defval=close)+price = input.source(title="Price Source", defval=close)//Define Trade Agression Level-aggro=input(title="Aggressive = 0, Conservative = 1", defval=0, options=[0, 1])+aggro=input.int(title="Aggressive = 0, Conservative = 1", defval=0, options=[0, 1])//Define Gap percentage allowed between 8EMA and 20SMA-GAP=input(title="Gap% between 8EMA & 20SMA", defval=0.15, minval=0, maxval=25, step=.01)/100+GAP=input.float(title="Gap% between 8EMA & 20SMA", defval=0.15, minval=0, maxval=25, step=.01)/100//Does user want to run the Strategy for Trending Up or Trending Down-RunTrend=input(title="Run Strategy Trending Up, Down, or Both", defval="Both", options=["Up", "Down", "Both"])+RunTrend=input.string(title="Run Strategy Trending Up, Down, or Both", defval="Both", options=["Up", "Down", "Both"])//Initialize 8/34EMA 20/50/200/200SMA -SH_EMA_length= input(title="SH EMA Length", defval=8) //short EMA length+SH_EMA_length= input.int(title="SH EMA Length", defval=8) //short EMA length-MD_EMA_length= input(title="MD EMA Length", defval=34) //medium EMA length+MD_EMA_length= input.int(title="MD EMA Length", defval=34) //medium EMA length-SH_SMA_length= input(title="SH SMA Length", defval=20) //short SMA length+SH_SMA_length= input.int(title="SH SMA Length", defval=20) //short SMA length-MD_SMA_length= input(title="MD SMA Length", defval=50) //medium SMA length+MD_SMA_length= input.int(title="MD SMA Length", defval=50) //medium SMA length-LG_SMA_length= input(title="LG SMA Length", defval=200) //long SMA length+LG_SMA_length= input.int(title="LG SMA Length", defval=200) //long SMA lengthSH_EMA=ta.ema(price, SH_EMA_length) //short EMA ⋯plotshape(MAbuy, title= "HC-LB", color=color.lime, style=shape.circle, text="HC-LB")-strategy.entry("HC-Long", strategy.long, comment="HC-Long", when = MAbuy and window())+if MAbuy and window()+ strategy.entry("HC-Long", strategy.long, comment="HC-Long")MAsell=false//short entry variable⋯plotshape(MAsell, title= "HC-SB", color=color.purple, style=shape.circle, text="HC-SB")-strategy.entry("HC-Short", strategy.short, comment="HC-Short", when = MAsell and window())+if MAsell and window()+ strategy.entry("HC-Short", strategy.short, comment="HC-Short")// 5. Submit Profit and Loss Exit Calculations Orders*************************************************************************************************************************************************************************//Stop Criteria-StpCri=input(title="Stop Criteria: SL, TRL, or SH_EMA", defval="TRL", options=["SL", "TRL", "SH_EMA"])+StpCri=input.string(title="Stop Criteria: SL, TRL, or SH_EMA", defval="TRL", options=["SL", "TRL", "SH_EMA"])//Profit Criteria-ProCri=input(title="Profit Criteria: TGT%, TRL, MD_EMA, or MD_SMA", defval="MD_EMA", options=["TGT%", "TRL", "MD_EMA", "MD_SMA"])+ProCri=input.string(title="Profit Criteria: TGT%, TRL, MD_EMA, or MD_SMA", defval="MD_EMA", options=["TGT%", "TRL", "MD_EMA", "MD_SMA"])//Does user want to run a Trailing Stop jointly-trailStop_YN=input(title="Run a Trailing Stop Jointly", defval="N", options=["Y", "N"])+trailStop_YN=input.string(title="Run a Trailing Stop Jointly", defval="N", options=["Y", "N"])// User Options to Change Inputs (%)⋯//Percent of SH_EMA to use for StopLoss-SH_EMA_percent = input(100, title="% of SH_EMA for Stop")/100+SH_EMA_percent = input.int(100, title="% of SH_EMA for Stop")/100//Percent of MD_EMA to use for Take Profit-MD_EMA_percent = input(100, title="% of MD_EMA for Profit")/100+MD_EMA_percent = input.int(100, title="% of MD_EMA for Profit")/100//Percent of MD_SMA to use for Take Profit-MD_SMA_percent = input(100, title="% of MD_SMA for Profit")/100+MD_SMA_percent = input.int(100, title="% of MD_SMA for Profit")/100//calculate Trail stop price for MD_EMA TGT% condition⋯ if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="Y"- strategy.exit(id="Close Long", when = window(), stop=longStop, limit=longTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Long", stop=longStop, limit=longTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss- strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Long Trailing Stop", stop=longStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="N"- strategy.exit(id="Close Long", when = window(), stop=longStop, limit=longTake)// sell when either the TGT or the SL is hit + if window()+ strategy.exit(id="Close Long", stop=longStop, limit=longTake)// sell when either the TGT or the SL is hit if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="Y"- strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_EMA_percent*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SL then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Long (50%)", stop=longStop, limit=MD_EMA_percent*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SL then transition to a trailing stop loss- strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Long Trailing Stop", stop=longStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="Y"- strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_SMA_percent*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SL then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Long (50%)", stop=longStop, limit=MD_SMA_percent*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SL then transition to a trailing stop loss- strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Long Trailing Stop", stop=longStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="N"- strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_EMA_percent*MD_EMA)// sell when MD_EMA or SL hit+ if window()+ strategy.exit(id="Close Long (50%)", stop=longStop, limit=MD_EMA_percent*MD_EMA)// sell when MD_EMA or SL hit if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="N"- strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_SMA_percent*MD_SMA)// sell when MD_SMA or SL hit+ if window()+ strategy.exit(id="Close Long (50%)", stop=longStop, limit=MD_SMA_percent*MD_SMA)// sell when MD_SMA or SL hit if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="Y"- strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_EMA_percent*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SH_EMA hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Long (50%)", stop=SH_EMA*SH_EMA_percent, limit=MD_EMA_percent*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SH_EMA hit then transition to a trailing stop loss- strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Long Trailing Stop", stop=longStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="Y"- strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_SMA_percent*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SH_EMA hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Long (50%)", stop=SH_EMA*SH_EMA_percent, limit=MD_SMA_percent*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SH_EMA hit then transition to a trailing stop loss- strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Long Trailing Stop", stop=longStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="N"- strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_EMA_percent*MD_EMA)// sell when MD_EMA or SH_EMA hit + if window()+ strategy.exit(id="Close Long (50%)", stop=SH_EMA*SH_EMA_percent, limit=MD_EMA_percent*MD_EMA)// sell when MD_EMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="N"- strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_SMA_percent*MD_SMA)// sell when MD_SMA or SH_EMA hit+ if window()+ strategy.exit(id="Close Long (50%)", stop=SH_EMA*SH_EMA_percent, limit=MD_SMA_percent*MD_SMA)// sell when MD_SMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="Y"- strategy.exit(id="Close Long", when = window(), stop=SH_EMA*SH_EMA_percent, limit=longTake, qty_percent=50)// sell 50% when either the TGT or the SH_EMA is hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Long", stop=SH_EMA*SH_EMA_percent, limit=longTake, qty_percent=50)// sell 50% when either the TGT or the SH_EMA is hit then transition to a trailing stop loss- strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Long Trailing Stop", stop=longStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="N"- strategy.exit(id="Close Long", when = window(), stop=SH_EMA*SH_EMA_percent, limit=longTake)// sell when either the TGT or the SH_EMA is hit+ if window()+ strategy.exit(id="Close Long", stop=SH_EMA*SH_EMA_percent, limit=longTake)// sell when either the TGT or the SH_EMA is hit if StpCri=="TRL" and ProCri=="TGT%" and trailStop_YN=="N"- strategy.exit(id="Close Long", when = window(), stop=longStopPrice, limit=longTake)// sell when either the TGT or the TRL is hit+ if window()+ strategy.exit(id="Close Long", stop=longStopPrice, limit=longTake)// sell when either the TGT or the TRL is hit if StpCri=="TRL" and ProCri=="MD_EMA" and trailStop_YN=="N"- strategy.exit(id="Close Long", when = window(), stop=longStopPrice, limit=MD_EMA_percent*MD_EMA)// sell when either the MD_EMA or the TRL is hit+ if window()+ strategy.exit(id="Close Long", stop=longStopPrice, limit=MD_EMA_percent*MD_EMA)// sell when either the MD_EMA or the TRL is hit if StpCri=="TRL" and ProCri=="MD_SMA" and trailStop_YN=="N"- strategy.exit(id="Close Long", when = window(), stop=longStopPrice, limit=MD_SMA_percent*MD_SMA)// sell when either the MD_SMA or the TRL is hit+ if window()+ strategy.exit(id="Close Long", stop=longStopPrice, limit=MD_SMA_percent*MD_SMA)// sell when either the MD_SMA or the TRL is hit if StpCri=="TRL" and ProCri=="TRL" and trailStop_YN=="N"- strategy.exit(id="Close Long", when = window(), stop=longStopPrice)// sell when the TRL is hit+ if window()+ strategy.exit(id="Close Long", stop=longStopPrice)// sell when the TRL is hit ⋯ if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="Y"- strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Short", stop=shortStop, limit=shortTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss- strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Short Trailing Stop", stop=shortStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="N"- strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake)// sell when either the TGT or the SL is hit+ if window()+ strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)// sell when either the TGT or the SL is hit if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="Y"- strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_EMA_percent)*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SL then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Short (50%)", stop=shortStop, limit=(2-MD_EMA_percent)*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SL then transition to a trailing stop loss- strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Short Trailing Stop", stop=shortStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="Y"- strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_SMA_percent)*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SL then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Short (50%)", stop=shortStop, limit=(2-MD_SMA_percent)*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SL then transition to a trailing stop loss- strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Short Trailing Stop", stop=shortStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="N"- strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_EMA_percent)*MD_EMA) // sell when MD_EMA or SL hit+ if window()+ strategy.exit(id="Close Short (50%)", stop=shortStop, limit=(2-MD_EMA_percent)*MD_EMA) // sell when MD_EMA or SL hit if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="N"- strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_SMA_percent)*MD_SMA) // sell when MD_SMA or SL hit+ if window()+ strategy.exit(id="Close Short (50%)", stop=shortStop, limit=(2-MD_SMA_percent)*MD_SMA) // sell when MD_SMA or SL hit if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="Y"- strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_EMA_percent)*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SH_EMA hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Short (50%)", stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_EMA_percent)*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SH_EMA hit then transition to a trailing stop loss- strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Short Trailing Stop", stop=shortStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="Y"- strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_SMA_percent)*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SH_EMA hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Short (50%)", stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_SMA_percent)*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SH_EMA hit then transition to a trailing stop loss- strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Short Trailing Stop", stop=shortStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="N"- strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_EMA_percent)*MD_EMA) // sell when MD_EMA or SH_EMA hit+ if window()+ strategy.exit(id="Close Short (50%)", stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_EMA_percent)*MD_EMA) // sell when MD_EMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="N"- strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_SMA_percent)*MD_SMA) // sell when MD_SMA or SH_EMA hit+ if window()+ strategy.exit(id="Close Short (50%)", stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_SMA_percent)*MD_SMA) // sell when MD_SMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="Y"- strategy.exit(id="Close Short", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=shortTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss+ if window()+ strategy.exit(id="Close Short", stop=SH_EMA*(2-SH_EMA_percent), limit=shortTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss- strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100)+ if window()+ strategy.exit(id="Close Short Trailing Stop", stop=shortStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="N"- strategy.exit(id="Close Short", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=shortTake)// sell when either the TGT or the SH_EMA is hit+ if window()+ strategy.exit(id="Close Short", stop=SH_EMA*(2-SH_EMA_percent), limit=shortTake)// sell when either the TGT or the SH_EMA is hit if StpCri=="TRL" and ProCri=="TGT%" and trailStop_YN=="N"- strategy.exit(id="Close Short", when = window(), stop=shortStopPrice, limit=shortTake)// sell when either the TGT or the TRL is hit+ if window()+ strategy.exit(id="Close Short", stop=shortStopPrice, limit=shortTake)// sell when either the TGT or the TRL is hit if StpCri=="TRL" and ProCri=="MD_EMA" and trailStop_YN=="N"- strategy.exit(id="Close Short", when = window(), stop=shortStopPrice, limit=(2-MD_EMA_percent)*MD_EMA) // sell when either the MD_EMA or the TRL is hit+ if window()+ strategy.exit(id="Close Short", stop=shortStopPrice, limit=(2-MD_EMA_percent)*MD_EMA) // sell when either the MD_EMA or the TRL is hit if StpCri=="TRL" and ProCri=="MD_SMA" and trailStop_YN=="N"- strategy.exit(id="Close Short", when = window(), stop=shortStopPrice, limit=(2-MD_SMA_percent)*MD_SMA) // sell when either the MD_SMA or the TRL is hit+ if window()+ strategy.exit(id="Close Short", stop=shortStopPrice, limit=(2-MD_SMA_percent)*MD_SMA) // sell when either the MD_SMA or the TRL is hit if StpCri=="TRL" and ProCri=="TRL" and trailStop_YN=="N"- strategy.exit(id="Close Short", when = window(), stop=shortStopPrice) // sell when the TRL is hit+ if window()+ strategy.exit(id="Close Short", stop=shortStopPrice) // sell when the TRL is hit// Plot stop trailing loss values for confirmation⋯Does it run in Pine Script v6?
Yes
Same 32 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯Settings
Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Price Source | close | source | SH_EMA MD_EMA SH_SMA MD_SMA LG_SMA SB_Gap SB_TGR_Up SB_TGR_DWN |
| Aggressive = 0, Conservative = 1 | 0 | int · options [0, 1] | SB_TGR_Up SB_TGR_DWN |
| Gap% between 8EMA & 20SMA | 0.15 | float · min 0 · max 25 · step .01 | SB_Gap |
| Run Strategy Trending Up, Down, or Both | Both | string · options ["Up", "Down", "Both"] | RunTrendUp RunTrendDWN |
| SH EMA Length | 8 | int | SH_EMA |
| MD EMA Length | 34 | int | MD_EMA |
| SH SMA Length | 20 | int | SH_SMA |
| MD SMA Length | 50 | int | MD_SMA |
| LG SMA Length | 200 | int | LG_SMA |
| From Month | 1 | int (written input.integer) · min 1 · max 12 | start |
| From Day | 1 | int (written input.integer) · min 1 · max 31 | start |
| From Year | 2022 | int (written input.integer) · min 1970 | start |
| Thru Month | 1 | int (written input.integer) · min 1 · max 12 | finish |
| Thru Day | 1 | int (written input.integer) · min 1 · max 31 | finish |
| Thru Year | 2112 | int (written input.integer) · min 1970 | finish |
| Show Date Range | true | bool | bgcolor |
| Stop Criteria: SL, TRL, or SH_EMA | TRL | string · options ["SL", "TRL", "SH_EMA"] | exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long (50%) exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long (50%) exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Long exit Close Long exit Close Long exit Close Long exit Close Short exit Close Short Trailing Stop exit Close Short exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short (50%) exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short (50%) exit Close Short exit Close Short Trailing Stop exit Close Short exit Close Short exit Close Short exit Close Short exit Close Short line 139 line 142 line 144 line 147 line 150 line 152 line 154 line 157 line 160 line 162 line 164 line 167 line 169 line 171 line 173 line 175 line 179 line 182 line 184 line 187 line 190 line 192 line 194 line 197 line 200 line 202 line 204 line 207 line 209 line 211 line 213 line 215 Long Trail Stop Short Trail Stop |
| Profit Criteria: TGT%, TRL, MD_EMA, or MD_SMA | MD_EMA | string · options ["TGT%", "TRL", "MD_EMA", "MD_SMA"] | exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long (50%) exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long (50%) exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Long exit Close Long exit Close Long exit Close Long exit Close Short exit Close Short Trailing Stop exit Close Short exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short (50%) exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short (50%) exit Close Short exit Close Short Trailing Stop exit Close Short exit Close Short exit Close Short exit Close Short exit Close Short line 139 line 142 line 144 line 147 line 150 line 152 line 154 line 157 line 160 line 162 line 164 line 167 line 169 line 171 line 173 line 175 line 179 line 182 line 184 line 187 line 190 line 192 line 194 line 197 line 200 line 202 line 204 line 207 line 209 line 211 line 213 line 215 Short Trail Stop |
| Run a Trailing Stop Jointly | N | string · options ["Y", "N"] | exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long (50%) exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long Trailing Stop exit Close Long (50%) exit Close Long (50%) exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Long exit Close Long exit Close Long exit Close Long exit Close Short exit Close Short Trailing Stop exit Close Short exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short (50%) exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short Trailing Stop exit Close Short (50%) exit Close Short (50%) exit Close Short exit Close Short Trailing Stop exit Close Short exit Close Short exit Close Short exit Close Short exit Close Short line 139 line 142 line 144 line 147 line 150 line 152 line 154 line 157 line 160 line 162 line 164 line 167 line 169 line 171 line 173 line 175 line 179 line 182 line 184 line 187 line 190 line 192 line 194 line 197 line 200 line 202 line 204 line 207 line 209 line 211 line 213 line 215 Long Trail Stop Short Trail Stop |
| Trail Loss % | 0.45 | float · min 0 · step 0.01 | longStopPrice stopValue shortStopPrice shortStopValue |
| Stop Loss % | 0.5 | float | longStop shortStop |
| Take Profit % | 0.5 | float | shortTake longTake |
| % of SH_EMA for Stop | 100 | int | exit Close Long (50%) exit Close Long exit Close Short (50%) exit Close Short Short Trail Stop |
| % of MD_EMA for Profit | 100 | int | exit Close Long (50%) exit Close Long exit Close Short (50%) exit Close Short Short Trail Stop |
| % of MD_SMA for Profit | 100 | int | exit Close Long (50%) exit Close Long exit Close Short (50%) exit Close Short Short Trail Stop |
What it calculates
- EMA (
ema,ta.emain v5)Price Source (close), SH EMA Length (8)SH_EMA · line 39 - EMA (
ema,ta.emain v5)Price Source (close), MD EMA Length (34)MD_EMA · line 40 - SMA (
sma,ta.smain v5)Price Source (close), SH SMA Length (20)SH_SMA · line 41 - SMA (
sma,ta.smain v5)Price Source (close), MD SMA Length (50)MD_SMA · line 42 - SMA (
sma,ta.smain v5)Price Source (close), LG SMA Length (200)LG_SMA · line 43
Signal flow
How price data and settings reach the orders. Each box is a name in the script, or several computed the same way; each arrow is a value one of them reads. Hover a box for its members.
The script has 67 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open HC-Long long | MAbuy and window() | On | 95 |
| Open HC-Short short | MAsell and window() | On | 99 |
| Exit Close Long stop and limit | window() | On | 140 |
| Exit Close Long Trailing Stop stop | window() | On | 141 |
| Exit Close Long stop and limit | window() | On | 143 |
| Exit Close Long (50%) stop and limit | window() | On | 145 |
| Exit Close Long Trailing Stop stop | window() | On | 146 |
| Exit Close Long (50%) stop and limit | window() | On | 148 |
| Exit Close Long Trailing Stop stop | window() | On | 149 |
| Exit Close Long (50%) stop and limit | window() | On | 151 |
| Exit Close Long (50%) stop and limit | window() | On | 153 |
| Exit Close Long (50%) stop and limit | window() | On | 155 |
| Exit Close Long Trailing Stop stop | window() | On | 156 |
| Exit Close Long (50%) stop and limit | window() | On | 158 |
| Exit Close Long Trailing Stop stop | window() | On | 159 |
| Exit Close Long (50%) stop and limit | window() | On | 161 |
| Exit Close Long (50%) stop and limit | window() | On | 163 |
| Exit Close Long stop and limit | window() | On | 165 |
| Exit Close Long Trailing Stop stop | window() | On | 166 |
| Exit Close Long stop and limit | window() | On | 168 |
| Exit Close Long stop and limit | window() | On | 170 |
| Exit Close Long stop and limit | window() | On | 172 |
| Exit Close Long stop and limit | window() | On | 174 |
| Exit Close Long stop | window() | On | 176 |
| Exit Close Short stop and limit | window() | On | 180 |
| Exit Close Short Trailing Stop stop | window() | On | 181 |
| Exit Close Short stop and limit | window() | On | 183 |
| Exit Close Short (50%) stop and limit | window() | On | 185 |
| Exit Close Short Trailing Stop stop | window() | On | 186 |
| Exit Close Short (50%) stop and limit | window() | On | 188 |
| Exit Close Short Trailing Stop stop | window() | On | 189 |
| Exit Close Short (50%) stop and limit | window() | On | 191 |
| Exit Close Short (50%) stop and limit | window() | On | 193 |
| Exit Close Short (50%) stop and limit | window() | On | 195 |
| Exit Close Short Trailing Stop stop | window() | On | 196 |
| Exit Close Short (50%) stop and limit | window() | On | 198 |
| Exit Close Short Trailing Stop stop | window() | On | 199 |
| Exit Close Short (50%) stop and limit | window() | On | 201 |
| Exit Close Short (50%) stop and limit | window() | On | 203 |
| Exit Close Short stop and limit | window() | On | 205 |
| Exit Close Short Trailing Stop stop | window() | On | 206 |
| Exit Close Short stop and limit | window() | On | 208 |
| Exit Close Short stop and limit | window() | On | 210 |
| Exit Close Short stop and limit | window() | On | 212 |
| Exit Close Short stop and limit | window() | On | 214 |
| Exit Close Short stop | window() | On | 216 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
No
No repainting patterns in the source, and the live replay matched history.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- ClearIntrabar recalculationNo varip, calc_on_every_tick or realtime-only state. Orders are decided once per closed bar.
- ClearOrder timingOrders fill at the next bar's open, after the signal bar has closed.
The checks read the source; the replay runs it. Together they catch the common causes, not every possible one.
Other markets
The same script with default settings on 8 Binance markets. Each row is a separate backtest over the most recent 1,000 bars.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | +0.1% | +102.5% | 32 | 50% | 1.09 | 0.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.3% | +5.5% | 30 | 30% | 0.52 | 0.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.1% | +9.8% | 34 | 44% | 0.72 | 2.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.1% | +8.4% | 34 | 38% | 0.87 | 0.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +1.4% | +16.7% | 35 | 57% | 1.62 | 1.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.2% | +21.5% | 31 | 42% | 0.77 | 0.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.9% | +148.3% | 29 | 38% | 0.50 | 1.1% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.4% | +17.1% | 35 | 34% | 0.55 | 0.5% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
The trades
By market direction
| At entry, last 90 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 16 | 56% | $49.09 |
| Sideways market | 5 | 100% | $77.91 |
| Falling market | 11 | 18% | −$109.94 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
0.6%
The 95th percentile of the worst drawdown when the same 32 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 0.2% to 0.6%; the tick is what actually happened, 0.4%. Order luck alone moves it this much.
All 32 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 18 Aug 2024 59,492.00 | 19 Aug 2024 58,164.40 | 1 | −$22.32 |
| long | 20 Aug 2024 59,438.50 | 21 Aug 2024 60,775.30 | 1 | $22.46 |
| short | 2 Nov 2024 69,496.00 | 4 Nov 2024 69,085.50 | 2 | $5.91 |
| short | 26 Nov 2024 93,010.00 | 27 Nov 2024 92,379.00 | 1 | $6.79 |
| short | 3 Dec 2024 95,840.60 | 4 Dec 2024 96,281.00 | 1 | −$4.60 |
| short | 10 Dec 2024 97,276.50 | 11 Dec 2024 97,027.70 | 1 | $2.56 |
| short | 19 Dec 2024 100,204.00 | 20 Dec 2024 97,900.50 | 1 | $23.00 |
| short | 27 Jan 2025 102,620.00 | 28 Jan 2025 102,542.00 | 1 | $0.76 |
| short | 1 Feb 2025 102,430.00 | 2 Feb 2025 101,089.00 | 1 | $13.11 |
| long | 10 Apr 2025 82,615.20 | 11 Apr 2025 79,249.00 | 1 | −$40.84 |
| long | 12 Apr 2025 83,423.90 | 13 Apr 2025 85,276.90 | 1 | $22.22 |
| short | 29 May 2025 107,782.00 | 30 May 2025 106,065.00 | 1 | $15.95 |
| short | 13 Jun 2025 105,672.00 | 14 Jun 2025 104,602.00 | 1 | $10.15 |
| short | 18 Jun 2025 104,551.00 | 19 Jun 2025 104,811.00 | 1 | −$2.49 |
| short | 21 Jul 2025 117,265.00 | 22 Jul 2025 117,909.00 | 1 | −$5.50 |
| short | 26 Jul 2025 117,614.00 | 27 Jul 2025 118,451.00 | 1 | −$7.13 |
| short | 29 Jul 2025 118,062.00 | 30 Jul 2025 118,482.00 | 1 | −$3.56 |
| short | 15 Aug 2025 118,295.00 | 16 Aug 2025 117,870.00 | 1 | $3.60 |
| long | 27 Nov 2025 90,484.00 | 28 Nov 2025 90,922.90 | 1 | $4.86 |
| long | 3 Dec 2025 91,277.90 | 4 Dec 2025 93,009.50 | 1 | $19.01 |
| long | 20 Dec 2025 88,137.00 | 21 Dec 2025 87,963.30 | 1 | −$1.98 |
| long | 28 Dec 2025 87,877.00 | 29 Dec 2025 87,556.90 | 1 | −$3.65 |
| long | 31 Dec 2025 88,485.50 | 2 Jan 2026 88,439.20 | 2 | −$0.52 |
| long | 15 Feb 2026 69,823.00 | 16 Feb 2026 68,522.80 | 1 | −$18.67 |
| long | 26 Feb 2026 67,988.10 | 27 Feb 2026 67,181.50 | 1 | −$11.89 |
| long | 1 Mar 2026 66,973.30 | 2 Mar 2026 65,480.40 | 1 | −$22.32 |
| long | 3 Mar 2026 68,830.10 | 4 Mar 2026 68,030.40 | 1 | −$11.62 |
| long | 12 Jun 2026 63,626.00 | 14 Jun 2026 64,167.90 | 2 | $8.51 |
| long | 3 Jul 2026 61,560.00 | 5 Jul 2026 62,859.80 | 2 | $21.12 |
| short | 29 Sep 2026 83,500.00 | 30 Sep 2026 84,040.20 | 1 | −$6.48 |
| short | 1 Oct 2026 83,623.60 | 2 Oct 2026 85,262.00 | 1 | −$19.61 |
| short | 8 Oct 2026 83,321.80 | 9 Oct 2026 81,635.90 | 1 | $20.23 |
Similar scripts
Indicators and strategies built from the same calculations, run on BTCUSDT 1D with their own defaults so they can be compared directly.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| SNAP BACK 2.0 Strategy this page | — | v4 strategy | +0.1% | 32 | 0.5% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 75 ms | Yes 1,000 bars in 133 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
“Matches TradingView” needs this script's own output exported from TradingView and compared bar by bar, which has not been done for this script. The engine is built on OpenPineScript, whose conformance results are in its conformance report. PineTS runs Pine Script v5 and v6 only.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
//@version=4// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © MakeMoneyCoESTB2020//*********************Notes for continued work***************//************************************************************//I. Intro//This strategy is designed to allow you to catch the bounce or "SNAP Back" of a equity that has been in a downward trend.//Once the moving averages are in the order of 200SMA > 50 SMA > 34EMA > 20SMA > 8EMA, the strategy is setup.//Next you wait for a trigger of the closing price crossing over the 8EMA, while there is a desired gap size between the 8EMA and the 20SMA (2-10% of stock value preferred).//Exit position based on target profit reached (conservative sell half at 34EMA and engage a trailing stop loss for remainder or set static limit) or price crosses 8EMA or stop loss%//*)This code also allows you to determine your desired backtesting date compliments of alanaster//This code is the product of many hours of hard work on the part of the greater tradingview community. The credit goes to everyone in the community who has put code out there for the greater good.//The idea for the coding came from video I watched on YouTube presented by TradeStation called Snap Back - thank you guys for the inspiration.//UPDATE: I have coded the other side of the strategy to allow you to take advantage of the same set-up in an uptrend for Short plays. You can turn the up or downsides on, off, or both.//Happy Hunting!//II. Table Of Contents // 1. Define Strategy Variables // 2. Perform Calculations // 3. Display Chart Information // 4. Determine Entry Conditions // 5. Determine Exit Conditions// 1. Define Strategy Variables*************************************************************************************************************************************************************************//Titlestrategy("SNAP BACK 2.0 Strategy", shorttitle="SNAP Back 2.0", default_qty_type=strategy.percent_of_equity, default_qty_value=5, initial_capital=20000,slippage=2, currency=currency.USD, overlay=true, pyramiding = 2)//Define calculations price sourceprice = input(title="Price Source", defval=close)//Define Trade Agression Levelaggro=input(title="Aggressive = 0, Conservative = 1", defval=0, options=[0, 1])//Define Gap percentage allowed between 8EMA and 20SMAGAP=input(title="Gap% between 8EMA & 20SMA", defval=0.15, minval=0, maxval=25, step=.01)/100//Does user want to run the Strategy for Trending Up or Trending DownRunTrend=input(title="Run Strategy Trending Up, Down, or Both", defval="Both", options=["Up", "Down", "Both"])//Initialize 8/34EMA 20/50/200/200SMA SH_EMA_length= input(title="SH EMA Length", defval=8) //short EMA lengthMD_EMA_length= input(title="MD EMA Length", defval=34) //medium EMA lengthSH_SMA_length= input(title="SH SMA Length", defval=20) //short SMA lengthMD_SMA_length= input(title="MD SMA Length", defval=50) //medium SMA lengthLG_SMA_length= input(title="LG SMA Length", defval=200) //long SMA lengthSH_EMA=ema(price, SH_EMA_length) //short EMA MD_EMA=ema(price, MD_EMA_length) //medium EMASH_SMA=sma(price, SH_SMA_length) //short SMA MD_SMA=sma(price, MD_SMA_length) //medium SMALG_SMA=sma(price, LG_SMA_length) //long SMA// 2. Perform Calculations*************************************************************************************************************************************************************************// ************************************ INPUT BACKTEST RANGE ******************************************=== coutesy of alanasterfromMonth = input(defval = 1, title = "From Month", type = input.integer, minval = 1, maxval = 12)fromDay = input(defval = 1, title = "From Day", type = input.integer, minval = 1, maxval = 31)fromYear = input(defval = 2022, title = "From Year", type = input.integer, minval = 1970)thruMonth = input(defval = 1, title = "Thru Month", type = input.integer, minval = 1, maxval = 12)thruDay = input(defval = 1, title = "Thru Day", type = input.integer, minval = 1, maxval = 31)thruYear = input(defval = 2112, title = "Thru Year", type = input.integer, minval = 1970)// === INPUT SHOW PLOT ===showDate = input(defval = true, title = "Show Date Range", type = input.bool)// === FUNCTION EXAMPLE ===start = timestamp(fromYear, fromMonth, fromDay, 00, 00) // backtest start windowfinish = timestamp(thruYear, thruMonth, thruDay, 23, 59) // backtest finish windowwindow() => time >= start and time <= finish ? true : false // create function "within window of time"bgcolor(color = showDate and window() ? color.gray : na, transp = 90) // 3. Display Chart Information*************************************************************************************************************************************************************************//plot EMAsplot(SH_EMA, title = "SH EMA", color = color.blue)plot(MD_EMA, title = "MD EMA", color = color.yellow)//plot SMAsplot(SH_SMA, title = "SH SMA", color = color.green)plot(MD_SMA, title = "MD SMA", color = color.orange)plot(LG_SMA, title = "LG SMA", color = color.red, linewidth = 4, transp = 70)// 4. Determine Entry Conditions*************************************************************************************************************************************************************************//Determine if SNAP Back (SB) setup is present: SB_RB_Up= false //SB_RB_Up = Snap Back RainBow for an Uptrend SwingSB_RB_Up:= iff(LG_SMA>MD_SMA and MD_SMA>MD_EMA and MD_EMA>SH_SMA and SH_SMA>SH_EMA, true, false) //is the 200SMA > 50 SMA > 34EMA > 20SMA > 8EMA // plotshape(SB_RB, title= "SB_RB", color=color.black, style=shape.cross, text="Rainbow") //for testing onlySB_RB_DWN= false //SB_RB_DWN = Snap Back RainBow for a Downtrend SwingSB_RB_DWN:= iff(LG_SMA<MD_SMA and MD_SMA<MD_EMA and MD_EMA<SH_SMA and SH_SMA<SH_EMA, true, false) //is the 200SMA < 50 SMA < 34EMA < 20SMA < 8EMA SB_Gap=falseSB_Gap:= iff(abs(SH_SMA-SH_EMA)>(price*GAP), true, false) //is there a greater than "GAP"% of the price gap between the 8EMA and 20SMASB_SetUp_Up=falseSB_SetUp_Up:= iff(SB_RB_Up and SB_Gap, true, false)//Uptrend Setup both conditions must be true//plotshape(SB_SetUp, title= "SB_SetUp", color=color.white, style=shape.diamond, text="Set Up") //for testingSB_SetUp_DWN=falseSB_SetUp_DWN:= iff(SB_RB_DWN and SB_Gap, true, false)//Downtrend Setup both conditions must be true//Determine trigger (TGR) for entrySB_TGR_Up=falseSB_TGR_Up:= iff(iff(aggro==0, crossover(price, SH_EMA), true) and iff(aggro==1, crossover(price[aggro],SH_EMA) and price>open[aggro], true), true, false) //if the price crosses over the 8EMA that is our entry signal, aggro determines how aggressively we enter the position (wait for a confirmaiton bar or not)SB_TGR_DWN=falseSB_TGR_DWN:= iff(iff(aggro==0, crossunder(price, SH_EMA), true) and iff(aggro==1, crossunder(price[aggro],SH_EMA) and price<open[aggro], true), true, false) //if the price crosses under the 8EMA that is our entry signal, aggro determines how aggressively we enter the position (wait for a confirmaiton bar or not)//Determine when to run the strategy based on user input for uptrend or downtrendRunTrendUp=false //Varibile for running the Strategy in an UpTrendRunTrendUp:= iff(RunTrend == "Up" or RunTrend == "Both", true, false)RunTrendDWN=false //Varibile for running the Strategy in a DownTrendRunTrendDWN:= iff(RunTrend == "Down" or RunTrend == "Both", true, false)//Determine full buy conditionsMAbuy=false//long entry variableMAbuy := iff(SB_SetUp_Up and SB_TGR_Up and RunTrendUp, true, false) //when both the setup, the trigger, and RunTrend are true return trueplotshape(MAbuy, title= "HC-LB", color=color.lime, style=shape.circle, text="HC-LB")strategy.entry("HC-Long", strategy.long, comment="HC-Long", when = MAbuy and window())MAsell=false//short entry variableMAsell := iff(SB_SetUp_DWN and SB_TGR_DWN and RunTrendDWN, true, false) //when both the setup, the trigger, and RunTrend are true return trueplotshape(MAsell, title= "HC-SB", color=color.purple, style=shape.circle, text="HC-SB")strategy.entry("HC-Short", strategy.short, comment="HC-Short", when = MAsell and window())// 5. Submit Profit and Loss Exit Calculations Orders*************************************************************************************************************************************************************************//Stop CriteriaStpCri=input(title="Stop Criteria: SL, TRL, or SH_EMA", defval="TRL", options=["SL", "TRL", "SH_EMA"])//Profit CriteriaProCri=input(title="Profit Criteria: TGT%, TRL, MD_EMA, or MD_SMA", defval="MD_EMA", options=["TGT%", "TRL", "MD_EMA", "MD_SMA"])//Does user want to run a Trailing Stop jointlytrailStop_YN=input(title="Run a Trailing Stop Jointly", defval="N", options=["Y", "N"])// User Options to Change Inputs (%)TrailPerc = input(title="Trail Loss %", type=input.float, minval=0, step=0.01, defval=0.45) /100stopPer = input(0.5, title='Stop Loss %', type=input.float) / 100takePer = input(0.5, title='Take Profit %', type=input.float) / 100//Percent of SH_EMA to use for StopLossSH_EMA_percent = input(100, title="% of SH_EMA for Stop")/100//Percent of MD_EMA to use for Take ProfitMD_EMA_percent = input(100, title="% of MD_EMA for Profit")/100//Percent of MD_SMA to use for Take ProfitMD_SMA_percent = input(100, title="% of MD_SMA for Profit")/100//calculate Trail stop price for MD_EMA TGT% conditionlongStopPrice=0.0//long side entry stop variablelongStopPrice := if (strategy.position_size > 0) stopValue = close * (1 - TrailPerc) max(stopValue, longStopPrice[1])else 0 shortStopPrice=0.0//short side entry stop variableshortStopPrice := if (strategy.position_size < 0) shortStopValue = close * (1 + TrailPerc) min(shortStopValue, shortStopPrice[1])else 999999 // Determine where you've entered and in what directionlongStop = strategy.position_avg_price * (1 - stopPer)shortStop = strategy.position_avg_price * (1 + stopPer)shortTake = strategy.position_avg_price * (1 - takePer)longTake = strategy.position_avg_price * (1 + takePer)//exit position conditions and ordersif strategy.position_size > 0 //long side exit conditions if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="Y" strategy.exit(id="Close Long", when = window(), stop=longStop, limit=longTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="N" strategy.exit(id="Close Long", when = window(), stop=longStop, limit=longTake)// sell when either the TGT or the SL is hit if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="Y" strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_EMA_percent*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SL then transition to a trailing stop loss strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="Y" strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_SMA_percent*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SL then transition to a trailing stop loss strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="N" strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_EMA_percent*MD_EMA)// sell when MD_EMA or SL hit if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="N" strategy.exit(id="Close Long (50%)", when = window(), stop=longStop, limit=MD_SMA_percent*MD_SMA)// sell when MD_SMA or SL hit if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="Y" strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_EMA_percent*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SH_EMA hit then transition to a trailing stop loss strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="Y" strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_SMA_percent*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SH_EMA hit then transition to a trailing stop loss strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="N" strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_EMA_percent*MD_EMA)// sell when MD_EMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="N" strategy.exit(id="Close Long (50%)", when = window(), stop=SH_EMA*SH_EMA_percent, limit=MD_SMA_percent*MD_SMA)// sell when MD_SMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="Y" strategy.exit(id="Close Long", when = window(), stop=SH_EMA*SH_EMA_percent, limit=longTake, qty_percent=50)// sell 50% when either the TGT or the SH_EMA is hit then transition to a trailing stop loss strategy.exit(id="Close Long Trailing Stop", when = window(), stop=longStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="N" strategy.exit(id="Close Long", when = window(), stop=SH_EMA*SH_EMA_percent, limit=longTake)// sell when either the TGT or the SH_EMA is hit if StpCri=="TRL" and ProCri=="TGT%" and trailStop_YN=="N" strategy.exit(id="Close Long", when = window(), stop=longStopPrice, limit=longTake)// sell when either the TGT or the TRL is hit if StpCri=="TRL" and ProCri=="MD_EMA" and trailStop_YN=="N" strategy.exit(id="Close Long", when = window(), stop=longStopPrice, limit=MD_EMA_percent*MD_EMA)// sell when either the MD_EMA or the TRL is hit if StpCri=="TRL" and ProCri=="MD_SMA" and trailStop_YN=="N" strategy.exit(id="Close Long", when = window(), stop=longStopPrice, limit=MD_SMA_percent*MD_SMA)// sell when either the MD_SMA or the TRL is hit if StpCri=="TRL" and ProCri=="TRL" and trailStop_YN=="N" strategy.exit(id="Close Long", when = window(), stop=longStopPrice)// sell when the TRL is hit if strategy.position_size < 0 //short side exit conditions if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="Y" strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="TGT%" and trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake)// sell when either the TGT or the SL is hit if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="Y" strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_EMA_percent)*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SL then transition to a trailing stop loss strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="Y" strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_SMA_percent)*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SL then transition to a trailing stop loss strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100) if StpCri=="SL" and ProCri=="MD_EMA" and trailStop_YN=="N" strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_EMA_percent)*MD_EMA) // sell when MD_EMA or SL hit if StpCri=="SL" and ProCri=="MD_SMA" and trailStop_YN=="N" strategy.exit(id="Close Short (50%)", when = window(), stop=shortStop, limit=(2-MD_SMA_percent)*MD_SMA) // sell when MD_SMA or SL hit if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="Y" strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_EMA_percent)*MD_EMA, qty_percent=50)// sell 50% when MD_EMA hit or SH_EMA hit then transition to a trailing stop loss strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="Y" strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_SMA_percent)*MD_SMA, qty_percent=50)// sell 50% when MD_SMA hit or SH_EMA hit then transition to a trailing stop loss strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="MD_EMA" and trailStop_YN=="N" strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_EMA_percent)*MD_EMA) // sell when MD_EMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="MD_SMA" and trailStop_YN=="N" strategy.exit(id="Close Short (50%)", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=(2-MD_SMA_percent)*MD_SMA) // sell when MD_SMA or SH_EMA hit if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="Y" strategy.exit(id="Close Short", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=shortTake, qty_percent=50)// sell 50% when either the TGT or the SL is hit then transition to a trailing stop loss strategy.exit(id="Close Short Trailing Stop", when = window(), stop=shortStopPrice, qty_percent=100) if StpCri=="SH_EMA" and ProCri=="TGT%" and trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=SH_EMA*(2-SH_EMA_percent), limit=shortTake)// sell when either the TGT or the SH_EMA is hit if StpCri=="TRL" and ProCri=="TGT%" and trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=shortStopPrice, limit=shortTake)// sell when either the TGT or the TRL is hit if StpCri=="TRL" and ProCri=="MD_EMA" and trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=shortStopPrice, limit=(2-MD_EMA_percent)*MD_EMA) // sell when either the MD_EMA or the TRL is hit if StpCri=="TRL" and ProCri=="MD_SMA" and trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=shortStopPrice, limit=(2-MD_SMA_percent)*MD_SMA) // sell when either the MD_SMA or the TRL is hit if StpCri=="TRL" and ProCri=="TRL" and trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=shortStopPrice) // sell when the TRL is hit// Plot stop trailing loss values for confirmationplot(series=(strategy.position_size > 0 and ((trailStop_YN=="Y") or StpCri=="TRL")) ? longStopPrice : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Long Trail Stop") //plot the trailing stop on the chart for an uptrendplot(series=(strategy.position_size < 0 and ((trailStop_YN=="Y") or StpCri=="TRL")) ? shortStopPrice : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the trailing stop on the chart for a downtrend//plot fixed stop loss valueplot(series=(strategy.position_size > 0 and (StpCri == "SL")) ? longStop : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Long Trail Stop") //plot the stop on the chart for an uptrendplot(series=(strategy.position_size < 0 and (StpCri == "SL")) ? shortStop : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the stop on the chart for a downtrend//plot highlight of SH_EMA% used for stop exit conditionplot(series=(strategy.position_size > 0 and (StpCri == "SH_EMA")) ? SH_EMA*SH_EMA_percent : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the SH_EMA based stop on the chart for a uptrendplot(series=(strategy.position_size < 0 and (StpCri == "SH_EMA")) ? SH_EMA*(2-SH_EMA_percent) : na, color=color.fuchsia, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the SH_EMA based stop on the chart for a downtrend//plot the TGT profit pointsplot(series=(strategy.position_size > 0 and (ProCri == "TGT%")) ? longTake : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the TGT% for long positionplot(series=(strategy.position_size > 0 and (ProCri == "MD_EMA")) ? MD_EMA_percent*MD_EMA : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the MD_EMA % TGT for long positionplot(series=(strategy.position_size > 0 and (ProCri == "MD_SMA")) ? MD_SMA_percent*MD_SMA : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the MD_SMA % TGT for long positionplot(series=(strategy.position_size < 0 and (ProCri == "TGT%")) ? shortTake : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the TGT% for short positionplot(series=(strategy.position_size < 0 and (ProCri == "MD_EMA")) ? (2-MD_EMA_percent)*MD_EMA : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the MD_EMA % TGT for short positionplot(series=(strategy.position_size < 0 and (ProCri == "MD_SMA")) ? (2-MD_SMA_percent)*MD_SMA : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the MD_SMA % TGT for short position