Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Bollinger Bands + ADX Strategy

A Pine Script v4 strategy built on Change (high), RMA (tr, len) and SMA (source, 20). It has 5 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 100% of equity and pays 0.04 commission. 13 settings; defaults include Swing Lo/Hi Lookback 20, SL Expander 0 and TP Expander 0.

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 Bollinger Bands + ADX Strategy repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
How did Bollinger Bands + ADX Strategy do on BTCUSDT 1D with default settings?
−9.88%
From 26 Jan 2024 to 21 Oct 2026, over 19 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Bollinger Bands + ADX 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 Bollinger Bands + ADX Strategy use a stop loss?
Yes
strategy.exit sets a stop on line 66, 67.
Does Bollinger Bands + ADX Strategy run in Zeal Terminal?
Yes
It compiles in 21 ms and runs 1,000 bars in 60 ms, in the browser.

BTCUSDT · 1D · default settings

  • Plot
  • Plot
  • 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 (13)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$9,012$20,253Equity from $10,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. Zeal Terminal's engine currently reads an order side written as long = i_reverse?false:true, long = i_reverse?true:false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 60, 61) so the engine trades the way the script says.
Net profit
−9.88%
−$987.86
Buy and hold
+102.5%
same period
Closed trades
19
9 won · 10 lost
Profitable
47.4%
of closed trades
Profit factor
0.84
gross profit ÷ gross loss
Max drawdown
33.03%
−$3,302.87

One run with the author's default settings, $10,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

    2 settings do nothing

    “-----------BB Inputs-----------”, “MAlen” are declared but never read.

  • Check

    Every trade uses all the equity

    default_qty_value is 100% of equity.

  • Note

    4 settings have no label

    In the settings dialog adxlevel (line 33), length (line 36), mult (line 37), MAlen (line 38) show 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 60, 61, 64. The upgraded version below moves each call inside an if block.

  • Note

    Uses long =, which v5 renamed line 60

    long = on line 60, 61. v5 writes it as direction = strategy.long or strategy.short.

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 change → ta.change, rma → ta.rma, abs → math.abs, sma → ta.sma, stdev → ta.stdev, crossover → ta.crossover, crossunder → ta.crossunder, lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, atr → ta.atr.
  • 3 strategy calls use the when = argument (line 60, 61, 64). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

Same 19 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 7: //@version=4 → //@version=5
  • Line 20: change → ta.change
  • Line 21: change → ta.change
  • Line 24: rma → ta.rma
  • Line 24: tr → ta.tr
  • Line 25: rma → ta.rma
23 more edits
  • Line 26: rma → ta.rma
  • Line 31: rma → ta.rma
  • Line 31: abs → math.abs
  • Line 40: sma → ta.sma
  • Line 41: stdev → ta.stdev
  • Line 45: crossover → ta.crossover
  • Line 46: crossunder → ta.crossunder
  • Line 48: lowest → ta.lowest
  • Line 49: highest → ta.highest
  • Line 51: valuewhen → ta.valuewhen
  • Line 51: valuewhen → ta.valuewhen
  • Line 51: atr → ta.atr
  • Line 52: valuewhen → ta.valuewhen
  • Line 52: valuewhen → ta.valuewhen
  • Line 52: atr → ta.atr
  • Line 53: valuewhen → ta.valuewhen
  • Line 53: valuewhen → ta.valuewhen
  • Line 53: atr → ta.atr
  • Line 54: valuewhen → ta.valuewhen
  • Line 54: valuewhen → ta.valuewhen
  • Line 54: atr → ta.atr
  • Line 60: long = i_reverse?false:true → direction = i_reverse?strategy.short:strategy.long
  • Line 61: long = i_reverse?true:false → direction = i_reverse?strategy.long:strategy.short
