Pine Script v4 · Strategy
ATR_Trade_strategy
A Pine Script v4 strategy built on TR (true). It has 8 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 10 settings; defaults include Length 14, Smoothing EMA and Max Loss on one Trade 0.5.
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 ATR_Trade_strategy repaint?
- Yes
- At least one pattern in the source can repaint: higher-timeframe data.
- How did ATR_Trade_strategy do on BTCUSDT 1D with default settings?
- +521.27%
- From 26 Jan 2024 to 21 Oct 2026, over 36 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can ATR_Trade_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 ATR_Trade_strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 58, 59.
- Does ATR_Trade_strategy run in Zeal Terminal?
- Yes
- It compiles in 17 ms and runs 1,000 bars in 74 ms, in the browser.
BTCUSDT · 1D · default settings
- ATR-HIGH
- ATR-LOW
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +521.27%
- $521,268.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 36
- 14 won · 22 lost
- Profitable
- 38.9%
- of closed trades
- Profit factor
- 1.76
- gross profit ÷ gross loss
- Max drawdown
- 57.78%
- −$199,977.00
One run with the author's default settings, $100,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
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 51, 53, 54, 55, 56, 57. The upgraded version below moves each call inside an if block.
-
Note
Uses transp =, which v5 removed line 37
transp = on line 37, 38, 39. 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 security → request.security, rma → ta.rma, sma → ta.sma, ema → ta.ema, wma → ta.wma, tr → ta.tr.
- 6 strategy calls use the when = argument (line 51, 53, 54, 55, 56, 57). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 36 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 6: input(type = input.bool) → input.bool()
- Line 7: input(type = input.bool) → input.bool()
- Line 8: input(type = input.bool) → input.bool()
- Line 11: input(type = input.float) → input.float()
- Line 12: input(type = input.integer) → input.int()
16 more edits
- Line 13: input(type = input.integer) → input.int()
- Line 14: input(type = input.bool) → input.bool()
- Line 15: input(type = input.bool) → input.bool()
- Line 16: security → request.security
- Line 25: rma → ta.rma
- Line 28: sma → ta.sma
- Line 31: ema → ta.ema
- Line 33: wma → ta.wma
- Line 34: tr → ta.tr
- Line 35: tr → ta.tr
- Line 37: transp = 80 → color.new(#991515, 80)
- Line 38: transp = 80 → color.new(#991515, 80)
- Line 39: transp = 60 → color.new(#80cbc4, 60)
- Line 41: ema → ta.ema
- Line 54: ema → ta.ema
- Line 55: ema → ta.ema
⋯//Thanks to Vamsikrishna Kona who planned this Idea in my mind. helped ot inprove it-//@version=4+//@version=5strategy("ATR_Trade_strategy-2",shorttitle="ATR_Trade", overlay=true) -showMiddle = input(false, title="Show Middle Line", type=input.bool)+showMiddle = input.bool(false, title="Show Middle Line")-showEMA = input(true, title="Show EMA Line", type=input.bool)+showEMA = input.bool(true, title="Show EMA Line")-showTypical = input(false, title="Show Typical Line", type=input.bool)+showTypical = input.bool(false, title="Show Typical Line")length = input(title="Length", defval=14, minval=1)smoothing = input(title="Smoothing", defval="EMA", options=["RMA", "SMA", "EMA", "WMA"])-maxLoss = input(0.5, title="Max Loss on one Trade", type=input.float)+maxLoss = input.float(0.5, title="Max Loss on one Trade")-emaCount = input(10, title="EMA Periods", type=input.integer)+emaCount = input.int(10, title="EMA Periods")-tradeQty = input(10, title="Trade Quanity", type=input.integer)+tradeQty = input.int(10, title="Trade Quanity")-SlasMiddle = input(false, title="Stop Loss as Middle", type=input.bool)+SlasMiddle = input.bool(false, title="Stop Loss as Middle")-onTypical = input(false, title="Trade on Typical", type=input.bool)+onTypical = input.bool(false, title="Trade on Typical")-getSeries(_e, _timeFrame) => security(syminfo.tickerid, _timeFrame, _e, lookahead=barmerge.lookahead_on) +getSeries(_e, _timeFrame) => request.security(syminfo.tickerid, _timeFrame, _e, lookahead=barmerge.lookahead_on) previousClose= getSeries(close[1], 'D')⋯ if smoothing == "RMA"- rma(source, length)+ ta.rma(source, length) else if smoothing == "SMA"- sma(source, length)+ ta.sma(source, length) else if smoothing == "EMA"- ema(source, length)+ ta.ema(source, length) else- wma(source, length)+ ta.wma(source, length)-ATRH=ma_function(tr(true), length)+previousClose+ATRH=ma_function(ta.tr(true), length)+previousClose-ATRL=previousClose-ma_function(tr(true), length)+ATRL=previousClose-ma_function(ta.tr(true), length)ATRM= (ATRH+ATRL)/2-plot(ATRH, title = "ATR-HIGH", color=#991515, transp=80)+plot(ATRH, title = "ATR-HIGH", color=color.new(#991515, 80))-plot(ATRL, title = "ATR-LOW", color=#991515, transp=80)+plot(ATRL, title = "ATR-LOW", color=color.new(#991515, 80))-plot(showMiddle?ATRM:na, title = "ATR-MIDDLE", color=#80cbc4, transp=60)+plot(showMiddle?ATRM:na, title = "ATR-MIDDLE", color=color.new(#80cbc4, 60))plot(showTypical?typicalPrice:na, title = "ATR-MIDDLE", color=color.black)-plot(showEMA?ema(close, emaCount):na, color=color.blue)+plot(showEMA?ta.ema(close, emaCount):na, color=color.blue)lognCondition = onTypical? typicalPrice> (ATRH):close> (ATRH*1.01) ⋯ strategy.entry("sell", strategy.short, tradeQty,when=strategy.position_size > 0)-strategy.close("buy", when = close < ema(close, emaCount), comment = "close if blow ema")+strategy.close("buy", when = close < ta.ema(close, emaCount), comment = "close if blow ema")-strategy.close("sell", when = close > ema(close, emaCount), comment = "close if blow ema")+strategy.close("sell", when = close > ta.ema(close, emaCount), comment = "close if blow ema")strategy.close("buy", when = (close < ATRM) and SlasMiddle, comment = "close if blow ATRM")⋯Same 36 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.string()
- Line 51: when = strategy.position_size <= 0 → if block
- Line 53: when = strategy.position_size > 0 → if block
- Line 54: when = close < ta.ema(close, emaCount) → if block
- Line 55: when = close > ta.ema(close, emaCount) → if block
- Line 56: when = (close < ATRM) and SlasMiddle → if block
- Line 57: when = (close > ATRM) and SlasMiddle → if block
⋯showTypical = input.bool(false, title="Show Typical Line")-length = input(title="Length", defval=14, minval=1)+length = input.int(title="Length", defval=14, minval=1)-smoothing = input(title="Smoothing", defval="EMA", options=["RMA", "SMA", "EMA", "WMA"])+smoothing = input.string(title="Smoothing", defval="EMA", options=["RMA", "SMA", "EMA", "WMA"])maxLoss = input.float(0.5, title="Max Loss on one Trade")⋯if(lognCondition and year>2009) - strategy.entry("Buy", strategy.long, tradeQty,when=strategy.position_size <= 0) + if strategy.position_size <= 0+ strategy.entry("Buy", strategy.long, tradeQty) if(shortCondition and year>2009) - strategy.entry("sell", strategy.short, tradeQty,when=strategy.position_size > 0)+ if strategy.position_size > 0+ strategy.entry("sell", strategy.short, tradeQty)-strategy.close("buy", when = close < ta.ema(close, emaCount), comment = "close if blow ema")+if close < ta.ema(close, emaCount)+ strategy.close("buy", comment = "close if blow ema")-strategy.close("sell", when = close > ta.ema(close, emaCount), comment = "close if blow ema")+if close > ta.ema(close, emaCount)+ strategy.close("sell", comment = "close if blow ema")-strategy.close("buy", when = (close < ATRM) and SlasMiddle, comment = "close if blow ATRM")+if (close < ATRM) and SlasMiddle+ strategy.close("buy", comment = "close if blow ATRM")-strategy.close("sell", when = (close > ATRM) and SlasMiddle, comment = "close if ABOVE ATRM")+if (close > ATRM) and SlasMiddle+ strategy.close("sell", comment = "close if ABOVE ATRM")strategy.exit("Buy", "long", loss = mxloss)⋯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: 36 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.
⋯//Thanks to Vamsikrishna Kona who planned this Idea in my mind. helped ot inprove it-//@version=5+//@version=6strategy("ATR_Trade_strategy-2",shorttitle="ATR_Trade", overlay=true) ⋯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 |
|---|---|---|---|
| Show Middle Line | false | bool | ATR-MIDDLE |
| Show EMA Line | true | bool | plot |
| Show Typical Line | false | bool | ATR-MIDDLE |
| Length | 14 | int · min 1 | ATRH ATRL line 25 line 28 line 31 line 33 |
| Smoothing | EMA | string · options ["RMA", "SMA", "EMA", "WMA"] | line 24 line 27 line 30 |
| Max Loss on one Trade | 0.5 | float | mxloss |
| EMA Periods | 10 | int (written input.integer) | plot close buy close sell |
| Trade Quanity | 10 | int (written input.integer) | entry Buy entry sell |
| Stop Loss as Middle | false | bool | close buy close sell |
| Trade on Typical | false | bool | lognCondition shortCondition |
What it calculates
- TR (
tr,ta.trin v5)trueATRH · line 34 - TR (
tr,ta.trin v5)trueATRL · line 35
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 Buy long | strategy.position_size <= 0 | On | 51 |
| Open sell short | strategy.position_size > 0 | On | 53 |
| Close buy | close < ema(close, emaCount) | On | 54 |
| Close sell | close > ema(close, emaCount) | On | 55 |
| Close buy | (close < ATRM) and SlasMiddle | On | 56 |
| Close sell | (close > ATRM) and SlasMiddle | On | 57 |
| Exit Buy stop | every bar | On | 58 |
| Exit sell stop | every bar | On | 59 |
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 data1 security call(s) use lookahead_on (line 16). 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 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 | +521.3% | +102.5% | 36 | 39% | 1.76 | 57.8% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −117.6% | +5.5% | 32 | 28% | 0.57 | 144.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −10.3% | +9.8% | 35 | 31% | 0.82 | 22.2% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −4.8% | +8.4% | 38 | 32% | 0.53 | 5.6% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 35 | 37% | 1.00 | 1.7% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 40 | 28% | 1.08 | 0.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −2.8% | +148.3% | 30 | 43% | 0.74 | 6.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +1.3% | +17.1% | 32 | 28% | 1.85 | 0.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 | 12 | 33% | −$69,299.40 |
| Sideways market | 17 | 41% | $220,854.00 |
| Falling market | 7 | 43% | $369,713.00 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
72.3%
The 95th percentile of the worst drawdown when the same 36 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 40.3% to 72.3%; the tick is what actually happened, 40.4%. Order luck alone moves it this much.
All 36 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 6 Mar 2024 63,724.00 | 8 | $92,475.30 |
| short | 6 Mar 2024 63,724.00 | 7 Mar 2024 66,074.00 | 1 | −$23,500.30 |
| long | 21 Mar 2024 67,840.50 | 3 Apr 2024 65,464.00 | 13 | −$23,765.20 |
| short | 3 Apr 2024 65,464.00 | 5 Apr 2024 68,487.80 | 2 | −$30,238.10 |
| long | 16 May 2024 66,206.50 | 12 Jun 2024 67,314.20 | 27 | $11,077.20 |
| short | 12 Jun 2024 67,314.20 | 1 Jul 2024 62,772.00 | 19 | $45,422.20 |
| long | 16 Jul 2024 64,724.10 | 3 Aug 2024 61,498.30 | 18 | −$32,257.20 |
| short | 3 Aug 2024 61,498.30 | 9 Aug 2024 61,686.00 | 6 | −$1,876.60 |
| long | 9 Aug 2024 61,686.00 | 28 Aug 2024 59,415.00 | 19 | −$22,710.00 |
| short | 28 Aug 2024 59,415.00 | 10 Sep 2024 57,042.00 | 13 | $23,729.90 |
| long | 15 Oct 2024 66,084.00 | 26 Nov 2024 93,010.00 | 42 | $269,260.00 |
| short | 26 Nov 2024 93,010.00 | 28 Nov 2024 95,863.10 | 2 | −$28,531.00 |
| long | 3 Mar 2025 94,270.00 | 4 Mar 2025 86,221.20 | 1 | −$80,488.30 |
| short | 4 Mar 2025 86,221.20 | 6 Mar 2025 90,606.00 | 2 | −$43,848.40 |
| long | 10 Apr 2025 82,615.20 | 24 May 2025 107,318.00 | 44 | $247,031.00 |
| short | 24 May 2025 107,318.00 | 25 May 2025 107,762.00 | 1 | −$4,436.00 |
| long | 10 Jun 2025 110,263.00 | 13 Jun 2025 105,672.00 | 3 | −$45,912.80 |
| short | 13 Jun 2025 105,672.00 | 17 Jun 2025 106,795.00 | 4 | −$11,227.90 |
| long | 11 Jul 2025 116,010.00 | 15 Aug 2025 118,295.00 | 35 | $22,850.80 |
| short | 15 Aug 2025 118,295.00 | 23 Aug 2025 116,936.00 | 8 | $13,590.90 |
| long | 23 Aug 2025 116,936.00 | 30 Aug 2025 108,377.00 | 7 | −$85,586.00 |
| short | 30 Aug 2025 108,377.00 | 3 Sep 2025 111,240.00 | 4 | −$28,626.10 |
| long | 2 Oct 2025 118,595.00 | 8 Oct 2025 121,333.00 | 6 | $27,379.70 |
| short | 8 Oct 2025 121,333.00 | 9 Oct 2025 123,306.00 | 1 | −$19,730.50 |
| long | 3 Dec 2025 91,277.90 | 21 Jan 2026 88,427.70 | 49 | −$28,502.20 |
| short | 21 Jan 2026 88,427.70 | 7 Feb 2026 70,580.30 | 17 | $178,474.00 |
| long | 7 Feb 2026 70,580.30 | 24 Feb 2026 64,656.00 | 17 | −$59,242.50 |
| short | 24 Feb 2026 64,656.00 | 26 Feb 2026 67,988.00 | 2 | −$33,320.30 |
| long | 26 Feb 2026 67,988.00 | 23 May 2026 75,539.50 | 86 | $75,514.60 |
| short | 23 May 2026 75,539.50 | 14 Jun 2026 64,458.00 | 22 | $110,815.00 |
| long | 15 Jul 2026 65,044.00 | 28 Jul 2026 63,755.90 | 13 | −$12,881.30 |
| short | 28 Jul 2026 63,755.90 | 31 Jul 2026 64,780.00 | 3 | −$10,241.70 |
| long | 20 Aug 2026 69,334.80 | 16 Sep 2026 75,644.50 | 27 | $63,097.00 |
| short | 16 Sep 2026 75,644.50 | 19 Sep 2026 80,883.90 | 3 | −$52,393.80 |
| long | 19 Sep 2026 80,883.90 | 8 Oct 2026 83,321.80 | 19 | $24,379.50 |
| short | 8 Oct 2026 83,321.80 | 12 Oct 2026 83,773.10 | 4 | −$4,513.10 |
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 |
|---|---|---|---|---|---|
| ATR_Trade_strategy this page | — | v4 strategy | +521.3% | 36 | 57.8% |
| Phoenix085-Strategies==-MTF - Average True Range + MovAvg by Phoenix085 | tr | v4 strategy | No trades on this market | ||
| Vortex Indicator by Zeal Terminal | tr | v5 indicator | indicator | ||
| Ultimate Oscillator by Zeal Terminal | tr | v5 indicator | indicator | ||
| [MACLEN] TRUE RANGE by MaclenMtz | tr | v5 strategy | +2.5% | 27 | 3.5% |
| FXFUNDINGMATE TREND INDICATOR by FXFUNDINGMATE | tr | v4 strategy | −6.4% | 10 | 13.3% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 17 ms | Yes 1,000 bars in 74 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.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © arameshraju//Thanks to Vamsikrishna Kona who planned this Idea in my mind. helped ot inprove it//@version=4strategy("ATR_Trade_strategy-2",shorttitle="ATR_Trade", overlay=true) showMiddle = input(false, title="Show Middle Line", type=input.bool)showEMA = input(true, title="Show EMA Line", type=input.bool)showTypical = input(false, title="Show Typical Line", type=input.bool)length = input(title="Length", defval=14, minval=1)smoothing = input(title="Smoothing", defval="EMA", options=["RMA", "SMA", "EMA", "WMA"])maxLoss = input(0.5, title="Max Loss on one Trade", type=input.float)emaCount = input(10, title="EMA Periods", type=input.integer)tradeQty = input(10, title="Trade Quanity", type=input.integer)SlasMiddle = input(false, title="Stop Loss as Middle", type=input.bool)onTypical = input(false, title="Trade on Typical", type=input.bool)getSeries(_e, _timeFrame) => security(syminfo.tickerid, _timeFrame, _e, lookahead=barmerge.lookahead_on) previousClose= getSeries(close[1], 'D')previousHigh= getSeries(high[1], 'D')previousLow= getSeries(low[1], 'D')typicalPrice=(close+high+low)/3mxloss=close*maxLoss ma_function(source, length) => if smoothing == "RMA" rma(source, length) else if smoothing == "SMA" sma(source, length) else if smoothing == "EMA" ema(source, length) else wma(source, length)ATRH=ma_function(tr(true), length)+previousCloseATRL=previousClose-ma_function(tr(true), length)ATRM= (ATRH+ATRL)/2plot(ATRH, title = "ATR-HIGH", color=#991515, transp=80)plot(ATRL, title = "ATR-LOW", color=#991515, transp=80)plot(showMiddle?ATRM:na, title = "ATR-MIDDLE", color=#80cbc4, transp=60)plot(showTypical?typicalPrice:na, title = "ATR-MIDDLE", color=color.black)plot(showEMA?ema(close, emaCount):na, color=color.blue)lognCondition = onTypical? typicalPrice> (ATRH):close> (ATRH*1.01) shortCondition = onTypical ? typicalPrice< (ATRL) : close< (ATRL) // if onTypical// lognCondition=typicalPrice> (ATRH) // shortCondition = typicalPrice< (ATRL) // else// lognCondition=close> (ATRH*1.01) // shortCondition = close< (ATRL) if(lognCondition and year>2009) strategy.entry("Buy", strategy.long, tradeQty,when=strategy.position_size <= 0) if(shortCondition and year>2009) strategy.entry("sell", strategy.short, tradeQty,when=strategy.position_size > 0)strategy.close("buy", when = close < ema(close, emaCount), comment = "close if blow ema")strategy.close("sell", when = close > ema(close, emaCount), comment = "close if blow ema")strategy.close("buy", when = (close < ATRM) and SlasMiddle, comment = "close if blow ATRM")strategy.close("sell", when = (close > ATRM) and SlasMiddle, comment = "close if ABOVE ATRM")strategy.exit("Buy", "long", loss = mxloss)strategy.exit("sell", "long", loss = mxloss)//plot(strategy.equity)