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
- 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
VerifiedSame 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)⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Length | 8 | int (written input.integer) · min 1 | out1 |
| EMA | true | bool | 1st EMA |
| Length | 21 | int · min 1 | out2 |
| EMA | true | bool | 2nd EMA |
| Length | 50 | int · min 1 | out3 |
| SMA | true | bool | 1st MA |
| Length | 200 | int · min 1 | out4 |
| SMA | true | bool | 2nd MA |
| Profit/lost % | 8 | float · min 1 | takeProfitPrice longStopPrice shortStopPrice |
What it calculates
- EMA (
ema,ta.emain v5)src, Length (8)out1 · line 9 - EMA (
ema,ta.emain v5)src, Length (21)out2 · line 13 - SMA (
sma,ta.smain v5)src, Length (50)out3 · line 17 - SMA (
sma,ta.smain 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Enter L long | (longCondition2) | On | 35 |
| Open Enter S short | (shortCondition2) | On | 38 |
| Exit Exit L stop and limit | (strategy.position_size > 0) | On | 40 |
| Exit Exit S stop and limit | (strategy.position_size < 0) | On | 42 |
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 | +12.4% | +102.5% | 27 | 56% | 1.15 | 31.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | +1.4% | +5.5% | 16 | 38% | 1.08 | 8.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.5% | +9.8% | 25 | 56% | 1.20 | 1.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.4% | +8.4% | 19 | 32% | 0.67 | 1.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −0.0% | +16.7% | 31 | 52% | 0.97 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 22 | 41% | 1.32 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | −0.2% | +148.3% | 32 | 44% | 0.79 | 0.4% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −0.1% | +17.1% | 23 | 26% | 0.76 | 0.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.
| Length ↓ · Length → | 4 | 5 | 6 | 7 | 8 | 10 | 11 | 14 |
|---|---|---|---|---|---|---|---|---|
| 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% |
| Length ↓ · Length → | 4 | 5 | 6 | 7 | 8 | 10 | 11 | 14 |
|---|---|---|---|---|---|---|---|---|
| 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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 7 | 29% | −$21,508.10 |
| Sideways market | 16 | 75% | $47,585.10 |
| Falling market | 4 | 25% | −$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.
All 27 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 17 Apr 2024 63,793.40 | 1 May 2024 58,684.10 | 14 | $5,109.28 |
| long | 22 May 2024 70,148.30 | 18 Jun 2024 64,536.50 | 27 | −$5,611.87 |
| short | 23 Jun 2024 64,262.00 | 24 Jun 2024 59,121.00 | 1 | $5,140.97 |
| short | 7 Aug 2024 56,022.00 | 8 Aug 2024 60,503.80 | 1 | −$4,481.76 |
| short | 28 Aug 2024 59,415.00 | 6 Sep 2024 54,777.30 | 9 | $4,637.68 |
| long | 20 Sep 2024 62,948.00 | 16 Oct 2024 68,234.70 | 26 | $5,286.72 |
| long | 17 Oct 2024 67,620.00 | 29 Oct 2024 73,061.70 | 12 | $5,441.74 |
| short | 2 Jan 2025 94,591.80 | 6 Jan 2025 102,159.00 | 4 | −$7,567.35 |
| short | 9 Jan 2025 95,060.60 | 17 Jan 2025 102,665.00 | 8 | −$7,604.85 |
| long | 19 Jan 2025 104,556.00 | 2 Feb 2025 96,191.70 | 14 | −$8,364.50 |
| short | 14 Mar 2025 81,115.80 | 24 Mar 2025 87,605.10 | 10 | −$6,489.27 |
| short | 30 Mar 2025 82,648.50 | 7 Apr 2025 75,965.50 | 8 | $6,683.04 |
| long | 22 Apr 2025 87,516.20 | 23 Apr 2025 94,517.50 | 1 | $7,001.30 |
| long | 24 Apr 2025 93,691.10 | 8 May 2025 101,438.00 | 14 | $7,746.73 |
| short | 23 Jun 2025 100,964.00 | 1 Jul 2025 107,147.00 | 8 | −$6,182.64 |
| long | 1 Jul 2025 107,147.00 | 10 Jul 2025 115,860.00 | 9 | $8,713.84 |
| short | 24 Aug 2025 115,438.00 | 2 Oct 2025 118,595.00 | 39 | −$3,156.93 |
| long | 2 Oct 2025 118,595.00 | 10 Oct 2025 109,107.00 | 8 | −$9,487.60 |
| short | 19 Oct 2025 107,185.00 | 27 Oct 2025 115,760.00 | 8 | −$8,574.80 |
| short | 4 Nov 2025 106,583.00 | 13 Nov 2025 98,338.30 | 9 | $8,244.72 |
| long | 6 Jan 2026 93,859.70 | 25 Jan 2026 86,350.90 | 19 | −$7,508.78 |
| long | 19 Mar 2026 71,246.60 | 29 Mar 2026 65,546.80 | 10 | −$5,699.73 |
| long | 8 Apr 2026 71,924.20 | 17 Apr 2026 77,909.00 | 9 | $5,984.80 |
| short | 29 May 2026 73,617.50 | 2 Jun 2026 67,723.60 | 4 | $5,893.96 |
| long | 20 Jul 2026 64,722.60 | 19 Aug 2026 69,885.10 | 30 | $5,162.57 |
| long | 20 Aug 2026 69,334.80 | 21 Aug 2026 74,881.60 | 1 | $5,546.79 |
| long | 22 Aug 2026 78,338.00 | 21 Sep 2026 84,838.00 | 30 | $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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| Combined EMA & MA crossovers [CDI] this page | — | v4 strategy | +12.4% | 27 | 31.1% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 16 ms | Yes 1,000 bars in 57 ms | Not measured |
| PineTS | No 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)