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
- 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
VerifiedSame 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⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Reverse Trades | false | bool | entry long entry short |
| ADX Close | true | bool | close_all line 63 |
| Use Swing Lo/Hi Stop Loss & Take Profit | false | bool | exit longexit exit shortexit line 65 SL TP |
| Swing Lo/Hi Lookback | 20 | int | SwingLow SwingHigh |
| SL Expander | 0 | int · step .5 | LSL SSL |
| TP Expander | 0 | int · step .5 | lTP sTP |
| ADX Smoothing | 14 | int | adx sig |
| DI Length | 20 | int | plus minus sig |
| Adxlevel (no label in the script) | 20 | int · step 5 | BUY SELL close_all |
| -----------BB Inputs----------- | false | bool | unused |
| Length (no label in the script) | 20 | int · min 1 | basis dev |
| Mult (no label in the script) | 2.0 | float · min 0.001 · max 50 | dev |
| MAlen (no label in the script) | 9 | int | unused |
What it calculates
- Change (
change,ta.changein v5)highup · line 20 - Change (
change,ta.changein v5)lowdown · line 21 - RMA (
rma,ta.rmain v5)tr, lentruerange · line 24 - RMA (
rma,ta.rmain v5)abs(plus - minus) / (sum == 0 ? 1 : sum), ADX Smoothing (14)adx · line 31 - SMA (
sma,ta.smain v5)source, Length (20)basis · line 40 - Stdev (
stdev,ta.stdevin v5)source, Length (20)dev · line 41 - Lowest (
lowest,ta.lowestin v5)Swing Lo/Hi Lookback (20)SwingLow · line 48 - Highest (
highest,ta.highestin v5)Swing Lo/Hi Lookback (20)SwingHigh · line 49 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0LSL · line 51 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0SSL · line 52 - Valuewhen / ATR (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0lTP · line 53 - Valuewhen / ATR (
valuewhen,ta.valuewhenin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long i_reverse?false:true | BUYcrossover(source, lower) and sig < adxlevel | On | 60 |
| Open short i_reverse?true:false | SELLcrossunder(source, upper) and sig < adxlevel | On | 61 |
| Close | sig > adxlevel | On “ADX Close” | 64 |
| Exit longexit stop and limit | i_SL | Off “Use Swing Lo/Hi Stop Loss & Take Profit” | 66 |
| Exit shortexit stop and limit | i_SL | Off “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.
- 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 | −9.9% | +102.5% | 19 | 47% | 0.84 | 33.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.1% | +5.5% | 19 | 53% | 0.95 | 10.6% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −47.6% | +9.8% | 10 | 20% | 0.30 | 55.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −9.0% | +8.4% | 18 | 67% | 0.70 | 22.4% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −81.4% | +16.7% | 13 | 23% | 0.15 | 83.6% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +1.2% | +21.5% | 16 | 56% | 1.04 | 21.1% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −6.8% | +148.3% | 16 | 56% | 0.91 | 35.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +34.1% | +17.1% | 18 | 67% | 3.35 | 7.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 8 | 38% | −$1,140.46 |
| Sideways market | 10 | 60% | $666.43 |
| Falling market | 1 | 0% | −$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.
All 19 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 15 Apr 2024 65,661.90 | 1 May 2024 60,672.00 | 16 | −$767.32 |
| short | 17 May 2024 65,235.20 | 16 Jun 2024 66,228.30 | 30 | −$147.93 |
| long | 16 Jun 2024 66,228.30 | 28 Jun 2024 61,706.50 | 12 | −$627.30 |
| short | 25 Aug 2024 64,157.00 | 8 Sep 2024 54,160.90 | 14 | $1,310.97 |
| short | 21 Sep 2024 63,201.10 | 9 Nov 2024 76,509.80 | 49 | −$2,064.83 |
| short | 8 Jan 2025 96,954.60 | 7 Feb 2025 96,554.40 | 30 | $25.64 |
| long | 7 Feb 2025 96,554.40 | 12 Feb 2025 95,778.20 | 5 | −$68.29 |
| short | 26 Apr 2025 94,638.70 | 1 May 2025 94,172.00 | 5 | $31.65 |
| long | 7 Jun 2025 104,288.00 | 16 Jul 2025 117,758.00 | 39 | $986.61 |
| short | 16 Jul 2025 117,758.00 | 18 Jul 2025 119,178.00 | 2 | −$111.61 |
| long | 4 Aug 2025 114,209.00 | 15 Aug 2025 118,295.00 | 11 | $299.44 |
| short | 15 Aug 2025 118,295.00 | 27 Aug 2025 111,763.00 | 12 | $482.71 |
| long | 27 Aug 2025 111,763.00 | 14 Sep 2025 115,918.00 | 18 | $339.97 |
| short | 14 Sep 2025 115,918.00 | 27 Sep 2025 109,643.00 | 13 | $516.98 |
| long | 27 Sep 2025 109,643.00 | 5 Oct 2025 122,391.00 | 8 | $1,177.98 |
| short | 5 Oct 2025 122,391.00 | 7 Oct 2025 124,659.00 | 2 | −$220.12 |
| long | 6 Nov 2025 103,885.00 | 7 Nov 2025 101,346.00 | 1 | −$281.59 |
| short | 8 Jan 2026 91,364.20 | 16 Jan 2026 95,604.80 | 8 | −$513.84 |
| long | 24 May 2026 76,752.00 | 3 Jun 2026 66,760.80 | 10 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Bollinger Bands + ADX Strategy this page | — | v4 strategy | −9.9% | 19 | 33.0% |
| ATR GOD Strategy by TradeSmart (PineConnector-compatible) by TradeSmart22 | atr lowest highest sma stdev rma | v5 strategy | No trades on this market | ||
| Strategy Myth-Busting #7 - MACDBB+SSL+VSF - [MYN] by myncrypto | stdev sma highest lowest valuewhen rma | v5 strategy | No trades on this market | ||
| Money Flow Index 5 min Strategy by tweakerID | lowest highest valuewhen atr sma | v4 strategy | −6.8% | 18 | 28.4% |
| MA Bollinger Bands + RSI by LucasVivien | stdev sma atr lowest highest | v5 strategy | −73.9% | 4 | 77.3% |
| HiLo Extension by Saravanan_Ragavan | valuewhen lowest highest rma | v4 strategy | No trades on this market | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 21 ms | With a workaround 1,000 bars in 60 ms | Not measured |
| PineTS | No 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)