Pine Script v4 · Strategy
ADX strategy (considering ADX and +DI only )
A Pine Script v4 strategy built on SMA (DX, 11) and EMA (close, 13). It has 4 order rules, 2 entries among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is 3 contracts and pays no commission. 5 settings; defaults include ADX Length 11, threshold 30 and Fast EMA 13.
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 ADX strategy (considering ADX and +DI only ) repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
- How did ADX strategy (considering ADX and +DI only ) do on BTCUSDT 1D with default settings?
- +678.85%
- From 26 Jan 2024 to 21 Oct 2026, over 13 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can ADX strategy (considering ADX and +DI only ) 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 ADX strategy (considering ADX and +DI only ) use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does ADX strategy (considering ADX and +DI only ) run in Zeal Terminal?
- Yes
- It compiles in 28 ms and runs 1,000 bars in 47 ms, in the browser.
BTCUSDT · 1D · default settings
- DI+
- ADX
- HLine
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +678.85%
- $67,884.60
- Buy and hold
- +102.5%
- same period
- Closed trades
- 13
- 7 won · 6 lost
- Profitable
- 53.8%
- of closed trades
- Profit factor
- 1.45
- gross profit ÷ gross loss
- Max drawdown
- 693.46%
- −$117,728.00
One run with the author's default settings, $10,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.
-
Check
No stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
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 33, 37, 40, 43. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 33
long = on line 33, 37. 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 max → math.max, abs → math.abs, sma → ta.sma, ema → ta.ema, crossover → ta.crossover, crossunder → ta.crossunder.
- 4 strategy calls use the when = argument (line 33, 37, 40, 43). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 13 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 3: //@version=4 → //@version=5
- Line 14: max → math.max
- Line 14: max → math.max
- Line 14: abs → math.abs
- Line 14: abs → math.abs
- Line 15: max → math.max
10 more edits
- Line 16: max → math.max
- Line 22: abs → math.abs
- Line 23: sma → ta.sma
- Line 28: ema → ta.ema
- Line 29: ema → ta.ema
- Line 31: crossover → ta.crossover
- Line 33: long = true → direction = strategy.long
- Line 37: long = true → direction = strategy.long
- Line 37: crossover → ta.crossover
- Line 42: crossunder → ta.crossunder
⋯// © mohanee-//@version=4+//@version=5//ADX strategy⋯stopLoss =input(8, title="Stop Loss",minval=1) //-TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))+TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))-DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0+DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? math.max(high-nz(high[1]), 0): 0-DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0+DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? math.max(nz(low[1])-low, 0): 0SmoothedTrueRange:= nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRange⋯DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100-DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100+DX = math.abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100-ADX = sma(DX, len)+ADX = ta.sma(DX, len)plot(DIPlus, color=color.green, title="DI+")⋯hline(threshold, color=color.black, linestyle=hline.style_dashed)-fastEmaVal=ema(close,fastEma)+fastEmaVal=ta.ema(close,fastEma)-slowEmaVal=ema(close,slowEma)+slowEmaVal=ta.ema(close,slowEma)//long condition-longCondition= ADX < threshold and crossover(DIPlus,ADX) and fastEmaVal > slowEmaVal+longCondition= ADX < threshold and ta.crossover(DIPlus,ADX) and fastEmaVal > slowEmaValbarcolor(longCondition ? color.yellow: na)-strategy.entry(id="ADXLE", long=true, when= longCondition and strategy.position_size<1) +strategy.entry(id="ADXLE", direction = strategy.long, when= longCondition and strategy.position_size<1) barcolor(strategy.position_size>1 ? color.blue: na)⋯//Add-strategy.entry(id="ADXLE", comment="Add", long=true, when= strategy.position_size>1 and close<strategy.position_avg_price and crossover(DIPlus,ADX) ) +strategy.entry(id="ADXLE", comment="Add", direction = strategy.long, when= strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(DIPlus,ADX) ) //calculate stop Loss⋯//exit condition-exitCondition= ADX > threshold and crossunder(DIPlus,ADX) // and fastEmaVal > slowEmaVal+exitCondition= ADX > threshold and ta.crossunder(DIPlus,ADX) // and fastEmaVal > slowEmaValstrategy.close(id="ADXLE",comment="TPExitAll", qty=strategy.position_size , when= exitCondition) //close allSame 13 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 11: input() → input.int()
- Line 12: input() → input.int()
- Line 13: input() → input.int()
- Line 33: when = longCondition and strategy.position_size<1 → if block
3 more edits
- Line 37: when = strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(DIPlus,ADX) → if block
- Line 40: when = close<stopLossVal → if block
- Line 43: when = exitCondition → if block
⋯strategy(title="ADX strategy", overlay=false,pyramiding=3, default_qty_type=strategy.fixed, default_qty_value=3, initial_capital=10000, currency=currency.USD)-len = input(11, title="ADX Length", minval=1)+len = input.int(11, title="ADX Length", minval=1)-threshold = input(30, title="threshold", minval=5)+threshold = input.int(30, title="threshold", minval=5)-fastEma=input(13, title="Fast EMA",minval=1, maxval=50)+fastEma=input.int(13, title="Fast EMA",minval=1, maxval=50)-slowEma=input(55, title="Slow EMA",minval=10, maxval=200)+slowEma=input.int(55, title="Slow EMA",minval=10, maxval=200)-stopLoss =input(8, title="Stop Loss",minval=1) //+stopLoss =input.int(8, title="Stop Loss",minval=1) //TrueRange = math.max(math.max(high-low, math.abs(high-nz(close[1]))), math.abs(low-nz(close[1])))⋯barcolor(longCondition ? color.yellow: na)-strategy.entry(id="ADXLE", direction = strategy.long, when= longCondition and strategy.position_size<1) +if longCondition and strategy.position_size<1+ strategy.entry(id="ADXLE", direction = strategy.long) barcolor(strategy.position_size>1 ? color.blue: na)⋯//Add-strategy.entry(id="ADXLE", comment="Add", direction = strategy.long, when= strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(DIPlus,ADX) ) +if strategy.position_size>1 and close<strategy.position_avg_price and ta.crossover(DIPlus,ADX)+ strategy.entry(id="ADXLE", comment="Add", direction = strategy.long ) //calculate stop LossstopLossVal = strategy.position_avg_price - (strategy.position_avg_price*stopLoss*0.01)-strategy.close(id="ADXLE",comment="SL Exit", when=close<stopLossVal) //close all on stop loss+if close<stopLossVal+ strategy.close(id="ADXLE",comment="SL Exit") //close all on stop loss//exit conditionexitCondition= ADX > threshold and ta.crossunder(DIPlus,ADX) // and fastEmaVal > slowEmaVal-strategy.close(id="ADXLE",comment="TPExitAll", qty=strategy.position_size , when= exitCondition) //close all+if exitCondition+ strategy.close(id="ADXLE",comment="TPExitAll", qty=strategy.position_size ) //close allDoes 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: 13 before, 0 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.
⋯// © mohanee-//@version=5+//@version=6//ADX strategy⋯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 |
|---|---|---|---|
| ADX Length | 11 | int · min 1 | SmoothedTrueRange SmoothedDirectionalMovementPlus SmoothedDirectionalMovementMinus ADX |
| threshold | 30 | int · min 5 | longCondition exitCondition hline |
| Fast EMA | 13 | int · min 1 · max 50 | fastEmaVal |
| Slow EMA | 55 | int · min 10 · max 200 | slowEmaVal |
| Stop Loss | 8 | int · min 1 | stopLossVal |
What it calculates
- SMA (
sma,ta.smain v5)DX, ADX Length (11)ADX · line 23 - EMA (
ema,ta.emain v5)close, Fast EMA (13)fastEmaVal · line 28 - EMA (
ema,ta.emain v5)close, Slow EMA (55)slowEmaVal · line 29
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 ADXLE long | longCondition and strategy.position_size<1 | On | 33 |
| Open ADXLE long | strategy.position_size>1 and close<strategy.position_avg_price and crossover(DIPlus,ADX) | On | 37 |
| Close ADXLE | close<stopLossVal | On | 40 |
| Close ADXLE | exitConditionADX > threshold and crossunder(DIPlus,ADX) | On | 43 |
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 | +678.8% | +102.5% | 13 | 54% | 1.45 | 693.5% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +401.4% | +5.5% | 11 | 73% | 2.14 | 450.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −59.3% | +9.8% | 13 | 23% | 0.47 | 99.9% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +37.5% | +8.4% | 14 | 79% | 2.62 | 14.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +2.7% | +16.7% | 13 | 54% | 1.56 | 2.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +1.3% | +21.5% | 15 | 73% | 2.50 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +3.3% | +148.3% | 15 | 60% | 1.27 | 9.2% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +9.5% | +17.1% | 17 | 100% | — | 1.8% | 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 | 5 | 60% | $46,311.10 |
| Sideways market | 7 | 43% | $18,017.10 |
| Falling market | 1 | 100% | $3,556.41 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
1,255.6%
The 95th percentile of the worst drawdown when the same 13 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 152.1% to 1,255.6%; the tick is what actually happened, 595.1%. Order luck alone moves it this much.
All 13 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 26 Mar 2024 69,880.00 | 14 Apr 2024 63,924.50 | 19 | −$17,866.40 |
| long | 3 Jun 2024 67,765.60 | 25 Jun 2024 60,293.30 | 22 | −$22,417.00 |
| long | 27 Jul 2024 67,908.00 | 3 Aug 2024 61,498.30 | 7 | −$19,229.00 |
| long | 8 Oct 2024 62,224.00 | 15 Nov 2024 87,325.60 | 38 | $75,304.70 |
| long | 4 Jan 2025 98,174.20 | 26 Feb 2025 88,680.40 | 53 | −$28,481.30 |
| long | 23 May 2025 111,696.00 | 17 Jul 2025 118,630.00 | 55 | $20,802.70 |
| long | 3 Jun 2025 105,858.00 | 17 Jul 2025 118,630.00 | 44 | $38,317.30 |
| long | 17 Jun 2025 106,795.00 | 17 Jul 2025 118,630.00 | 30 | $35,507.70 |
| long | 5 Aug 2025 115,055.00 | 8 Oct 2025 121,333.00 | 64 | $18,833.80 |
| long | 9 Sep 2025 112,065.00 | 8 Oct 2025 121,333.00 | 29 | $27,803.20 |
| long | 18 Apr 2026 77,072.00 | 24 Apr 2026 78,257.50 | 6 | $3,556.41 |
| long | 22 May 2026 77,615.50 | 3 Jun 2026 66,760.80 | 12 | −$32,564.00 |
| long | 26 May 2026 77,322.00 | 3 Jun 2026 66,760.80 | 8 | −$31,683.50 |
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 |
|---|---|---|---|---|---|
| ADX strategy (considering ADX and +DI only ) this page | — | v4 strategy | +678.8% | 13 | 693.5% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 28 ms | With a workaround 1,000 bars in 47 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 33, 37) 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/// © mohanee//@version=4//ADX strategySmoothedTrueRange=0.00SmoothedDirectionalMovementPlus=0.00SmoothedDirectionalMovementMinus=0.00strategy(title="ADX strategy", overlay=false,pyramiding=3, default_qty_type=strategy.fixed, default_qty_value=3, initial_capital=10000, currency=currency.USD)len = input(11, title="ADX Length", minval=1)threshold = input(30, title="threshold", minval=5)fastEma=input(13, title="Fast EMA",minval=1, maxval=50)slowEma=input(55, title="Slow EMA",minval=10, maxval=200)stopLoss =input(8, title="Stop Loss",minval=1) //TrueRange = max(max(high-low, abs(high-nz(close[1]))), abs(low-nz(close[1])))DirectionalMovementPlus = high-nz(high[1]) > nz(low[1])-low ? max(high-nz(high[1]), 0): 0DirectionalMovementMinus = nz(low[1])-low > high-nz(high[1]) ? max(nz(low[1])-low, 0): 0SmoothedTrueRange:= nz(SmoothedTrueRange[1]) - (nz(SmoothedTrueRange[1])/len) + TrueRangeSmoothedDirectionalMovementPlus := nz(SmoothedDirectionalMovementPlus[1]) - (nz(SmoothedDirectionalMovementPlus[1])/len) + DirectionalMovementPlusSmoothedDirectionalMovementMinus:= nz(SmoothedDirectionalMovementMinus[1]) - (nz(SmoothedDirectionalMovementMinus[1])/len) + DirectionalMovementMinusDIPlus = SmoothedDirectionalMovementPlus / SmoothedTrueRange * 100DIMinus = SmoothedDirectionalMovementMinus / SmoothedTrueRange * 100DX = abs(DIPlus-DIMinus) / (DIPlus+DIMinus)*100ADX = sma(DX, len)plot(DIPlus, color=color.green, title="DI+")//plot(DIMinus, color=color.red, title="DI-")plot(ADX, color=color.black, title="ADX")hline(threshold, color=color.black, linestyle=hline.style_dashed)fastEmaVal=ema(close,fastEma)slowEmaVal=ema(close,slowEma)//long conditionlongCondition= ADX < threshold and crossover(DIPlus,ADX) and fastEmaVal > slowEmaValbarcolor(longCondition ? color.yellow: na)strategy.entry(id="ADXLE", long=true, when= longCondition and strategy.position_size<1) barcolor(strategy.position_size>1 ? color.blue: na)bgcolor(strategy.position_size>1 ? color.blue: na)//Addstrategy.entry(id="ADXLE", comment="Add", long=true, when= strategy.position_size>1 and close<strategy.position_avg_price and crossover(DIPlus,ADX) ) //calculate stop LossstopLossVal = strategy.position_avg_price - (strategy.position_avg_price*stopLoss*0.01)strategy.close(id="ADXLE",comment="SL Exit", when=close<stopLossVal) //close all on stop loss//exit conditionexitCondition= ADX > threshold and crossunder(DIPlus,ADX) // and fastEmaVal > slowEmaValstrategy.close(id="ADXLE",comment="TPExitAll", qty=strategy.position_size , when= exitCondition) //close all