Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

ATR Trailing Stop Strategy by ceyhun

A Pine Script v4 strategy built on ATR (5), Crossover (Trail1, Trail2) and Crossunder (Trail1, Trail2). 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. 6 settings; defaults include Source close, Fast ATR period 5 and Fast ATR multiplier 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 Trailing Stop Strategy by ceyhun repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 6 of 6 trades.
How did ATR Trailing Stop Strategy by ceyhun do on BTCUSDT 1D with default settings?
−27.38%
From 26 Jan 2024 to 21 Oct 2026, over 23 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can ATR Trailing Stop Strategy by ceyhun 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 Trailing Stop Strategy by ceyhun use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does ATR Trailing Stop Strategy by ceyhun run in Zeal Terminal?
Yes
It compiles in 13 ms and runs 1,000 bars in 53 ms, in the browser.

BTCUSDT · 1D · default settings

  • Fast Trail
  • Slow Trail
  • 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 (6)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$92,071$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. The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
Net profit
−27.38%
−$27,376.30
Buy and hold
+102.5%
same period
Closed trades
23
7 won · 16 lost
Profitable
30.4%
of closed trades
Profit factor
0.75
gross profit ÷ gross loss
Max drawdown
59.99%
−$65,690.00

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 18

    iff() on line 18, 18, 18, 24, 24, 24, 39. 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 atr → ta.atr, max → math.max, min → math.min, barssince → ta.barssince, crossover → ta.crossover, crossunder → ta.crossunder.

v4 → v5

VerifiedDownload the v5 script

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

  • Line 3: //@version=4 → //@version=5
  • Line 12: input(type = input.source) → input.source()
  • Line 14: input(type = input.integer) → input.int()
  • Line 15: input(type = input.float) → input.float()
  • Line 16: atr → ta.atr
  • Line 18: iff(c, a, b) → (c ? a : b)
20 more edits
  • Line 18: max → math.max
  • Line 18: iff(c, a, b) → (c ? a : b)
  • Line 18: min → math.min
  • Line 18: iff(c, a, b) → (c ? a : b)
  • Line 20: input(type = input.integer) → input.int()
  • Line 21: input(type = input.float) → input.float()
  • Line 22: atr → ta.atr
  • Line 24: iff(c, a, b) → (c ? a : b)
  • Line 24: max → math.max
  • Line 24: iff(c, a, b) → (c ? a : b)
  • Line 24: min → math.min
  • Line 24: iff(c, a, b) → (c ? a : b)
  • Line 31: barssince → ta.barssince
  • Line 31: barssince → ta.barssince
  • Line 32: barssince → ta.barssince
  • Line 32: barssince → ta.barssince
  • Line 33: crossover → ta.crossover
  • Line 34: crossunder → ta.crossunder
  • Line 38: input(type = input.bool) → input.bool()
  • Line 39: iff(c, a, b) → (c ? a : b)
