Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (14)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$995$2,025Equity from $1,000
Binance BTCUSDT 1D candles, 26 Jan 2024 to 21 Oct 2026, 1,000 bars, default settings. Triangles mark entries; circles mark exits. Lines are what the script plots.
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.

SettingDefaultType and rangeFeeds
Timeframe (no label in the script)timeframeMA_S1 MA_S2
Allow RepaintingfalseboolMA_S1 MA_S2
Show Crossingsfalseboolplotshape
Moving Averages
FastExponentialstringMA_1
SrcclosesourceMA_S1
Len20intMA_1 Wilder_MA1 KA_EF1
SlowExponentialstringMA_2
SrcclosesourceMA_S2
Len50intMA_2 Wilder_MA2 KA_EF2
RSI
RSI Length9intLongcond2 Exitcond2 Exitcond3
RSI SourceclosesourceExitcond2 Exitcond3
Min RSI39intLongcond2 Exitcond2
Max RSI80intExitcond3
Strategy
Stop %10floatStop_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_2Bull_MA_Cross · line 81
  • CrossoverMA_2, MA_1Bear_MA_Cross · line 82
  • CrossoverMA_1, MA_2Longcond1 · line 86
  • CrossoverMA_2, MA_1Exitcond1 · 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.

MA_L1Lensetting · 20MA_T1Fastsetting · ExponentialMA_L2Lensetting · 50MA_T2Slowsetting · Exponentialclosecloseprice dataTimeframeTimeframesetting · Stop_PercStop %setting · 10lowlowprice dataRSI_LengthRSI Lengthsetting · 9MinRSIMin RSIsetting · 39MaxRSIMax RSIsetting · 80timetimeprice dataMA_1SMA / EMA / WMA / VWMA …→ MA_1MA_2SMA / EMA / WMA / VWMA …→ MA_2Bull_MA_CrossCrossover→ Bull_MA_CrossBear_MA_CrossCrossover→ Bear_MA_CrossLongcond1Crossover→ Longcond1Exitcond1Crossover→ Exitcond1MA_S1, MA_S2MA_S1 · MA_S22 valuesStop_MultiStop_MultivalueWilder_MA1Wilder_MA1valueKA_D1KA_D1valueKA_EF1KA_EF1valueKAMA1KAMA1valueWilder_MA2Wilder_MA2valueKA_D2KA_D2valueKA_EF2KA_EF2valueKAMA2KAMA2valuestoplossstoplossvalueLongcond2Longcond2conditionExitcond2Exitcond2conditionExitcond3Exitcond3conditionInPosInPosconditionNoPosNoPosconditionTimePeriodTimePeriodconditionentry:LongOpen Longstrategy.entryexit:Exitexit Exitstrategy.exitclose:LongClose Longstrategy.closeplot:77Fast MAplotplot:78Slow MAplotplotshape:83plotshapeplotshapeplotshape:84plotshapeplotshape

Order rules

OrderFires whenBy defaultLine
Open Long long(TimePeriod and Longcond1 and Longcond2 and NoPos)On95
Exit Exit stop(TimePeriod and Longcond1 and Longcond2 and NoPos)On97
Close Long(Exitcond1 and Exitcond2 and InPos) or (Exitcond3 and InPos)On99

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.

3/3trades identical when replayed liveThe last 300 bars, from 26 Dec 2025, were fed to the engine as live ticks, four per bar, and compared with the historical run. On 0 of them the position changed while the bar was forming before it settled. Anyone watching live sees those changes; once the bar closes it matches history.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D−0.5%+102.5%933%0.9922.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+5.5%+5.5%667%2.056.0%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−3.4%+9.8%633%0.9125.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.6%+8.4%560%1.0410.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+10.9%+16.7%1050%1.1832.9%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+23.0%+21.5%683%5.515.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−27.1%+148.3%633%0.3233.3%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−9.3%+17.1%1030%0.3311.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 barsTradesWonP&L
Rising market10%−$59.60
Sideways market743%$109.74
Falling market10%−$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.

0%33%
All 9 trades
SideEntryExitBarsP&L
long6 May 2024 64,012.0025 Jun 2024 60,293.3050−$59.60
long24 Jul 2024 65,936.004 Aug 2024 58,897.0011−$101.74
long24 Sep 2024 63,340.0011 Nov 2024 80,370.0048$223.79
long26 Apr 2025 94,638.709 May 2025 103,262.0013$94.95
long17 Sep 2025 116,789.0027 Sep 2025 109,643.0010−$72.55
long3 Oct 2025 120,529.007 Oct 2025 124,659.004$35.37
long19 Jan 2026 93,673.1022 Jan 2026 89,454.703−$52.16
long17 Apr 2026 75,154.302 Jun 2026 71,408.9046−$54.85
long22 Aug 2026 78,338.0023 Aug 2026 77,074.901−$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.

ScriptSharesTypeNetTradesMax drawdown
Two Moving Average Cross test this page—v5 strategy−0.5%922.0%
High-Low Channel Multi averages Crypto Swing strategy by exlux99sma ema wma alma rma linreg vwmav4 strategyNo trades on this market
Ultra Moving Average Rating Trend Strategy by exlux99ema wma sma rma alma linreg vwma hmav5 strategy+2.8%52.4%
Multi Trend Cross Strategy Template by TradeAutomationalma ema hma linreg rma sma vwma wmav5 strategy+33.0%37.3%
Multi MA Trend Following Strategy Template by TradeAutomationalma ema hma linreg rma sma vwma wmav5 strategy+12.3%326.9%
Open Close Cross Strategy plus Explorer by roses27sma ema wma vwma linreg almav4 strategydoes 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.

Clean v5

VerifiedDownload the v5 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.

Download the v6 script

⋯// © darkbrewery-// @version=5+// @version=6// Huge Thank-you to SGJoe and DTKing for their help!⋯

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 46 msYes 1,000 bars in 107 msNot measured
PineTSNot testedNot testedNot 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")