⋯// below a certain level, and exits all trades when it's above it.-//@version=4+//@version=5strategy("BB + ADX Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)⋯dirmov(len) =>-    up = change(high)+    up = ta.change(high)-    down = -change(low)+    down = -ta.change(low)    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)-    truerange = rma(tr, len)+    truerange = ta.rma(ta.tr, len)-    plus = fixnan(100 * rma(plusDM, len) / truerange)+    plus = fixnan(100 * ta.rma(plusDM, len) / truerange)-    minus = fixnan(100 * rma(minusDM, len) / truerange)+    minus = fixnan(100 * ta.rma(minusDM, len) / truerange)    [plus, minus]⋯    sum = plus + minus-    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)+    adx = 100 * ta.rma(math.abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)sig = adx(dilen, adxlen)⋯source = close-basis = sma(source, length)+basis = ta.sma(source, length)-dev = mult * stdev(source, length)+dev = mult * ta.stdev(source, length)upper = basis + dev⋯//Entry Logic-BUY = crossover(source, lower) and sig < adxlevel+BUY = ta.crossover(source, lower) and sig < adxlevel-SELL = crossunder(source, upper) and sig < adxlevel+SELL = ta.crossunder(source, upper) and sig < adxlevel//SL & TP Calculations-SwingLow=lowest(i_SwingLookback)+SwingLow=ta.lowest(i_SwingLookback)-SwingHigh=highest(i_SwingLookback)+SwingHigh=ta.highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]-LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0))*i_SLExpander)+LSL=ta.valuewhen(bought, SwingLow, 0)-((ta.valuewhen(bought, ta.atr(14), 0))*i_SLExpander)-SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0))*i_SLExpander)+SSL=ta.valuewhen(bought, SwingHigh, 0)+((ta.valuewhen(bought, ta.atr(14), 0))*i_SLExpander)-lTP=strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0))+((valuewhen(bought, atr(14), 0))*i_TPExpander))+lTP=strategy.position_avg_price + (strategy.position_avg_price-(ta.valuewhen(bought, SwingLow, 0))+((ta.valuewhen(bought, ta.atr(14), 0))*i_TPExpander))-sTP=strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0)-strategy.position_avg_price)-((valuewhen(bought, atr(14), 0))*i_TPExpander)+sTP=strategy.position_avg_price - (ta.valuewhen(bought, SwingHigh, 0)-strategy.position_avg_price)-((ta.valuewhen(bought, ta.atr(14), 0))*i_TPExpander)islong=strategy.position_size > 0⋯//Entries-strategy.entry("long", long=i_reverse?false:true, when=BUY)+strategy.entry("long", direction = i_reverse?strategy.short:strategy.long, when=BUY)-strategy.entry("short", long=i_reverse?true:false, when=SELL)+strategy.entry("short", direction = i_reverse?strategy.long:strategy.short, when=SELL)//EXITS⋯

Clean v5

VerifiedDownload the v5 script

Same 19 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 10: input() → input.bool()
  • Line 11: input() → input.bool()
  • Line 12: input() → input.bool()
  • Line 13: input() → input.int()
  • Line 14: input() → input.int()
  • Line 15: input() → input.int()
10 more edits
  • Line 17: input() → input.int()
  • Line 18: input() → input.int()
  • Line 33: input() → input.int()
  • Line 35: input() → input.bool()
  • Line 36: input() → input.int()
  • Line 37: input() → input.float()
  • Line 38: input() → input.int()
  • Line 60: when = BUY → if block
  • Line 61: when = SELL → if block
  • Line 64: when = sig > adxlevel → if block