⋯// © ceyhun-//@version=4+//@version=5strategy("ATR Trailing Stop Strategy by ceyhun", overlay=true)⋯//////////////////////////////////////////////////////-SC = input(close, "Source", input.source)+SC = input.source(close, "Source")// Fast Trail //-AP1 = input(5, "Fast ATR period", input.integer)  // ATR Period+AP1 = input.int(5, "Fast ATR period")  // ATR Period-AF1 = input(0.5, "Fast ATR multiplier", input.float)  // ATR Factor+AF1 = input.float(0.5, "Fast ATR multiplier")  // ATR Factor-SL1 = AF1 * atr(AP1)  // Stop Loss+SL1 = AF1 * ta.atr(AP1)  // Stop LossTrail1 = 0.0-Trail1 := iff(SC > nz(Trail1[1], 0) and SC[1] > nz(Trail1[1], 0), max(nz(Trail1[1], 0), SC - SL1), iff(SC < nz(Trail1[1], 0) and SC[1] < nz(Trail1[1], 0), min(nz(Trail1[1], 0), SC + SL1), iff(SC > nz(Trail1[1], 0), SC - SL1, SC + SL1)))+Trail1 := (SC > nz(Trail1[1], 0) and SC[1] > nz(Trail1[1], 0) ? math.max(nz(Trail1[1], 0), SC - SL1) : (SC < nz(Trail1[1], 0) and SC[1] < nz(Trail1[1], 0) ? math.min(nz(Trail1[1], 0), SC + SL1) : (SC > nz(Trail1[1], 0) ? SC - SL1 : SC + SL1)))// Slow Trail //-AP2 = input(10, "Slow ATR period", input.integer)  // ATR Period+AP2 = input.int(10, "Slow ATR period")  // ATR Period-AF2 = input(3, "Slow ATR multiplier", input.float)  // ATR Factor+AF2 = input.float(3, "Slow ATR multiplier")  // ATR Factor-SL2 = AF2 * atr(AP2)  // Stop Loss+SL2 = AF2 * ta.atr(AP2)  // Stop LossTrail2 = 0.0-Trail2 := iff(SC > nz(Trail2[1], 0) and SC[1] > nz(Trail2[1], 0), max(nz(Trail2[1], 0), SC - SL2), iff(SC < nz(Trail2[1], 0) and SC[1] < nz(Trail2[1], 0), min(nz(Trail2[1], 0), SC + SL2), iff(SC > nz(Trail2[1], 0), SC - SL2, SC + SL2)))+Trail2 := (SC > nz(Trail2[1], 0) and SC[1] > nz(Trail2[1], 0) ? math.max(nz(Trail2[1], 0), SC - SL2) : (SC < nz(Trail2[1], 0) and SC[1] < nz(Trail2[1], 0) ? math.min(nz(Trail2[1], 0), SC + SL2) : (SC > nz(Trail2[1], 0) ? SC - SL2 : SC + SL2)))// Bar color for trade signal //⋯// Signals //-Bull = barssince(Green) < barssince(Red)+Bull = ta.barssince(Green) < ta.barssince(Red)-Bear = barssince(Red) < barssince(Green)+Bear = ta.barssince(Red) < ta.barssince(Green)-Buy = crossover(Trail1, Trail2)+Buy = ta.crossover(Trail1, Trail2)-Sell = crossunder(Trail1, Trail2)+Sell = ta.crossunder(Trail1, Trail2)TS1 = plot(Trail1, "Fast Trail", style=plot.style_line,color=Trail1 > Trail2 ? color.blue : color.yellow, linewidth=2, display=display.none)⋯fill(TS1, TS2, Bull ? color.new(color.green,90) : color.new(color.red,90))-plotcolor = input(true, "Paint color on chart", input.bool)+plotcolor = input.bool(true, "Paint color on chart")-bcl = iff(plotcolor == 1, Blue ? color.blue : Green ? color.lime : Yellow ? color.yellow : Red ? color.red : color.white, na)+bcl = (plotcolor == 1 ? Blue ? color.blue : Green ? color.lime : Yellow ? color.yellow : Red ? color.red : color.white : na)barcolor(bcl)⋯

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: 23 before, 23 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 Stop Strategy by ceyhun", 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
SourceclosesourceTrail1 Trail2
Fast ATR period5intSL1
Fast ATR multiplier0.5floatSL1
Slow ATR period10intSL2
Slow ATR multiplier3intSL2
Paint color on charttrueboolbcl

What it calculates

  • ATR (atr, ta.atr in v5)Fast ATR period (5)SL1 · line 16
  • ATR (atr, ta.atr in v5)Slow ATR period (10)SL2 · line 22
  • Crossover (crossover, ta.crossover in v5)Trail1, Trail2Buy · line 33
  • Crossunder (crossunder, ta.crossunder in v5)Trail1, Trail2Sell · line 34

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 dataAF1Fast ATR multipliersetting · 0.5AP1Fast ATR periodsetting · 5AF2Slow ATR multipliersetting · 3AP2Slow ATR periodsetting · 10closecloseprice datalowlowprice datahighhighprice dataSL1ATR→ SL1SL2ATR→ SL2BuyCrossover→ BuySellCrossunder→ SellTrail1Trail1conditionTrail2Trail2conditionGreen, BlueGreen · Blue2 conditionsRed, YellowRed · Yellow2 conditionsBull, BearBull · Bear2 conditionsbclColourbclentry:BuyOpen Buystrategy.entryentry:SellOpen Sellstrategy.entryplot:35Fast Trailplotplot:36Slow Trailplotfill:37Fillfillbarcolor:40barcolorbarcolor

Order rules

OrderFires whenBy defaultLine
Open Buy longBuyOn42
Open Sell shortSellOn45

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.

6/6trades 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−27.4%+102.5%2330%0.7560.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−1.2%+5.5%2737%0.9716.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−1.9%+9.8%2635%0.753.4%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.2%+8.4%2335%1.130.7%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.0%+16.7%2438%0.870.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%2138%1.830.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.8%+148.3%3027%0.461.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%2339%1.060.1%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 market813%−$66,360.80
Sideways market1233%$32,727.10
Falling market367%$6,257.42

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

65.5%

The 95th percentile of the worst drawdown when the same 23 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 38.7% to 65.5%; the tick is what actually happened, 50.2%. Order luck alone moves it this much.

