Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

EMA Crossover Strategy

A Pine Script v4 strategy built on EMA (hl2, 20), Crossover (fast, slow) and Crossunder (fast, slow). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade commits 10% of equity and pays 0.1% commission. 4 settings; defaults include Backtest Start Year 2020, Length 20 and Multiplier (3x length, 4x length, etc) 3.

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 Crossover Strategy repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 2 of 2 trades.
How did EMA Crossover Strategy do on BTCUSDT 1D with default settings?
−1.17%
From 26 Jan 2024 to 21 Oct 2026, over 13 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can EMA Crossover Strategy be converted to Pine Script v5?
No
The automatic step from v4 to v5 changes the results, so no v5 download is offered; the edits are shown below.
Does EMA Crossover Strategy use a stop loss?
No
Every exit waits for a signal; nothing limits a losing trade before it fires.
Does EMA Crossover Strategy run in Zeal Terminal?
Yes
It compiles in 12 ms and runs 1,000 bars in 36 ms, in the browser.

BTCUSDT · 1D · default settings

  • Fast
  • Slow
  • Long Triangle
  • Short Triangle
  • 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 (4)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$99,633$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 = strategy.long, long = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 52, 60) so the engine trades the way the script says.
Net profit
−1.17%
−$1,169.15
Buy and hold
+102.5%
same period
Closed trades
13
3 won · 10 lost
Profitable
23.1%
of closed trades
Profit factor
0.88
gross profit ÷ gross loss
Max drawdown
4.97%
−$4,974.85

One run with the author's default settings, $100,000 starting capital, commission as the script sets it. A long 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 stop loss

    Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.

  • 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 52, 53, 60, 61. The upgraded version below moves each call inside an if block.

  • Note

    Uses long =, which v5 renamed line 52

    long = on line 52, 60. 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.
  • 4 strategy calls use the when = argument (line 52, 53, 60, 61). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Differs

Trades differ: 13 before, 13 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.

  • Line 1: //@version=4 → //@version=5
  • Line 34: input(type = input.integer) → input.int()
  • Line 35: input(type = input.integer) → input.int()
  • Line 36: input(type = input.bool) → input.bool()
  • Line 37: ema → ta.ema
  • Line 38: ema → ta.ema
7 more edits
  • Line 41: crossover → ta.crossover
  • Line 42: crossunder → ta.crossunder
  • Line 48: crossover → ta.crossover
  • Line 51: crossunder → ta.crossunder
  • Line 52: long = strategy.long → direction = strategy.long
  • Line 56: crossunder → ta.crossunder
  • Line 60: long = strategy.short → direction = strategy.short
-//@version=4+//@version=5// ⋯// === LOGIC ===-length = input(type=input.integer,defval=20,minval=1,title="Length")+length = input.int(defval=20,minval=1,title="Length")-ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])+ratio = input.int(defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])-longOnly = input(type=input.bool,defval=false,title="Long Only")+longOnly = input.bool(defval=false,title="Long Only")-fast = ema(hl2,length)+fast = ta.ema(hl2,length)-slow = ema(hl2,length * ratio)+slow = ta.ema(hl2,length * ratio)plot(fast,linewidth=2,color=color.orange,title="Fast")plot(slow,linewidth=2,color=color.blue,title="Slow")-longEntry = crossover(fast,slow)+longEntry = ta.crossover(fast,slow)-shortEntry = crossunder(fast,slow)+shortEntry = ta.crossunder(fast,slow)plotshape(longEntry ? close : na,style=shape.triangleup,color=color.green,location=location.belowbar,size=size.small,title="Long Triangle")⋯enterLong() =>-    crossover(fast,slow) and +    ta.crossover(fast,slow) and        time > timestamp(start_year, 1, 1, 01, 01)exitLong() =>-    longOnly and crossunder(fast,slow)+    longOnly and ta.crossunder(fast,slow)-strategy.entry(id="Long", long=strategy.long, when=enterLong())+strategy.entry(id="Long", direction = strategy.long, when=enterLong())strategy.close(id="Long", when=exitLong())⋯enterShort() =>-    not longOnly and crossunder(fast,slow) and +    not longOnly and ta.crossunder(fast,slow) and        time > timestamp(start_year, 1, 1, 01, 01)⋯    false-strategy.entry(id="Short", long=strategy.short, when=enterShort())+strategy.entry(id="Short", direction = strategy.short, when=enterShort())strategy.close(id="Short", when=exitShort())