⋯//Inputs-i_reverse=input(false, title="Reverse Trades")+i_reverse=input.bool(false, title="Reverse Trades")-i_ADXClose=input(true, title="ADX Close")+i_ADXClose=input.bool(true, title="ADX Close")-i_SL=input(false, title="Use Swing Lo/Hi Stop Loss & Take Profit")+i_SL=input.bool(false, title="Use Swing Lo/Hi Stop Loss & Take Profit")-i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")+i_SwingLookback=input.int(20, title="Swing Lo/Hi Lookback")-i_SLExpander=input(defval=0, step=.5, title="SL Expander")+i_SLExpander=input.int(defval=0, step=.5, title="SL Expander")-i_TPExpander=input(defval=0, step=.5, title="TP Expander")+i_TPExpander=input.int(defval=0, step=.5, title="TP Expander")//ADX Calculations-adxlen = input(14, title="ADX Smoothing")+adxlen = input.int(14, title="ADX Smoothing")-dilen = input(20, title="DI Length")+dilen = input.int(20, title="DI Length")dirmov(len) =>⋯sig = adx(dilen, adxlen)-adxlevel=input(20, step=5)+adxlevel=input.int(20, step=5)//BB Calculations-BBCALC=input(false, title="-----------BB Inputs-----------")+BBCALC=input.bool(false, title="-----------BB Inputs-----------")-length = input(20, minval=1)+length = input.int(20, minval=1)-mult = input(2.0, minval=0.001, maxval=50)+mult = input.float(2.0, minval=0.001, maxval=50)-MAlen=input(defval=9)+MAlen=input.int(defval=9)source = close⋯//Entries-strategy.entry("long", direction = i_reverse?strategy.short:strategy.long, when=BUY)+if BUY+    strategy.entry("long", direction = i_reverse?strategy.short:strategy.long)-strategy.entry("short", direction = i_reverse?strategy.long:strategy.short, when=SELL)+if SELL+    strategy.entry("short", direction = i_reverse?strategy.long:strategy.short)//EXITSif i_ADXClose-    strategy.close_all(when=sig > adxlevel)+    if sig > adxlevel+        strategy.close_all()if i_SL⋯

Does it run in Pine Script v6?

No

The v6 version runs in Zeal Terminal's engine, but its results differ from the v5 version: Trades differ: 19 before, 25 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.

⋯// below a certain level, and exits all trades when it's above it.-//@version=5+//@version=6strategy("BB + ADX Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Reverse Tradesfalseboolentry long entry short
ADX Closetrueboolclose_all line 63
Use Swing Lo/Hi Stop Loss & Take Profitfalseboolexit longexit exit shortexit line 65 SL TP
Swing Lo/Hi Lookback20intSwingLow SwingHigh
SL Expander0int · step .5LSL SSL
TP Expander0int · step .5lTP sTP
ADX Smoothing14intadx sig
DI Length20intplus minus sig
Adxlevel (no label in the script)20int · step 5BUY SELL close_all
-----------BB Inputs-----------falseboolunused
Length (no label in the script)20int · min 1basis dev
Mult (no label in the script)2.0float · min 0.001 · max 50dev
MAlen (no label in the script)9intunused

What it calculates

  • Change (change, ta.change in v5)highup · line 20
  • Change (change, ta.change in v5)lowdown · line 21
  • RMA (rma, ta.rma in v5)tr, lentruerange · line 24
  • RMA (rma, ta.rma in v5)abs(plus - minus) / (sum == 0 ? 1 : sum), ADX Smoothing (14)adx · line 31
  • SMA (sma, ta.sma in v5)source, Length (20)basis · line 40
  • Stdev (stdev, ta.stdev in v5)source, Length (20)dev · line 41
  • Lowest (lowest, ta.lowest in v5)Swing Lo/Hi Lookback (20)SwingLow · line 48
  • Highest (highest, ta.highest in v5)Swing Lo/Hi Lookback (20)SwingHigh · line 49
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingLow, 0LSL · line 51
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingHigh, 0SSL · line 52
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingLow, 0lTP · line 53
  • Valuewhen / ATR (valuewhen, ta.valuewhen in v5)bought, SwingHigh, 0sTP · line 54

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.

highhighprice datalowlowprice dataadxlenADX Smoothingsetting · 14lengthLengthsetting · 20multMultsetting · 2.0i_SwingLookbackSwing Lo/Hi Lookbacksetting · 20i_SLExpanderSL Expandersetting · 0i_TPExpanderTP Expandersetting · 0dilenDI Lengthsetting · 20closecloseprice dataadxlevelAdxlevelsetting · 20upChange→ updownChange→ downtruerangeRMA→ truerangeadxRMA→ adxbasisSMA→ basisdevStdev→ devSwingLowLowest→ SwingLowSwingHighHighest→ SwingHighLSLValuewhen / ATR→ LSLSSLValuewhen / ATR→ SSLlTPValuewhen / ATR→ lTPsTPValuewhen / ATR→ sTPplusDM, minusDMplusDM · minusDM2 valuesplus, minusplus · minus2 valuessumsumvaluesigsigvaluesourcesourcevalueupper, lowerupper · lower2 valuesSLSLvalueTPTPvalueBUYBUYconditionSELLSELLconditionbought, islong, isshortbought · islong +13 conditionsentry:longOpen longstrategy.entryentry:shortOpen shortstrategy.entryclose_all:close_all strategy.close_allexit:longexitexit longexitstrategy.exitexit:shortexitexit shortexitstrategy.exitplot:69SLplotplot:70TPplotplot:71plotplotplot:72plotplot

