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
- 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.
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
VerifiedNothing 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Days Back | 120 | int (written input.integer) | fromDate |
| Selling Coefficent For ATR | 1.5 | float · min 0.01 · step 0.1 | stopLossPoint |
| Buying Coefficent For ATR | 1.2 | float · min 0.01 · step 0.1 | buyPoint |
What it calculates
- ATR (
atr,ta.atrin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open GG long | (high > BuyLine and time >= fromDate and time <= toDate ) | On | 16 |
| Open GG short | (low < StoplossLine and strategy.position_avg_price < close and time >= fromDate and time <= toDate ) | On | 18 |
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.
- 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −0.5% | +102.5% | 0 | 0% | 0.00 | 0.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +25.0% | +5.5% | 18 | 61% | 9.49 | 5.3% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +5.8% | +9.8% | 5 | 100% | 58.93 | 3.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +16.6% | +8.4% | 18 | 72% | 6.42 | 15.2% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +7.3% | +16.7% | 10 | 50% | 3.40 | 8.5% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +18.1% | +21.5% | 25 | 56% | 3.73 | 17.8% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +18.8% | +148.3% | 5 | 100% | 188.63 | 0.7% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +18.0% | +17.1% | 18 | 67% | 6.27 | 9.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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| ATR Stop Buy Strategy this page | — | v4 strategy | −0.5% | 0 | 0.3% |
| SOJA PIVOT by ParaBellum68 | atr | v4 strategy | +3.3% | 9 | 18.2% |
| Volatility System by EduardoMattje | atr | v5 strategy | +150.2% | 2 | 21.5% |
| VBand Strategy by Saravanan_Ragavan | atr | v4 strategy | No trades on this market | ||
| Volatility Breakout Strategy [Angel Algo] by AngelAlgo | atr | v5 strategy | +3.1% | 18 | 3.6% |
| ATR Mean Reversion Strategy V1 by Bcullen175 | atr | v5 strategy | −20.0% | 79 | 59.6% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 9 ms | Yes 1,000 bars in 24 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/// © 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)