Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

Combined EMA & MA crossovers [CDI]

A Pine Script v4 strategy built on EMA (src, 8) and SMA (src, 50). It has 4 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 is one contract and pays no commission. 9 settings; defaults include Length 8, Length 21 and Length 50.

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 Combined EMA & MA crossovers [CDI] repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 7 of 7 trades.
How did Combined EMA & MA crossovers [CDI] do on BTCUSDT 1D with default settings?
+12.36%
From 26 Jan 2024 to 21 Oct 2026, over 27 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can Combined EMA & MA crossovers [CDI] 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 Combined EMA & MA crossovers [CDI] use a stop loss?
Yes
strategy.exit sets a stop on line 40, 42.
Does Combined EMA & MA crossovers [CDI] run in Zeal Terminal?
Yes
It compiles in 16 ms and runs 1,000 bars in 57 ms, in the browser.

BTCUSDT · 1D · default settings

  • 1st EMA
  • 2nd EMA
  • 1st MA
  • 2nd 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 (9)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$112,367$202,527Equity from $100,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
+12.36%
$12,364.10
Buy and hold
+102.5%
same period
Closed trades
27
15 won · 12 lost
Profitable
55.6%
of closed trades
Profit factor
1.15
gross profit ÷ gross loss
Max drawdown
31.06%
−$35,877.40

One run with the author's default settings, $100,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

    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.

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 ema → ta.ema, sma → ta.sma, crossover → ta.crossover, crossunder → ta.crossunder.

v4 → v5

Verified

Same 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 3: //@version=4 → //@version=5
  • Line 7: input(type = input.integer) → input.int()
  • Line 9: ema → ta.ema
  • Line 13: ema → ta.ema
  • Line 17: sma → ta.sma
  • Line 21: sma → ta.sma
17 more edits
  • Line 23: input(type = input.float) → input.float()
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 33: crossover → ta.crossover
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
  • Line 36: crossunder → ta.crossunder
⋯// © JMLSlop-//@version=4+//@version=5src = close⋯// first fast EMA-len = input(8, "Length", type=input.integer, minval=1)+len = input.int(8, "Length", minval=1)doma1 = input(true, title="EMA")-out1 = ema(src, len) +out1 = ta.ema(src, len) //Second fast EMA⋯doma2 = input(true, title="EMA")-out2 = ema(src, len2)+out2 = ta.ema(src, len2)//First slow MA⋯doma3 = input(true, title="SMA")-out3 = sma(src, len3)+out3 = ta.sma(src, len3)//Second slow MA⋯doma4 = input(true, title="SMA")-out4 = sma(src, len4)+out4 = ta.sma(src, len4)// Profit-profit = input(8, "Profit/lost %", type=input.float, minval=1) * 0.01+profit = input.float(8, "Profit/lost %", minval=1) * 0.01plot(doma1 and out1 ? out1: na, color=color.blue, linewidth=1, title="1st EMA")⋯shortStopPrice = strategy.position_avg_price * (1 + profit)-longCondition2 = (out2>out3 and (crossover(out1, out4) or crossover(out1[1], out4[1]) or crossover(out1[2], out4[2]) or (crossover(out1[3], out4[3]))) or (out2>out3 and (crossover(out1, out3) or crossover(out1[1], out3[1]) or crossover(out1[2], out3[2]) or crossover(out1[3], out3[3]))))+longCondition2 = (out2>out3 and (ta.crossover(out1, out4) or ta.crossover(out1[1], out4[1]) or ta.crossover(out1[2], out4[2]) or (ta.crossover(out1[3], out4[3]))) or (out2>out3 and (ta.crossover(out1, out3) or ta.crossover(out1[1], out3[1]) or ta.crossover(out1[2], out3[2]) or ta.crossover(out1[3], out3[3]))))if (longCondition2)    strategy.entry("Enter L", strategy.long)-shortCondition2 = (out2<out3 and (crossunder(out1, out4) or crossunder(out1[1], out4[1]) or crossunder(out1[2], out4[2]) or crossunder(out1[3], out4[3]))) or (out2<out3 and (crossunder(out1, out3) or crossunder(out1[1], out3[1]) or crossunder(out1[2], out3[2]) or crossunder(out1[3], out3[3])))+shortCondition2 = (out2<out3 and (ta.crossunder(out1, out4) or ta.crossunder(out1[1], out4[1]) or ta.crossunder(out1[2], out4[2]) or ta.crossunder(out1[3], out4[3]))) or (out2<out3 and (ta.crossunder(out1, out3) or ta.crossunder(out1[1], out3[1]) or ta.crossunder(out1[2], out3[2]) or ta.crossunder(out1[3], out3[3])))if (shortCondition2)⋯

