Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

ATR Trailing Stoploss Strategy

A Pine Script v4 strategy built on Highest or ATR (Atr, 10), Crossover (close, TS) and Crossunder (close, TS). 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 is one contract and pays no commission. 4 settings; defaults include Atr Period 5, HHV Period 10 and Multiplier 2.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 Trailing Stoploss Strategy repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 23 of 23 trades.
How did ATR Trailing Stoploss Strategy do on BTCUSDT 1D with default settings?
−17.56%
From 26 Jan 2024 to 21 Oct 2026, over 102 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can ATR Trailing Stoploss Strategy be converted to Pine Script v5?
No
The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
Does ATR Trailing Stoploss Strategy use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does ATR Trailing Stoploss Strategy run in Zeal Terminal?
Yes
It compiles in 10 ms and runs 1,000 bars in 28 ms, in the browser.

BTCUSDT · 1D · default settings

  • ATR Trailing Stoploss
  • 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 (4)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$84,901$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
−17.56%
−$17,564.00
Buy and hold
+102.5%
same period
Closed trades
102
28 won · 74 lost
Profitable
27.5%
of closed trades
Profit factor
0.91
gross profit ÷ gross loss
Max drawdown
65.43%
−$74,709.70

One run with the author's default settings, $100,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 iff(), which v5 removed line 10

    iff() on line 10, 10, 11, 11. v5 writes it as the ternary c ? a : b.

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 highest → ta.highest, atr → ta.atr, barssince → ta.barssince, cum → ta.cum, crossover → ta.crossover, crossunder → ta.crossunder.

v4 → v5

Differs

Trades differ: 102 before, 154 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 9: highest → ta.highest
  • Line 9: atr → ta.atr
  • Line 9: barssince → ta.barssince
  • Line 9: highest → ta.highest
  • Line 9: atr → ta.atr
11 more edits
  • Line 10: iff(c, a, b) → (c ? a : b)
  • Line 10: cum → ta.cum
  • Line 10: iff(c, a, b) → (c ? a : b)
  • Line 10: highest → ta.highest
  • Line 10: atr → ta.atr
  • Line 10: highest → ta.highest
  • Line 10: atr → ta.atr
  • Line 11: iff(c, a, b) → (c ? a : b)
  • Line 11: iff(c, a, b) → (c ? a : b)
  • Line 14: crossover → ta.crossover
  • Line 15: crossunder → ta.crossunder
⋯// © ceyhun-//@version=4+//@version=5strategy("ATR Trailing Stoploss Strategy ",overlay=true)⋯Barcolor=input(true,title="Barcolor")-Prev = highest(high-Mult*atr(Atr),Hhv),barssince(close>highest(high-Mult*atr(Atr),Hhv) and close>close[1])+Prev = ta.highest(high-Mult*ta.atr(Atr),Hhv),ta.barssince(close>ta.highest(high-Mult*ta.atr(Atr),Hhv) and close>close[1])-TS = iff(cum(1)<16 ,close,iff( close > highest(high-Mult*atr(Atr),Hhv) and close>close[1],highest(high-Mult*atr(Atr),Hhv),Prev))+TS = (ta.cum(1)<16 ? close : ( close > ta.highest(high-Mult*ta.atr(Atr),Hhv) and close>close[1] ? ta.highest(high-Mult*ta.atr(Atr),Hhv) : Prev))-Color=iff(close>TS,color.green,iff(close<TS,color.red,color.black))+Color=(close>TS ? color.green : (close<TS ? color.red : color.black))barcolor(Barcolor? Color:na)plot(TS,color=Color,linewidth=3,title="ATR Trailing Stoploss")-Buy  = crossover(close,TS)+Buy  = ta.crossover(close,TS)-Sell = crossunder(close,TS)+Sell = ta.crossunder(close,TS)if Buy⋯

Clean v5

Verified

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

  • Line 5: input() → input.int()
  • Line 6: input() → input.int()
  • Line 7: input() → input.float()
  • Line 8: input() → input.bool()
