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
- 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
DiffersTrades 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
VerifiedSame 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Atr Period | 5 | int · min 1 · max 500 | Prev TS line 9 |
| HHV Period | 10 | int · min 1 · max 500 | Prev TS line 9 |
| Multiplier | 2.5 | float · min 0.1 | Prev TS line 9 |
| Barcolor | true | bool | barcolor |
What it calculates
- Highest / ATR (
highest,ta.highestin v5)high-Mult*atr(Atr), HHV Period (10)Prev · line 9 - Crossover (
crossover,ta.crossoverin v5)close, TSBuy · line 14 - Crossunder (
crossunder,ta.crossunderin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Buy long | Buy | On | 17 |
| Open Sell short | Sell | On | 20 |
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 | −17.6% | +102.5% | 102 | 27% | 0.91 | 65.4% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +3.2% | +5.5% | 84 | 32% | 1.06 | 13.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.3% | +9.8% | 86 | 35% | 1.04 | 3.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.3% | +8.4% | 94 | 28% | 0.87 | 0.7% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.3% | +16.7% | 102 | 28% | 0.62 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 96 | 29% | 1.08 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.2% | +148.3% | 80 | 34% | 0.87 | 0.8% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.1% | +17.1% | 114 | 32% | 0.80 | 0.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 32 | 22% | −$37,401.90 |
| Sideways market | 51 | 27% | $12,766.50 |
| Falling market | 19 | 37% | $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.
All 102 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 11 Feb 2024 47,751.10 | 20 Mar 2024 61,937.40 | 38 | $14,186.30 |
| short | 20 Mar 2024 61,937.40 | 21 Mar 2024 67,840.50 | 1 | −$5,903.10 |
| long | 21 Mar 2024 67,840.50 | 23 Mar 2024 63,796.60 | 2 | −$4,043.87 |
| short | 23 Mar 2024 63,796.60 | 25 Mar 2024 67,210.00 | 2 | −$3,413.36 |
| long | 25 Mar 2024 67,210.00 | 14 Apr 2024 63,924.50 | 20 | −$3,285.48 |
| short | 14 Apr 2024 63,924.50 | 15 Apr 2024 65,661.90 | 1 | −$1,737.33 |
| long | 15 Apr 2024 65,661.90 | 16 Apr 2024 63,420.00 | 1 | −$2,241.86 |
| short | 16 Apr 2024 63,420.00 | 21 Apr 2024 64,940.60 | 5 | −$1,520.59 |
| long | 21 Apr 2024 64,940.60 | 2 May 2024 58,365.00 | 11 | −$6,575.61 |
| short | 2 May 2024 58,365.00 | 4 May 2024 62,882.00 | 2 | −$4,517.04 |
| long | 4 May 2024 62,882.00 | 14 Jun 2024 66,773.00 | 41 | $3,891.00 |
| short | 14 Jun 2024 66,773.00 | 17 Jun 2024 66,676.90 | 3 | $96.15 |
| long | 17 Jun 2024 66,676.90 | 19 Jun 2024 65,175.30 | 2 | −$1,501.54 |
| short | 19 Jun 2024 65,175.30 | 21 Jun 2024 64,870.00 | 2 | $305.33 |
| long | 21 Jun 2024 64,870.00 | 22 Jun 2024 64,143.60 | 1 | −$726.43 |
| short | 22 Jun 2024 64,143.60 | 23 Jun 2024 64,262.00 | 1 | −$118.45 |
| long | 23 Jun 2024 64,262.00 | 25 Jun 2024 60,293.30 | 2 | −$3,968.71 |
| short | 25 Jun 2024 60,293.30 | 1 Jul 2024 62,772.00 | 6 | −$2,478.71 |
| long | 1 Jul 2024 62,772.00 | 5 Jul 2024 57,050.00 | 4 | −$5,721.99 |
| short | 5 Jul 2024 57,050.00 | 13 Jul 2024 57,889.10 | 8 | −$839.07 |
| long | 13 Jul 2024 57,889.10 | 3 Aug 2024 61,498.30 | 21 | $3,609.25 |
| short | 3 Aug 2024 61,498.30 | 9 Aug 2024 61,686.00 | 6 | −$187.66 |
| long | 9 Aug 2024 61,686.00 | 10 Aug 2024 60,838.00 | 1 | −$848.01 |
| short | 10 Aug 2024 60,838.00 | 11 Aug 2024 60,923.50 | 1 | −$85.52 |
| long | 11 Aug 2024 60,923.50 | 29 Aug 2024 59,034.90 | 18 | −$1,888.61 |
| short | 29 Aug 2024 59,034.90 | 9 Sep 2024 54,869.90 | 11 | $4,164.95 |
| long | 9 Sep 2024 54,869.90 | 2 Oct 2024 60,804.90 | 23 | $5,934.97 |
| short | 2 Oct 2024 60,804.90 | 5 Oct 2024 62,086.00 | 3 | −$1,281.08 |
| long | 5 Oct 2024 62,086.00 | 6 Oct 2024 62,058.00 | 1 | −$27.99 |
| short | 6 Oct 2024 62,058.00 | 7 Oct 2024 62,819.90 | 1 | −$761.90 |
| long | 7 Oct 2024 62,819.90 | 10 Oct 2024 60,636.00 | 3 | −$2,183.90 |
| short | 10 Oct 2024 60,636.00 | 11 Oct 2024 60,326.40 | 1 | $309.61 |
| long | 11 Oct 2024 60,326.40 | 20 Dec 2024 97,461.90 | 70 | $37,135.50 |
| short | 20 Dec 2024 97,461.90 | 26 Dec 2024 99,429.60 | 6 | −$1,967.75 |
| long | 26 Dec 2024 99,429.60 | 27 Dec 2024 95,791.60 | 1 | −$3,638.01 |
| short | 27 Dec 2024 95,791.60 | 29 Dec 2024 95,300.00 | 2 | $491.60 |
| long | 29 Dec 2024 95,300.00 | 10 Jan 2025 92,552.50 | 12 | −$2,747.51 |
| short | 10 Jan 2025 92,552.50 | 15 Jan 2025 96,560.90 | 5 | −$4,008.36 |
| long | 15 Jan 2025 96,560.90 | 6 Feb 2025 96,612.40 | 22 | $51.59 |
| short | 6 Feb 2025 96,612.40 | 10 Feb 2025 96,462.80 | 4 | $149.69 |
| long | 10 Feb 2025 96,462.80 | 25 Feb 2025 91,552.90 | 15 | −$4,909.87 |
| short | 25 Feb 2025 91,552.90 | 3 Mar 2025 94,270.00 | 6 | −$2,717.11 |
| long | 3 Mar 2025 94,270.00 | 4 Mar 2025 86,221.20 | 1 | −$8,048.83 |
| short | 4 Mar 2025 86,221.20 | 6 Mar 2025 90,606.00 | 2 | −$4,384.84 |
| long | 6 Mar 2025 90,606.00 | 10 Mar 2025 80,734.50 | 4 | −$9,871.52 |
| short | 10 Mar 2025 80,734.50 | 12 Mar 2025 82,933.00 | 2 | −$2,198.51 |
| long | 12 Mar 2025 82,933.00 | 7 Apr 2025 78,430.00 | 26 | −$4,502.99 |
| short | 7 Apr 2025 78,430.00 | 10 Apr 2025 82,615.20 | 3 | −$4,185.22 |
| long | 10 Apr 2025 82,615.20 | 11 Apr 2025 79,607.30 | 1 | −$3,007.92 |
| short | 11 Apr 2025 79,607.30 | 12 Apr 2025 83,423.80 | 1 | −$3,816.53 |
| long | 12 Apr 2025 83,423.80 | 6 Jun 2025 101,509.00 | 55 | $18,084.90 |
| short | 6 Jun 2025 101,509.00 | 7 Jun 2025 104,288.00 | 1 | −$2,779.74 |
| long | 7 Jun 2025 104,288.00 | 21 Jun 2025 103,298.00 | 14 | −$990.45 |
| short | 21 Jun 2025 103,298.00 | 22 Jun 2025 102,120.00 | 1 | $1,177.96 |
| long | 22 Jun 2025 102,120.00 | 23 Jun 2025 100,964.00 | 1 | −$1,156.15 |
| short | 23 Jun 2025 100,964.00 | 24 Jun 2025 105,334.00 | 1 | −$4,370.07 |
| long | 24 Jun 2025 105,334.00 | 2 Aug 2025 113,298.00 | 39 | $7,963.98 |
| short | 2 Aug 2025 113,298.00 | 4 Aug 2025 114,209.00 | 2 | −$910.89 |
| long | 4 Aug 2025 114,209.00 | 20 Aug 2025 112,873.00 | 16 | −$1,335.86 |
| short | 20 Aug 2025 112,873.00 | 23 Aug 2025 116,936.00 | 3 | −$4,063.05 |
| long | 23 Aug 2025 116,936.00 | 26 Aug 2025 110,112.00 | 3 | −$6,824.02 |
| short | 26 Aug 2025 110,112.00 | 28 Aug 2025 111,262.00 | 2 | −$1,150.03 |
| long | 28 Aug 2025 111,262.00 | 30 Aug 2025 108,377.00 | 2 | −$2,884.61 |
| short | 30 Aug 2025 108,377.00 | 2 Sep 2025 109,237.00 | 3 | −$860.03 |
| long | 2 Sep 2025 109,237.00 | 23 Sep 2025 112,651.00 | 21 | $3,413.56 |
| short | 23 Sep 2025 112,651.00 | 25 Sep 2025 113,307.00 | 2 | −$656.02 |
| long | 25 Sep 2025 113,307.00 | 26 Sep 2025 108,994.00 | 1 | −$4,312.52 |
| short | 26 Sep 2025 108,994.00 | 29 Sep 2025 112,164.00 | 3 | −$3,169.47 |
| long | 29 Sep 2025 112,164.00 | 11 Oct 2025 112,774.00 | 12 | $610.53 |
| short | 11 Oct 2025 112,774.00 | 20 Oct 2025 108,643.00 | 9 | $4,131.72 |
| long | 20 Oct 2025 108,643.00 | 4 Nov 2025 106,583.00 | 15 | −$2,059.72 |
| short | 4 Nov 2025 106,583.00 | 10 Nov 2025 104,723.00 | 6 | $1,860.10 |
| long | 10 Nov 2025 104,723.00 | 12 Nov 2025 103,059.00 | 2 | −$1,663.95 |
| short | 12 Nov 2025 103,059.00 | 14 Nov 2025 99,692.00 | 2 | $3,366.97 |
| long | 14 Nov 2025 99,692.00 | 15 Nov 2025 94,594.00 | 1 | −$5,098.03 |
| short | 15 Nov 2025 94,594.00 | 25 Nov 2025 88,300.00 | 10 | $6,293.99 |
| long | 25 Nov 2025 88,300.00 | 19 Dec 2025 85,516.40 | 24 | −$2,783.60 |
| short | 19 Dec 2025 85,516.40 | 20 Dec 2025 88,136.90 | 1 | −$2,620.54 |
| long | 20 Dec 2025 88,136.90 | 21 Jan 2026 88,427.70 | 32 | $290.71 |
| short | 21 Jan 2026 88,427.70 | 29 Jan 2026 89,300.00 | 8 | −$872.34 |
| long | 29 Jan 2026 89,300.00 | 30 Jan 2026 84,650.20 | 1 | −$4,649.84 |
| short | 30 Jan 2026 84,650.20 | 14 Feb 2026 68,854.00 | 15 | $15,796.20 |
| long | 14 Feb 2026 68,854.00 | 22 Mar 2026 68,918.10 | 36 | $64.15 |
| short | 22 Mar 2026 68,918.10 | 24 Mar 2026 70,906.40 | 2 | −$1,988.33 |
| long | 24 Mar 2026 70,906.40 | 27 Mar 2026 68,820.30 | 3 | −$2,086.14 |
| short | 27 Mar 2026 68,820.30 | 29 Mar 2026 66,377.00 | 2 | $2,443.27 |
| long | 29 Mar 2026 66,377.00 | 18 May 2026 77,457.70 | 50 | $11,080.60 |
| short | 18 May 2026 77,457.70 | 22 May 2026 77,615.50 | 4 | −$157.85 |
| long | 22 May 2026 77,615.50 | 23 May 2026 75,539.50 | 1 | −$2,076.02 |
| short | 23 May 2026 75,539.50 | 24 May 2026 76,752.00 | 1 | −$1,212.50 |
| long | 24 May 2026 76,752.00 | 29 May 2026 73,617.50 | 5 | −$3,134.48 |
| short | 29 May 2026 73,617.50 | 31 May 2026 73,884.40 | 2 | −$266.86 |
| long | 31 May 2026 73,884.40 | 2 Jun 2026 71,408.90 | 2 | −$2,475.48 |
| short | 2 Jun 2026 71,408.90 | 13 Jun 2026 63,580.00 | 11 | $7,828.90 |
| long | 13 Jun 2026 63,580.00 | 25 Jun 2026 61,078.00 | 12 | −$2,502.00 |
| short | 25 Jun 2026 61,078.00 | 3 Jul 2026 61,560.00 | 8 | −$482.00 |
| long | 3 Jul 2026 61,560.00 | 9 Oct 2026 81,754.50 | 98 | $20,194.50 |
| short | 9 Oct 2026 81,754.50 | 10 Oct 2026 82,635.60 | 1 | −$881.10 |
| long | 10 Oct 2026 82,635.60 | 13 Oct 2026 81,850.00 | 3 | −$785.55 |
| short | 13 Oct 2026 81,850.00 | 14 Oct 2026 83,169.00 | 1 | −$1,319.00 |
| long | 14 Oct 2026 83,169.00 | 15 Oct 2026 81,682.00 | 1 | −$1,486.99 |
| short | 15 Oct 2026 81,682.00 | 16 Oct 2026 82,245.90 | 1 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| ATR Trailing Stoploss Strategy this page | — | v4 strategy | −17.6% | 102 | 65.4% |
| S&P Bear Warning Indicator by gary_trades | atr highest | v4 strategy | No trades on this market | ||
| Choppiness Index by Zeal Terminal | atr highest | v5 indicator | indicator | ||
| Aggresive Scalper-Swing Crypto Strategy by exlux99 | highest atr | v4 strategy | −7.4% | 3 | 16.0% |
| Donchian Channels Strategy by KrisWaters by kriswaters | highest atr | v4 strategy | +55.8% | 21 | 23.3% |
| Chandelier Exit - Strategy by melihtuna | atr highest | v4 strategy | −34.8% | 11 | 43.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | Yes 1,000 bars in 28 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/// © 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")