0%75%
All 23 trades
SideEntryExitBarsP&L
short20 Mar 2024 61,937.4021 May 2024 71,446.6062−$9,509.21
long21 May 2024 71,446.6022 Jun 2024 64,143.6032−$7,303.06
short22 Jun 2024 64,143.6016 Jul 2024 64,724.1024−$580.50
long16 Jul 2024 64,724.104 Aug 2024 60,698.0019−$4,026.07
short4 Aug 2024 60,698.0019 Sep 2024 61,760.0046−$1,061.99
long19 Sep 2024 61,760.0030 Dec 2024 93,738.20102$31,978.20
short30 Dec 2024 93,738.2018 Jan 2025 104,077.0019−$10,339.30
long18 Jan 2025 104,077.0025 Feb 2025 91,552.9038−$12,524.60
short25 Feb 2025 91,552.9023 Apr 2025 93,443.0057−$1,890.11
long23 Apr 2025 93,443.006 Jun 2025 101,509.0044$8,065.70
short6 Jun 2025 101,509.0010 Jun 2025 110,263.004−$8,754.33
long10 Jun 2025 110,263.0023 Jun 2025 100,964.0013−$9,299.15
short23 Jun 2025 100,964.0010 Jul 2025 111,234.0017−$10,270.10
long10 Jul 2025 111,234.0020 Aug 2025 112,873.0041$1,638.95
short20 Aug 2025 112,873.002 Oct 2025 118,595.0043−$5,722.04
long2 Oct 2025 118,595.0011 Oct 2025 112,774.009−$5,820.50
short11 Oct 2025 112,774.0014 Jan 2026 95,414.0095$17,360.50
long14 Jan 2026 95,414.0021 Jan 2026 88,427.707−$6,986.33
short21 Jan 2026 88,427.7017 Mar 2026 74,884.7055$13,543.00
long17 Mar 2026 74,884.7030 Mar 2026 66,010.9013−$8,873.74
short30 Mar 2026 66,010.9014 Apr 2026 74,418.0015−$8,407.07
long14 Apr 2026 74,418.0023 May 2026 75,539.5039$1,121.50
short23 May 2026 75,539.5021 Jul 2026 65,255.5059$10,284.00

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 Stop Strategy by ceyhun this page—v4 strategy−27.4%2360.0%
SOJA PIVOT by ParaBellum68atrv4 strategy+3.3%918.2%
Volatility System by EduardoMattjeatrv5 strategy+150.2%221.5%
ATR Stop Buy Strategy by phobo3satrv4 strategyNo trades on this market
VBand Strategy by Saravanan_Ragavanatrv4 strategyNo trades on this market
Volatility Breakout Strategy [Angel Algo] by AngelAlgoatrv5 strategy+3.1%183.6%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 13 msWith a workaround 1,000 bars in 53 msNot measured
PineTSNo v5 and v6 only—Not measured

The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.

“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 Stop Strategy by ceyhun", overlay=true)/////////notes////////////////////////////////////////// This is based on the ATR trailing stop indicator //// width addition of two levels of stops and        //// different interpretation.                        //// This is a fast-reacting system and is better     //// suited for higher volatility markets             ////////////////////////////////////////////////////////SC = input(close, "Source", input.source)// Fast Trail //AP1 = input(5, "Fast ATR period", input.integer)  // ATR PeriodAF1 = input(0.5, "Fast ATR multiplier", input.float)  // ATR FactorSL1 = AF1 * atr(AP1)  // Stop LossTrail1 = 0.0Trail1 := iff(SC > nz(Trail1[1], 0) and SC[1] > nz(Trail1[1], 0), max(nz(Trail1[1], 0), SC - SL1), iff(SC < nz(Trail1[1], 0) and SC[1] < nz(Trail1[1], 0), min(nz(Trail1[1], 0), SC + SL1), iff(SC > nz(Trail1[1], 0), SC - SL1, SC + SL1)))// Slow Trail //AP2 = input(10, "Slow ATR period", input.integer)  // ATR PeriodAF2 = input(3, "Slow ATR multiplier", input.float)  // ATR FactorSL2 = AF2 * atr(AP2)  // Stop LossTrail2 = 0.0Trail2 := iff(SC > nz(Trail2[1], 0) and SC[1] > nz(Trail2[1], 0), max(nz(Trail2[1], 0), SC - SL2), iff(SC < nz(Trail2[1], 0) and SC[1] < nz(Trail2[1], 0), min(nz(Trail2[1], 0), SC + SL2), iff(SC > nz(Trail2[1], 0), SC - SL2, SC + SL2)))// Bar color for trade signal //Green = Trail1 > Trail2 and close > Trail2 and low > Trail2Blue = Trail1 > Trail2 and close > Trail2 and low < Trail2Red = Trail2 > Trail1 and close < Trail2 and high < Trail2Yellow = Trail2 > Trail1 and close < Trail2 and high > Trail2// Signals //Bull = barssince(Green) < barssince(Red)Bear = barssince(Red) < barssince(Green)Buy = crossover(Trail1, Trail2)Sell = crossunder(Trail1, Trail2)TS1 = plot(Trail1, "Fast Trail", style=plot.style_line,color=Trail1 > Trail2 ? color.blue : color.yellow, linewidth=2, display=display.none)TS2 = plot(Trail2, "Slow Trail", style=plot.style_line,color=Trail1 > Trail2 ? color.green : color.red, linewidth=2)fill(TS1, TS2, Bull ? color.new(color.green,90) : color.new(color.red,90))plotcolor = input(true, "Paint color on chart", input.bool)bcl = iff(plotcolor == 1, Blue ? color.blue : Green ? color.lime : Yellow ? color.yellow : Red ? color.red : color.white, na)barcolor(bcl)if Buy    strategy.entry("Buy", strategy.long, comment="Buy")    if Sell    strategy.entry("Sell", strategy.short, comment="Sell")