Pine Script v5 · Strategy
SuperTrend Multi Time Frame Long and Short Trading Strategy
A Pine Script v5 strategy built on ATR (22), Highest (close, 22) and Lowest (close, 22). It has 12 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 60% of equity and pays no commission. 18 settings; defaults include Timeframe 1 D, Timeframe 2 W and ATR Period 22.
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 SuperTrend Multi Time Frame Long and Short Trading Strategy repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did SuperTrend Multi Time Frame Long and Short Trading Strategy do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. On BTCUSDT 4h it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
- Does SuperTrend Multi Time Frame Long and Short Trading Strategy run in Pine Script v6?
- No
- The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L29:C63: 'linewidth' must be at least 1 in Pine Script v6. The v6 version is shown for reference and is not offered as a download.
- Does SuperTrend Multi Time Frame Long and Short Trading Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 114, 115, 116, 117, 119, 120, 121, 122.
- Does SuperTrend Multi Time Frame Long and Short Trading Strategy run in Zeal Terminal?
- Yes
- It compiles in 80 ms and runs 1,000 bars in 217 ms, in the browser.
BTCUSDT · 1D · default settings
- Long Stop
- Long Stop Start
- Short Stop
- Short Stop Start
- Timeframe 1 CE
- Timeframe 2 CE
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
One run with the author's default settings, $2,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
1 setting does nothing
“Show Buy/Sell Labels ?” is declared but never read.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Note
1 setting has no label
In the settings dialog KeepLastPosition (line 99) shows an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses an argument v6 removed
when = on line 101, 102, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Timeframe 1 | D | timeframe | ce1_plot ce1 |
| Timeframe 2 | W | timeframe | ce2_plot ce2 |
| ATR Period | 22 | int | atr longStop shortStop atr2 longStop2 shortStop2 |
| ATR Multiplier | 3.0 | float · step 0.1 | atr atr2 |
| Show Buy/Sell Labels ? | true | bool | unused |
| Use Close Price for Extremums ? | true | bool | longStop shortStop longStop2 shortStop2 |
| Highlight State ? | true | bool | longFillColor shortFillColor |
| What trades should be taken : | BOTH | string · options ['LONG', 'SHORT', 'BOTH'] | line 57 line 60 line 63 entry long entry short |
| Backtesting Start Time | timestamp('01 Jun 2021 13:30 +0000') | time | timeCond |
| Backtesting End Time | timestamp('30 Sep 2099 19:30 +0000') | time | timeCond |
| Keep Last Position (no label in the script) | false | bool | exit Exit4 |
| Trades | |||
| Stop Loss % | 5.0 | float · min 0 | sl |
| TP1 % | 5 | int | exit Exit1 plot |
| TP2 % | 10 | int | exit Exit2 plot |
| TP3 % | 15 | int | exit Exit3 plot |
| TP1 Amount % | 10 | int | Qty1 |
| TP2 Amount % | 15 | int | Qty2 |
| TP3 Amount % | 20 | int | Qty3 |
What it calculates
- ATRATR Period (22)
atr · line 12 - Highestclose, ATR Period (22)
longStop · line 13 - Lowestclose, ATR Period (22)
shortStop · line 16 - ATRATR Period (22)
atr2 · line 36 - Highestclose, ATR Period (22)
longStop2 · line 37 - Lowestclose, ATR Period (22)
shortStop2 · line 40 - ValuewhenlongCond, close, 0
long_entry · line 73 - ValuewhenshortCond, close, 0
short_entry · line 74
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 85 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 long long | longCond == true and tradeType != 'SHORT' and timeCond | On | 101 |
| Open short short | shortCond == true and tradeType != 'LONG' and timeCond | On | 102 |
| Exit Exit1 stop and limit | strategy.position_size > 0 | On | 114 |
| Exit Exit2 stop and limit | strategy.position_size > 0 | On | 115 |
| Exit Exit3 stop and limit | strategy.position_size > 0 | On | 116 |
| Exit Exit4 stop | strategy.position_size > 0 and KeepLastPosition == false | On | 117 |
| Close long | long_exitsellSignal and pos[1] == 1 | On | 118 |
| Exit Exit1 stop and limit | strategy.position_size < 0 | On | 119 |
| Exit Exit2 stop and limit | strategy.position_size < 0 | On | 120 |
| Exit Exit3 stop and limit | strategy.position_size < 0 | On | 121 |
| Exit Exit4 stop | strategy.position_size < 0 and KeepLastPosition == false | On | 122 |
| Close short | short_exitbuySignal and pos[1] == -1 | On | 123 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- CheckHigher-timeframe data4 request.security call(s) use lookahead_on (line 48, 49, 50, 51). Without a [1] offset this reads future bars in history.
- 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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.3% | +5.5% | 1 | 0% | 0.00 | 0.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −1.2% | +17.1% | 1 | 0% | 0.00 | 1.4% | 8 May 2026 – 21 Oct 2026 |
Higher-timeframe data for request.security(): BINANCE:BTCUSDT W (Binance BTCUSDT 1w), fetched from Binance's public market data for each market.
Backtests are simulations on historical data and are not investment advice.
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 |
|---|---|---|---|---|---|
| SuperTrend Multi Time Frame Long and Short Trading Strategy this page | — | v5 strategy | 0.0% | 0 | 0.0% |
| Positive Bars - Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −40.3% | 7 | 54.7% |
| Big Bar Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −56.6% | 13 | 57.1% |
| Inside Bar Strategy w- SL by tweakerID | lowest highest valuewhen atr | v4 strategy | −61.3% | 25 | 65.0% |
| Modulo Logic + EMA Strat by tweakerID | lowest highest valuewhen atr | v4 strategy | −50.8% | 16 | 54.7% |
| Delayed RSI Strategy by tweakerID | lowest highest valuewhen atr | v4 strategy | −40.4% | 7 | 42.2% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 12 strategy calls use the when = argument (line 101, 102, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- 5 settings use the untyped input() (line 7, 9, 10, 11, 99). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 1 trades and the same P&L on BTCUSDT 4h, 1,000 bars, in Zeal Terminal's engine.
- Line 7: input() → input.int()
- Line 9: input() → input.bool()
- Line 10: input() → input.bool()
- Line 11: input() → input.bool()
- Line 99: input() → input.bool()
- Line 101: when = longCond == true and tradeType != 'SHORT' and timeCond → if block
11 more edits
- Line 102: when = shortCond == true and tradeType != 'LONG' and timeCond → if block
- Line 114: when = strategy.position_size > 0 → if block
- Line 115: when = strategy.position_size > 0 → if block
- Line 116: when = strategy.position_size > 0 → if block
- Line 117: when = strategy.position_size > 0 and KeepLastPosition == false → if block
- Line 118: when = long_exit → if block
- Line 119: when = strategy.position_size < 0 → if block
- Line 120: when = strategy.position_size < 0 → if block
- Line 121: when = strategy.position_size < 0 → if block
- Line 122: when = strategy.position_size < 0 and KeepLastPosition == false → if block
- Line 123: when = short_exit → if block
⋯tf2 = input.timeframe('W', title='Timeframe 2')-length = input(title='ATR Period', defval=22)+length = input.int(title='ATR Period', defval=22)mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0)-showLabels = input(title='Show Buy/Sell Labels ?', defval=true)+showLabels = input.bool(title='Show Buy/Sell Labels ?', defval=true)-useClose = input(title='Use Close Price for Extremums ?', defval=true)+useClose = input.bool(title='Use Close Price for Extremums ?', defval=true)-highlightState = input(title='Highlight State ?', defval=true)+highlightState = input.bool(title='Highlight State ?', defval=true)atr = mult * ta.atr(length)⋯timeCond = time > i_startTime and time < i_endTime-KeepLastPosition = input(false)+KeepLastPosition = input.bool(false)// Make sure we are within the bar range, Set up entries and exit conditions-strategy.entry('long', strategy.long, when=longCond == true and tradeType != 'SHORT' and timeCond)+if longCond == true and tradeType != 'SHORT' and timeCond+ strategy.entry('long', strategy.long)-strategy.entry('short', strategy.short, when=shortCond == true and tradeType != 'LONG' and timeCond)+if shortCond == true and tradeType != 'LONG' and timeCond+ strategy.entry('short', strategy.short)var float Qty1 = na⋯ Qty4-strategy.exit('Exit1', qty=Qty1, stop=sl_long, limit=strategy.position_avg_price * (1 + tp1percent), when=strategy.position_size > 0)+if strategy.position_size > 0+ strategy.exit('Exit1', qty=Qty1, stop=sl_long, limit=strategy.position_avg_price * (1 + tp1percent))-strategy.exit('Exit2', qty=Qty2, stop=sl_long, limit=strategy.position_avg_price * (1 + tp2percent), when=strategy.position_size > 0)+if strategy.position_size > 0+ strategy.exit('Exit2', qty=Qty2, stop=sl_long, limit=strategy.position_avg_price * (1 + tp2percent))-strategy.exit('Exit3', qty=Qty3, stop=sl_long, limit=strategy.position_avg_price * (1 + tp3percent), when=strategy.position_size > 0)+if strategy.position_size > 0+ strategy.exit('Exit3', qty=Qty3, stop=sl_long, limit=strategy.position_avg_price * (1 + tp3percent))-strategy.exit('Exit4', qty=Qty4, stop=sl_long, when=strategy.position_size > 0 and KeepLastPosition == false)+if strategy.position_size > 0 and KeepLastPosition == false+ strategy.exit('Exit4', qty=Qty4, stop=sl_long)-strategy.close('long', when=long_exit, comment='CE Exit')+if long_exit+ strategy.close('long', comment='CE Exit')-strategy.exit('Exit1', qty=Qty1, stop=sl_short, limit=strategy.position_avg_price * (1 - tp1percent), when=strategy.position_size < 0)+if strategy.position_size < 0+ strategy.exit('Exit1', qty=Qty1, stop=sl_short, limit=strategy.position_avg_price * (1 - tp1percent))-strategy.exit('Exit2', qty=Qty2, stop=sl_short, limit=strategy.position_avg_price * (1 - tp2percent), when=strategy.position_size < 0)+if strategy.position_size < 0+ strategy.exit('Exit2', qty=Qty2, stop=sl_short, limit=strategy.position_avg_price * (1 - tp2percent))-strategy.exit('Exit3', qty=Qty3, stop=sl_short, limit=strategy.position_avg_price * (1 - tp3percent), when=strategy.position_size < 0)+if strategy.position_size < 0+ strategy.exit('Exit3', qty=Qty3, stop=sl_short, limit=strategy.position_avg_price * (1 - tp3percent))-strategy.exit('Exit4', qty=Qty4, stop=sl_short, when=strategy.position_size < 0 and KeepLastPosition == false)+if strategy.position_size < 0 and KeepLastPosition == false+ strategy.exit('Exit4', qty=Qty4, stop=sl_short)-strategy.close('short', when=short_exit, comment='CE Exit')+if short_exit+ strategy.close('short', comment='CE Exit')plot(strategy.position_size > 0 ? strategy.position_avg_price * (1 + tp1percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)⋯Does it run in Pine Script v6?
No
The v6 version does not run in Zeal Terminal's engine: Pine Script v6 Error at @L29:C63: 'linewidth' must be at least 1 in Pine Script v6. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
⋯// © ranga_trading-//@version=5+//@version=6strategy(title='SuperTrend Multi Time Frame Long and Short Trading Strategy with Take Profit, Stop Loss and in build alerts V01', shorttitle='SuperTrend Multi Time Frame Long and Short Trading Strategy with Take Profit, Stop Loss and in build alerts V01 ', overlay=true, default_qty_value=60, initial_capital=2000, default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 80 ms | With a workaround 1,000 bars in 217 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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.
Source
Copied verbatim from the author's published script, with its licence header intact. The author may have updated the original since this snapshot.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © ranga_trading//@version=5strategy(title='SuperTrend Multi Time Frame Long and Short Trading Strategy with Take Profit, Stop Loss and in build alerts V01', shorttitle='SuperTrend Multi Time Frame Long and Short Trading Strategy with Take Profit, Stop Loss and in build alerts V01 ', overlay=true, default_qty_value=60, initial_capital=2000, default_qty_type=strategy.percent_of_equity, pyramiding=0, process_orders_on_close=true)tf1 = input.timeframe('D', title='Timeframe 1')tf2 = input.timeframe('W', title='Timeframe 2')length = input(title='ATR Period', defval=22)mult = input.float(title='ATR Multiplier', step=0.1, defval=3.0)showLabels = input(title='Show Buy/Sell Labels ?', defval=true)useClose = input(title='Use Close Price for Extremums ?', defval=true)highlightState = input(title='Highlight State ?', defval=true)atr = mult * ta.atr(length)longStop = (useClose ? ta.highest(close, length) : ta.highest(length)) - atrlongStopPrev = nz(longStop[1], longStop)longStop := close[1] > longStopPrev ? math.max(longStop, longStopPrev) : longStopshortStop = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + atrshortStopPrev = nz(shortStop[1], shortStop)shortStop := close[1] < shortStopPrev ? math.min(shortStop, shortStopPrev) : shortStopvar int dir = 1dir := close > shortStopPrev ? 1 : close < longStopPrev ? -1 : dirvar color longColor = color.greenvar color shortColor = color.redlongStopPlot = plot(dir == 1 ? longStop : na, title='Long Stop', style=plot.style_linebr, linewidth=2, color=color.new(longColor, 0))buySignal = dir == 1 and dir[1] == -1plotshape(buySignal ? longStop : na, title='Long Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(longColor, 0))shortStopPlot = plot(dir == 1 ? na : shortStop, title='Short Stop', style=plot.style_linebr, linewidth=2, color=color.new(shortColor, 0))sellSignal = dir == -1 and dir[1] == 1plotshape(sellSignal ? shortStop : na, title='Short Stop Start', location=location.absolute, style=shape.circle, size=size.tiny, color=color.new(shortColor, 0))midPricePlot = plot(ohlc4, title='', style=plot.style_circles, linewidth=0, display=display.none, editable=false)longFillColor = highlightState ? dir == 1 ? longColor : na : nashortFillColor = highlightState ? dir == -1 ? shortColor : na : nafill(midPricePlot, longStopPlot, title='Long State Filling', color=longFillColor, transp=90)fill(midPricePlot, shortStopPlot, title='Short State Filling', color=shortFillColor, transp=90)// CE Functionce() => atr2 = mult * ta.atr(length) longStop2 = (useClose ? ta.highest(close, length) : ta.highest(length)) - atr2 longStop2Prev = nz(longStop2[1], longStop2) longStop2 := close[1] > longStop2Prev ? math.max(longStop2, longStop2Prev) : longStop2 shortStop2 = (useClose ? ta.lowest(close, length) : ta.lowest(length)) + atr2 shortStop2Prev = nz(shortStop2[1], shortStop2) shortStop2 := close[1] < shortStop2Prev ? math.min(shortStop2, shortStop2Prev) : shortStop2 var int dir2 = 1 dir2 := close > shortStop2Prev ? 1 : close < longStop2Prev ? -1 : dir2 ce = dir2 == 1 ? longStop2 : shortStop2 [dir2, ce][side, ce_plot] = ce()ce1_plot = request.security(syminfo.tickerid, tf1, ce_plot[1], barmerge.gaps_off, barmerge.lookahead_on)ce2_plot = request.security(syminfo.tickerid, tf2, ce_plot[1], barmerge.gaps_off, barmerge.lookahead_on)ce1 = request.security(syminfo.tickerid, tf1, side[1], barmerge.gaps_off, barmerge.lookahead_on)ce2 = request.security(syminfo.tickerid, tf2, side[1], barmerge.gaps_off, barmerge.lookahead_on)long = buySignal and ce1 > 0 and ce2 > 0short = sellSignal and ce1 < 0 and ce2 < 0tradeType = input.string('BOTH', title='What trades should be taken : ', options=['LONG', 'SHORT', 'BOTH'])// Position Management Toolspos = 0.0if tradeType == 'BOTH' pos := long ? 1 : short ? -1 : pos[1] posif tradeType == 'LONG' pos := long ? 1 : pos[1] posif tradeType == 'SHORT' pos := short ? -1 : pos[1] poslongCond = long and (pos[1] != 1 or na(pos[1]))shortCond = short and (pos[1] != -1 or na(pos[1]))plot(ce1_plot, title='Timeframe 1 CE', color=ce1 > 0 ? #008000 : #800000, linewidth=2)plot(ce2_plot, title='Timeframe 2 CE', color=ce2 > 0 ? color.green : color.red, linewidth=2)// EXIT FUNCTIONS //i_sl = input.float(5.0, title='Stop Loss %', minval=0, group='Trades')sl = i_sl > 0 ? i_sl / 100 : 99999long_entry = ta.valuewhen(longCond, close, 0)short_entry = ta.valuewhen(shortCond, close, 0)// Simple Stop Loss + 2 Take Profitssl_long = strategy.position_avg_price * (1 - sl)sl_short = strategy.position_avg_price * (1 + sl)// Position Adjustmentlong_sl = low < sl_long and pos[1] == 1short_sl = high > sl_short and pos[1] == -1if long_sl or short_sl pos := 0 poslong_exit = sellSignal and pos[1] == 1short_exit = buySignal and pos[1] == -1if long_exit or short_exit pos := 0 postp1percent = input.int(5, title='TP1 %', group='Trades') / 100.0tp2percent = input.int(10, title='TP2 %', group='Trades') / 100.0tp3percent = input.int(15, title='TP3 %', group='Trades') / 100.0tp1amt = input.int(10, title='TP1 Amount %', group='Trades')tp2amt = input.int(15, title='TP2 Amount %', group='Trades')tp3amt = input.int(20, title='TP3 Amount %', group='Trades')// Strategy Backtest Limiting Algorithmi_startTime = input.time(defval=timestamp('01 Jun 2021 13:30 +0000'), title='Backtesting Start Time')i_endTime = input.time(defval=timestamp('30 Sep 2099 19:30 +0000'), title='Backtesting End Time')timeCond = time > i_startTime and time < i_endTimeKeepLastPosition = input(false)// Make sure we are within the bar range, Set up entries and exit conditionsstrategy.entry('long', strategy.long, when=longCond == true and tradeType != 'SHORT' and timeCond)strategy.entry('short', strategy.short, when=shortCond == true and tradeType != 'LONG' and timeCond)var float Qty1 = navar float Qty2 = navar float Qty3 = navar float Qty4 = naif strategy.position_size == 0 equity_q = (strategy.initial_capital + strategy.netprofit) / close Qty1 := equity_q * tp1amt / 100.0 Qty2 := equity_q * tp2amt / 100.0 Qty3 := equity_q * tp3amt / 100.0 Qty4 := equity_q - Qty1 - Qty2 - Qty3 Qty4strategy.exit('Exit1', qty=Qty1, stop=sl_long, limit=strategy.position_avg_price * (1 + tp1percent), when=strategy.position_size > 0)strategy.exit('Exit2', qty=Qty2, stop=sl_long, limit=strategy.position_avg_price * (1 + tp2percent), when=strategy.position_size > 0)strategy.exit('Exit3', qty=Qty3, stop=sl_long, limit=strategy.position_avg_price * (1 + tp3percent), when=strategy.position_size > 0)strategy.exit('Exit4', qty=Qty4, stop=sl_long, when=strategy.position_size > 0 and KeepLastPosition == false)strategy.close('long', when=long_exit, comment='CE Exit')strategy.exit('Exit1', qty=Qty1, stop=sl_short, limit=strategy.position_avg_price * (1 - tp1percent), when=strategy.position_size < 0)strategy.exit('Exit2', qty=Qty2, stop=sl_short, limit=strategy.position_avg_price * (1 - tp2percent), when=strategy.position_size < 0)strategy.exit('Exit3', qty=Qty3, stop=sl_short, limit=strategy.position_avg_price * (1 - tp3percent), when=strategy.position_size < 0)strategy.exit('Exit4', qty=Qty4, stop=sl_short, when=strategy.position_size < 0 and KeepLastPosition == false)strategy.close('short', when=short_exit, comment='CE Exit')plot(strategy.position_size > 0 ? strategy.position_avg_price * (1 + tp1percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)plot(strategy.position_size > 0 ? strategy.position_avg_price * (1 + tp2percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)plot(strategy.position_size > 0 ? strategy.position_avg_price * (1 + tp3percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)plot(strategy.position_size > 0 ? sl_long : na, color=color.new(color.red, 0), style=plot.style_linebr)plot(strategy.position_size > 0 ? strategy.position_avg_price : na, color=color.new(color.gray, 0), style=plot.style_linebr)plot(strategy.position_size < 0 ? strategy.position_avg_price * (1 - tp1percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)plot(strategy.position_size < 0 ? strategy.position_avg_price * (1 - tp2percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)plot(strategy.position_size < 0 ? strategy.position_avg_price * (1 - tp3percent) : na, color=color.new(color.green, 0), style=plot.style_linebr)plot(strategy.position_size < 0 ? sl_short : na, color=color.new(color.red, 0), style=plot.style_linebr)plot(strategy.position_size < 0 ? strategy.position_avg_price : na, color=color.new(color.gray, 0), style=plot.style_linebr)