Clean v5

Verified

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

  • Line 32: input() → input.int()
  • Line 52: when = enterLong() → if block
  • Line 53: when = exitLong() → if block
  • Line 60: when = enterShort() → if block
  • Line 61: when = exitShort() → if block
⋯//strategy start date-start_year = input(defval=2020, title="Backtest Start Year")+start_year = input.int(defval=2020, title="Backtest Start Year")// === LOGIC ===⋯    longOnly and ta.crossunder(fast,slow)-strategy.entry(id="Long", direction = strategy.long, when=enterLong())+if enterLong()+    strategy.entry(id="Long", direction = strategy.long)-strategy.close(id="Long", when=exitLong())+if exitLong()+    strategy.close(id="Long")// === STRATEGY - SHORT POSITION EXECUTION ===⋯    false-strategy.entry(id="Short", direction = strategy.short, when=enterShort())+if enterShort()+    strategy.entry(id="Short", direction = strategy.short)-strategy.close(id="Short", when=exitShort())+if exitShort()+    strategy.close(id="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: 13 before, 13 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.

-//@version=5+//@version=6// ⋯

Settings

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

SettingDefaultType and rangeFeeds
Backtest Start Year2020intline 49 line 57
Length20int (written input.integer) · min 1fast slow
Multiplier (3x length, 4x length, etc)3int (written input.integer) · options [3,4,5,6,7,8,9,10]slow
Long OnlyfalseboolShort Triangle Exit Sign line 51 line 56

What it calculates

  • EMA (ema, ta.ema in v5)hl2, Length (20)fast · line 37
  • EMA (ema, ta.ema in v5)hl2, length * ratioslow · line 38
  • Crossover (crossover, ta.crossover in v5)fast, slowlongEntry · line 41
  • Crossunder (crossunder, ta.crossunder in v5)fast, slowshortEntry · line 42

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.

