Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (5)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$132,306$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. 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.
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

Verified

Same 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)    ⋯

Clean v5

VerifiedDownload the v5 script

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.

SettingDefaultType and rangeFeeds
Fast EMA10int (written input.integer) · min 1 · max 9999fastEMA
Slow EMA20int (written input.integer) · min 1 · max 9999slowEMA
Trade DirectionBothstring · options ["Long", "Short", "Both"]longOK shortOK
Start Datetimestamp("01 Jan 1970 00:00")timeinDateRange
End Datetimestamp("31 Dec 2170 23:59")timeinDateRange

What it calculates

  • EMA (ema, ta.ema in v5)close, Fast EMA (10)fastEMA · line 22
  • EMA (ema, ta.ema in v5)close, Slow EMA (20)slowEMA · line 23
  • Crossover (crossover, ta.crossover in v5)fastEMA, slowEMAlongCondition · line 35
  • Crossunder (crossunder, ta.crossunder in 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.

closecloseprice dataemaFastFast EMAsetting · 10emaSlowSlow EMAsetting · 20timetimeprice datastartDateStart Datesetting · timestamp("01 Jan 1…endDateEnd Datesetting · timestamp("31 Dec 2…tradeDirectionTrade Directionsetting · BothfastEMAEMA→ fastEMAslowEMAEMA→ slowEMAlongConditionCrossover→ longConditionshortConditionCrossunder→ shortConditioninDateRangeinDateRangeconditionlongOKlongOKconditionshortOKshortOKconditionentry:longOpen longstrategy.entryentry:shortOpen shortstrategy.entryexit:exit longexit exit longstrategy.exitexit:exit shortexit exit shortstrategy.exitplot:26plotplotplot:27plotplot

Order rules

OrderFires whenBy defaultLine
Open long longlongOK(tradeDirection == "Long") or (tradeDirection == "Both")On40
Open short shortshortOK(tradeDirection == "Short") or (tradeDirection == "Both")On42
Exit exit long stop(strategy.position_size > 0 and shortCondition)On46
Exit exit short stop(strategy.position_size < 0 and longCondition)On48

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.

11/11trades 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 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.

MarketNetBuy and holdTradesWonProfit factorMax drawdownPeriod
BTCUSDT 1D+35.3%+102.5%4229%1.3341.3%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−7.6%+5.5%5127%0.8414.9%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+1.5%+9.8%3631%1.242.8%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.6%+8.4%5721%0.741.1%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%3642%1.150.1%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%3727%1.980.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.4%+148.3%4028%1.470.5%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%4931%1.050.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 barsTradesWonP&L
Rising market1625%−$15,633.60
Sideways market1729%$26,103.70
Falling market933%$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.

0%54%
All 42 trades
SideEntryExitBarsP&L
short14 Apr 2024 63,924.5017 May 2024 65,235.2033−$1,310.69
long17 May 2024 65,235.2015 Jun 2024 66,044.0029$808.78
short15 Jun 2024 66,044.0017 Jul 2024 65,044.0032$999.99
long17 Jul 2024 65,044.004 Aug 2024 60,698.0018−$4,346.01
short4 Aug 2024 60,698.0024 Aug 2024 64,037.2020−$3,339.25
long24 Aug 2024 64,037.2030 Aug 2024 59,359.006−$4,678.24
short30 Aug 2024 59,359.0018 Sep 2024 60,314.0019−$954.99
long18 Sep 2024 60,314.0010 Oct 2024 60,636.0022$322.02
short10 Oct 2024 60,636.0013 Oct 2024 63,206.203−$2,570.22
long13 Oct 2024 63,206.2024 Dec 2024 94,881.5072$31,675.20
short24 Dec 2024 94,881.5025 Dec 2024 98,663.601−$3,782.11
long25 Dec 2024 98,663.6027 Dec 2024 95,791.602−$2,871.98
short27 Dec 2024 95,791.607 Jan 2025 102,236.0011−$6,444.00
long7 Jan 2025 102,236.0010 Jan 2025 92,552.503−$9,683.11
short10 Jan 2025 92,552.5017 Jan 2025 99,987.307−$7,434.81
long17 Jan 2025 99,987.305 Feb 2025 97,763.1019−$2,224.16
short5 Feb 2025 97,763.1028 Mar 2025 87,232.0051$10,531.10
long28 Mar 2025 87,232.0029 Mar 2025 84,424.401−$2,807.63
short29 Mar 2025 84,424.4018 Apr 2025 84,947.9020−$523.54
long18 Apr 2025 84,947.907 Jun 2025 104,288.0050$19,340.50
short7 Jun 2025 104,288.008 Jun 2025 105,552.001−$1,263.72
long8 Jun 2025 105,552.0019 Jun 2025 104,887.0011−$665.36
short19 Jun 2025 104,887.0027 Jun 2025 106,947.008−$2,060.27
long27 Jun 2025 106,947.003 Aug 2025 112,546.0037$5,599.29
short3 Aug 2025 112,546.0010 Aug 2025 116,462.007−$3,915.90
long10 Aug 2025 116,462.0020 Aug 2025 112,873.0010−$3,589.30
short20 Aug 2025 112,873.0013 Sep 2025 116,029.0024−$3,156.46
long13 Sep 2025 116,029.0026 Sep 2025 108,994.0013−$7,034.92
short26 Sep 2025 108,994.002 Oct 2025 118,595.006−$9,600.50
long2 Oct 2025 118,595.0014 Oct 2025 115,166.0012−$3,428.99
short14 Oct 2025 115,166.004 Jan 2026 90,628.0082$24,538.00
long4 Jan 2026 90,628.0023 Jan 2026 89,559.7019−$1,068.33
short23 Jan 2026 89,559.7013 Mar 2026 70,541.3049$19,018.30
long13 Mar 2026 70,541.3028 Mar 2026 66,407.3015−$4,134.06
short28 Mar 2026 66,407.309 Apr 2026 71,069.9012−$4,662.65
long9 Apr 2026 71,069.9020 May 2026 76,834.4041$5,764.43
short20 May 2026 76,834.4011 Jul 2026 64,161.7052$12,672.60
long11 Jul 2026 64,161.702 Aug 2026 62,823.7022−$1,338.07
short2 Aug 2026 62,823.708 Aug 2026 64,923.206−$2,099.55
long8 Aug 2026 64,923.2013 Aug 2026 63,480.005−$1,443.21
short13 Aug 2026 63,480.0020 Aug 2026 69,334.807−$5,854.79
long20 Aug 2026 69,334.8015 Oct 2026 81,682.0056$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.

ScriptSharesTypeNetTradesMax drawdown
EMA Cross Strategy this page—v4 strategy+35.3%4241.3%
EMA Slope AyE by fajardoarelysemav4 strategyNo trades on this market
ROoT by realisticDove62527emav5 strategy−4.7%2259.4%
sachin5986 by mohitesachin78emav4 strategy+32.1%3517.6%
sonu1997 by Sonu1997emav4 strategy+960.7%21,103.8%
Moving Average Crossover by Muh4mm4demav4 strategy+19,990.1%301,274.2%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 10 msWith a workaround 1,000 bars in 29 msNot measured
PineTSNo 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)