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
- 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.
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
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: 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Source | close | source | Trail1 Trail2 |
| Fast ATR period | 5 | int | SL1 |
| Fast ATR multiplier | 0.5 | float | SL1 |
| Slow ATR period | 10 | int | SL2 |
| Slow ATR multiplier | 3 | int | SL2 |
| Paint color on chart | true | bool | bcl |
What it calculates
- ATR (
atr,ta.atrin v5)Fast ATR period (5)SL1 · line 16 - ATR (
atr,ta.atrin v5)Slow ATR period (10)SL2 · line 22 - Crossover (
crossover,ta.crossoverin v5)Trail1, Trail2Buy · line 33 - Crossunder (
crossunder,ta.crossunderin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Buy long | Buy | On | 42 |
| Open Sell short | Sell | On | 45 |
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.
- 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.
| Market | Net | Buy and hold | Trades | Won | Profit factor | Max drawdown | Period |
|---|---|---|---|---|---|---|---|
| BTCUSDT 1D | −27.4% | +102.5% | 23 | 30% | 0.75 | 60.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −1.2% | +5.5% | 27 | 37% | 0.97 | 16.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −1.9% | +9.8% | 26 | 35% | 0.75 | 3.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.2% | +8.4% | 23 | 35% | 1.13 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 24 | 38% | 0.87 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 21 | 38% | 1.83 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.8% | +148.3% | 30 | 27% | 0.46 | 1.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 23 | 39% | 1.06 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 8 | 13% | −$66,360.80 |
| Sideways market | 12 | 33% | $32,727.10 |
| Falling market | 3 | 67% | $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.
All 23 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 20 Mar 2024 61,937.40 | 21 May 2024 71,446.60 | 62 | −$9,509.21 |
| long | 21 May 2024 71,446.60 | 22 Jun 2024 64,143.60 | 32 | −$7,303.06 |
| short | 22 Jun 2024 64,143.60 | 16 Jul 2024 64,724.10 | 24 | −$580.50 |
| long | 16 Jul 2024 64,724.10 | 4 Aug 2024 60,698.00 | 19 | −$4,026.07 |
| short | 4 Aug 2024 60,698.00 | 19 Sep 2024 61,760.00 | 46 | −$1,061.99 |
| long | 19 Sep 2024 61,760.00 | 30 Dec 2024 93,738.20 | 102 | $31,978.20 |
| short | 30 Dec 2024 93,738.20 | 18 Jan 2025 104,077.00 | 19 | −$10,339.30 |
| long | 18 Jan 2025 104,077.00 | 25 Feb 2025 91,552.90 | 38 | −$12,524.60 |
| short | 25 Feb 2025 91,552.90 | 23 Apr 2025 93,443.00 | 57 | −$1,890.11 |
| long | 23 Apr 2025 93,443.00 | 6 Jun 2025 101,509.00 | 44 | $8,065.70 |
| short | 6 Jun 2025 101,509.00 | 10 Jun 2025 110,263.00 | 4 | −$8,754.33 |
| long | 10 Jun 2025 110,263.00 | 23 Jun 2025 100,964.00 | 13 | −$9,299.15 |
| short | 23 Jun 2025 100,964.00 | 10 Jul 2025 111,234.00 | 17 | −$10,270.10 |
| long | 10 Jul 2025 111,234.00 | 20 Aug 2025 112,873.00 | 41 | $1,638.95 |
| short | 20 Aug 2025 112,873.00 | 2 Oct 2025 118,595.00 | 43 | −$5,722.04 |
| long | 2 Oct 2025 118,595.00 | 11 Oct 2025 112,774.00 | 9 | −$5,820.50 |
| short | 11 Oct 2025 112,774.00 | 14 Jan 2026 95,414.00 | 95 | $17,360.50 |
| long | 14 Jan 2026 95,414.00 | 21 Jan 2026 88,427.70 | 7 | −$6,986.33 |
| short | 21 Jan 2026 88,427.70 | 17 Mar 2026 74,884.70 | 55 | $13,543.00 |
| long | 17 Mar 2026 74,884.70 | 30 Mar 2026 66,010.90 | 13 | −$8,873.74 |
| short | 30 Mar 2026 66,010.90 | 14 Apr 2026 74,418.00 | 15 | −$8,407.07 |
| long | 14 Apr 2026 74,418.00 | 23 May 2026 75,539.50 | 39 | $1,121.50 |
| short | 23 May 2026 75,539.50 | 21 Jul 2026 65,255.50 | 59 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| ATR Trailing Stop Strategy by ceyhun this page | — | v4 strategy | −27.4% | 23 | 60.0% |
| SOJA PIVOT by ParaBellum68 | atr | v4 strategy | +3.3% | 9 | 18.2% |
| Volatility System by EduardoMattje | atr | v5 strategy | +150.2% | 2 | 21.5% |
| ATR Stop Buy Strategy by phobo3s | atr | v4 strategy | No trades on this market | ||
| 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% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 13 ms | With a workaround 1,000 bars in 53 ms | Not measured |
| PineTS | No 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")