Pine Script v4 · Strategy
EMA Cross Strategy
A Pine Script v4 strategy built on EMA (close, 10), Crossover (fastEMA, slowEMA) and Crossunder (fastEMA, slowEMA). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade is one contract and pays no commission. 5 settings; defaults include Fast EMA 10, Slow EMA 20 and Trade Direction Both.
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 EMA Cross Strategy repaint?
- Yes
- At least one pattern in the source can repaint: intrabar recalculation.
- How did EMA Cross Strategy do on BTCUSDT 1D with default settings?
- +35.33%
- From 26 Jan 2024 to 21 Oct 2026, over 42 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Can EMA Cross Strategy 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 EMA Cross Strategy use a stop loss?
- Yes
- strategy.exit sets a stop on line 46, 48.
- Does EMA Cross Strategy run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 29 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +35.33%
- $35,330.80
- Buy and hold
- +102.5%
- same period
- Closed trades
- 42
- 12 won · 30 lost
- Profitable
- 28.6%
- of closed trades
- Profit factor
- 1.33
- gross profit ÷ gross loss
- Max drawdown
- 41.32%
- −$48,181.80
One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A short position was still open at the last bar. 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.
-
Note
Uses an argument v6 removed
when = on line 40, 42. The upgraded version below moves each call inside an if block.
-
Note
Uses long =, which v5 renamed line 40
long = on line 40, 42. v5 writes it as direction = strategy.long or strategy.short.
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, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 40, 42). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
VerifiedSame 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 4: //@version=4 → //@version=5
- Line 13: input(type = input.integer) → input.int()
- Line 14: input(type = input.integer) → input.int()
- Line 18: input(type = input.time) → input.time()
- Line 19: input(type = input.time) → input.time()
- Line 22: ema → ta.ema
5 more edits
- Line 23: ema → ta.ema
- Line 35: crossover → ta.crossover
- Line 36: crossunder → ta.crossunder
- Line 40: long = true → direction = strategy.long
- Line 42: long = false → direction = strategy.short
⋯// © kirilov-//@version=4+//@version=5strategy(⋯// Options to enter fast and slow Exponential Moving Average (EMA) values-emaFast = input(title="Fast EMA", type=input.integer, defval=10, minval=1, maxval=9999)+emaFast = input.int(title="Fast EMA", defval=10, minval=1, maxval=9999)-emaSlow = input(title="Slow EMA", type=input.integer, defval=20, minval=1, maxval=9999)+emaSlow = input.int(title="Slow EMA", defval=20, minval=1, maxval=9999)// Option to select trade directions⋯// Options that configure the backtest date range-startDate = input(title="Start Date", type=input.time, defval=timestamp("01 Jan 1970 00:00"))+startDate = input.time(title="Start Date", defval=timestamp("01 Jan 1970 00:00"))-endDate = input(title="End Date", type=input.time, defval=timestamp("31 Dec 2170 23:59"))+endDate = input.time(title="End Date", defval=timestamp("31 Dec 2170 23:59"))// CALCULATIONS:// Use the built-in function to calculate two EMA lines-fastEMA = ema(close, emaFast)+fastEMA = ta.ema(close, emaFast)-slowEMA = ema(close, emaSlow)+slowEMA = ta.ema(close, emaSlow)// PLOT:⋯// Decide if we should go long or short using the built-in functions-longCondition = crossover(fastEMA, slowEMA)+longCondition = ta.crossover(fastEMA, slowEMA)-shortCondition = crossunder(fastEMA, slowEMA)+shortCondition = ta.crossunder(fastEMA, slowEMA)// ORDERS:⋯if (longCondition and inDateRange)- strategy.entry(id="long", long=true, when = longOK)+ strategy.entry(id="long", direction = strategy.long, when = longOK)if (shortCondition and inDateRange)- strategy.entry(id="short", long=false, when = shortOK)+ strategy.entry(id="short", direction = strategy.short, when = shortOK) ⋯Same 42 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 16: input() → input.string()
- Line 40: when = longOK → if block
- Line 42: when = shortOK → if block
⋯// Option to select trade directions-tradeDirection = input(title="Trade Direction", options=["Long", "Short", "Both"], defval="Both")+tradeDirection = input.string(title="Trade Direction", options=["Long", "Short", "Both"], defval="Both")// Options that configure the backtest date range⋯if (longCondition and inDateRange)- strategy.entry(id="long", direction = strategy.long, when = longOK)+ if longOK+ strategy.entry(id="long", direction = strategy.long)if (shortCondition and inDateRange)- strategy.entry(id="short", direction = strategy.short, when = shortOK)+ if shortOK+ strategy.entry(id="short", direction = strategy.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: 42 before, 36 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.
⋯// © kirilov-//@version=5+//@version=6strategy(⋯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 |
|---|---|---|---|
| Fast EMA | 10 | int (written input.integer) · min 1 · max 9999 | fastEMA |
| Slow EMA | 20 | int (written input.integer) · min 1 · max 9999 | slowEMA |
| Trade Direction | Both | string · options ["Long", "Short", "Both"] | longOK shortOK |
| Start Date | timestamp("01 Jan 1970 00:00") | time | inDateRange |
| End Date | timestamp("31 Dec 2170 23:59") | time | inDateRange |
What it calculates
- EMA (
ema,ta.emain v5)close, Fast EMA (10)fastEMA · line 22 - EMA (
ema,ta.emain v5)close, Slow EMA (20)slowEMA · line 23 - Crossover (
crossover,ta.crossoverin v5)fastEMA, slowEMAlongCondition · line 35 - Crossunder (
crossunder,ta.crossunderin v5)fastEMA, slowEMAshortCondition · line 36
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 | longOK(tradeDirection == "Long") or (tradeDirection == "Both") | On | 40 |
| Open short short | shortOK(tradeDirection == "Short") or (tradeDirection == "Both") | On | 42 |
| Exit exit long stop | (strategy.position_size > 0 and shortCondition) | On | 46 |
| Exit exit short stop | (strategy.position_size < 0 and longCondition) | On | 48 |
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 | +35.3% | +102.5% | 42 | 29% | 1.33 | 41.3% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −7.6% | +5.5% | 51 | 27% | 0.84 | 14.9% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +1.5% | +9.8% | 36 | 31% | 1.24 | 2.8% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.6% | +8.4% | 57 | 21% | 0.74 | 1.1% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 36 | 42% | 1.15 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 37 | 27% | 1.98 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.4% | +148.3% | 40 | 28% | 1.47 | 0.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 49 | 31% | 1.05 | 0.1% | 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 | 16 | 25% | −$15,633.60 |
| Sideways market | 17 | 29% | $26,103.70 |
| Falling market | 9 | 33% | $24,860.70 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
46.7%
The 95th percentile of the worst drawdown when the same 42 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 23.2% to 46.7%; the tick is what actually happened, 30.0%. Order luck alone moves it this much.
All 42 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 14 Apr 2024 63,924.50 | 17 May 2024 65,235.20 | 33 | −$1,310.69 |
| long | 17 May 2024 65,235.20 | 15 Jun 2024 66,044.00 | 29 | $808.78 |
| short | 15 Jun 2024 66,044.00 | 17 Jul 2024 65,044.00 | 32 | $999.99 |
| long | 17 Jul 2024 65,044.00 | 4 Aug 2024 60,698.00 | 18 | −$4,346.01 |
| short | 4 Aug 2024 60,698.00 | 24 Aug 2024 64,037.20 | 20 | −$3,339.25 |
| long | 24 Aug 2024 64,037.20 | 30 Aug 2024 59,359.00 | 6 | −$4,678.24 |
| short | 30 Aug 2024 59,359.00 | 18 Sep 2024 60,314.00 | 19 | −$954.99 |
| long | 18 Sep 2024 60,314.00 | 10 Oct 2024 60,636.00 | 22 | $322.02 |
| short | 10 Oct 2024 60,636.00 | 13 Oct 2024 63,206.20 | 3 | −$2,570.22 |
| long | 13 Oct 2024 63,206.20 | 24 Dec 2024 94,881.50 | 72 | $31,675.20 |
| short | 24 Dec 2024 94,881.50 | 25 Dec 2024 98,663.60 | 1 | −$3,782.11 |
| long | 25 Dec 2024 98,663.60 | 27 Dec 2024 95,791.60 | 2 | −$2,871.98 |
| short | 27 Dec 2024 95,791.60 | 7 Jan 2025 102,236.00 | 11 | −$6,444.00 |
| long | 7 Jan 2025 102,236.00 | 10 Jan 2025 92,552.50 | 3 | −$9,683.11 |
| short | 10 Jan 2025 92,552.50 | 17 Jan 2025 99,987.30 | 7 | −$7,434.81 |
| long | 17 Jan 2025 99,987.30 | 5 Feb 2025 97,763.10 | 19 | −$2,224.16 |
| short | 5 Feb 2025 97,763.10 | 28 Mar 2025 87,232.00 | 51 | $10,531.10 |
| long | 28 Mar 2025 87,232.00 | 29 Mar 2025 84,424.40 | 1 | −$2,807.63 |
| short | 29 Mar 2025 84,424.40 | 18 Apr 2025 84,947.90 | 20 | −$523.54 |
| long | 18 Apr 2025 84,947.90 | 7 Jun 2025 104,288.00 | 50 | $19,340.50 |
| short | 7 Jun 2025 104,288.00 | 8 Jun 2025 105,552.00 | 1 | −$1,263.72 |
| long | 8 Jun 2025 105,552.00 | 19 Jun 2025 104,887.00 | 11 | −$665.36 |
| short | 19 Jun 2025 104,887.00 | 27 Jun 2025 106,947.00 | 8 | −$2,060.27 |
| long | 27 Jun 2025 106,947.00 | 3 Aug 2025 112,546.00 | 37 | $5,599.29 |
| short | 3 Aug 2025 112,546.00 | 10 Aug 2025 116,462.00 | 7 | −$3,915.90 |
| long | 10 Aug 2025 116,462.00 | 20 Aug 2025 112,873.00 | 10 | −$3,589.30 |
| short | 20 Aug 2025 112,873.00 | 13 Sep 2025 116,029.00 | 24 | −$3,156.46 |
| long | 13 Sep 2025 116,029.00 | 26 Sep 2025 108,994.00 | 13 | −$7,034.92 |
| short | 26 Sep 2025 108,994.00 | 2 Oct 2025 118,595.00 | 6 | −$9,600.50 |
| long | 2 Oct 2025 118,595.00 | 14 Oct 2025 115,166.00 | 12 | −$3,428.99 |
| short | 14 Oct 2025 115,166.00 | 4 Jan 2026 90,628.00 | 82 | $24,538.00 |
| long | 4 Jan 2026 90,628.00 | 23 Jan 2026 89,559.70 | 19 | −$1,068.33 |
| short | 23 Jan 2026 89,559.70 | 13 Mar 2026 70,541.30 | 49 | $19,018.30 |
| long | 13 Mar 2026 70,541.30 | 28 Mar 2026 66,407.30 | 15 | −$4,134.06 |
| short | 28 Mar 2026 66,407.30 | 9 Apr 2026 71,069.90 | 12 | −$4,662.65 |
| long | 9 Apr 2026 71,069.90 | 20 May 2026 76,834.40 | 41 | $5,764.43 |
| short | 20 May 2026 76,834.40 | 11 Jul 2026 64,161.70 | 52 | $12,672.60 |
| long | 11 Jul 2026 64,161.70 | 2 Aug 2026 62,823.70 | 22 | −$1,338.07 |
| short | 2 Aug 2026 62,823.70 | 8 Aug 2026 64,923.20 | 6 | −$2,099.55 |
| long | 8 Aug 2026 64,923.20 | 13 Aug 2026 63,480.00 | 5 | −$1,443.21 |
| short | 13 Aug 2026 63,480.00 | 20 Aug 2026 69,334.80 | 7 | −$5,854.79 |
| long | 20 Aug 2026 69,334.80 | 15 Oct 2026 81,682.00 | 56 | $12,347.20 |
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 |
|---|---|---|---|---|---|
| EMA Cross Strategy this page | — | v4 strategy | +35.3% | 42 | 41.3% |
| EMA Slope AyE by fajardoarelys | ema | v4 strategy | No trades on this market | ||
| ROoT by realisticDove62527 | ema | v5 strategy | −4.7% | 225 | 9.4% |
| sachin5986 by mohitesachin78 | ema | v4 strategy | +32.1% | 35 | 17.6% |
| sonu1997 by Sonu1997 | ema | v4 strategy | +960.7% | 2 | 1,103.8% |
| Moving Average Crossover by Muh4mm4d | ema | v4 strategy | +19,990.1% | 30 | 1,274.2% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | With a workaround 1,000 bars in 29 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as long = true, long = false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 40, 42) so the engine trades the way the script says.
“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/// This strategy has been created for illustration purposes only and should not be relied upon as a basis for buying, selling, or holding any asset or security.// © kirilov//@version=4strategy( "EMA Cross Strategy", overlay=true, calc_on_every_tick=true, currency=currency.USD )// INPUT:// Options to enter fast and slow Exponential Moving Average (EMA) valuesemaFast = input(title="Fast EMA", type=input.integer, defval=10, minval=1, maxval=9999)emaSlow = input(title="Slow EMA", type=input.integer, defval=20, minval=1, maxval=9999)// Option to select trade directionstradeDirection = input(title="Trade Direction", options=["Long", "Short", "Both"], defval="Both")// Options that configure the backtest date rangestartDate = input(title="Start Date", type=input.time, defval=timestamp("01 Jan 1970 00:00"))endDate = input(title="End Date", type=input.time, defval=timestamp("31 Dec 2170 23:59"))// CALCULATIONS:// Use the built-in function to calculate two EMA linesfastEMA = ema(close, emaFast)slowEMA = ema(close, emaSlow)// PLOT:// Draw the EMA lines on the chartplot(series=fastEMA, color=color.orange, linewidth=2)plot(series=slowEMA, color=color.blue, linewidth=2)// CONDITIONS:// Check if the close time of the current bar falls inside the date rangeinDateRange = (time >= startDate) and (time < endDate)// Translate input into trading conditionslongOK = (tradeDirection == "Long") or (tradeDirection == "Both")shortOK = (tradeDirection == "Short") or (tradeDirection == "Both")// Decide if we should go long or short using the built-in functionslongCondition = crossover(fastEMA, slowEMA)shortCondition = crossunder(fastEMA, slowEMA)// ORDERS:// Submit entry (or reverse) ordersif (longCondition and inDateRange) strategy.entry(id="long", long=true, when = longOK)if (shortCondition and inDateRange) strategy.entry(id="short", long=false, when = shortOK) // Submit exit orders in the cases where we trade only long or only shortif (strategy.position_size > 0 and shortCondition) strategy.exit(id="exit long", stop=close)if (strategy.position_size < 0 and longCondition) strategy.exit(id="exit short", stop=close)