Clean v5

VerifiedDownload the v5 script

Same 27 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 8: input() → input.bool()
  • Line 11: input() → input.int()
  • Line 12: input() → input.bool()
  • Line 15: input() → input.int()
  • Line 16: input() → input.bool()
  • Line 19: input() → input.int()
  • Line 20: input() → input.bool()
⋯len = input.int(8, "Length", minval=1)-doma1 = input(true, title="EMA")+doma1 = input.bool(true, title="EMA")out1 = ta.ema(src, len) //Second fast EMA-len2 = input(21, minval=1, title="Length")+len2 = input.int(21, minval=1, title="Length")-doma2 = input(true, title="EMA")+doma2 = input.bool(true, title="EMA")out2 = ta.ema(src, len2)//First slow MA-len3 = input(50, minval=1, title="Length")+len3 = input.int(50, minval=1, title="Length")-doma3 = input(true, title="SMA")+doma3 = input.bool(true, title="SMA")out3 = ta.sma(src, len3)//Second slow MA-len4 = input(200, minval=1, title="Length")+len4 = input.int(200, minval=1, title="Length")-doma4 = input(true, title="SMA")+doma4 = input.bool(true, title="SMA")out4 = ta.sma(src, len4)⋯

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: 27 before, 45 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.

⋯// © JMLSlop-//@version=5+//@version=6src = close⋯

Settings

Every input the script declares, with its default and what it feeds. Bounds are the ones the script enforces.

SettingDefaultType and rangeFeeds
Length8int (written input.integer) · min 1out1
EMAtruebool1st EMA
Length21int · min 1out2
EMAtruebool2nd EMA
Length50int · min 1out3
SMAtruebool1st MA
Length200int · min 1out4
SMAtruebool2nd MA
Profit/lost %8float · min 1takeProfitPrice longStopPrice shortStopPrice

What it calculates

  • EMA (ema, ta.ema in v5)src, Length (8)out1 · line 9
  • EMA (ema, ta.ema in v5)src, Length (21)out2 · line 13
  • SMA (sma, ta.sma in v5)src, Length (50)out3 · line 17
  • SMA (sma, ta.sma in v5)src, Length (200)out4 · line 21

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.

lenLengthsetting · 8len2Lengthsetting · 21len3Lengthsetting · 50len4Lengthsetting · 200closecloseprice dataopenopenprice dataprofitProfit/lost %setting · 8out1EMA→ out1out2EMA→ out2out3SMA→ out3out4SMA→ out4srcsrcvaluetakeProfitPricetakeProfitPricevaluelongStopPrice, shortStopPricelongStopPrice · shortSt…2 valueslongCondition2longCondition2conditionshortCondition2shortCondition2conditionentry:Enter LOpen Enter Lstrategy.entryentry:Enter SOpen Enter Sstrategy.entryexit:Exit Lexit Exit Lstrategy.exitexit:Exit Sexit Exit Sstrategy.exitplot:241st EMAplotplot:252nd EMAplotplot:261st MAplotplot:272nd MAplot

Order rules

OrderFires whenBy defaultLine
Open Enter L long(longCondition2)On35
Open Enter S short(shortCondition2)On38
Exit Exit L stop and limit(strategy.position_size > 0)On40
Exit Exit S stop and limit(strategy.position_size < 0)On42

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.

7/7trades 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. Orders are decided once per closed bar, so nothing can change mid-bar.
  • 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+12.4%+102.5%2756%1.1531.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h+1.4%+5.5%1638%1.088.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.5%+9.8%2556%1.201.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.4%+8.4%1932%0.671.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−0.0%+16.7%3152%0.970.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%2241%1.320.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D−0.2%+148.3%3244%0.790.4%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−0.1%+17.1%2326%0.760.1%8 May 2026 – 21 Oct 2026