hl2hl2price datalengthLengthsetting · 20ratioMultiplier (3x length, …setting · 3closecloseprice datafastEMA→ fastslowEMA→ slowlongEntryCrossover→ longEntryshortEntryCrossunder→ shortEntryentry:LongOpen Longstrategy.entryclose:LongClose Longstrategy.closeentry:ShortOpen Shortstrategy.entryclose:ShortClose Shortstrategy.closeplot:39Fastplotplot:40Slowplotplotshape:43Long Triangleplotshapeplotshape:44Short Triangleplotshapeplotshape:45Exit Signplotshape

Order rules

OrderFires whenBy defaultLine
Open Long longenterLong()On52
Close LongexitLong()On53
Open Short shortenterShort()On60
Close ShortexitShort()On61

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.

2/2trades 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−1.2%+102.5%1323%0.885.0%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.0%+5.5%1233%1.001.5%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+7.6%+9.8%967%2.346.0%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h+0.0%+8.4%1118%1.001.8%8 May 2026 – 21 Oct 2026
SOLUSDT 1D−9.5%+16.7%1926%0.5016.3%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+2.0%+21.5%933%1.811.6%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+3.2%+148.3%1136%1.762.5%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−1.4%+17.1%2025%0.683.4%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 market0—$0.00
Sideways market1225%−$572.12
Falling market10%−$587.15

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

How bad could the drawdown be?

8.3%

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

0%10%
All 13 trades
SideEntryExitBarsP&L
short25 Jun 2024 60,293.3025 Jul 2024 65,376.0030−$862.98
long25 Jul 2024 65,376.006 Aug 2024 54,018.8012−$1,738.78
short6 Aug 2024 54,018.8024 Sep 2024 63,340.0049−$1,700.25
long24 Sep 2024 63,340.0025 Feb 2025 91,552.90154$4,235.45
short25 Feb 2025 91,552.9026 Apr 2025 94,638.7060−$356.84
long26 Apr 2025 94,638.701 Sep 2025 108,246.00128$1,409.16
short1 Sep 2025 108,246.0015 Sep 2025 115,268.0014−$675.32
long15 Sep 2025 115,268.0028 Sep 2025 109,636.0013−$509.25
short28 Sep 2025 109,636.003 Oct 2025 120,529.005−$1,011.67
long3 Oct 2025 120,529.0018 Oct 2025 106,432.0015−$1,173.05
short18 Oct 2025 106,432.0019 Apr 2026 75,691.80183$2,800.13
long19 Apr 2026 75,691.802 Jun 2026 71,408.9044−$587.15
short2 Jun 2026 71,408.9022 Aug 2026 78,338.0081−$988.73

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 Crossover Strategy this page—v4 strategy−1.2%135.0%
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 12 msWith a workaround 1,000 bars in 36 msNot measured
PineTSNo v5 and v6 only—Not measured

Zeal Terminal's engine currently reads an order side written as long = strategy.long, long = strategy.short as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 52, 60) 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.

//@version=4// // This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © gregoirejohnb//// Moving average crossover systems measure drift in the market. They are great strategies for time-limited people.// So, why don't more people use them?// // I think it's due to poor choice in choosing EMA lengths: Market Wizard Ed Seykota has a guideline for moving average crossovers: the slow line should be at least 3x the fast line.// This removes a lot of the whipsaws inherent in moving average systems, which means greater profitability.// His other piece of advice: long-only strategies are best in stock markets where there's a lot more upside potential.//// Using these simple rules, we can reduce a lot of the whipsaws and low profitability trades! This strategy was made so you can see for yourself before trading.//// === HOW TO USE THIS INDICATOR ===// 1) Choose your market and timeframe.// 2) Choose the length.// 3) Choose the multiplier.// 4) Choose if the strategy is long-only or bidirectional. //// Don't overthink the above! We don't know the best answers, that's why this strategy exists! We're going to test and find out.//  After you find a good combination, set up an alert system with the default Exponential Moving Average indicators provided by TradingView.//// === TIPS ===// Increase the multiplier to reduce whipsaws (back and forth trades).// Increase the length to take fewer trades, decrease the length to take more trades.// Try a Long-Only strategy to see if that performs better.//strategy(title="EMA Crossover Strategy", shorttitle="EMA COS", overlay=true, pyramiding=0, default_qty_type=strategy.percent_of_equity, default_qty_value=10, currency=currency.USD,commission_type=strategy.commission.percent,commission_value=0.1)// === GENERAL INPUTS ===//strategy start datestart_year = input(defval=2020, title="Backtest Start Year")// === LOGIC ===length = input(type=input.integer,defval=20,minval=1,title="Length")ratio = input(type=input.integer,defval=3,title="Multiplier (3x length, 4x length, etc)",options=[3,4,5,6,7,8,9,10])longOnly = input(type=input.bool,defval=false,title="Long Only")fast = ema(hl2,length)slow = ema(hl2,length * ratio)plot(fast,linewidth=2,color=color.orange,title="Fast")plot(slow,linewidth=2,color=color.blue,title="Slow")longEntry = crossover(fast,slow)shortEntry = crossunder(fast,slow)plotshape(longEntry ? close : na,style=shape.triangleup,color=color.green,location=location.belowbar,size=size.small,title="Long Triangle")plotshape(shortEntry and not longOnly ? close : na,style=shape.triangledown,color=color.red,location=location.abovebar,size=size.small,title="Short Triangle")plotshape(shortEntry and longOnly ? close : na,style=shape.xcross,color=color.black,location=location.abovebar,size=size.small,title="Exit Sign")// === STRATEGY - LONG POSITION EXECUTION ===enterLong() =>    crossover(fast,slow) and        time > timestamp(start_year, 1, 1, 01, 01)exitLong() =>    longOnly and crossunder(fast,slow)strategy.entry(id="Long", long=strategy.long, when=enterLong())strategy.close(id="Long", when=exitLong())// === STRATEGY - SHORT POSITION EXECUTION ===enterShort() =>    not longOnly and crossunder(fast,slow) and        time > timestamp(start_year, 1, 1, 01, 01)exitShort() =>    falsestrategy.entry(id="Short", long=strategy.short, when=enterShort())strategy.close(id="Short", when=exitShort())