Order rules

OrderFires whenBy defaultLine
Open long i_reverse?false:trueBUYcrossover(source, lower) and sig < adxlevelOn60
Open short i_reverse?true:falseSELLcrossunder(source, upper) and sig < adxlevelOn61
Close sig > adxlevelOn “ADX Close”64
Exit longexit stop and limiti_SLOff “Use Swing Lo/Hi Stop Loss & Take Profit”66
Exit shortexit stop and limiti_SLOff “Use Swing Lo/Hi Stop Loss & Take Profit”67

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.

2/2trades 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−9.9%+102.5%1947%0.8433.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−1.1%+5.5%1953%0.9510.6%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−47.6%+9.8%1020%0.3055.3%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−9.0%+8.4%1867%0.7022.4%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−81.4%+16.7%1323%0.1583.6%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+1.2%+21.5%1656%1.0421.1%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−6.8%+148.3%1656%0.9135.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+34.1%+17.1%1867%3.357.3%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 market838%−$1,140.46
Sideways market1060%$666.43
Falling market10%−$513.84

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

42.0%

The 95th percentile of the worst drawdown when the same 19 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 22.7% to 42.0%; the tick is what actually happened, 23.4%. Order luck alone moves it this much.

0%48%
All 19 trades
SideEntryExitBarsP&L
long15 Apr 2024 65,661.901 May 2024 60,672.0016−$767.32
short17 May 2024 65,235.2016 Jun 2024 66,228.3030−$147.93
long16 Jun 2024 66,228.3028 Jun 2024 61,706.5012−$627.30
short25 Aug 2024 64,157.008 Sep 2024 54,160.9014$1,310.97
short21 Sep 2024 63,201.109 Nov 2024 76,509.8049−$2,064.83
short8 Jan 2025 96,954.607 Feb 2025 96,554.4030$25.64
long7 Feb 2025 96,554.4012 Feb 2025 95,778.205−$68.29
short26 Apr 2025 94,638.701 May 2025 94,172.005$31.65
long7 Jun 2025 104,288.0016 Jul 2025 117,758.0039$986.61
short16 Jul 2025 117,758.0018 Jul 2025 119,178.002−$111.61
long4 Aug 2025 114,209.0015 Aug 2025 118,295.0011$299.44
short15 Aug 2025 118,295.0027 Aug 2025 111,763.0012$482.71
long27 Aug 2025 111,763.0014 Sep 2025 115,918.0018$339.97
short14 Sep 2025 115,918.0027 Sep 2025 109,643.0013$516.98
long27 Sep 2025 109,643.005 Oct 2025 122,391.008$1,177.98
short5 Oct 2025 122,391.007 Oct 2025 124,659.002−$220.12
long6 Nov 2025 103,885.007 Nov 2025 101,346.001−$281.59
short8 Jan 2026 91,364.2016 Jan 2026 95,604.808−$513.84
long24 May 2026 76,752.003 Jun 2026 66,760.8010−$1,357.01

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
Bollinger Bands + ADX Strategy this page—v4 strategy−9.9%1933.0%
ATR GOD Strategy by TradeSmart (PineConnector-compatible) by TradeSmart22atr lowest highest sma stdev rmav5 strategyNo trades on this market
Strategy Myth-Busting #7 - MACDBB+SSL+VSF - [MYN] by myncryptostdev sma highest lowest valuewhen rmav5 strategyNo trades on this market
Money Flow Index 5 min Strategy by tweakerIDlowest highest valuewhen atr smav4 strategy−6.8%1828.4%
MA Bollinger Bands + RSI by LucasVivienstdev sma atr lowest highestv5 strategy−73.9%477.3%
HiLo Extension by Saravanan_Ragavanvaluewhen lowest highest rmav4 strategyNo trades on this market

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 21 msWith a workaround 1,000 bars in 60 msNot measured
PineTSNo v5 and v6 only—Not measured

