Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (25)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$20,017$40,505Equity from $20,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
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

Verified

Same 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⋯

Clean v5

VerifiedDownload the v5 script

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.

Download the v6 script

-//@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.

SettingDefaultType and rangeFeeds
Price SourceclosesourceSH_EMA MD_EMA SH_SMA MD_SMA LG_SMA SB_Gap SB_TGR_Up SB_TGR_DWN
Aggressive = 0, Conservative = 10int · options [0, 1]SB_TGR_Up SB_TGR_DWN
Gap% between 8EMA & 20SMA0.15float · min 0 · max 25 · step .01SB_Gap
Run Strategy Trending Up, Down, or BothBothstring · options ["Up", "Down", "Both"]RunTrendUp RunTrendDWN
SH EMA Length8intSH_EMA
MD EMA Length34intMD_EMA
SH SMA Length20intSH_SMA
MD SMA Length50intMD_SMA
LG SMA Length200intLG_SMA
From Month1int (written input.integer) · min 1 · max 12start
From Day1int (written input.integer) · min 1 · max 31start
From Year2022int (written input.integer) · min 1970start
Thru Month1int (written input.integer) · min 1 · max 12finish
Thru Day1int (written input.integer) · min 1 · max 31finish
Thru Year2112int (written input.integer) · min 1970finish
Show Date Rangetrueboolbgcolor
Stop Criteria: SL, TRL, or SH_EMATRLstring · 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_SMAMD_EMAstring · 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 JointlyNstring · 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.45float · min 0 · step 0.01longStopPrice stopValue shortStopPrice shortStopValue
Stop Loss %0.5floatlongStop shortStop
Take Profit %0.5floatshortTake longTake
% of SH_EMA for Stop100intexit Close Long (50%) exit Close Long exit Close Short (50%) exit Close Short Short Trail Stop
% of MD_EMA for Profit100intexit Close Long (50%) exit Close Long exit Close Short (50%) exit Close Short Short Trail Stop
% of MD_SMA for Profit100intexit Close Long (50%) exit Close Long exit Close Short (50%) exit Close Short Short Trail Stop

