Pine Script v4 · Strategy
EMA Crossover Strategy Example
A Pine Script v4 strategy built on Crossover (shortMA, longMA) and Crossunder (close, exitMA). It has 3 order rules, 1 entry among them, and trades long only with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 12 settings; defaults include From Day 1, From Month 1 and From Year 2020.
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 Example repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did EMA Crossover Strategy Example do on BTCUSDT 1D with default settings?
- Not measured
- It closed no trades there from 26 Jan 2024 to 21 Oct 2026 with the default settings, so there is no result to report. Its code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Can EMA Crossover Strategy Example be converted to Pine Script v5?
- Not tested
- The steps are shown below, but neither version placed a trade on any market tested, so there was nothing to compare and no download is offered.
- Does EMA Crossover Strategy Example use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does EMA Crossover Strategy Example run in Zeal Terminal?
- Yes
- It compiles in 13 ms and runs 1,000 bars in 40 ms, in the browser.
BTCUSDT · 1D · default settings
- The Short Moving Average
- The Long Moving Average
- The Exit CrossUnder Moving Average
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- 0.00%
- $0.00
- Buy and hold
- +102.5%
- same period
- Closed trades
- 0
- 0 won · 0 lost
- Profitable
- 0.0%
- of closed trades
- Profit factor
- —
- gross profit ÷ gross loss
- Max drawdown
- 0.00%
- $0.00
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 stop loss
Every exit waits for a signal. Nothing limits a losing trade before the exit condition fires.
-
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 66, 67. The upgraded version below moves each call inside an if block.
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 rma → ta.rma, sma → ta.sma, ema → ta.ema, wma → ta.wma, vwma → ta.vwma, round → math.round, sqrt → math.sqrt, crossover → ta.crossover, crossunder → ta.crossunder.
- 2 strategy calls use the when = argument (line 66, 67). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 2: //@version=4 → //@version=5
- Line 35: rma → ta.rma
- Line 38: sma → ta.sma
- Line 41: ema → ta.ema
- Line 44: wma → ta.wma
- Line 47: vwma → ta.vwma
9 more edits
- Line 50: sma → ta.sma
- Line 54: wma → ta.wma
- Line 54: wma → ta.wma
- Line 54: wma → ta.wma
- Line 54: round → math.round
- Line 54: sqrt → math.sqrt
- Line 64: crossover → ta.crossover
- Line 65: crossunder → ta.crossunder
- Line 66: strategy.entry(…, true) → strategy.entry(…, strategy.long)
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=4+//@version=5strategy("EMA Crossover Strategy", overlay=true)⋯ if smoothing == "RMA"- rma(src, length)+ ta.rma(src, length) else if smoothing == "SMA"- sma(src, length)+ ta.sma(src, length) else if smoothing == "EMA"- ema(src, length)+ ta.ema(src, length) else if smoothing == "WMA"- wma(src, length)+ ta.wma(src, length) else if smoothing == "VWMA"- vwma(src, length)+ ta.vwma(src, length) else if smoothing == "SMMA"- na(src[1]) ? sma(src, length) : (src[1] * (length - 1) + src) / length+ na(src[1]) ? ta.sma(src, length) : (src[1] * (length - 1) + src) / length ⋯ if smoothing == "HullMA"- wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)))+ ta.wma(2 * ta.wma(src, length / 2) - ta.wma(src, length), math.round(math.sqrt(length)))//ASSIGN A MOVING AVERAGE RESULT TO A VARIABLE⋯//BUY STRATEGY-buy = crossover(shortMA,longMA) ? true : na+buy = ta.crossover(shortMA,longMA) ? true : na-exit = crossunder(close,exitMA) ? true : na+exit = ta.crossunder(close,exitMA) ? true : na-strategy.entry("long",true,when=buy and time_cond)+strategy.entry("long",strategy.long,when=buy and time_cond)strategy.close("long",when=exit and time_cond)⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 8: input() → input.int()
- Line 9: input() → input.int()
- Line 10: input() → input.int()
- Line 13: input() → input.int()
- Line 14: input() → input.int()
- Line 15: input() → input.int()
8 more edits
- Line 24: input() → input.int()
- Line 25: input() → input.string()
- Line 26: input() → input.int()
- Line 27: input() → input.string()
- Line 28: input() → input.int()
- Line 29: input() → input.string()
- Line 66: when = buy and time_cond → if block
- Line 67: when = exit and time_cond → if block
⋯// From Date Inputs-fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)+fromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)+fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-fromYear = input(defval = 2020, title = "From Year", minval = 1970)+fromYear = input.int(defval = 2020, title = "From Year", minval = 1970) // To Date Inputs-toDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)+toDay = input.int(defval = 1, title = "To Day", minval = 1, maxval = 31)-toMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)+toMonth = input.int(defval = 1, title = "To Month", minval = 1, maxval = 12)-toYear = input(defval = 2021, title = "To Year", minval = 1970)+toYear = input.int(defval = 2021, title = "To Year", minval = 1970) ⋯//CREATE USER-INPUT VARIABLES-periodShort = input(13, minval=1, title="Enter Period for Short Moving Average")+periodShort = input.int(13, minval=1, title="Enter Period for Short Moving Average")-smoothingShort = input(title="Choose Smoothing Type for Short Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])+smoothingShort = input.string(title="Choose Smoothing Type for Short Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])-periodLong = input(48, minval=1, title="Enter Period for Long Moving Average")+periodLong = input.int(48, minval=1, title="Enter Period for Long Moving Average")-smoothingLong = input(title="Choose Smoothing Type for Long Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])+smoothingLong = input.string(title="Choose Smoothing Type for Long Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])-periodExit = input(30, minval=1, title="Enter Period for Exit Moving Average")+periodExit = input.int(30, minval=1, title="Enter Period for Exit Moving Average")-smoothingExit = input(title="Choose Smoothing Type for Exit Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])+smoothingExit = input.string(title="Choose Smoothing Type for Exit Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])src1 = close⋯exit = ta.crossunder(close,exitMA) ? true : na-strategy.entry("long",strategy.long,when=buy and time_cond)+if buy and time_cond+ strategy.entry("long",strategy.long)-strategy.close("long",when=exit and time_cond)+if exit and time_cond+ strategy.close("long")if (not time_cond)⋯Does it run in Pine Script v6?
Not tested
Neither version placed a trade on any of the 8 markets, so there was nothing to compare. The v6 version is shown for reference and is not offered as a download. It differs from the v5 version by 1 edit.
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/-//@version=5+//@version=6strategy("EMA Crossover Strategy", overlay=true)⋯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 |
|---|---|---|---|
| From Day | 1 | int · min 1 · max 31 | startDate |
| From Month | 1 | int · min 1 · max 12 | startDate |
| From Year | 2020 | int · min 1970 | startDate |
| To Day | 1 | int · min 1 · max 31 | finishDate |
| To Month | 1 | int · min 1 · max 12 | finishDate |
| To Year | 2021 | int · min 1970 | finishDate |
| Enter Period for Short Moving Average | 13 | int · min 1 | shortMA |
| Choose Smoothing Type for Short Moving Average | EMA | string · options ["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"] | shortMA |
| Enter Period for Long Moving Average | 48 | int · min 1 | longMA |
| Choose Smoothing Type for Long Moving Average | EMA | string · options ["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"] | longMA |
| Enter Period for Exit Moving Average | 30 | int · min 1 | exitMA |
| Choose Smoothing Type for Exit Moving Average | EMA | string · options ["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"] | exitMA |
What it calculates
- Crossover (
crossover,ta.crossoverin v5)shortMA, longMAbuy · line 64 - Crossunder (
crossunder,ta.crossunderin v5)close, exitMAexit · line 65
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 | buy and time_cond | On | 66 |
| Close long | exit and time_cond | On | 67 |
| Close | (not time_cond) | On | 69 |
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.
- 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 | 0.0% | +102.5% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | 0.0% | +5.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | 0.0% | +9.8% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | 0.0% | +8.4% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | 0.0% | +16.7% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | 0.0% | +21.5% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | 0.0% | +148.3% | 0 | 0% | — | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | 0.0% | +17.1% | 0 | 0% | — | 0.0% | 8 May 2026 – 21 Oct 2026 |
Backtests are simulations on historical data and are not investment advice.
Similar scripts
No other indicator or strategy here shares enough of its calculations.
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 13 ms | With a workaround 1,000 bars in 40 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (line 66) 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///@version=4strategy("EMA Crossover Strategy", overlay=true)////////////////////////////////////////////////////////////////////////////////// BACKTESTING RANGE // From Date InputsfromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)fromYear = input(defval = 2020, title = "From Year", minval = 1970) // To Date InputstoDay = input(defval = 1, title = "To Day", minval = 1, maxval = 31)toMonth = input(defval = 1, title = "To Month", minval = 1, maxval = 12)toYear = input(defval = 2021, title = "To Year", minval = 1970) // Calculate start/end date and time conditionstartDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)finishDate = timestamp(toYear, toMonth, toDay, 00, 00)time_cond = time >= startDate and time <= finishDate //////////////////////////////////////////////////////////////////////////////////CREATE USER-INPUT VARIABLESperiodShort = input(13, minval=1, title="Enter Period for Short Moving Average")smoothingShort = input(title="Choose Smoothing Type for Short Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])periodLong = input(48, minval=1, title="Enter Period for Long Moving Average")smoothingLong = input(title="Choose Smoothing Type for Long Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])periodExit = input(30, minval=1, title="Enter Period for Exit Moving Average")smoothingExit = input(title="Choose Smoothing Type for Exit Moving Average", defval="EMA", options=["RMA", "SMA", "EMA", "WMA", "VWMA", "SMMA", "DEMA", "TEMA", "HullMA", "LSMA"])src1 = closepivot = (high + low + close) / 3//MA CALCULATION FUNCTIONma(smoothing, src, length) => if smoothing == "RMA" rma(src, length) else if smoothing == "SMA" sma(src, length) else if smoothing == "EMA" ema(src, length) else if smoothing == "WMA" wma(src, length) else if smoothing == "VWMA" vwma(src, length) else if smoothing == "SMMA" na(src[1]) ? sma(src, length) : (src[1] * (length - 1) + src) / length else if smoothing == "HullMA" wma(2 * wma(src, length / 2) - wma(src, length), round(sqrt(length)))//ASSIGN A MOVING AVERAGE RESULT TO A VARIABLEshortMA=ma(smoothingShort, pivot, periodShort)longMA=ma(smoothingLong, pivot, periodLong)exitMA=ma(smoothingExit, pivot, periodExit)//PLOT THOSE VARIABLESplot(shortMA, linewidth=4, color=color.yellow,title="The Short Moving Average")plot(longMA, linewidth=4, color=color.blue,title="The Long Moving Average")plot(exitMA, linewidth=1, color=color.red,title="The Exit CrossUnder Moving Average")//BUY STRATEGYbuy = crossover(shortMA,longMA) ? true : naexit = crossunder(close,exitMA) ? true : nastrategy.entry("long",true,when=buy and time_cond)strategy.close("long",when=exit and time_cond)if (not time_cond) strategy.close_all()