Zeal Terminal's engine currently reads an order side written as long = i_reverse?false:true, long = i_reverse?true:false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 60, 61) so the engine trades the way the script says.

“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.

// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © tweakerID// This strategy uses Bollinger Bands to buy when the price // crosses over the lower band and sell when it crosses down// the upper band. It only takes trades when the ADX is // below a certain level, and exits all trades when it's above it.//@version=4strategy("BB + ADX Strategy", overlay=true, default_qty_type=strategy.percent_of_equity, default_qty_value=100, commission_value = 0.04, initial_capital=10000)//Inputsi_reverse=input(false, title="Reverse Trades")i_ADXClose=input(true, title="ADX Close")i_SL=input(false, title="Use Swing Lo/Hi Stop Loss & Take Profit")i_SwingLookback=input(20, title="Swing Lo/Hi Lookback")i_SLExpander=input(defval=0, step=.5, title="SL Expander")i_TPExpander=input(defval=0, step=.5, title="TP Expander")//ADX Calculationsadxlen = input(14, title="ADX Smoothing")dilen = input(20, title="DI Length")dirmov(len) =>    up = change(high)    down = -change(low)    plusDM = na(up) ? na : (up > down and up > 0 ? up : 0)    minusDM = na(down) ? na : (down > up and down > 0 ? down : 0)    truerange = rma(tr, len)    plus = fixnan(100 * rma(plusDM, len) / truerange)    minus = fixnan(100 * rma(minusDM, len) / truerange)    [plus, minus]adx(dilen, adxlen) =>    [plus, minus] = dirmov(dilen)    sum = plus + minus    adx = 100 * rma(abs(plus - minus) / (sum == 0 ? 1 : sum), adxlen)sig = adx(dilen, adxlen)adxlevel=input(20, step=5)//BB CalculationsBBCALC=input(false, title="-----------BB Inputs-----------")length = input(20, minval=1)mult = input(2.0, minval=0.001, maxval=50)MAlen=input(defval=9)source = closebasis = sma(source, length)dev = mult * stdev(source, length)upper = basis + devlower = basis - dev//Entry LogicBUY = crossover(source, lower) and sig < adxlevelSELL = crossunder(source, upper) and sig < adxlevel//SL & TP CalculationsSwingLow=lowest(i_SwingLookback)SwingHigh=highest(i_SwingLookback)bought=strategy.position_size != strategy.position_size[1]LSL=valuewhen(bought, SwingLow, 0)-((valuewhen(bought, atr(14), 0))*i_SLExpander)SSL=valuewhen(bought, SwingHigh, 0)+((valuewhen(bought, atr(14), 0))*i_SLExpander)lTP=strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0))+((valuewhen(bought, atr(14), 0))*i_TPExpander))sTP=strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0)-strategy.position_avg_price)-((valuewhen(bought, atr(14), 0))*i_TPExpander)islong=strategy.position_size > 0isshort=strategy.position_size < 0SL= islong ? LSL : isshort ? SSL : naTP= islong ? lTP : isshort ? sTP : na//Entriesstrategy.entry("long", long=i_reverse?false:true, when=BUY)strategy.entry("short", long=i_reverse?true:false, when=SELL)//EXITSif i_ADXClose    strategy.close_all(when=sig > adxlevel)if i_SL    strategy.exit("longexit", "long", stop=SL, limit=TP)    strategy.exit("shortexit", "short", stop=SL, limit=TP)//Plots plot(i_SL ? SL : na, color=color.red, style=plot.style_cross, title="SL")plot(i_SL ? TP : na, color=color.green, style=plot.style_cross, title="TP")plot(upper)plot(lower)