Pine Script v5 · Strategy
Two Moving Average Cross test
A Pine Script v5 strategy built on SMA or EMA or WMA or VWMA or HMA or SWMA or ALMA or Linreg or RMA (MA_S1, 20) and Crossover (MA_1, MA_2). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits use a stop loss. Each trade commits 100% of equity and pays 0.08 commission. 14 settings; defaults include Fast Exponential, Src close and Len 20.
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 Two Moving Average Cross test repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did Two Moving Average Cross test do on BTCUSDT 1D with default settings?
- −0.47%
- From 26 Jan 2024 to 21 Oct 2026, over 9 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does Two Moving Average Cross test run in Pine Script v6?
- Yes
- Same 9 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. The v6 script can be downloaded below.
- Does Two Moving Average Cross test use a stop loss?
- Yes
- strategy.exit sets a stop on line 97.
- Does Two Moving Average Cross test run in Zeal Terminal?
- Yes
- It compiles in 46 ms and runs 1,000 bars in 107 ms, in the browser.
BTCUSDT · 1D · default settings
- Fast MA
- Slow MA
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- −0.47%
- −$4.71
- Buy and hold
- +102.5%
- same period
- Closed trades
- 9
- 3 won · 6 lost
- Profitable
- 33.3%
- of closed trades
- Profit factor
- 0.99
- gross profit ÷ gross loss
- Max drawdown
- 22.01%
- −$220.14
One run with the author's default settings, $1,000 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.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
1 setting has no label
In the settings dialog Timeframe (line 8) shows 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.
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 |
|---|---|---|---|
| Timeframe (no label in the script) | | timeframe | MA_S1 MA_S2 |
| Allow Repainting | false | bool | MA_S1 MA_S2 |
| Show Crossings | false | bool | plotshape |
| Moving Averages | |||
| Fast | Exponential | string | MA_1 |
| Src | close | source | MA_S1 |
| Len | 20 | int | MA_1 Wilder_MA1 KA_EF1 |
| Slow | Exponential | string | MA_2 |
| Src | close | source | MA_S2 |
| Len | 50 | int | MA_2 Wilder_MA2 KA_EF2 |
| RSI | |||
| RSI Length | 9 | int | Longcond2 Exitcond2 Exitcond3 |
| RSI Source | close | source | Exitcond2 Exitcond3 |
| Min RSI | 39 | int | Longcond2 Exitcond2 |
| Max RSI | 80 | int | Exitcond3 |
| Strategy | |||
| Stop % | 10 | float | Stop_Multi |
What it calculates
- SMA / EMA / WMA / VWMA / HMA / SWMA / ALMA / Linreg / RMAMA_S1, Len (20)
MA_1 · line 27 - SMA / EMA / WMA / VWMA / HMA / SWMA / ALMA / Linreg / RMAMA_S2, Len (50)
MA_2 · line 52 - CrossoverMA_1, MA_2
Bull_MA_Cross · line 81 - CrossoverMA_2, MA_1
Bear_MA_Cross · line 82 - CrossoverMA_1, MA_2
Longcond1 · line 86 - CrossoverMA_2, MA_1
Exitcond1 · line 88
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 Long long | (TimePeriod and Longcond1 and Longcond2 and NoPos) | On | 95 |
| Exit Exit stop | (TimePeriod and Longcond1 and Longcond2 and NoPos) | On | 97 |
| Close Long | (Exitcond1 and Exitcond2 and InPos) or (Exitcond3 and InPos) | On | 99 |
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 data2 request.security call(s), none with lookahead on.
- ClearFuture-bar referencesNo negative history offsets such as close[-1].
- CheckIntrabar recalculationreads realtime-only state. 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 | −0.5% | +102.5% | 9 | 33% | 0.99 | 22.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +5.5% | +5.5% | 6 | 67% | 2.05 | 6.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −3.4% | +9.8% | 6 | 33% | 0.91 | 25.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.6% | +8.4% | 5 | 60% | 1.04 | 10.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +10.9% | +16.7% | 10 | 50% | 1.18 | 32.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +23.0% | +21.5% | 6 | 83% | 5.51 | 5.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −27.1% | +148.3% | 6 | 33% | 0.32 | 33.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −9.3% | +17.1% | 10 | 30% | 0.33 | 11.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 | 1 | 0% | −$59.60 |
| Sideways market | 7 | 43% | $109.74 |
| Falling market | 1 | 0% | −$54.85 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
28.9%
The 95th percentile of the worst drawdown when the same 9 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 14.9% to 28.9%; the tick is what actually happened, 16.1%. Order luck alone moves it this much.
All 9 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 6 May 2024 64,012.00 | 25 Jun 2024 60,293.30 | 50 | −$59.60 |
| long | 24 Jul 2024 65,936.00 | 4 Aug 2024 58,897.00 | 11 | −$101.74 |
| long | 24 Sep 2024 63,340.00 | 11 Nov 2024 80,370.00 | 48 | $223.79 |
| long | 26 Apr 2025 94,638.70 | 9 May 2025 103,262.00 | 13 | $94.95 |
| long | 17 Sep 2025 116,789.00 | 27 Sep 2025 109,643.00 | 10 | −$72.55 |
| long | 3 Oct 2025 120,529.00 | 7 Oct 2025 124,659.00 | 4 | $35.37 |
| long | 19 Jan 2026 93,673.10 | 22 Jan 2026 89,454.70 | 3 | −$52.16 |
| long | 17 Apr 2026 75,154.30 | 2 Jun 2026 71,408.90 | 46 | −$54.85 |
| long | 22 Aug 2026 78,338.00 | 23 Aug 2026 77,074.90 | 1 | −$17.93 |
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 |
|---|---|---|---|---|---|
| Two Moving Average Cross test this page | — | v5 strategy | −0.5% | 9 | 22.0% |
| High-Low Channel Multi averages Crypto Swing strategy by exlux99 | sma ema wma alma rma linreg vwma | v4 strategy | No trades on this market | ||
| Ultra Moving Average Rating Trend Strategy by exlux99 | ema wma 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 | v5 strategy | +33.0% | 3 | 7.3% |
| Multi MA Trend Following Strategy Template by TradeAutomation | alma ema hma linreg rma sma vwma wma | v5 strategy | +12.3% | 3 | 26.9% |
| Open Close Cross Strategy plus Explorer by roses27 | sma ema wma vwma linreg alma | 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.
- 1 setting uses the untyped input() (line 9). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 9 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 9: input() → input.bool()
⋯Timeframe = input.timeframe( '','TimeFrame', inline='top')-Repaint = input( false,'Allow Repainting', inline='top')+Repaint = input.bool( false,'Allow Repainting', inline='top')MA_T1 = input.string( "Exponential", "Fast", ["Simple","Exponential","Double Exponential","Triple Exponential","Quadruple Exponential","Weighted","Volume-weighted","Hull","Symmetrical","Arnaud Legoux","Welles Wilder","Triangular","Least Squares","Relative Strength","Ehlers Kaufman"], inline="MA", group="Moving Averages")⋯Does it run in Pine Script v6?
Yes
Same 9 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. Both versions were run on Zeal Terminal's engine, and the results are identical. It differs from the v5 version by 1 edit.
⋯// © darkbrewery-// @version=5+// @version=6// Huge Thank-you to SGJoe and DTKing for their help!⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 46 ms | Yes 1,000 bars in 107 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/// © darkbrewery// @version=5// Huge Thank-you to SGJoe and DTKing for their help!// Ehlers / Kaufman Code borrowed from Franklin Moormann (cheatcountry)strategy("Two Moving Average Cross", "2MAX", true, default_qty_value = 100, default_qty_type = strategy.percent_of_equity, initial_capital=1000, commission_value = 0.08)// ---- User Inputs ----Timeframe = input.timeframe( '','TimeFrame', inline='top')Repaint = input( false,'Allow Repainting', inline='top')MA_T1 = input.string( "Exponential", "Fast", ["Simple","Exponential","Double Exponential","Triple Exponential","Quadruple Exponential","Weighted","Volume-weighted","Hull","Symmetrical","Arnaud Legoux","Welles Wilder","Triangular","Least Squares","Relative Strength","Ehlers Kaufman"], inline="MA", group="Moving Averages")MA_S1_Input = input.source( close, "Src", inline="MA", group="Moving Averages")MA_L1 = input.int( 20, "Len", 1, 200, inline="MA", group="Moving Averages")MA_T2 = input.string( "Exponential", "Slow", ["Simple","Exponential","Double Exponential","Triple Exponential","Quadruple Exponential","Weighted","Volume-weighted","Hull","Symmetrical","Arnaud Legoux","Welles Wilder","Triangular","Least Squares","Relative Strength","Ehlers Kaufman"], inline="MA2", group="Moving Averages")MA_S2_Input = input.source( close, "Src", inline="MA2", group="Moving Averages")MA_L2 = input.int( 50,"Len", 1, 200, inline="MA2", group="Moving Averages")MA_S1 = request.security(syminfo.tickerid, Timeframe, MA_S1_Input[Repaint ? 0 : barstate.isrealtime ? 1 : 0])[Repaint ? 0 : barstate.isrealtime ? 0 : 1]MA_S2 = request.security(syminfo.tickerid, Timeframe, MA_S2_Input[Repaint ? 0 : barstate.isrealtime ? 1 : 0])[Repaint ? 0 : barstate.isrealtime ? 0 : 1]// ---- RSI Inputs ----RSI_Length = input.int(9, "RSI Length", inline="RSI", group="RSI")RSI_Source = input.source(close, "RSI Source", inline="RSI", group="RSI")MinRSI = input.int(39,"Min RSI", inline="RSI", group="RSI")MaxRSI = input.int(80,"Max RSI", inline="RSI", group="RSI")// ---- Strategy Inputs ----Stop_Perc = input.float(10, "Stop %", inline="Strat", group="Strategy")Stop_Multi = (Stop_Perc - 100) * -0.01// ---- Calculate Moving Averages ----MA_1 = switch MA_T1 "Simple" => ta.sma(MA_S1,MA_L1) "Exponential" => ta.ema(MA_S1,MA_L1) "Double Exponential" => 2 * ta.ema(MA_S1, MA_L1) - ta.ema(ta.ema(MA_S1, MA_L1), MA_L1) "Triple Exponential" => 3 * (ta.ema(MA_S1, MA_L1) - ta.ema(ta.ema(MA_S1, MA_L1), MA_L1)) + ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1) "Quadruple Exponential" => 5 * ta.ema(MA_S1,MA_L1) - 10 * ta.ema(ta.ema(MA_S1, MA_L1), MA_L1) + 10 * ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1) - 5 * ta.ema(ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1), MA_L1) + ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(MA_S1, MA_L1), MA_L1), MA_L1), MA_L1), MA_L1) "Weighted" => ta.wma(MA_S1,MA_L1) "Volume-weighted" => ta.vwma(MA_S1,MA_L1) "Hull" => ta.hma(MA_S1,MA_L1) "Symmetrical" => ta.swma(MA_S1) "Arnaud Legoux" => ta.alma(MA_S1, MA_L1, 0.85, 6) "Least Squares" => ta.linreg(MA_S1, MA_L1, 0) "Relative Strength" => ta.rma(MA_S1,MA_L1) "Welles Wilder" => Wilder_MA1 = .0 Wilder_MA1 := 1 / MA_L1 * MA_S1 + (1 - 1 / MA_L1) * nz(Wilder_MA1[1]) "Triangular" => ta.sma(ta.sma(MA_S1,MA_L1),MA_L1) "Ehlers Kaufman" => KA_D1 = .0 for int i = 0 to MA_L1 - 1 by 1 KA_D1 += math.abs(nz(MA_S1[i]) - nz(MA_S1[i + 1])) KA_EF1 = KA_D1 != 0 ? math.min(math.abs(MA_S1 - nz(MA_S1[MA_L1 - 1])) / KA_D1, 1) : 0 KAMA1 = .0 KAMA1 := (math.pow((0.6667 * KA_EF1) + 0.0645, 2) * MA_S1) + ((1 - math.pow((0.6667 * KA_EF1) + 0.0645, 2)) * nz(KAMA1[1])) MA_2 = switch MA_T2 "Simple" => ta.sma(MA_S2,MA_L2) "Exponential" => ta.ema(MA_S2,MA_L2) "Double Exponential" => 2 * ta.ema(MA_S2, MA_L2) - ta.ema(ta.ema(MA_S2, MA_L2), MA_L2) "Triple Exponential" => 3 * (ta.ema(MA_S2, MA_L2) - ta.ema(ta.ema(MA_S2, MA_L2), MA_L2)) + ta.ema(ta.ema(ta.ema(MA_S2, MA_L2), MA_L2), MA_L2) "Quadruple Exponential" => 5 * ta.ema(MA_S2,MA_L2) - 10 * ta.ema(ta.ema(MA_S2, MA_L2), MA_L2) + 10 * ta.ema(ta.ema(ta.ema(MA_S2, MA_L2), MA_L2), MA_L2) - 5 * ta.ema(ta.ema(ta.ema(ta.ema(MA_S2, MA_L2), MA_L2), MA_L2), MA_L2) + ta.ema(ta.ema(ta.ema(ta.ema(ta.ema(MA_S2, MA_L2), MA_L2), MA_L2), MA_L2), MA_L2) "Weighted" => ta.wma(MA_S2,MA_L2) "Volume-weighted" => ta.vwma(MA_S2,MA_L2) "Hull" => ta.hma(MA_S2,MA_L2) "Symmetrical" => ta.swma(MA_S2) "Arnaud Legoux" => ta.alma(MA_S2, MA_L2, 0.85, 6) "Least Squares" => ta.linreg(MA_S2, MA_L2, 0) "Relative Strength" => ta.rma(MA_S2,MA_L2) "Welles Wilder" => Wilder_MA2 = .0 Wilder_MA2 := 1 / MA_L2 * MA_S2 + (1 - 1 / MA_L2) * nz(Wilder_MA2[1]) "Triangular" => ta.sma(ta.sma(MA_S2,MA_L2),MA_L2) "Ehlers Kaufman" => KA_D2 = .0 for int i = 0 to MA_L2 - 1 by 1 KA_D2 += math.abs(nz(MA_S2[i]) - nz(MA_S2[i + 1])) KA_EF2 = KA_D2 != 0 ? math.min(math.abs(MA_S2 - nz(MA_S2[MA_L2 - 1])) / KA_D2, 1) : 0 KAMA2 = .0 KAMA2 := (math.pow((0.6667 * KA_EF2) + 0.0645, 2) * MA_S2) + ((1 - math.pow((0.6667 * KA_EF2) + 0.0645, 2)) * nz(KAMA2[1]))// ---- Plot Moving Averages ----plot(MA_1, title="Fast MA", color=color.yellow)plot(MA_2, title="Slow MA", color=color.blue)// ---- Moving Average Crossovers ----Show_MA_Cross = input.bool(false, "Show Crossings")Bull_MA_Cross = ta.crossover(MA_1,MA_2)Bear_MA_Cross = ta.crossover(MA_2,MA_1)plotshape(Bull_MA_Cross and Show_MA_Cross, "Bullish Cross", shape.triangleup, location.belowbar, color.green, size="small")plotshape(Bear_MA_Cross and Show_MA_Cross, "Bearish Cross", shape.triangledown, location.abovebar, color.red, size="small")// ---- Strategy ----Longcond1 = ta.crossover(MA_1, MA_2)Longcond2 = ta.rsi(close,RSI_Length) > MinRSIExitcond1 = ta.crossover(MA_2, MA_1)Exitcond2 = ta.rsi(RSI_Source,RSI_Length) < MinRSIExitcond3 = ta.rsi(RSI_Source,RSI_Length) > MaxRSIInPos = strategy.position_size > 0NoPos = strategy.position_size <=0TimePeriod = time >= timestamp(syminfo.timezone, 2020, 1, 1, 0, 0)if (TimePeriod and Longcond1 and Longcond2 and NoPos) strategy.entry("Long", strategy.long) stoploss = low * Stop_Multi strategy.exit("Exit", "Long", stop=stoploss)if (Exitcond1 and Exitcond2 and InPos) or (Exitcond3 and InPos) strategy.close("Long")