Backtests are simulations on historical data and are not investment advice.

Settings heatmap

Two settings that feed the entry signal, moved around their defaults. The best in-sample pair (Length 10, Length 29) made +43.9% before 26 Dec 2025 and +16.1% after it, against +15.9% for the defaults. 52% of the 64 combinations made money in-sample and 100% out-of-sample.

Before 26 Dec 2025 · in-sample
Length ↓ · Length →45678101114
11−2%−2%+4%−3%−3%+27%+22%+10%
13−27%−20%−12%−18%−18%+10%+6%+10%
15−30%−10%−3%−20%−12%+16%+12%+16%
18−15%−9%+2%−20%−20%+16%+12%+16%
21−8%−19%−8%−4%−4%+12%+8%+20%
25−16%−26%−20%−21%−21%+5%+0%−5%
29+7%+14%+20%+13%+16%+44%+39%+34%
36−0%−0%+11%+5%+13%+35%+30%+30%
From 26 Dec 2025 · out-of-sample
Length ↓ · Length →45678101114
11+9%+14%+14%+14%+14%+14%+14%+18%
13+5%+18%+18%+18%+18%+18%+18%+18%
15+11%+11%+11%+11%+11%+11%+17%+18%
18+16%+16%+16%+16%+16%+16%+23%+18%
21+16%+16%+16%+16%+16%+16%+23%+23%
25+16%+16%+16%+16%+16%+16%+16%+23%
29+16%+16%+16%+16%+16%+16%+16%+17%
36+10%+16%+16%+16%+16%+16%+16%+16%

Net P&L of trades opened in each period, as a share of starting capital. marks the defaults and the best in-sample cell. Hover a cell for its trade count. Backtests are simulations on historical data and are not investment advice.

The trades

By market direction

At entry, last 90 barsTradesWonP&L
Rising market729%−$21,508.10
Sideways market1675%$47,585.10
Falling market425%−$13,713.00

Rising and falling mean the market moved more than 15% over the 90 bars before entry.

How bad could the drawdown be?

37.4%

The 95th percentile of the worst drawdown when the same 27 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 17.2% to 37.4%; the tick is what actually happened, 27.9%. Order luck alone moves it this much.

