Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (10)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$621,336$202,527Equity from $100,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.
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

Verified

Same 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")⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
Show Middle LinefalseboolATR-MIDDLE
Show EMA Linetrueboolplot
Show Typical LinefalseboolATR-MIDDLE
Length14int · min 1ATRH ATRL line 25 line 28 line 31 line 33
SmoothingEMAstring · options ["RMA", "SMA", "EMA", "WMA"]line 24 line 27 line 30
Max Loss on one Trade0.5floatmxloss
EMA Periods10int (written input.integer)plot close buy close sell
Trade Quanity10int (written input.integer)entry Buy entry sell
Stop Loss as Middlefalseboolclose buy close sell
Trade on TypicalfalseboollognCondition shortCondition

What it calculates

  • TR (tr, ta.tr in v5)trueATRH · line 34
  • TR (tr, ta.tr in 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.

high · low · closehigh · low · closeprice datalengthLengthsetting · 14closecloseprice datahighhighprice datalowlowprice datamaxLossMax Loss on one Tradesetting · 0.5emaCountEMA Periodssetting · 10ATRHTR→ ATRHATRLTR→ ATRLpreviousClosepreviousClosevaluepreviousHighpreviousHighvaluepreviousLowpreviousLowvaluetypicalPricetypicalPricevaluemxlossmxlossvalueATRMATRMvaluelognConditionlognConditionvalueshortConditionshortConditionvalueentry:BuyOpen Buystrategy.entryentry:sellOpen sellstrategy.entryclose:buyClose buystrategy.closeclose:sellClose sellstrategy.closeexit:Buyexit Buystrategy.exitexit:sellexit sellstrategy.exitplot:37ATR-HIGHplotplot:38ATR-LOWplotplot:39ATR-MIDDLEplotplot:40ATR-MIDDLEplotplot:41plotplot

Order rules

OrderFires whenBy defaultLine
Open Buy longstrategy.position_size <= 0On51
Open sell shortstrategy.position_size > 0On53
Close buyclose < ema(close, emaCount)On54
Close sellclose > ema(close, emaCount)On55
Close buy(close < ATRM) and SlasMiddleOn56
Close sell(close > ATRM) and SlasMiddleOn57
Exit Buy stopevery barOn58
Exit sell stopevery barOn59

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.

11/11trades 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.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+521.3%+102.5%3639%1.7657.8%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−117.6%+5.5%3228%0.57144.5%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−10.3%+9.8%3531%0.8222.2%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−4.8%+8.4%3832%0.535.6%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.0%+16.7%3537%1.001.7%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%4028%1.080.2%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−2.8%+148.3%3043%0.746.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+1.3%+17.1%3228%1.850.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 barsTradesWonP&L
Rising market1233%−$69,299.40
Sideways market1741%$220,854.00
Falling market743%$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.

0%83%
All 36 trades
SideEntryExitBarsP&L
long27 Feb 2024 54,476.506 Mar 2024 63,724.008$92,475.30
short6 Mar 2024 63,724.007 Mar 2024 66,074.001−$23,500.30
long21 Mar 2024 67,840.503 Apr 2024 65,464.0013−$23,765.20
short3 Apr 2024 65,464.005 Apr 2024 68,487.802−$30,238.10
long16 May 2024 66,206.5012 Jun 2024 67,314.2027$11,077.20
short12 Jun 2024 67,314.201 Jul 2024 62,772.0019$45,422.20
long16 Jul 2024 64,724.103 Aug 2024 61,498.3018−$32,257.20
short3 Aug 2024 61,498.309 Aug 2024 61,686.006−$1,876.60
long9 Aug 2024 61,686.0028 Aug 2024 59,415.0019−$22,710.00
short28 Aug 2024 59,415.0010 Sep 2024 57,042.0013$23,729.90
long15 Oct 2024 66,084.0026 Nov 2024 93,010.0042$269,260.00
short26 Nov 2024 93,010.0028 Nov 2024 95,863.102−$28,531.00
long3 Mar 2025 94,270.004 Mar 2025 86,221.201−$80,488.30
short4 Mar 2025 86,221.206 Mar 2025 90,606.002−$43,848.40
long10 Apr 2025 82,615.2024 May 2025 107,318.0044$247,031.00
short24 May 2025 107,318.0025 May 2025 107,762.001−$4,436.00
long10 Jun 2025 110,263.0013 Jun 2025 105,672.003−$45,912.80
short13 Jun 2025 105,672.0017 Jun 2025 106,795.004−$11,227.90
long11 Jul 2025 116,010.0015 Aug 2025 118,295.0035$22,850.80
short15 Aug 2025 118,295.0023 Aug 2025 116,936.008$13,590.90
long23 Aug 2025 116,936.0030 Aug 2025 108,377.007−$85,586.00
short30 Aug 2025 108,377.003 Sep 2025 111,240.004−$28,626.10
long2 Oct 2025 118,595.008 Oct 2025 121,333.006$27,379.70
short8 Oct 2025 121,333.009 Oct 2025 123,306.001−$19,730.50
long3 Dec 2025 91,277.9021 Jan 2026 88,427.7049−$28,502.20
short21 Jan 2026 88,427.707 Feb 2026 70,580.3017$178,474.00
long7 Feb 2026 70,580.3024 Feb 2026 64,656.0017−$59,242.50
short24 Feb 2026 64,656.0026 Feb 2026 67,988.002−$33,320.30
long26 Feb 2026 67,988.0023 May 2026 75,539.5086$75,514.60
short23 May 2026 75,539.5014 Jun 2026 64,458.0022$110,815.00
long15 Jul 2026 65,044.0028 Jul 2026 63,755.9013−$12,881.30
short28 Jul 2026 63,755.9031 Jul 2026 64,780.003−$10,241.70
long20 Aug 2026 69,334.8016 Sep 2026 75,644.5027$63,097.00
short16 Sep 2026 75,644.5019 Sep 2026 80,883.903−$52,393.80
long19 Sep 2026 80,883.908 Oct 2026 83,321.8019$24,379.50
short8 Oct 2026 83,321.8012 Oct 2026 83,773.104−$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.

ScriptSharesTypeNetTradesMax drawdown
ATR_Trade_strategy this page—v4 strategy+521.3%3657.8%
Phoenix085-Strategies==-MTF - Average True Range + MovAvg by Phoenix085trv4 strategyNo trades on this market
Vortex Indicator by Zeal Terminaltrv5 indicatorindicator
Ultimate Oscillator by Zeal Terminaltrv5 indicatorindicator
[MACLEN] TRUE RANGE by MaclenMtztrv5 strategy+2.5%273.5%
FXFUNDINGMATE TREND INDICATOR by FXFUNDINGMATEtrv4 strategy−6.4%1013.3%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 17 msYes 1,000 bars in 74 msNot measured
PineTSNo 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)