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
- 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
DiffersTrades 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
VerifiedSame 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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Backtest Start Year | 2020 | int | line 49 line 57 |
| Length | 20 | int (written input.integer) · min 1 | fast slow |
| Multiplier (3x length, 4x length, etc) | 3 | int (written input.integer) · options [3,4,5,6,7,8,9,10] | slow |
| Long Only | false | bool | Short Triangle Exit Sign line 51 line 56 |
What it calculates
- EMA (
ema,ta.emain v5)hl2, Length (20)fast · line 37 - EMA (
ema,ta.emain v5)hl2, length * ratioslow · line 38 - Crossover (
crossover,ta.crossoverin v5)fast, slowlongEntry · line 41 - Crossunder (
crossunder,ta.crossunderin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open Long long | enterLong() | On | 52 |
| Close Long | exitLong() | On | 53 |
| Open Short short | enterShort() | On | 60 |
| Close Short | exitShort() | On | 61 |
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 | −1.2% | +102.5% | 13 | 23% | 0.88 | 5.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.0% | +5.5% | 12 | 33% | 1.00 | 1.5% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +7.6% | +9.8% | 9 | 67% | 2.34 | 6.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | +0.0% | +8.4% | 11 | 18% | 1.00 | 1.8% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | −9.5% | +16.7% | 19 | 26% | 0.50 | 16.3% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +2.0% | +21.5% | 9 | 33% | 1.81 | 1.6% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +3.2% | +148.3% | 11 | 36% | 1.76 | 2.5% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −1.4% | +17.1% | 20 | 25% | 0.68 | 3.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 0 | — | $0.00 |
| Sideways market | 12 | 25% | −$572.12 |
| Falling market | 1 | 0% | −$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.
All 13 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| short | 25 Jun 2024 60,293.30 | 25 Jul 2024 65,376.00 | 30 | −$862.98 |
| long | 25 Jul 2024 65,376.00 | 6 Aug 2024 54,018.80 | 12 | −$1,738.78 |
| short | 6 Aug 2024 54,018.80 | 24 Sep 2024 63,340.00 | 49 | −$1,700.25 |
| long | 24 Sep 2024 63,340.00 | 25 Feb 2025 91,552.90 | 154 | $4,235.45 |
| short | 25 Feb 2025 91,552.90 | 26 Apr 2025 94,638.70 | 60 | −$356.84 |
| long | 26 Apr 2025 94,638.70 | 1 Sep 2025 108,246.00 | 128 | $1,409.16 |
| short | 1 Sep 2025 108,246.00 | 15 Sep 2025 115,268.00 | 14 | −$675.32 |
| long | 15 Sep 2025 115,268.00 | 28 Sep 2025 109,636.00 | 13 | −$509.25 |
| short | 28 Sep 2025 109,636.00 | 3 Oct 2025 120,529.00 | 5 | −$1,011.67 |
| long | 3 Oct 2025 120,529.00 | 18 Oct 2025 106,432.00 | 15 | −$1,173.05 |
| short | 18 Oct 2025 106,432.00 | 19 Apr 2026 75,691.80 | 183 | $2,800.13 |
| long | 19 Apr 2026 75,691.80 | 2 Jun 2026 71,408.90 | 44 | −$587.15 |
| short | 2 Jun 2026 71,408.90 | 22 Aug 2026 78,338.00 | 81 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| EMA Crossover Strategy this page | — | v4 strategy | −1.2% | 13 | 5.0% |
| 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 12 ms | With a workaround 1,000 bars in 36 ms | Not measured |
| PineTS | No 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())