Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

ATR Stop Buy Strategy

A Pine Script v4 strategy built on ATR (14). It has 2 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 20% of equity and pays 1 commission. 3 settings; defaults include Days Back 120, Selling Coefficent For ATR 1.5 and Buying Coefficent For ATR 1.2.

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 Stop Buy Strategy repaint?
Yes
At least one pattern in the source can repaint: intrabar recalculation.
How did ATR Stop Buy Strategy do on BTCUSDT 1D with default settings?
Not measured
It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings; it opened a long position that was still open on the last bar, so there is no result to report. On BTCUSDT 4h it did trade; that row of the markets table below has its figures. A backtest, not investment advice.
Can ATR Stop Buy 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 Stop Buy Strategy use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does ATR Stop Buy Strategy run in Zeal Terminal?
Yes
It compiles in 9 ms and runs 1,000 bars in 24 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 (3)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$1,163$2,025Equity from $1,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
−0.50%
−$5.00
Buy and hold
+102.5%
same period
Closed trades
0
0 won · 0 lost
Profitable
0.0%
of closed trades
Profit factor
0.00
gross profit ÷ gross loss
Max drawdown
0.26%
−$2.56

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

  • Note

    No slippage

    Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.

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 atr → ta.atr.

v4 → v5

VerifiedDownload the v5 script

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

  • Line 3: //@version=4 → //@version=5
  • Line 5: input(type = input.integer) → input.int()
  • Line 6: input(type = input.float) → input.float()
  • Line 7: input(type = input.float) → input.float()
  • Line 10: atr → ta.atr
⋯// © phobo3s-//@version=4+//@version=5strategy("ATR Stop Buy Strategy",shorttitle="ATR-ST",initial_capital=1000, overlay = true, default_qty_type = strategy.percent_of_equity, pyramiding = 5, default_qty_value = 20, commission_type = strategy.commission.cash_per_order, commission_value = 1, calc_on_every_tick = true)-daysBack = input(defval=120, title="Days Back", type=input.integer)+daysBack = input.int(defval=120, title="Days Back")-sellCoeff = input(defval=1.5, title="Selling Coefficent For ATR", type=input.float, minval= 0.01, step=0.1)+sellCoeff = input.float(defval=1.5, title="Selling Coefficent For ATR", minval= 0.01, step=0.1)-buyCoeff = input(defval=1.2, title = "Buying Coefficent For ATR", type=input.float, minval= 0.01, step=0.1)+buyCoeff = input.float(defval=1.2, title = "Buying Coefficent For ATR", minval= 0.01, step=0.1)fromDate = timenow - (daysBack*24*60*60*1000)toDate = timenow -ATR = atr(14)+ATR = ta.atr(14)stopLossPoint = ATR * sellCoeff⋯

Clean v5

Verified

Nothing to change.

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: 18 before, 21 after on BTCUSDT 4h, 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.

⋯// © phobo3s-//@version=5+//@version=6strategy("ATR Stop Buy Strategy",shorttitle="ATR-ST",initial_capital=1000, overlay = true, default_qty_type = strategy.percent_of_equity, pyramiding = 5, default_qty_value = 20, commission_type = strategy.commission.cash_per_order, commission_value = 1, calc_on_every_tick = true)⋯

Settings

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

SettingDefaultType and rangeFeeds
Days Back120int (written input.integer)fromDate
Selling Coefficent For ATR1.5float · min 0.01 · step 0.1stopLossPoint
Buying Coefficent For ATR1.2float · min 0.01 · step 0.1buyPoint

What it calculates

  • ATR (atr, ta.atr in v5)14ATR · line 10

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 datadaysBackDays Backsetting · 120sellCoeffSelling Coefficent For …setting · 1.5buyCoeffBuying Coefficent For A…setting · 1.2closecloseprice datalowlowprice datatimetimeprice datahighhighprice dataATRATR→ ATRfromDatefromDatevaluetoDatetoDatevaluestopLossPointstopLossPointvaluebuyPointbuyPointvalueStoplossLineStoplossLinevalueBuyLineBuyLinevalueentry:GGOpen GGstrategy.entryplot:23plotplotplot:24plotplot

Order rules

OrderFires whenBy defaultLine
Open GG long(high > BuyLine and time >= fromDate and time <= toDate )On16
Open GG short(low < StoplossLine and strategy.position_avg_price < close and time >= fromDate and time <= toDate )On18

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.

Not measuredNo trades to compareThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, but neither the live nor the historical run produced any trades in them, so the replay says nothing either way.
  • ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
  • ClearFuture-bar referencesNo negative history offsets such as close[-1].
  • CheckIntrabar recalculationcalc_on_every_tick = true. Live values can differ from history.
  • 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−0.5%+102.5%00%0.000.3%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+25.0%+5.5%1861%9.495.3%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+5.8%+9.8%5100%58.933.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+16.6%+8.4%1872%6.4215.2%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+7.3%+16.7%1050%3.408.5%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+18.1%+21.5%2556%3.7317.8%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+18.8%+148.3%5100%188.630.7%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+18.0%+17.1%1867%6.279.8%8 May 2026 – 21 Oct 2026

Backtests are simulations on historical data and are not investment advice.

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 Stop Buy Strategy this page—v4 strategy−0.5%00.3%
SOJA PIVOT by ParaBellum68atrv4 strategy+3.3%918.2%
Volatility System by EduardoMattjeatrv5 strategy+150.2%221.5%
VBand Strategy by Saravanan_Ragavanatrv4 strategyNo trades on this market
Volatility Breakout Strategy [Angel Algo] by AngelAlgoatrv5 strategy+3.1%183.6%
ATR Mean Reversion Strategy V1 by Bcullen175atrv5 strategy−20.0%7959.6%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 9 msYes 1,000 bars in 24 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/// © phobo3s//@version=4strategy("ATR Stop Buy Strategy",shorttitle="ATR-ST",initial_capital=1000, overlay = true, default_qty_type = strategy.percent_of_equity, pyramiding = 5, default_qty_value = 20, commission_type = strategy.commission.cash_per_order, commission_value = 1, calc_on_every_tick = true)daysBack = input(defval=120, title="Days Back", type=input.integer)sellCoeff = input(defval=1.5, title="Selling Coefficent For ATR", type=input.float, minval= 0.01, step=0.1)buyCoeff = input(defval=1.2, title = "Buying Coefficent For ATR", type=input.float, minval= 0.01, step=0.1)fromDate = timenow - (daysBack*24*60*60*1000)toDate = timenow ATR = atr(14)stopLossPoint = ATR * sellCoeffbuyPoint = ATR * buyCoeffStoplossLine =  close[1] - stopLossPoint[1]BuyLine = close[1] + buyPoint[1]if (high > BuyLine and time >= fromDate and time <= toDate )    strategy.entry("GG", strategy.long, comment="Gir")if (low < StoplossLine and strategy.position_avg_price < close and time >= fromDate and time <= toDate )    strategy.entry("GG", strategy.short, comment="Çık")//longFlags = close < StoplossLine//shortFlags = close > BuyLine//plotshape(shortFlags, style=shape.triangledown, location=location.abovebar, color=color.red)//plotshape(longFlags, style=shape.triangleup, location=location.belowbar, color=color.blue)plot(StoplossLine)plot(BuyLine)