0%43%
All 27 trades
SideEntryExitBarsP&L
short17 Apr 2024 63,793.401 May 2024 58,684.1014$5,109.28
long22 May 2024 70,148.3018 Jun 2024 64,536.5027−$5,611.87
short23 Jun 2024 64,262.0024 Jun 2024 59,121.001$5,140.97
short7 Aug 2024 56,022.008 Aug 2024 60,503.801−$4,481.76
short28 Aug 2024 59,415.006 Sep 2024 54,777.309$4,637.68
long20 Sep 2024 62,948.0016 Oct 2024 68,234.7026$5,286.72
long17 Oct 2024 67,620.0029 Oct 2024 73,061.7012$5,441.74
short2 Jan 2025 94,591.806 Jan 2025 102,159.004−$7,567.35
short9 Jan 2025 95,060.6017 Jan 2025 102,665.008−$7,604.85
long19 Jan 2025 104,556.002 Feb 2025 96,191.7014−$8,364.50
short14 Mar 2025 81,115.8024 Mar 2025 87,605.1010−$6,489.27
short30 Mar 2025 82,648.507 Apr 2025 75,965.508$6,683.04
long22 Apr 2025 87,516.2023 Apr 2025 94,517.501$7,001.30
long24 Apr 2025 93,691.108 May 2025 101,438.0014$7,746.73
short23 Jun 2025 100,964.001 Jul 2025 107,147.008−$6,182.64
long1 Jul 2025 107,147.0010 Jul 2025 115,860.009$8,713.84
short24 Aug 2025 115,438.002 Oct 2025 118,595.0039−$3,156.93
long2 Oct 2025 118,595.0010 Oct 2025 109,107.008−$9,487.60
short19 Oct 2025 107,185.0027 Oct 2025 115,760.008−$8,574.80
short4 Nov 2025 106,583.0013 Nov 2025 98,338.309$8,244.72
long6 Jan 2026 93,859.7025 Jan 2026 86,350.9019−$7,508.78
long19 Mar 2026 71,246.6029 Mar 2026 65,546.8010−$5,699.73
long8 Apr 2026 71,924.2017 Apr 2026 77,909.009$5,984.80
short29 May 2026 73,617.502 Jun 2026 67,723.604$5,893.96
long20 Jul 2026 64,722.6019 Aug 2026 69,885.1030$5,162.57
long20 Aug 2026 69,334.8021 Aug 2026 74,881.601$5,546.79
long22 Aug 2026 78,338.0021 Sep 2026 84,838.0030$6,500.00

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
Combined EMA & MA crossovers [CDI] this page—v4 strategy+12.4%2731.1%
MACD Long Strat by TheGrindTodayema smav4 strategy+24.0%7920.1%
Stock trending strategy by exlux99ema smav4 strategy+35.3%2114.7%
4-Hour Stochastic EMA Trend by slymnturkogluema smav4 strategyNo trades on this market
4X EMA and volume strategy by SoftKill21ema smav4 strategy+1.8%325.8%
Simple Momentum Strategy Based on SMA, EMA and Volume by slip_streamsma emav4 strategy+7.4%1827.4%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 16 msYes 1,000 bars in 57 msNot measured
PineTSNo 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/// © JMLSlop//@version=4src = closestrategy("Crossover moving averages", shorttitle="Cross MA-EMA", overlay=true, calc_on_order_fills=false)// first fast EMAlen = input(8, "Length", type=input.integer, minval=1)doma1 = input(true, title="EMA")out1 = ema(src, len) //Second fast EMAlen2 = input(21, minval=1, title="Length")doma2 = input(true, title="EMA")out2 = ema(src, len2)//First slow MAlen3 = input(50, minval=1, title="Length")doma3 = input(true, title="SMA")out3 = sma(src, len3)//Second slow MAlen4 = input(200, minval=1, title="Length")doma4 = input(true, title="SMA")out4 = sma(src, len4)// Profitprofit = input(8, "Profit/lost %", type=input.float, minval=1) * 0.01plot(doma1 and out1 ? out1: na, color=color.blue, linewidth=1, title="1st EMA")plot(doma2 and out2 ? out2: na, color=color.red, linewidth=1, title="2nd EMA")plot(doma3 and out3 ? out3: na, color=color.green, linewidth=2, title="1st MA")plot(doma4 and out4 ? out4: na, color=color.orange, linewidth=3, title="2nd MA")// Orders configtakeProfitPrice =     (strategy.position_size > 0) ? strategy.position_avg_price + open*profit : (strategy.position_size < 0) ? strategy.position_avg_price - (open*profit) : nalongStopPrice  = strategy.position_avg_price * (1 - profit)shortStopPrice = strategy.position_avg_price * (1 + profit)longCondition2 = (out2>out3 and (crossover(out1, out4) or crossover(out1[1], out4[1]) or crossover(out1[2], out4[2]) or (crossover(out1[3], out4[3]))) or (out2>out3 and (crossover(out1, out3) or crossover(out1[1], out3[1]) or crossover(out1[2], out3[2]) or crossover(out1[3], out3[3]))))if (longCondition2)    strategy.entry("Enter L", strategy.long)shortCondition2 = (out2<out3 and (crossunder(out1, out4) or crossunder(out1[1], out4[1]) or crossunder(out1[2], out4[2]) or crossunder(out1[3], out4[3]))) or (out2<out3 and (crossunder(out1, out3) or crossunder(out1[1], out3[1]) or crossunder(out1[2], out3[2]) or crossunder(out1[3], out3[3])))if (shortCondition2)    strategy.entry("Enter S", strategy.short)if (strategy.position_size > 0)    strategy.exit("Exit L", limit=takeProfitPrice, stop=longStopPrice)if (strategy.position_size < 0)    strategy.exit("Exit S", limit=takeProfitPrice, stop=shortStopPrice)