⋯strategy("ATR Trailing Stoploss Strategy ",overlay=true)-Atr=input(defval=5,title="Atr Period",minval=1,maxval=500)+Atr=input.int(defval=5,title="Atr Period",minval=1,maxval=500)-Hhv=input(defval=10,title="HHV Period",minval=1,maxval=500)+Hhv=input.int(defval=10,title="HHV Period",minval=1,maxval=500)-Mult=input(defval=2.5,title="Multiplier",minval=0.1)+Mult=input.float(defval=2.5,title="Multiplier",minval=0.1)-Barcolor=input(true,title="Barcolor")+Barcolor=input.bool(true,title="Barcolor")Prev = ta.highest(high-Mult*ta.atr(Atr),Hhv),ta.barssince(close>ta.highest(high-Mult*ta.atr(Atr),Hhv) and close>close[1])⋯

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: 154 before, 108 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.

⋯// © ceyhun-//@version=5+//@version=6strategy("ATR Trailing Stoploss Strategy ",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
Atr Period5int · min 1 · max 500Prev TS line 9
HHV Period10int · min 1 · max 500Prev TS line 9
Multiplier2.5float · min 0.1Prev TS line 9
Barcolortrueboolbarcolor

What it calculates

  • Highest / ATR (highest, ta.highest in v5)high-Mult*atr(Atr), HHV Period (10)Prev · line 9
  • Crossover (crossover, ta.crossover in v5)close, TSBuy · line 14
  • Crossunder (crossunder, ta.crossunder in v5)close, TSSell · line 15

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.

highhighprice dataMultMultipliersetting · 2.5AtrAtr Periodsetting · 5HhvHHV Periodsetting · 10closecloseprice dataPrevHighest / ATR→ PrevBuyCrossover→ BuySellCrossunder→ SellTSTSconditionColorColourColorentry:BuyOpen Buystrategy.entryentry:SellOpen Sellstrategy.entrybarcolor:12barcolorbarcolorplot:13ATR Trailing Stoplossplot

Order rules

OrderFires whenBy defaultLine
Open Buy longBuyOn17
Open Sell shortSellOn20

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.

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

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D−17.6%+102.5%10227%0.9165.4%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+3.2%+5.5%8432%1.0613.7%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.3%+9.8%8635%1.043.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.3%+8.4%9428%0.870.7%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.3%+16.7%10228%0.620.4%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%9629%1.080.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.2%+148.3%8034%0.870.8%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.1%+17.1%11432%0.800.2%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 market3222%−$37,401.90
Sideways market5127%$12,766.50
Falling market1937%$7,071.38

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 102 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 43.6% to 72.3%; the tick is what actually happened, 59.3%. Order luck alone moves it this much.

