Pine Script v5 · Strategy
Swing Multi Moving Averages Crypto and Stocks Strategy
A Pine Script v5 strategy built on EMA (close, 17), WMA (src, len) and Change (close, 17). It has 6 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 100% of equity and pays 0.03% commission. 9 settings; defaults include Source close, Moving Averages 17 and MA Type ALMA.
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 Swing Multi Moving Averages Crypto and Stocks Strategy repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Swing Multi Moving Averages Crypto and Stocks Strategy do on BTCUSDT 1D with default settings?
- +60.17%
- From 26 Jan 2024 to 21 Oct 2026, over 22 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Swing Multi Moving Averages Crypto and Stocks Strategy 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: 22 before, 22 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does Swing Multi Moving Averages Crypto and Stocks Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 146, 150.
- Does Swing Multi Moving Averages Crypto and Stocks Strategy run in Zeal Terminal?
- Yes
- It compiles in 58 ms and runs 1,000 bars in 32 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +60.17%
- $60.17
- Buy and hold
- +102.5%
- same period
- Closed trades
- 22
- 14 won · 8 lost
- Profitable
- 63.6%
- of closed trades
- Profit factor
- 2.77
- gross profit ÷ gross loss
- Max drawdown
- 13.15%
- −$18.07
One run with the author's default settings, $100 starting capital, commission as the script sets it. 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.
-
Likely bug
The “SMMA” option calculates RMA line 131
Choosing SMMA in “MA Type” runs ta.rma, not ta.smma. Anyone picking SMMA gets a different average from the one they asked for.
out_ma_source == 'SMMA' ? ta.rma(out_ma_source == 'SMMA' ? ta.smma( -
Likely bug
The “LSMA” option calculates LINREG line 131
Choosing LSMA in “MA Type” runs ta.linreg, not ta.lsma. Anyone picking LSMA gets a different average from the one they asked for.
out_ma_source == 'LSMA' ? ta.linreg(out_ma_source == 'LSMA' ? ta.lsma( -
Likely bug
The “HULL” option calculates HMA line 131
Choosing HULL in “MA Type” runs ta.hma, not ta.hull. Anyone picking HULL gets a different average from the one they asked for.
out_ma_source == 'HULL' ? ta.hma(out_ma_source == 'HULL' ? ta.hull( -
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
2 settings have no label
In the settings dialog longEntry (line 5), shortEntry (line 6) show an empty name. The labels on this page come from the variable names.
-
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 145, 147, 149, 151. The upgraded version below moves each call inside an if block.
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 |
|---|---|---|---|
| Type of Entries | |||
| Long Entry (no label in the script) | true | bool | entry long exit LONG EXIT close long line 144 |
| Short Entry (no label in the script) | false | bool | entry short exit SHORT EXIT close short line 148 |
| Parameters | |||
| Source | close | source | dema hullma change volatility kama e zlemadata_1 Price mom vidya MA1 Det0 ema1 TRIMA out_ma |
| Moving Averages | 17 | int · min 1 | dema hullma change volatility e tema zlemalag_1 zlema N upSum downSum beta ema1 ema2 ema3 ema4 ema5 ema6 out_ma alpha |
| MA Type | ALMA | string · options ['SMA', 'EMA', 'WMA', 'ALMA', 'SMMA', 'LSMA', 'VWMA', 'DEMA', 'HULL', 'KAMA', 'FRAMA', 'VIDYA', 'JMA', 'TEMA', 'ZLEMA', 'T3', 'TRIM'] | out_ma |
| Fixed Risk Management | |||
| LONG Stop Loss % | 10.0 | float | longStop |
| LONG Take Profit % | 30.0 | float | longTake |
| SHORT Stop Loss % | 5.0 | float | shortStop |
| SHORT Take Profit % | 10.0 | float | shortTake |
What it calculates
- EMASource (close), Moving Averages (17)
dema · line 9 - WMA2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))
hullma · line 13 - ChangeSource (close), Moving Averages (17)
change · line 17 - ChangeSource (close)
volatility · line 18 - EMASource (close), Moving Averages (17)
e · line 28 - EMAe, Moving Averages (17)
tema · line 29 - EMAzlemadata_1, Moving Averages (17)
zlema · line 35 - Highest / LowestN
N3 · line 52 - HighestN / 2 - 1
HH · line 53 - LowestN / 2 - 1
LL · line 54 - ChangeSource (close)
mom · line 83 - EMASource (close), Moving Averages (17)
ema1 · line 110 - EMAema1, Moving Averages (17)
ema2 · line 111 - EMAema2, Moving Averages (17)
ema3 · line 112 - EMAema3, Moving Averages (17)
ema4 · line 113 - EMAema4, Moving Averages (17)
ema5 · line 114 - EMAema5, Moving Averages (17)
ema6 · line 115 - SMAta.sma(src, Nm), Nm
TRIMA · line 126 - SMA / EMA / WMA / ALMA / RMA / Linreg / VWMA / HMASource (close), Moving Averages (17) · chosen by MA Type
out_ma · line 131
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.
The script has 63 named inputs, calculations, conditions and outputs, too many to draw legibly here; the tables above list them.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long long | longclose> out_ma and close[1] < out_ma and dayofweek==dayofweek.monday | On | 145 |
| Exit LONG EXIT stop and limit | (longEntry) | On | 146 |
| Close long | dayofweek==dayofweek.sunday | On | 147 |
| Open short short | shortclose< out_ma and close[1] > out_ma and dayofweek==dayofweek.monday | On | 149 |
| Exit SHORT EXIT stop and limit | (shortEntry) | On | 150 |
| Close short | dayofweek==dayofweek.sunday | On | 151 |
Each order is guarded by the setting shown. “Off” orders never fire unless that setting is switched on.
Does it repaint?
Yes
At least one pattern in the source can repaint; it is marked Check below.
- ClearHigher-timeframe dataThe script requests no other symbol or timeframe.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationcalc_on_every_tick = true. Live values can differ from history.
- 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 | +60.2% | +102.5% | 22 | 64% | 2.77 | 13.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +21.7% | +5.5% | 12 | 67% | 2.54 | 13.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −6.5% | +9.8% | 26 | 42% | 0.91 | 44.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +15.1% | +8.4% | 16 | 56% | 1.53 | 24.5% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +142.2% | +16.7% | 16 | 69% | 3.33 | 26.2% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +14.2% | +21.5% | 14 | 50% | 1.33 | 23.2% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +5.5% | +148.3% | 19 | 63% | 1.23 | 18.6% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +38.3% | +17.1% | 17 | 59% | 2.97 | 8.5% | 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 | 6 | 83% | $27.12 |
| Sideways market | 8 | 63% | $16.48 |
| Falling market | 8 | 50% | $16.57 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
15.8%
The 95th percentile of the worst drawdown when the same 22 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 7.0% to 15.8%; the tick is what actually happened, 11.9%. Order luck alone moves it this much.
All 22 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 27 Feb 2024 54,476.50 | 4 Mar 2024 63,114.00 | 6 | $15.79 |
| long | 14 May 2024 62,940.10 | 20 May 2024 66,274.00 | 6 | $6.06 |
| long | 28 May 2024 69,436.40 | 3 Jun 2024 67,765.60 | 6 | −$3.00 |
| long | 4 Jun 2024 68,809.90 | 10 Jun 2024 69,648.10 | 6 | $1.38 |
| long | 13 Aug 2024 59,346.60 | 19 Aug 2024 58,427.30 | 6 | −$1.93 |
| long | 20 Aug 2024 59,438.50 | 26 Aug 2024 64,220.00 | 6 | $9.44 |
| long | 10 Sep 2024 57,042.00 | 16 Sep 2024 59,132.00 | 6 | $4.60 |
| long | 11 Feb 2025 97,430.80 | 17 Feb 2025 96,118.10 | 6 | −$1.86 |
| long | 18 Mar 2025 84,010.00 | 24 Mar 2025 86,082.50 | 6 | $3.14 |
| long | 17 Jun 2025 106,795.00 | 23 Jun 2025 100,964.00 | 6 | −$7.37 |
| long | 24 Jun 2025 105,334.00 | 30 Jun 2025 108,357.00 | 6 | $3.54 |
| long | 21 Oct 2025 110,532.00 | 27 Oct 2025 114,559.00 | 6 | $4.65 |
| long | 25 Nov 2025 88,300.00 | 1 Dec 2025 90,360.00 | 6 | $3.05 |
| long | 9 Dec 2025 90,634.40 | 15 Dec 2025 88,172.20 | 6 | −$3.81 |
| long | 13 Jan 2026 91,296.20 | 19 Jan 2026 93,673.10 | 6 | $3.40 |
| long | 3 Mar 2026 68,830.10 | 9 Mar 2026 65,971.20 | 6 | −$5.77 |
| long | 24 Mar 2026 70,906.40 | 30 Mar 2026 66,010.90 | 6 | −$9.14 |
| long | 14 Apr 2026 74,418.00 | 20 Apr 2026 73,801.80 | 6 | −$1.08 |
| long | 21 Apr 2026 75,841.00 | 27 Apr 2026 78,657.60 | 6 | $4.42 |
| long | 18 Aug 2026 64,532.10 | 24 Aug 2026 77,734.00 | 6 | $25.58 |
| long | 1 Sep 2026 78,581.30 | 7 Sep 2026 80,341.80 | 6 | $3.29 |
| long | 15 Sep 2026 78,189.20 | 21 Sep 2026 81,178.00 | 6 | $5.80 |
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 |
|---|---|---|---|---|---|
| Swing Multi Moving Averages Crypto and Stocks Strategy this page | — | v5 strategy | +60.2% | 22 | 13.1% |
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma highest lowest sma rma alma linreg vwma hma | v5 strategy | +2.8% | 5 | 2.4% |
| Multi Trend Cross Strategy Template by TradeAutomation | alma ema hma linreg rma sma vwma wma highest lowest | v5 strategy | +33.0% | 3 | 7.3% |
| Multi MA Trend Following Strategy Template by TradeAutomation | alma ema hma linreg rma sma vwma wma highest lowest | v5 strategy | +12.3% | 3 | 26.9% |
| High-Low Channel Multi averages Crypto Swing strategy by exlux99 | sma ema wma alma rma linreg vwma | v4 strategy | No trades on this market | ||
| VCP pivot buy by perrycc007 | sma ema rma vwma wma lowest highest | v4 strategy | does not run in Zeal Terminal | ||
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 4 strategy calls use the when = argument (line 145, 147, 149, 151). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
- No v6-only features. v6 is not needed to run this script.
Same 22 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 145: when = long → if block
- Line 147: when = dayofweek==dayofweek.sunday → if block
- Line 149: when = short → if block
- Line 151: when = dayofweek==dayofweek.sunday → if block
⋯if(longEntry)- strategy.entry("long",strategy.long,when=long )+ if long+ strategy.entry("long",strategy.long ) strategy.exit('LONG EXIT', "long", limit=longTake, stop=longStop)- strategy.close("long",when=dayofweek==dayofweek.sunday)+ if dayofweek==dayofweek.sunday+ strategy.close("long")if(shortEntry)- strategy.entry("short",strategy.short,when=short )+ if short+ strategy.entry("short",strategy.short ) strategy.exit('SHORT EXIT', "short", limit=shortTake, stop=shortStop)- strategy.close("short",when=dayofweek==dayofweek.sunday)+ if dayofweek==dayofweek.sunday+ strategy.close("short")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: 22 before, 22 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.
⋯// © exlux99-//@version=5+//@version=6strategy('Time MA strategy ', overlay=true, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, calc_on_every_tick=true, commission_type=strategy.commission.percent, commission_value=0.03)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 58 ms | Yes 1,000 bars in 32 ms | Not measured |
| PineTS | Not tested | Not tested | 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.
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/// © exlux99//@version=5strategy('Time MA strategy ', overlay=true, initial_capital=100, default_qty_type=strategy.percent_of_equity, default_qty_value=100, calc_on_every_tick=true, commission_type=strategy.commission.percent, commission_value=0.03)longEntry = input.bool(true, group="Type of Entries")shortEntry = input.bool(false, group="Type of Entries")//==========DEMAgetDEMA(src, len) => dema = 2 * ta.ema(src, len) - ta.ema(ta.ema(src, len), len) dema//==========HMAgetHULLMA(src, len) => hullma = ta.wma(2 * ta.wma(src, len / 2) - ta.wma(src, len), math.round(math.sqrt(len))) hullma//==========KAMAgetKAMA(src, len, k1, k2) => change = math.abs(ta.change(src, len)) volatility = math.sum(math.abs(ta.change(src)), len) efficiency_ratio = volatility != 0 ? change / volatility : 0 kama = 0.0 fast = 2 / (k1 + 1) slow = 2 / (k2 + 1) smooth_const = math.pow(efficiency_ratio * (fast - slow) + slow, 2) kama := nz(kama[1]) + smooth_const * (src - nz(kama[1])) kama//==========TEMAgetTEMA(src, len) => e = ta.ema(src, len) tema = 3 * (e - ta.ema(e, len)) + ta.ema(ta.ema(e, len), len) tema//==========ZLEMAgetZLEMA(src, len) => zlemalag_1 = (len - 1) / 2 zlemadata_1 = src + src - src[zlemalag_1] zlema = ta.ema(zlemadata_1, len) zlema//==========FRAMAgetFRAMA(src, len) => Price = src N = len if N % 2 != 0 N := N + 1 N N1 = 0.0 N2 = 0.0 N3 = 0.0 HH = 0.0 LL = 0.0 Dimen = 0.0 alpha = 0.0 Filt = 0.0 N3 := (ta.highest(N) - ta.lowest(N)) / N HH := ta.highest(N / 2 - 1) LL := ta.lowest(N / 2 - 1) N1 := (HH - LL) / (N / 2) HH := high[N / 2] LL := low[N / 2] for i = N / 2 to N - 1 by 1 if high[i] > HH HH := high[i] HH if low[i] < LL LL := low[i] LL N2 := (HH - LL) / (N / 2) if N1 > 0 and N2 > 0 and N3 > 0 Dimen := (math.log(N1 + N2) - math.log(N3)) / math.log(2) Dimen alpha := math.exp(-4.6 * (Dimen - 1)) if alpha < .01 alpha := .01 alpha if alpha > 1 alpha := 1 alpha Filt := alpha * Price + (1 - alpha) * nz(Filt[1], 1) if bar_index < N + 1 Filt := Price Filt Filt//==========VIDYAgetVIDYA(src, len) => mom = ta.change(src) upSum = math.sum(math.max(mom, 0), len) downSum = math.sum(-math.min(mom, 0), len) out = (upSum - downSum) / (upSum + downSum) cmo = math.abs(out) alpha = 2 / (len + 1) vidya = 0.0 vidya := src * alpha * cmo + nz(vidya[1]) * (1 - alpha * cmo) vidya//==========JMAgetJMA(src, len, power, phase) => phase_ratio = phase < -100 ? 0.5 : phase > 100 ? 2.5 : phase / 100 + 1.5 beta = 0.45 * (len - 1) / (0.45 * (len - 1) + 2) alpha = math.pow(beta, power) MA1 = 0.0 Det0 = 0.0 MA2 = 0.0 Det1 = 0.0 JMA = 0.0 MA1 := (1 - alpha) * src + alpha * nz(MA1[1]) Det0 := (src - MA1) * (1 - beta) + beta * nz(Det0[1]) MA2 := MA1 + phase_ratio * Det0 Det1 := (MA2 - nz(JMA[1])) * math.pow(1 - alpha, 2) + math.pow(alpha, 2) * nz(Det1[1]) JMA := nz(JMA[1]) + Det1 JMA//==========T3getT3(src, len, vFactor) => ema1 = ta.ema(src, len) ema2 = ta.ema(ema1, len) ema3 = ta.ema(ema2, len) ema4 = ta.ema(ema3, len) ema5 = ta.ema(ema4, len) ema6 = ta.ema(ema5, len) c1 = -1 * math.pow(vFactor, 3) c2 = 3 * math.pow(vFactor, 2) + 3 * math.pow(vFactor, 3) c3 = -6 * math.pow(vFactor, 2) - 3 * vFactor - 3 * math.pow(vFactor, 3) c4 = 1 + 3 * vFactor + math.pow(vFactor, 3) + 3 * math.pow(vFactor, 2) T3 = c1 * ema6 + c2 * ema5 + c3 * ema4 + c4 * ema3 T3//==========TRIMAgetTRIMA(src, len) => N = len + 1 Nm = math.round(N / 2) TRIMA = ta.sma(ta.sma(src, Nm), Nm) TRIMAsrc = input.source(close, title='Source', group='Parameters')len = input.int(17, minval=1, title='Moving Averages', group='Parameters')out_ma_source = input.string(title='MA Type', defval='ALMA', options=['SMA', 'EMA', 'WMA', 'ALMA', 'SMMA', 'LSMA', 'VWMA', 'DEMA', 'HULL', 'KAMA', 'FRAMA', 'VIDYA', 'JMA', 'TEMA', 'ZLEMA', 'T3', 'TRIM'], group='Parameters')out_ma = out_ma_source == 'SMA' ? ta.sma(src, len) : out_ma_source == 'EMA' ? ta.ema(src, len) : out_ma_source == 'WMA' ? ta.wma(src, len) : out_ma_source == 'ALMA' ? ta.alma(src, len, 0.85, 6) : out_ma_source == 'SMMA' ? ta.rma(src, len) : out_ma_source == 'LSMA' ? ta.linreg(src, len, 0) : out_ma_source == 'VWMA' ? ta.vwma(src, len) : out_ma_source == 'DEMA' ? getDEMA(src, len) : out_ma_source == 'HULL' ? ta.hma(src, len) : out_ma_source == 'KAMA' ? getKAMA(src, len, 2, 30) : out_ma_source == 'FRAMA' ? getFRAMA(src, len) : out_ma_source == 'VIDYA' ? getVIDYA(src, len) : out_ma_source == 'JMA' ? getJMA(src, len, 2, 50) : out_ma_source == 'TEMA' ? getTEMA(src, len) : out_ma_source == 'ZLEMA' ? getZLEMA(src, len) : out_ma_source == 'T3' ? getT3(src, len, 0.7) : out_ma_source == 'TRIM' ? getTRIMA(src, len) : naplot(out_ma)long = close> out_ma and close[1] < out_ma and dayofweek==dayofweek.mondayshort = close< out_ma and close[1] > out_ma and dayofweek==dayofweek.mondaystopPer = input.float(10.0, title='LONG Stop Loss % ', group='Fixed Risk Management') / 100takePer = input.float(30.0, title='LONG Take Profit %', group='Fixed Risk Management') / 100stopPerShort = input.float(5.0, title='SHORT Stop Loss % ', group='Fixed Risk Management') / 100takePerShort = input.float(10.0, title='SHORT Take Profit %', group='Fixed Risk Management') / 100longStop = strategy.position_avg_price * (1 - stopPer)longTake = strategy.position_avg_price * (1 + takePer)shortStop = strategy.position_avg_price * (1 + stopPerShort)shortTake = strategy.position_avg_price * (1 - takePerShort)strategy.risk.max_intraday_filled_orders(2) // After 10 orders are filled, no more strategy orders will be placed (except for a market order to exit current open market position, if there is any).if(longEntry) strategy.entry("long",strategy.long,when=long ) strategy.exit('LONG EXIT', "long", limit=longTake, stop=longStop) strategy.close("long",when=dayofweek==dayofweek.sunday)if(shortEntry) strategy.entry("short",strategy.short,when=short ) strategy.exit('SHORT EXIT', "short", limit=shortTake, stop=shortStop) strategy.close("short",when=dayofweek==dayofweek.sunday)