Pine Script v4 · Strategy
Wilder's Volatility Trailing Stop Strategy with various MA's
A Pine Script v4 strategy built on RMA or EMA or SMA or WMA (src, len), Lowest (7) and Highest (7). 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 multiplier 3.0, ATR length 7 and ATR moving average type RMA.
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 Wilder's Volatility Trailing Stop Strategy with various MA's repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 4 of 4 trades.
- How did Wilder's Volatility Trailing Stop Strategy with various MA's do on BTCUSDT 1D with default settings?
- +16.91%
- From 26 Jan 2024 to 21 Oct 2026, over 15 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can Wilder's Volatility Trailing Stop Strategy with various MA's 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 Wilder's Volatility Trailing Stop Strategy with various MA's use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does Wilder's Volatility Trailing Stop Strategy with various MA's run in Zeal Terminal?
- Yes
- It compiles in 8 ms and runs 1,000 bars in 21 ms, in the browser.
BTCUSDT · 1D · default settings
- trailing stop
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +16.91%
- $16,910.10
- Buy and hold
- +102.5%
- same period
- Closed trades
- 15
- 5 won · 10 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 1.28
- gross profit ÷ gross loss
- Max drawdown
- 41.57%
- −$41,573.20
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 an argument v6 removed
when = on line 36, 38. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 36
long = on line 36, 38. v5 writes it as direction = strategy.long or strategy.short.
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 rma → ta.rma, ema → ta.ema, sma → ta.sma, wma → ta.wma, round → math.round, sqrt → math.sqrt, lowest → ta.lowest, highest → ta.highest, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 36, 38). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 15 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 1: //@version=4 → //@version=5
- Line 13: type is reserved in v5: renamed type_
- Line 16: rma → ta.rma
- Line 18: ema → ta.ema
- Line 20: sma → ta.sma
- Line 22: wma → ta.wma
11 more edits
- Line 22: wma → ta.wma
- Line 22: wma → ta.wma
- Line 22: round → math.round
- Line 22: sqrt → math.sqrt
- Line 24: tr → ta.tr
- Line 25: lowest → ta.lowest
- Line 26: highest → ta.highest
- Line 32: crossover → ta.crossover
- Line 34: crossunder → ta.crossunder
- Line 36: long = true → direction = strategy.long
- Line 38: long = false → direction = strategy.short
-//@version=4+//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯//function for choosing moving averages-f_ma(type, src, len) =>+f_ma(type_, src, len) => float result = 0- if type == "RMA" // Wilder's moving averaege or Running moving average+ if type_ == "RMA" // Wilder's moving averaege or Running moving average- result := rma(src, len)+ result := ta.rma(src, len)- if type == "EMA" // Exponential moving average+ if type_ == "EMA" // Exponential moving average- result := ema(src, len)+ result := ta.ema(src, len)- if type == "SMA" // Simple moving average+ if type_ == "SMA" // Simple moving average- result := sma(src, len)+ result := ta.sma(src, len)- if type == "HULL" // Hull moving average+ if type_ == "HULL" // Hull moving average- result := wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len)))+ result := ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))) result-ATR = f_ma(ATRavgType, tr, ATRlength)+ATR = f_ma(ATRavgType, ta.tr, ATRlength)-upperTrail = lowest(sicType=="close"?close:low, ATRlength) + AtrMult * ATR+upperTrail = ta.lowest(sicType=="close"?close:low, ATRlength) + AtrMult * ATR-lowerTrail = highest(sicType=="close"?close:high, ATRlength) - AtrMult * ATR+lowerTrail = ta.highest(sicType=="close"?close:high, ATRlength) - AtrMult * ATRfloat TS = 0⋯//Strategy-buy = crossover(close, TS)+buy = ta.crossover(close, TS)//sell = close < TS-short = crossunder(close, TS)+short = ta.crossunder(close, TS)//cover = close > TS-strategy.entry(id="enter long", long=true, when=buy)+strategy.entry(id="enter long", direction = strategy.long, when=buy)//strategy.close(id="enter long", comment="exit long", when=sell)-strategy.entry(id="enter short", long=false, when=short)+strategy.entry(id="enter short", direction = strategy.short, when=short)//strategy.close(id="enter short", comment="exit short", when=cover)Same 15 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 8: input() → input.float()
- Line 9: input() → input.int()
- Line 10: input() → input.string()
- Line 11: input() → input.string()
- Line 36: when = buy → if block
- Line 38: when = short → if block
⋯strategy(title="Wilder's Volatility Trailing Stop Strategy with various MA's", shorttitle="Trailing Stop Strategy", overlay=true)-AtrMult = input(3.0, title="ATR multiplier")+AtrMult = input.float(3.0, title="ATR multiplier")-ATRlength = input(7, title="ATR length")+ATRlength = input.int(7, title="ATR length")-ATRavgType = input("RMA", title="ATR moving average type", options=["RMA", "EMA", "SMA", "HULL"])+ATRavgType = input.string("RMA", title="ATR moving average type", options=["RMA", "EMA", "SMA", "HULL"])-sicType = input("close", title="significant close type for trail calculation", options=["close", "high-low"])+sicType = input.string("close", title="significant close type for trail calculation", options=["close", "high-low"])//function for choosing moving averages⋯//cover = close > TS-strategy.entry(id="enter long", direction = strategy.long, when=buy)+if buy+ strategy.entry(id="enter long", direction = strategy.long)//strategy.close(id="enter long", comment="exit long", when=sell)-strategy.entry(id="enter short", direction = strategy.short, when=short)+if short+ strategy.entry(id="enter short", direction = strategy.short)//strategy.close(id="enter short", comment="exit short", when=cover)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: 15 before, 18 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.
-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯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 multiplier | 3.0 | float | upperTrail lowerTrail |
| ATR length | 7 | int | ATR upperTrail lowerTrail |
| ATR moving average type | RMA | string · options ["RMA", "EMA", "SMA", "HULL"] | ATR |
| significant close type for trail calculation | close | string · options ["close", "high-low"] | upperTrail lowerTrail |
What it calculates
- RMA / EMA / SMA / WMA (
rma,ta.rmain v5)src, lenresult · line 16 - Lowest (
lowest,ta.lowestin v5)sicType=="close"?close:low, ATR length (7) · chosen by significant close type for trail calculationupperTrail · line 25 - Highest (
highest,ta.highestin v5)sicType=="close"?close:high, ATR length (7) · chosen by significant close type for trail calculationlowerTrail · line 26 - Crossover (
crossover,ta.crossoverin v5)close, TSbuy · line 32 - Crossunder (
crossunder,ta.crossunderin v5)close, TSshort · 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 enter long long | buy | On | 36 |
| Open enter short short | short | On | 38 |
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 | +16.9% | +102.5% | 15 | 33% | 1.28 | 41.6% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.3% | +5.5% | 15 | 27% | 0.99 | 9.1% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.9% | +9.8% | 22 | 36% | 1.22 | 2.3% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.2% | +8.4% | 19 | 32% | 1.22 | 0.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.3% | +16.7% | 22 | 27% | 0.34 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 14 | 21% | 1.23 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.1% | +148.3% | 13 | 23% | 0.88 | 0.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 15 | 33% | 1.16 | 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 | 2 | 0% | −$24,235.00 |
| Sideways market | 11 | 36% | $33,034.10 |
| Falling market | 2 | 50% | $8,110.94 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
43.7%
The 95th percentile of the worst drawdown when the same 15 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 21.2% to 43.7%; the tick is what actually happened, 36.3%. Order luck alone moves it this much.
All 15 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 2 May 2024 58,365.00 | 21 May 2024 71,446.60 | 19 | −$13,081.60 |
| long | 21 May 2024 71,446.60 | 25 Jun 2024 60,293.30 | 35 | −$11,153.30 |
| short | 25 Jun 2024 60,293.30 | 16 Jul 2024 64,724.10 | 21 | −$4,430.76 |
| long | 16 Jul 2024 64,724.10 | 4 Aug 2024 60,698.00 | 19 | −$4,026.07 |
| short | 4 Aug 2024 60,698.00 | 25 Sep 2024 64,262.70 | 52 | −$3,564.71 |
| long | 25 Sep 2024 64,262.70 | 26 Feb 2025 88,680.40 | 154 | $24,417.70 |
| short | 26 Feb 2025 88,680.40 | 23 Apr 2025 93,443.00 | 56 | −$4,762.60 |
| long | 23 Apr 2025 93,443.00 | 20 Aug 2025 112,873.00 | 119 | $19,430.00 |
| 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 | 6 Jan 2026 93,859.70 | 87 | $18,914.80 |
| long | 6 Jan 2026 93,859.70 | 21 Jan 2026 88,427.70 | 15 | −$5,432.05 |
| short | 21 Jan 2026 88,427.70 | 17 Mar 2026 74,884.70 | 55 | $13,543.00 |
| long | 17 Mar 2026 74,884.70 | 2 Jun 2026 71,408.90 | 77 | −$3,475.77 |
| short | 2 Jun 2026 71,408.90 | 20 Aug 2026 69,334.80 | 79 | $2,074.12 |
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 |
|---|---|---|---|---|---|
| Wilder's Volatility Trailing Stop Strategy with various MA's this page | — | v4 strategy | +16.9% | 15 | 41.6% |
| MACD Willy Strategy by PtGambler | rma sma ema highest lowest wma | v5 strategy | No trades on this market | ||
| Bagheri IG Ether by Bagheri_IG | ema highest lowest rma sma | v4 strategy | +0.0% | 3 | 0.0% |
| Bagheri IG Ether v2 by Bagheri_IG | ema highest lowest rma sma | v4 strategy | −0.0% | 2 | 0.0% |
| Swing Multi Moving Averages Crypto and Stocks Strategy by exlux99 | ema wma highest lowest sma | v5 strategy | +60.2% | 22 | 13.1% |
| VCP pivot buy by perrycc007 | sma ema rma wma lowest highest | v4 strategy | does not run in Zeal Terminal | ||
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 8 ms | With a workaround 1,000 bars in 21 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 36, 38) so the engine trades the way the script says.
“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.
//@version=4// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// Wilder's Volatility Trailing Stop Strategy with various MA's// by SparkyFlary//For Educational Purposes//Results can differ on different markets and can fail at any time. Profit is not guaranteed.strategy(title="Wilder's Volatility Trailing Stop Strategy with various MA's", shorttitle="Trailing Stop Strategy", overlay=true)AtrMult = input(3.0, title="ATR multiplier")ATRlength = input(7, title="ATR length")ATRavgType = input("RMA", title="ATR moving average type", options=["RMA", "EMA", "SMA", "HULL"])sicType = input("close", title="significant close type for trail calculation", options=["close", "high-low"])//function for choosing moving averagesf_ma(type, src, len) => float result = 0 if type == "RMA" // Wilder's moving averaege or Running moving average result := rma(src, len) if type == "EMA" // Exponential moving average result := ema(src, len) if type == "SMA" // Simple moving average result := sma(src, len) if type == "HULL" // Hull moving average result := wma(2 * wma(src, len / 2) - wma(src, len), round(sqrt(len))) resultATR = f_ma(ATRavgType, tr, ATRlength)upperTrail = lowest(sicType=="close"?close:low, ATRlength) + AtrMult * ATRlowerTrail = highest(sicType=="close"?close:high, ATRlength) - AtrMult * ATRfloat TS = 0TS := close < TS[1] ? upperTrail[1] : close > TS[1] ? lowerTrail[1] : TS//plotplot(TS<=0?TS[1]:TS, title="trailing stop", color=close<TS?color.red:color.green)//Strategybuy = crossover(close, TS)//sell = close < TSshort = crossunder(close, TS)//cover = close > TSstrategy.entry(id="enter long", long=true, when=buy)//strategy.close(id="enter long", comment="exit long", when=sell)strategy.entry(id="enter short", long=false, when=short)//strategy.close(id="enter short", comment="exit short", when=cover)