0%83%
All 102 trades
SideEntryExitBarsP&L
long11 Feb 2024 47,751.1020 Mar 2024 61,937.4038$14,186.30
short20 Mar 2024 61,937.4021 Mar 2024 67,840.501−$5,903.10
long21 Mar 2024 67,840.5023 Mar 2024 63,796.602−$4,043.87
short23 Mar 2024 63,796.6025 Mar 2024 67,210.002−$3,413.36
long25 Mar 2024 67,210.0014 Apr 2024 63,924.5020−$3,285.48
short14 Apr 2024 63,924.5015 Apr 2024 65,661.901−$1,737.33
long15 Apr 2024 65,661.9016 Apr 2024 63,420.001−$2,241.86
short16 Apr 2024 63,420.0021 Apr 2024 64,940.605−$1,520.59
long21 Apr 2024 64,940.602 May 2024 58,365.0011−$6,575.61
short2 May 2024 58,365.004 May 2024 62,882.002−$4,517.04
long4 May 2024 62,882.0014 Jun 2024 66,773.0041$3,891.00
short14 Jun 2024 66,773.0017 Jun 2024 66,676.903$96.15
long17 Jun 2024 66,676.9019 Jun 2024 65,175.302−$1,501.54
short19 Jun 2024 65,175.3021 Jun 2024 64,870.002$305.33
long21 Jun 2024 64,870.0022 Jun 2024 64,143.601−$726.43
short22 Jun 2024 64,143.6023 Jun 2024 64,262.001−$118.45
long23 Jun 2024 64,262.0025 Jun 2024 60,293.302−$3,968.71
short25 Jun 2024 60,293.301 Jul 2024 62,772.006−$2,478.71
long1 Jul 2024 62,772.005 Jul 2024 57,050.004−$5,721.99
short5 Jul 2024 57,050.0013 Jul 2024 57,889.108−$839.07
long13 Jul 2024 57,889.103 Aug 2024 61,498.3021$3,609.25
short3 Aug 2024 61,498.309 Aug 2024 61,686.006−$187.66
long9 Aug 2024 61,686.0010 Aug 2024 60,838.001−$848.01
short10 Aug 2024 60,838.0011 Aug 2024 60,923.501−$85.52
long11 Aug 2024 60,923.5029 Aug 2024 59,034.9018−$1,888.61
short29 Aug 2024 59,034.909 Sep 2024 54,869.9011$4,164.95
long9 Sep 2024 54,869.902 Oct 2024 60,804.9023$5,934.97
short2 Oct 2024 60,804.905 Oct 2024 62,086.003−$1,281.08
long5 Oct 2024 62,086.006 Oct 2024 62,058.001−$27.99
short6 Oct 2024 62,058.007 Oct 2024 62,819.901−$761.90
long7 Oct 2024 62,819.9010 Oct 2024 60,636.003−$2,183.90
short10 Oct 2024 60,636.0011 Oct 2024 60,326.401$309.61
long11 Oct 2024 60,326.4020 Dec 2024 97,461.9070$37,135.50
short20 Dec 2024 97,461.9026 Dec 2024 99,429.606−$1,967.75
long26 Dec 2024 99,429.6027 Dec 2024 95,791.601−$3,638.01
short27 Dec 2024 95,791.6029 Dec 2024 95,300.002$491.60
long29 Dec 2024 95,300.0010 Jan 2025 92,552.5012−$2,747.51
short10 Jan 2025 92,552.5015 Jan 2025 96,560.905−$4,008.36
long15 Jan 2025 96,560.906 Feb 2025 96,612.4022$51.59
short6 Feb 2025 96,612.4010 Feb 2025 96,462.804$149.69
long10 Feb 2025 96,462.8025 Feb 2025 91,552.9015−$4,909.87
short25 Feb 2025 91,552.903 Mar 2025 94,270.006−$2,717.11
long3 Mar 2025 94,270.004 Mar 2025 86,221.201−$8,048.83
short4 Mar 2025 86,221.206 Mar 2025 90,606.002−$4,384.84
long6 Mar 2025 90,606.0010 Mar 2025 80,734.504−$9,871.52
short10 Mar 2025 80,734.5012 Mar 2025 82,933.002−$2,198.51
long12 Mar 2025 82,933.007 Apr 2025 78,430.0026−$4,502.99
short7 Apr 2025 78,430.0010 Apr 2025 82,615.203−$4,185.22
long10 Apr 2025 82,615.2011 Apr 2025 79,607.301−$3,007.92
short11 Apr 2025 79,607.3012 Apr 2025 83,423.801−$3,816.53
long12 Apr 2025 83,423.806 Jun 2025 101,509.0055$18,084.90
short6 Jun 2025 101,509.007 Jun 2025 104,288.001−$2,779.74
long7 Jun 2025 104,288.0021 Jun 2025 103,298.0014−$990.45
short21 Jun 2025 103,298.0022 Jun 2025 102,120.001$1,177.96
long22 Jun 2025 102,120.0023 Jun 2025 100,964.001−$1,156.15
short23 Jun 2025 100,964.0024 Jun 2025 105,334.001−$4,370.07
long24 Jun 2025 105,334.002 Aug 2025 113,298.0039$7,963.98
short2 Aug 2025 113,298.004 Aug 2025 114,209.002−$910.89
long4 Aug 2025 114,209.0020 Aug 2025 112,873.0016−$1,335.86
short20 Aug 2025 112,873.0023 Aug 2025 116,936.003−$4,063.05
long23 Aug 2025 116,936.0026 Aug 2025 110,112.003−$6,824.02
short26 Aug 2025 110,112.0028 Aug 2025 111,262.002−$1,150.03
long28 Aug 2025 111,262.0030 Aug 2025 108,377.002−$2,884.61
short30 Aug 2025 108,377.002 Sep 2025 109,237.003−$860.03
long2 Sep 2025 109,237.0023 Sep 2025 112,651.0021$3,413.56
short23 Sep 2025 112,651.0025 Sep 2025 113,307.002−$656.02
long25 Sep 2025 113,307.0026 Sep 2025 108,994.001−$4,312.52
short26 Sep 2025 108,994.0029 Sep 2025 112,164.003−$3,169.47
long29 Sep 2025 112,164.0011 Oct 2025 112,774.0012$610.53
short11 Oct 2025 112,774.0020 Oct 2025 108,643.009$4,131.72
long20 Oct 2025 108,643.004 Nov 2025 106,583.0015−$2,059.72
short4 Nov 2025 106,583.0010 Nov 2025 104,723.006$1,860.10
long10 Nov 2025 104,723.0012 Nov 2025 103,059.002−$1,663.95
short12 Nov 2025 103,059.0014 Nov 2025 99,692.002$3,366.97
long14 Nov 2025 99,692.0015 Nov 2025 94,594.001−$5,098.03
short15 Nov 2025 94,594.0025 Nov 2025 88,300.0010$6,293.99
long25 Nov 2025 88,300.0019 Dec 2025 85,516.4024−$2,783.60
short19 Dec 2025 85,516.4020 Dec 2025 88,136.901−$2,620.54
long20 Dec 2025 88,136.9021 Jan 2026 88,427.7032$290.71
short21 Jan 2026 88,427.7029 Jan 2026 89,300.008−$872.34
long29 Jan 2026 89,300.0030 Jan 2026 84,650.201−$4,649.84
short30 Jan 2026 84,650.2014 Feb 2026 68,854.0015$15,796.20
long14 Feb 2026 68,854.0022 Mar 2026 68,918.1036$64.15
short22 Mar 2026 68,918.1024 Mar 2026 70,906.402−$1,988.33
long24 Mar 2026 70,906.4027 Mar 2026 68,820.303−$2,086.14
short27 Mar 2026 68,820.3029 Mar 2026 66,377.002$2,443.27
long29 Mar 2026 66,377.0018 May 2026 77,457.7050$11,080.60
short18 May 2026 77,457.7022 May 2026 77,615.504−$157.85
long22 May 2026 77,615.5023 May 2026 75,539.501−$2,076.02
short23 May 2026 75,539.5024 May 2026 76,752.001−$1,212.50
long24 May 2026 76,752.0029 May 2026 73,617.505−$3,134.48
short29 May 2026 73,617.5031 May 2026 73,884.402−$266.86
long31 May 2026 73,884.402 Jun 2026 71,408.902−$2,475.48
short2 Jun 2026 71,408.9013 Jun 2026 63,580.0011$7,828.90
long13 Jun 2026 63,580.0025 Jun 2026 61,078.0012−$2,502.00
short25 Jun 2026 61,078.003 Jul 2026 61,560.008−$482.00
long3 Jul 2026 61,560.009 Oct 2026 81,754.5098$20,194.50
short9 Oct 2026 81,754.5010 Oct 2026 82,635.601−$881.10
long10 Oct 2026 82,635.6013 Oct 2026 81,850.003−$785.55
short13 Oct 2026 81,850.0014 Oct 2026 83,169.001−$1,319.00
long14 Oct 2026 83,169.0015 Oct 2026 81,682.001−$1,486.99
short15 Oct 2026 81,682.0016 Oct 2026 82,245.901−$563.91

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 Trailing Stoploss Strategy this page—v4 strategy−17.6%10265.4%
S&P Bear Warning Indicator by gary_tradesatr highestv4 strategyNo trades on this market
Choppiness Index by Zeal Terminalatr highestv5 indicatorindicator
Aggresive Scalper-Swing Crypto Strategy by exlux99highest atrv4 strategy−7.4%316.0%
Donchian Channels Strategy by KrisWaters by kriswatershighest atrv4 strategy+55.8%2123.3%
Chandelier Exit - Strategy by melihtunaatr highestv4 strategy−34.8%1143.2%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 10 msYes 1,000 bars in 28 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/// © ceyhun//@version=4strategy("ATR Trailing Stoploss Strategy ",overlay=true)Atr=input(defval=5,title="Atr Period",minval=1,maxval=500)Hhv=input(defval=10,title="HHV Period",minval=1,maxval=500)Mult=input(defval=2.5,title="Multiplier",minval=0.1)Barcolor=input(true,title="Barcolor")Prev = highest(high-Mult*atr(Atr),Hhv),barssince(close>highest(high-Mult*atr(Atr),Hhv) and close>close[1])TS = iff(cum(1)<16 ,close,iff( close > highest(high-Mult*atr(Atr),Hhv) and close>close[1],highest(high-Mult*atr(Atr),Hhv),Prev))Color=iff(close>TS,color.green,iff(close<TS,color.red,color.black))barcolor(Barcolor? Color:na)plot(TS,color=Color,linewidth=3,title="ATR Trailing Stoploss")Buy  = crossover(close,TS)Sell = crossunder(close,TS)if Buy    strategy.entry("Buy", strategy.long, comment="Buy")    if Sell    strategy.entry("Sell", strategy.short, comment="Sell")