What it calculates

  • EMA (ema, ta.ema in v5)Price Source (close), SH EMA Length (8)SH_EMA · line 39
  • EMA (ema, ta.ema in v5)Price Source (close), MD EMA Length (34)MD_EMA · line 40
  • SMA (sma, ta.sma in v5)Price Source (close), SH SMA Length (20)SH_SMA · line 41
  • SMA (sma, ta.sma in v5)Price Source (close), MD SMA Length (50)MD_SMA · line 42
  • SMA (sma, ta.sma in 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

OrderFires whenBy defaultLine
Open HC-Long longMAbuy and window()On95
Open HC-Short shortMAsell and window()On99
Exit Close Long stop and limitwindow()On140
Exit Close Long Trailing Stop stopwindow()On141
Exit Close Long stop and limitwindow()On143
Exit Close Long (50%) stop and limitwindow()On145
Exit Close Long Trailing Stop stopwindow()On146
Exit Close Long (50%) stop and limitwindow()On148
Exit Close Long Trailing Stop stopwindow()On149
Exit Close Long (50%) stop and limitwindow()On151
Exit Close Long (50%) stop and limitwindow()On153
Exit Close Long (50%) stop and limitwindow()On155
Exit Close Long Trailing Stop stopwindow()On156
Exit Close Long (50%) stop and limitwindow()On158
Exit Close Long Trailing Stop stopwindow()On159
Exit Close Long (50%) stop and limitwindow()On161
Exit Close Long (50%) stop and limitwindow()On163
Exit Close Long stop and limitwindow()On165
Exit Close Long Trailing Stop stopwindow()On166
Exit Close Long stop and limitwindow()On168
Exit Close Long stop and limitwindow()On170
Exit Close Long stop and limitwindow()On172
Exit Close Long stop and limitwindow()On174
Exit Close Long stopwindow()On176
Exit Close Short stop and limitwindow()On180
Exit Close Short Trailing Stop stopwindow()On181
Exit Close Short stop and limitwindow()On183
Exit Close Short (50%) stop and limitwindow()On185
Exit Close Short Trailing Stop stopwindow()On186
Exit Close Short (50%) stop and limitwindow()On188
Exit Close Short Trailing Stop stopwindow()On189
Exit Close Short (50%) stop and limitwindow()On191
Exit Close Short (50%) stop and limitwindow()On193
Exit Close Short (50%) stop and limitwindow()On195
Exit Close Short Trailing Stop stopwindow()On196
Exit Close Short (50%) stop and limitwindow()On198
Exit Close Short Trailing Stop stopwindow()On199
Exit Close Short (50%) stop and limitwindow()On201
Exit Close Short (50%) stop and limitwindow()On203
Exit Close Short stop and limitwindow()On205
Exit Close Short Trailing Stop stopwindow()On206
Exit Close Short stop and limitwindow()On208
Exit Close Short stop and limitwindow()On210
Exit Close Short stop and limitwindow()On212
Exit Close Short stop and limitwindow()On214
Exit Close Short stopwindow()On216

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.

11/11trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+0.1%+102.5%3250%1.090.5%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.3%+5.5%3030%0.520.5%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−1.1%+9.8%3444%0.722.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.1%+8.4%3438%0.870.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+1.4%+16.7%3557%1.621.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h−0.2%+21.5%3142%0.770.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.9%+148.3%2938%0.501.1%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.4%+17.1%3534%0.550.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 barsTradesWonP&L
Rising market1656%$49.09
Sideways market5100%$77.91
Falling market1118%−$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.

0%1%
All 32 trades
SideEntryExitBarsP&L
long18 Aug 2024 59,492.0019 Aug 2024 58,164.401−$22.32
long20 Aug 2024 59,438.5021 Aug 2024 60,775.301$22.46
short2 Nov 2024 69,496.004 Nov 2024 69,085.502$5.91
short26 Nov 2024 93,010.0027 Nov 2024 92,379.001$6.79
short3 Dec 2024 95,840.604 Dec 2024 96,281.001−$4.60
short10 Dec 2024 97,276.5011 Dec 2024 97,027.701$2.56
short19 Dec 2024 100,204.0020 Dec 2024 97,900.501$23.00
short27 Jan 2025 102,620.0028 Jan 2025 102,542.001$0.76
short1 Feb 2025 102,430.002 Feb 2025 101,089.001$13.11
long10 Apr 2025 82,615.2011 Apr 2025 79,249.001−$40.84
long12 Apr 2025 83,423.9013 Apr 2025 85,276.901$22.22
short29 May 2025 107,782.0030 May 2025 106,065.001$15.95
short13 Jun 2025 105,672.0014 Jun 2025 104,602.001$10.15
short18 Jun 2025 104,551.0019 Jun 2025 104,811.001−$2.49
short21 Jul 2025 117,265.0022 Jul 2025 117,909.001−$5.50
short26 Jul 2025 117,614.0027 Jul 2025 118,451.001−$7.13
short29 Jul 2025 118,062.0030 Jul 2025 118,482.001−$3.56
short15 Aug 2025 118,295.0016 Aug 2025 117,870.001$3.60
long27 Nov 2025 90,484.0028 Nov 2025 90,922.901$4.86
long3 Dec 2025 91,277.904 Dec 2025 93,009.501$19.01
long20 Dec 2025 88,137.0021 Dec 2025 87,963.301−$1.98
long28 Dec 2025 87,877.0029 Dec 2025 87,556.901−$3.65
long31 Dec 2025 88,485.502 Jan 2026 88,439.202−$0.52
long15 Feb 2026 69,823.0016 Feb 2026 68,522.801−$18.67
long26 Feb 2026 67,988.1027 Feb 2026 67,181.501−$11.89
long1 Mar 2026 66,973.302 Mar 2026 65,480.401−$22.32
long3 Mar 2026 68,830.104 Mar 2026 68,030.401−$11.62
long12 Jun 2026 63,626.0014 Jun 2026 64,167.902$8.51
long3 Jul 2026 61,560.005 Jul 2026 62,859.802$21.12
short29 Sep 2026 83,500.0030 Sep 2026 84,040.201−$6.48
short1 Oct 2026 83,623.602 Oct 2026 85,262.001−$19.61
short8 Oct 2026 83,321.809 Oct 2026 81,635.901$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.

ScriptSharesTypeNetTradesMax drawdown
SNAP BACK 2.0 Strategy this page—v4 strategy+0.1%320.5%
MACD Long Strat by TheGrindTodayema smav4 strategy+24.0%7920.1%
Stock trending strategy by exlux99ema smav4 strategy+35.3%2114.7%
4-Hour Stochastic EMA Trend by slymnturkogluema smav4 strategyNo trades on this market
4X EMA and volume strategy by SoftKill21ema smav4 strategy+1.8%325.8%
Simple Momentum Strategy Based on SMA, EMA and Volume by slip_streamsma emav4 strategy+7.4%1827.4%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 75 msYes 1,000 bars in 133 msNot measured
PineTSNo 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