Pine Script v4 · Strategy
WOMBO COMBO- EMA & VWAP & MACD & BB & STC
A Pine Script v4 strategy built on EMA (close, 200), SMA (macd, 9) and Stdev (close, lengthBB). It has 10 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. 24 settings; defaults include Price Source close, Run Strategy Trending Up, Down, or Both Both and EMA_Length 200.
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 WOMBO COMBO- EMA & VWAP & MACD & BB & STC repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 1 of 1 trades.
- How did WOMBO COMBO- EMA & VWAP & MACD & BB & STC do on BTCUSDT 1D with default settings?
- −0.53%
- From 26 Jan 2024 to 21 Oct 2026, over 6 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can WOMBO COMBO- EMA & VWAP & MACD & BB & STC 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 WOMBO COMBO- EMA & VWAP & MACD & BB & STC use a stop loss?
- Yes
- strategy.exit sets a stop on line 223, 225, 228, 229, 231, 236, 237, 239.
- Does WOMBO COMBO- EMA & VWAP & MACD & BB & STC run in Zeal Terminal?
- Yes
- It compiles in 46 ms and runs 1,000 bars in 190 ms, in the browser.
BTCUSDT · 1D · default settings
- EMA
- VWAP
- BB Upper Band
- BB Average
- BB Lower Band
- 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.53%
- −$106.12
- Buy and hold
- +102.5%
- same period
- Closed trades
- 6
- 2 won · 4 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.82
- gross profit ÷ gross loss
- Max drawdown
- 2.19%
- −$438.82
One run with the author's default settings, $20,000 starting capital, commission as the script sets it. A long position was still open at the last bar. 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.
-
Likely bug
ema(close, ma1length) ignores the settings line 110
Line 110 hard-codes close where line 36 uses “Price Source”. Changing those settings moves one band but not this one.
ema(close,ma1length)ema(price, ma1length) -
Likely bug
ema(close, EMAlength) ignores the settings line 111
Line 111 hard-codes close where line 36 uses “Price Source”. Changing those settings moves one band but not this one.
ema(close,EMAlength)ema(price, EMAlength) -
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 187, 191, 223, 225, 228, 229, 231, 236, 237, 239. The upgraded version below moves each call inside an if block.
-
Note
Uses iff(), which v5 removed line 173
iff() on line 173, 175, 185, 185, 185, 185, 185, 185, 189, 189, 189, 189, 189, 189. v5 writes it as the ternary c ? a : b.
-
Note
Uses transp =, which v5 removed line 86
transp = on line 86, 87, 88, 166. 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, stdev → ta.stdev, stoch → ta.stoch, crossover → ta.crossover, crossunder → ta.crossunder, min → math.min, max → math.max, abs → math.abs, highest → ta.highest, lowest → ta.lowest.
- 10 strategy calls use the when = argument (line 187, 191, 223, 225, 228, 229, 231, 236, 237, 239). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 36: ema → ta.ema
- Line 42: vwap → ta.vwap
- Line 43: vwap → ta.vwap
- Line 49: ema → ta.ema
- Line 50: ema → ta.ema
66 more edits
- Line 54: sma → ta.sma
- Line 77: sma → ta.sma
- Line 79: stdev → ta.stdev
- Line 84: input(type = input.string) → input.string()
- Line 86: transp = transP → color.new(color.aqua, transP)
- Line 87: transp = transP → color.new(color.red, transP)
- Line 88: transp = transP → color.new(color.aqua, transP)
- Line 95: input(type = input.integer) → input.int()
- Line 101: ema → ta.ema
- Line 101: ema → ta.ema
- Line 102: stoch → ta.stoch
- Line 103: ema → ta.ema
- Line 104: stoch → ta.stoch
- Line 105: ema → ta.ema
- Line 110: ema → ta.ema
- Line 111: ema → ta.ema
- Line 114: crossover → ta.crossover
- Line 115: crossunder → ta.crossunder
- Line 121: min → math.min
- Line 121: min → math.min
- Line 123: max → math.max
- Line 123: max → math.max
- Line 125: max → math.max
- Line 125: abs → math.abs
- Line 125: min → math.min
- Line 125: abs → math.abs
- Line 139: highest → ta.highest
- Line 142: lowest → ta.lowest
- Line 154: input(type = input.integer) → input.int()
- Line 155: input(type = input.integer) → input.int()
- Line 156: input(type = input.integer) → input.int()
- Line 157: input(type = input.integer) → input.int()
- Line 158: input(type = input.integer) → input.int()
- Line 159: input(type = input.integer) → input.int()
- Line 161: input(type = input.bool) → input.bool()
- Line 166: transp = 90 → color.new(showDate and window() ? color.gray : na, 90)
- Line 173: iff(c, a, b) → (c ? a : b)
- Line 175: iff(c, a, b) → (c ? a : b)
- Line 179: input(type = input.string) → input.string()
- Line 180: input(type = input.string) → input.string()
- Line 181: input(type = input.string) → input.string()
- Line 182: input(type = input.string) → input.string()
- Line 183: input(type = input.string) → input.string()
- Line 185: iff(c, a, b) → (c ? a : b)
- Line 185: iff(c, a, b) → (c ? a : b)
- Line 185: iff(c, a, b) → (c ? a : b)
- Line 185: vwap → ta.vwap
- Line 185: iff(c, a, b) → (c ? a : b)
- Line 185: crossunder → ta.crossunder
- Line 185: iff(c, a, b) → (c ? a : b)
- Line 185: iff(c, a, b) → (c ? a : b)
- Line 185: crossunder → ta.crossunder
- Line 189: iff(c, a, b) → (c ? a : b)
- Line 189: iff(c, a, b) → (c ? a : b)
- Line 189: iff(c, a, b) → (c ? a : b)
- Line 189: vwap → ta.vwap
- Line 189: iff(c, a, b) → (c ? a : b)
- Line 189: crossunder → ta.crossunder
- Line 189: iff(c, a, b) → (c ? a : b)
- Line 189: iff(c, a, b) → (c ? a : b)
- Line 189: crossover → ta.crossover
- Line 197: input(type = input.float) → input.float()
- Line 199: input(type = input.float) → input.float()
- Line 200: input(type = input.float) → input.float()
- Line 205: max → math.max
- Line 212: 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/⋯EMAlength = input(title="EMA_Length", defval=200)-EMA=ema(price, EMAlength)+EMA=ta.ema(price, EMAlength)//plot EMA⋯//calculate VWAP-ColorVWAP = (price > vwap) ? color.lime : color.maroon+ColorVWAP = (price > ta.vwap) ? color.lime : color.maroon-plot(vwap, title = "VWAP", color=ColorVWAP, linewidth=2)+plot(ta.vwap, title = "VWAP", color=ColorVWAP, linewidth=2)//*****************************⋯//define parameters to calculate MACD-fastMA = ema(price, fast)+fastMA = ta.ema(price, fast)-slowMA = ema(price, slow)+slowMA = ta.ema(price, slow)//define MACD line⋯//define SIGNAL line-signal = sma(macd, 9)+signal = ta.sma(macd, 9)//plot MACD line⋯//define BB average-basisBB = sma(price, lengthBB)+basisBB = ta.sma(price, lengthBB)//define BB standar deviation-devBB = multBB * stdev(price, lengthBB)+devBB = multBB * ta.stdev(price, lengthBB)//define BB upper and lower limits⋯//Plot BB graph-ShowBB = input(title="Show BB", defval="Y", type=input.string, options=["Y", "N"])+ShowBB = input.string(title="Show BB", defval="Y", options=["Y", "N"])transP = (ShowBB=="Y") ? 0 : 100-plot (upperBB, title = "BB Upper Band", color = color.aqua, transp=transP)+plot (upperBB, title = "BB Upper Band", color = color.new(color.aqua, transP))-plot (basisBB, title = "BB Average", color = color.red, transp=transP)+plot (basisBB, title = "BB Average", color = color.new(color.red, transP))-plot (lowerBB, title = "BB Lower Band", color = color.aqua, transp=transP)+plot (lowerBB, title = "BB Lower Band", color = color.new(color.aqua, transP))//*************************************************⋯slowLength = 50-cycleLength = input(title="Cycle Length", type=input.integer, defval=40)+cycleLength = input.int(title="Cycle Length", defval=40)//d1Length = input(title="1st %D Length", type=input.integer, defval=3)⋯srcSTC = close-macdSTC = ema(srcSTC, fastLength) - ema(srcSTC, slowLength)+macdSTC = ta.ema(srcSTC, fastLength) - ta.ema(srcSTC, slowLength)-k = nz(fixnan(stoch(macdSTC, macdSTC, macdSTC, cycleLength)))+k = nz(fixnan(ta.stoch(macdSTC, macdSTC, macdSTC, cycleLength)))-d = ema(k, d1Length)+d = ta.ema(k, d1Length)-kd = nz(fixnan(stoch(d, d, d, cycleLength)))+kd = nz(fixnan(ta.stoch(d, d, d, cycleLength)))-stc = ema(kd, d2Length)+stc = ta.ema(kd, d2Length)stc := stc > 100 ? 100 : stc < 0 ? 0 : stc⋯ma1length=35-ma1 = ema(close,ma1length)+ma1 = ta.ema(close,ma1length)-ma2 = ema(close,EMAlength)+ma2 = ta.ema(close,EMAlength)//STCbuy = crossover(stc, lowerSTC) and ma1>ma2 and close>ma1//STCsell = crossunder(stc, upperSTC) and ma1<ma2 and close<ma1-STCbuy = crossover(stc, lowerSTC) +STCbuy = ta.crossover(stc, lowerSTC) -STCsell = crossunder(stc, upperSTC) +STCsell = ta.crossunder(stc, upperSTC) //*************************************************⋯//plotchar(data, title="Doji", text='Doji', color=color.white)-data2=(close[2] > open[2] and min(open[1], close[1]) > close[2] and open < min(open[1], close[1]) and close < open )+data2=(close[2] > open[2] and math.min(open[1], close[1]) > close[2] and open < math.min(open[1], close[1]) and close < open )//plotshape(data2, title= "Evening Star", color=color.red, style=shape.arrowdown, text="Evening\nStar")-data3=(close[2] < open[2] and max(open[1], close[1]) < close[2] and open > max(open[1], close[1]) and close > open )+data3=(close[2] < open[2] and math.max(open[1], close[1]) < close[2] and open > math.max(open[1], close[1]) and close > open )//plotshape(data3, title= "Morning Star", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Morning\nStar")-data4=(open[1] < close[1] and open > close[1] and high - max(open, close) >= abs(open - close) * 3 and min(close, open) - low <= abs(open - close))+data4=(open[1] < close[1] and open > close[1] and high - math.max(open, close) >= math.abs(open - close) * 3 and math.min(close, open) - low <= math.abs(open - close))//plotshape(data4, title= "Shooting Star", color=color.red, style=shape.arrowdown, text="Shooting\nStar")⋯//plotshape(data9, title= "Bullish Engulfing", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Bullish\nEngulfling")-upper = highest(10)[1]+upper = ta.highest(10)[1]data10=(close[1] < open[1] and open < low[1] and close > close[1] + ((open[1] - close[1])/2) and close < open[1])//plotshape(data10, title= "Piercing Line", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Piercing\nLine")-lower = lowest(10)[1]+lower = ta.lowest(10)[1]data11=(low == open and open < lower and open < close and close > ((high[1] - low[1]) / 2) + low[1])⋯// ************************************ INPUT BACKTEST RANGE ******************************************===-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 = 2020, title = "From Year", type = input.integer, minval = 1970)+fromYear = input.int(defval = 2020, 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)) // === EXECUTION ===⋯RunTrendUp=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)//Define countback variable⋯//User input for what evaluations to run: EMA, VWAP, MACD, BB-EMA_Y_N=input(defval = "Y", title="Run EMA", type=input.string, options=["Y", "N"])+EMA_Y_N=input.string(defval = "Y", title="Run EMA", options=["Y", "N"])-VWAP_Y_N=input(defval = "Y", title="Run VWAP", type=input.string, options=["Y", "N"])+VWAP_Y_N=input.string(defval = "Y", title="Run VWAP", options=["Y", "N"])-MACD_Y_N=input(defval = "N", title="Run MACD", type=input.string, options=["Y", "N"])+MACD_Y_N=input.string(defval = "N", title="Run MACD", options=["Y", "N"])-BB_Y_N=input(defval = "N", title="Run BB", type=input.string, options=["Y", "N"])+BB_Y_N=input.string(defval = "N", title="Run BB", options=["Y", "N"])-STC_Y_N=input(defval = "Y", title="Run STC", type=input.string, options=["Y", "N"])+STC_Y_N=input.string(defval = "Y", title="Run STC", options=["Y", "N"])//long entry condition-dataHCLB=(RunTrendUp and iff(STC_Y_N=="Y", STCbuy, true) and iff(EMA_Y_N=="Y", price[countback]>EMA, true) and iff(VWAP_Y_N=="Y", price[countback]>vwap, true) and iff(MACD_Y_N=="Y", crossunder(signal[countback], macd[countback]), true) and iff(MACD_Y_N=="Y", macd[countback]<0, true) and iff(BB_Y_N=="Y", crossunder(price[countback], lowerBB), true))+dataHCLB=(RunTrendUp and (STC_Y_N=="Y" ? STCbuy : true) and (EMA_Y_N=="Y" ? price[countback]>EMA : true) and (VWAP_Y_N=="Y" ? price[countback]>ta.vwap : true) and (MACD_Y_N=="Y" ? ta.crossunder(signal[countback], macd[countback]) : true) and (MACD_Y_N=="Y" ? macd[countback]<0 : true) and (BB_Y_N=="Y" ? ta.crossunder(price[countback], lowerBB) : true))plotshape(dataHCLB, title= "HC-LB", color=color.lime, style=shape.circle, text="HC-LB")⋯//short entry condition-dataHCSB=(RunTrendDWN and iff(STC_Y_N=="Y", STCsell, true) and iff(EMA_Y_N=="Y", price[countback]<EMA, true) and iff(VWAP_Y_N=="Y", price[countback]<vwap, true) and iff(MACD_Y_N=="Y", crossunder(macd[countback], signal[countback]), true) and iff(MACD_Y_N=="Y", signal[countback]>0, true) and iff(BB_Y_N=="Y", crossover(price[countback], upperBB), true))+dataHCSB=(RunTrendDWN and (STC_Y_N=="Y" ? STCsell : true) and (EMA_Y_N=="Y" ? price[countback]<EMA : true) and (VWAP_Y_N=="Y" ? price[countback]<ta.vwap : true) and (MACD_Y_N=="Y" ? ta.crossunder(macd[countback], signal[countback]) : true) and (MACD_Y_N=="Y" ? signal[countback]>0 : true) and (BB_Y_N=="Y" ? ta.crossover(price[countback], upperBB) : true))plotshape(dataHCSB, title= "HC-SB", color=color.fuchsia, style=shape.circle, text="HC-SB")⋯trailStop_YN=input(title="Run a Trailing Stop", defval="N", options=["Y", "N"])-TrailPerc = input(title="Trail Loss %", type=input.float, minval=0, step=1, defval=6) /100+TrailPerc = input.float(title="Trail Loss %", minval=0, step=1, defval=6) /100// User Options to Change Inputs (%)-stopPer = input(15, title='Stop Loss %', type=input.float) / 100+stopPer = input.float(15, title='Stop Loss %') / 100-takePer = input(25, title='Take Profit %', type=input.float) / 100+takePer = input.float(25, title='Take Profit %') / 100//calculate Trail stop price for MD_EMA TGT% condition⋯ 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 6 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 30: input() → input.source()
- Line 32: input() → input.string()
- Line 35: input() → input.int()
- Line 107: input() → input.int()
- Line 108: input() → input.int()
- Line 177: input() → input.int()
11 more edits
- Line 187: when = dataHCLB and window() → if block
- Line 191: when = dataHCSB and window() → if block
- Line 196: input() → input.string()
- Line 223: when = window() → if block
- Line 225: when = window() → if block
- Line 228: when = window() → if block
- Line 229: when = window() → if block
- Line 231: when = window() → if block
- Line 236: when = window() → if block
- Line 237: when = window() → if block
- Line 239: when = window() → if block
⋯//define calculations price source-price = input(title="Price Source", defval=close)+price = input.source(title="Price Source", defval=close)//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"])//***************************//Calculate 20/50/100/200EMA -EMAlength = input(title="EMA_Length", defval=200)+EMAlength = input.int(title="EMA_Length", defval=200)EMA=ta.ema(price, EMAlength)⋯stc := stc > 100 ? 100 : stc < 0 ? 0 : stc-upperSTC = input(title="Upper STC limit", defval=85)+upperSTC = input.int(title="Upper STC limit", defval=85)-lowerSTC = input( title="Lower STC limit", defval=15)+lowerSTC = input.int( title="Lower STC limit", defval=15)ma1length=35⋯//Define countback variable-countback=input(minval=0, maxval=5, title="Price CountBack", defval=0)+countback=input.int(minval=0, maxval=5, title="Price CountBack", defval=0)//User input for what evaluations to run: EMA, VWAP, MACD, BB⋯plotshape(dataHCLB, title= "HC-LB", color=color.lime, style=shape.circle, text="HC-LB")-strategy.entry("HC-Long", strategy.long, comment="HC-Long", when = dataHCLB and window())+if dataHCLB and window()+ strategy.entry("HC-Long", strategy.long, comment="HC-Long")//short entry condition⋯plotshape(dataHCSB, title= "HC-SB", color=color.fuchsia, style=shape.circle, text="HC-SB")-strategy.entry("HC-Short", strategy.short, comment="HC-Short", when=dataHCSB and window())+if dataHCSB and window()+ strategy.entry("HC-Short", strategy.short, comment="HC-Short")//******************Exit Conditions******************************⋯//Does user want to run a Trailing Stop-trailStop_YN=input(title="Run a Trailing Stop", defval="N", options=["Y", "N"])+trailStop_YN=input.string(title="Run a Trailing Stop", defval="N", options=["Y", "N"])TrailPerc = input.float(title="Trail Loss %", minval=0, step=1, defval=6) /100⋯if strategy.position_size > 0//or crossunder(price[countback], upperBB)- strategy.exit(id="Close Long", when = window(), stop=longStop, limit=longTake)+ if window()+ strategy.exit(id="Close Long", stop=longStop, limit=longTake)if strategy.position_size < 0 //or crossover(price[countback], lowerBB)- strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake)//exit position conditions and orders+ if window()+ strategy.exit(id="Close Short", stop=shortStop, limit=shortTake)//exit position conditions and ordersif strategy.position_size > 0 //long side exit conditions if 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 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 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 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// Plot stop trailing loss values for confirmation⋯Does it run in Pine Script v6?
Yes
Same 6 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 | EMA ColorVWAP fastMA slowMA basisBB devBB dataHCLB dataHCSB |
| Run Strategy Trending Up, Down, or Both | Both | string · options ["Up", "Down", "Both"] | RunTrendUp RunTrendDWN |
| EMA_Length | 200 | int | EMA ColorEMA ma2 |
| Show BB | Y | string · options ["Y", "N"] | transP |
| Cycle Length | 40 | int (written input.integer) | k kd |
| Upper STC limit | 85 | int | STCsell |
| Lower STC limit | 15 | int | STCbuy |
| 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 | 2020 | 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 |
| Price CountBack | 0 | int · min 0 · max 5 | dataHCLB dataHCSB |
| Run EMA | Y | string · options ["Y", "N"] | dataHCLB dataHCSB |
| Run VWAP | Y | string · options ["Y", "N"] | dataHCLB dataHCSB |
| Run MACD | N | string · options ["Y", "N"] | dataHCLB dataHCSB |
| Run BB | N | string · options ["Y", "N"] | dataHCLB dataHCSB |
| Run STC | Y | string · options ["Y", "N"] | dataHCLB dataHCSB |
| Run a Trailing Stop | N | string · options ["Y", "N"] | exit Close Long exit Close Long Trailing Stop exit Close Long exit Close Short exit Close Short Trailing Stop exit Close Short line 227 line 230 line 235 line 238 Long Trail Stop Short Trail Stop |
| Trail Loss % | 6 | float · min 0 · step 1 | longStopPrice stopValue shortStopPrice shortStopValue |
| Stop Loss % | 15 | float | longStop shortStop |
| Take Profit % | 25 | float | shortTake longTake |
What it calculates
- EMA (
ema,ta.emain v5)Price Source (close), EMA_Length (200)EMA · line 36 - EMA (
ema,ta.emain v5)Price Source (close), fastfastMA · line 49 - EMA (
ema,ta.emain v5)Price Source (close), slowslowMA · line 50 - SMA (
sma,ta.smain v5)macd, 9signal · line 54 - SMA (
sma,ta.smain v5)Price Source (close), lengthBBbasisBB · line 77 - Stdev (
stdev,ta.stdevin v5)Price Source (close), lengthBBdevBB · line 79 - EMA (
ema,ta.emain v5)srcSTC, fastLengthmacdSTC · line 101 - Stoch (
stoch,ta.stochin v5)macdSTC, macdSTC, macdSTC, Cycle Length (40)k · line 102 - EMA (
ema,ta.emain v5)k, d1Lengthd · line 103 - Stoch (
stoch,ta.stochin v5)d, d, d, Cycle Length (40)kd · line 104 - EMA (
ema,ta.emain v5)kd, d2Lengthstc · line 105 - EMA (
ema,ta.emain v5)close, ma1lengthma1 · line 110 - EMA (
ema,ta.emain v5)close, EMA_Length (200)ma2 · line 111 - Crossover (
crossover,ta.crossoverin v5)stc, Lower STC limit (15)STCbuy · line 114 - Crossunder (
crossunder,ta.crossunderin v5)stc, Upper STC limit (85)STCsell · line 115 - Highest (
highest,ta.highestin v5)10upper · line 139 - Lowest (
lowest,ta.lowestin v5)10lower · line 142
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 81 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 | dataHCLB and window() | On | 187 |
| Open HC-Short short | dataHCSB and window() | On | 191 |
| Exit Close Long stop and limit | window() | On | 223 |
| Exit Close Short stop and limit | window() | On | 225 |
| Exit Close Long stop and limit | window() | On | 228 |
| Exit Close Long Trailing Stop stop | window() | On | 229 |
| Exit Close Long stop and limit | window() | On | 231 |
| Exit Close Short stop and limit | window() | On | 236 |
| Exit Close Short Trailing Stop stop | window() | On | 237 |
| Exit Close Short stop and limit | window() | On | 239 |
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.5% | +102.5% | 6 | 33% | 0.82 | 2.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +0.4% | +5.5% | 8 | 25% | 1.53 | 0.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +3.0% | +9.8% | 4 | 75% | 4.88 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +1.5% | +8.4% | 5 | 40% | 2.49 | 1.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +1.7% | +16.7% | 3 | 67% | 3.33 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | −0.2% | +21.5% | 5 | 20% | 0.86 | 1.5% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +2.2% | +148.3% | 5 | 60% | 2.47 | 1.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +2.1% | +17.1% | 4 | 50% | 6.31 | 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 | 2 | 0% | −$304.32 |
| Sideways market | 3 | 33% | −$47.40 |
| Falling market | 1 | 100% | $245.60 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.0%
The 95th percentile of the worst drawdown when the same 6 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.5% to 3.0%; the tick is what actually happened, 1.8%. Order luck alone moves it this much.
All 6 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 10 Oct 2024 60,636.00 | 28 Oct 2024 69,731.40 | 18 | −$150.00 |
| long | 22 Jan 2025 106,144.00 | 25 Feb 2025 90,222.20 | 34 | −$148.88 |
| long | 20 Mar 2025 86,845.90 | 21 May 2025 108,557.00 | 62 | $246.26 |
| long | 12 Jul 2025 117,528.00 | 4 Nov 2025 99,210.00 | 115 | −$155.45 |
| long | 14 Sep 2025 115,918.00 | 4 Nov 2025 99,210.00 | 51 | −$143.66 |
| short | 25 Jan 2026 89,225.30 | 5 Feb 2026 66,919.00 | 11 | $245.60 |
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 |
|---|---|---|---|---|---|
| WOMBO COMBO- EMA & VWAP & MACD & BB & STC this page | — | v4 strategy | −0.5% | 6 | 2.2% |
| Crunchster's Turtle and Trend System by Crunchster1 | lowest highest ema stdev sma | v5 strategy | +18.0% | 31 | 16.7% |
| The Z-score by jensenvilhelm | sma stdev ema highest lowest | v5 strategy | +2.4% | 52 | 5.5% |
| MZ SRSI Strategy V1.0 by MightyZinger | ema sma highest lowest stdev | v4 strategy | No trades on this market | ||
| BBPBΔ(OBV-PVT)BB - Time Series Decomposition & Volume Weighted by tathal | ema stdev sma highest lowest | v4 strategy | No trades on this market | ||
| Lawyers Trend Pro Strategy by ErdemDemir | sma stdev lowest highest | v4 strategy | +12.1% | 102 | 41.7% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 46 ms | Yes 1,000 bars in 190 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//Edition 383 update - window condition for code execution added (compliments to allanster for approach)//Edition 380 update - BB condition coded out of exit strategy//*********************Notes for continued work***************//3) add a Table of contents to each section of code//4) add candle stick pattern considerations to chart//9)//************************************************************//Hello my fellow investors//After hours of reading, backtesting, and YouTube video watching//I discovered that 200EMA, VWAP, BB, MACD, and STC //produce the most consistent results for investment planning.//This strategy allows you to pick between the aforementioned indicators or layer them together.//It works on the pricipal of://1) Always follow the market trend - buy/sell above/below 200EMA//2) Follow corporate investing trends - buy/sell above/below VWAP//3) Apply MACD check - buy--> MACD line above signal line // and corssover below histogram \\ sell --> MACD line below signal line // and crossover above histogram.//4) Check volitility with price against BB limits upper/Sell or lower/buy//5) When STC crosses about 10 buy and when it drops below 90 sell//6) Exit position when stop loss is triggered or profit target is hit. BB also provides a parameter to exit positions.//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.//Happy Hunting!//Titlestrategy("WOMBO COMBO: 100/200EMA & VWAP & MACD", shorttitle="WOMBO COMBO", 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)//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"])//***************************//Calculate 20/50/100/200EMA EMAlength = input(title="EMA_Length", defval=200)EMA=ema(price, EMAlength)//plot EMAColorEMA=EMAlength==200?color.blue:EMAlength==100?color.aqua:EMAlength==50?color.orange:color.redplot(EMA, title = "EMA", color = ColorEMA)//*****************************//calculate VWAPColorVWAP = (price > vwap) ? color.lime : color.maroonplot(vwap, title = "VWAP", color=ColorVWAP, linewidth=2)//*****************************//calculate MACD//define variables for speedfast = 12, slow = 26//define parameters to calculate MACDfastMA = ema(price, fast)slowMA = ema(price, slow)//define MACD linemacd = fastMA - slowMA//define SIGNAL linesignal = sma(macd, 9)//plot MACD line//plot(macd, title = "MACD", color=color.orange)//plot signal line//plot(signal, title = "Signal", color=color.purple)//plot histogram//define histogram colors//col_grow_above = color.green//col_grow_below = color.red//col_fall_above = color.lime//col_fall_below = color.maroon//define histogram value//hist = macd - signal//plot histogram//plot(hist, title="Histogram", style=plot.style_columns, color=(hist>=0 ? (hist[1] < hist ? col_grow_above : col_fall_above) : (hist[1] < hist ? col_grow_below : col_fall_below) ), transp=0 )//***************************************//Calculate Bollinger Bands//Define BB input variables//lengthBB = input(20, minval=1)//multBB = input(2.0, minval=0.001, maxval=50)lengthBB = 20multBB = 2//define BB averagebasisBB = sma(price, lengthBB)//define BB standar deviationdevBB = multBB * stdev(price, lengthBB)//define BB upper and lower limitsupperBB = basisBB + devBBlowerBB = basisBB - devBB//Plot BB graphShowBB = input(title="Show BB", defval="Y", type=input.string, options=["Y", "N"])transP = (ShowBB=="Y") ? 0 : 100plot (upperBB, title = "BB Upper Band", color = color.aqua, transp=transP)plot (basisBB, title = "BB Average", color = color.red, transp=transP)plot (lowerBB, title = "BB Lower Band", color = color.aqua, transp=transP)//*************************************************//Calculate STC//fastLength = input(title="MACD Fast Length", type=input.integer, defval=12)//slowLength = input(title="MACD Slow Length", type=input.integer, defval=26)fastLength = 23slowLength = 50cycleLength = input(title="Cycle Length", type=input.integer, defval=40)//d1Length = input(title="1st %D Length", type=input.integer, defval=3)//d2Length = input(title="2nd %D Length", type=input.integer, defval=3)d1Length = 3d2Length = 3srcSTC = closemacdSTC = ema(srcSTC, fastLength) - ema(srcSTC, slowLength)k = nz(fixnan(stoch(macdSTC, macdSTC, macdSTC, cycleLength)))d = ema(k, d1Length)kd = nz(fixnan(stoch(d, d, d, cycleLength)))stc = ema(kd, d2Length)stc := stc > 100 ? 100 : stc < 0 ? 0 : stcupperSTC = input(title="Upper STC limit", defval=85)lowerSTC = input( title="Lower STC limit", defval=15)ma1length=35ma1 = ema(close,ma1length)ma2 = ema(close,EMAlength)//STCbuy = crossover(stc, lowerSTC) and ma1>ma2 and close>ma1//STCsell = crossunder(stc, upperSTC) and ma1<ma2 and close<ma1STCbuy = crossover(stc, lowerSTC) STCsell = crossunder(stc, upperSTC) //*************************************************//Candle stick patterns//DojiSize = input(0.05, minval=0.01, title="Doji size")//data=(abs(open - close) <= (high - low) * DojiSize)//plotchar(data, title="Doji", text='Doji', color=color.white)data2=(close[2] > open[2] and min(open[1], close[1]) > close[2] and open < min(open[1], close[1]) and close < open )//plotshape(data2, title= "Evening Star", color=color.red, style=shape.arrowdown, text="Evening\nStar")data3=(close[2] < open[2] and max(open[1], close[1]) < close[2] and open > max(open[1], close[1]) and close > open )//plotshape(data3, title= "Morning Star", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Morning\nStar")data4=(open[1] < close[1] and open > close[1] and high - max(open, close) >= abs(open - close) * 3 and min(close, open) - low <= abs(open - close))//plotshape(data4, title= "Shooting Star", color=color.red, style=shape.arrowdown, text="Shooting\nStar")data5=(((high - low)>3*(open -close)) and ((close - low)/(.001 + high - low) > 0.6) and ((open - low)/(.001 + high - low) > 0.6))//plotshape(data5, title= "Hammer", location=location.belowbar, color=color.white, style=shape.diamond, text="H")data5b=(((high - low)>3*(open -close)) and ((high - close)/(.001 + high - low) > 0.6) and ((high - open)/(.001 + high - low) > 0.6))//plotshape(data5b, title= "Inverted Hammer", location=location.belowbar, color=color.white, style=shape.diamond, text="IH")data6=(close[1] > open[1] and open > close and open <= close[1] and open[1] <= close and open - close < close[1] - open[1] )//plotshape(data6, title= "Bearish Harami", color=color.red, style=shape.arrowdown, text="Bearish\nHarami")data7=(open[1] > close[1] and close > open and close <= open[1] and close[1] <= open and close - open < open[1] - close[1] )//plotshape(data7, title= "Bullish Harami", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Bullish\nHarami")data8=(close[1] > open[1] and open > close and open >= close[1] and open[1] >= close and open - close > close[1] - open[1] )//plotshape(data8, title= "Bearish Engulfing", color=color.red, style=shape.arrowdown, text="Bearish\nEngulfing")data9=(open[1] > close[1] and close > open and close >= open[1] and close[1] >= open and close - open > open[1] - close[1] )//plotshape(data9, title= "Bullish Engulfing", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Bullish\nEngulfling")upper = highest(10)[1]data10=(close[1] < open[1] and open < low[1] and close > close[1] + ((open[1] - close[1])/2) and close < open[1])//plotshape(data10, title= "Piercing Line", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Piercing\nLine")lower = lowest(10)[1]data11=(low == open and open < lower and open < close and close > ((high[1] - low[1]) / 2) + low[1])//plotshape(data11, title= "Bullish Belt", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Bullish\nBelt")data12=(open[1]>close[1] and open>=open[1] and close>open)//plotshape(data12, title= "Bullish Kicker", location=location.belowbar, color=color.lime, style=shape.arrowup, text="Bullish\nKicker")data13=(open[1]<close[1] and open<=open[1] and close<=open)//plotshape(data13, title= "Bearish Kicker", color=color.red, style=shape.arrowdown, text="Bearish\nKicker")data14=(((high-low>4*(open-close))and((close-low)/(.001+high-low)>=0.75)and((open-low)/(.001+high-low)>=0.75)) and high[1] < open and high[2] < open)//plotshape(data14, title= "Hanging Man", color=color.red, style=shape.arrowdown, text="Hanging\nMan")data15=((close[1]>open[1])and(((close[1]+open[1])/2)>close)and(open>close)and(open>close[1])and(close>open[1])and((open-close)/(.001+(high-low))>0.6))//plotshape(data15, title= "Dark Cloud Cover", color=color.red, style=shape.arrowdown, text="Dark\nCloudCover")// ************************************ INPUT BACKTEST RANGE ******************************************===fromMonth = 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 = 2020, 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) // === EXECUTION ===//strategy.entry("L", strategy.long, when = window() and crossOv) // enter long when "within window of time" AND crossover//strategy.close("L", when = window() and crossUn) // exit long when "within window of time" AND crossunder//**********Long & Short Entry Calculations***********************************//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)//Define countback variablecountback=input(minval=0, maxval=5, title="Price CountBack", defval=0)//User input for what evaluations to run: EMA, VWAP, MACD, BBEMA_Y_N=input(defval = "Y", title="Run EMA", type=input.string, options=["Y", "N"])VWAP_Y_N=input(defval = "Y", title="Run VWAP", type=input.string, options=["Y", "N"])MACD_Y_N=input(defval = "N", title="Run MACD", type=input.string, options=["Y", "N"])BB_Y_N=input(defval = "N", title="Run BB", type=input.string, options=["Y", "N"])STC_Y_N=input(defval = "Y", title="Run STC", type=input.string, options=["Y", "N"])//long entry conditiondataHCLB=(RunTrendUp and iff(STC_Y_N=="Y", STCbuy, true) and iff(EMA_Y_N=="Y", price[countback]>EMA, true) and iff(VWAP_Y_N=="Y", price[countback]>vwap, true) and iff(MACD_Y_N=="Y", crossunder(signal[countback], macd[countback]), true) and iff(MACD_Y_N=="Y", macd[countback]<0, true) and iff(BB_Y_N=="Y", crossunder(price[countback], lowerBB), true))plotshape(dataHCLB, title= "HC-LB", color=color.lime, style=shape.circle, text="HC-LB")strategy.entry("HC-Long", strategy.long, comment="HC-Long", when = dataHCLB and window())//short entry conditiondataHCSB=(RunTrendDWN and iff(STC_Y_N=="Y", STCsell, true) and iff(EMA_Y_N=="Y", price[countback]<EMA, true) and iff(VWAP_Y_N=="Y", price[countback]<vwap, true) and iff(MACD_Y_N=="Y", crossunder(macd[countback], signal[countback]), true) and iff(MACD_Y_N=="Y", signal[countback]>0, true) and iff(BB_Y_N=="Y", crossover(price[countback], upperBB), true))plotshape(dataHCSB, title= "HC-SB", color=color.fuchsia, style=shape.circle, text="HC-SB")strategy.entry("HC-Short", strategy.short, comment="HC-Short", when=dataHCSB and window())//******************Exit Conditions******************************// Profit and Loss Exit Calculations//User Options to Change Inputs //Does user want to run a Trailing StoptrailStop_YN=input(title="Run a Trailing Stop", defval="N", options=["Y", "N"])TrailPerc = input(title="Trail Loss %", type=input.float, minval=0, step=1, defval=6) /100// User Options to Change Inputs (%)stopPer = input(15, title='Stop Loss %', type=input.float) / 100takePer = input(25, title='Take Profit %', type=input.float) / 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//or crossunder(price[countback], upperBB) strategy.exit(id="Close Long", when = window(), stop=longStop, limit=longTake)if strategy.position_size < 0 //or crossover(price[countback], lowerBB) strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake)//exit position conditions and ordersif strategy.position_size > 0 //long side exit conditions if 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 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 strategy.position_size < 0 //short side exit conditions if 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 trailStop_YN=="N" strategy.exit(id="Close Short", when = window(), stop=shortStop, limit=shortTake)// sell when either the TGT or the SL is hit// Plot stop trailing loss values for confirmationplot(series=(strategy.position_size > 0 and (trailStop_YN=="Y")) ? 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")) ? 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 (trailStop_YN=="N")) ? 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 (trailStop_YN=="N")) ? 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 the TGT profit pointsplot(series=(strategy.position_size > 0 ) ? 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 ) ? shortTake : na, color=color.lime, style=plot.style_cross, linewidth=2, title="Short Trail Stop") //plot the TGT% for short position