Pine Script v5 · Strategy
EMA Strat
A Pine Script v5 strategy built on EMA (close, 21). It has 2 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. 2 settings; defaults include Length 21 and Year 2000.
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 Strat repaint?
- No
- No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 19 of 19 trades.
- How did EMA Strat do on BTCUSDT 1D with default settings?
- +3.15%
- From 26 Jan 2024 to 21 Oct 2026, over 57 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
- Does EMA Strat 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: 57 before, 49 after on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine. It is shown for reference and is not offered as a download.
- Does EMA Strat use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does EMA Strat run in Zeal Terminal?
- Yes
- It compiles in 10 ms and runs 1,000 bars in 19 ms, in the browser.
BTCUSDT · 1D · default settings
- Plot
- Plot
- Winning trade
- Losing trade
- Equity
- Buy and hold
- Net profit
- +3.15%
- $31,545.40
- Buy and hold
- +102.5%
- same period
- Closed trades
- 57
- 15 won · 42 lost
- Profitable
- 26.3%
- of closed trades
- Profit factor
- 1.43
- gross profit ÷ gross loss
- Max drawdown
- 2.21%
- −$22,994.10
One run with the author's default settings, $1,000,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
1 setting does nothing
“Year” is declared but never read.
-
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.
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 |
|---|---|---|---|
| Length | 21 | int | ema |
| Year | 2000 | int | unused |
What it calculates
- EMAclose, Length (21)
ema · line 7
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 | (close > ema and start == true) | On | 18 |
| Close Long | (below) | On | 28 |
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 | +3.2% | +102.5% | 57 | 26% | 1.43 | 2.2% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −0.2% | +5.5% | 62 | 21% | 0.94 | 1.2% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | +0.2% | +9.8% | 48 | 38% | 1.76 | 0.1% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −0.0% | +8.4% | 75 | 15% | 0.84 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +0.0% | +16.7% | 62 | 27% | 1.08 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +0.0% | +21.5% | 67 | 15% | 1.09 | 0.0% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +0.0% | +148.3% | 59 | 34% | 1.53 | 0.0% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | +0.0% | +17.1% | 67 | 28% | 1.04 | 0.0% | 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 | 22 | 36% | $28,938.50 |
| Sideways market | 22 | 23% | −$4,700.44 |
| Falling market | 13 | 15% | $7,307.28 |
Rising and falling mean the market moved more than 15% over the 90 bars before entry.
How bad could the drawdown be?
3.9%
The 95th percentile of the worst drawdown when the same 57 trades are reshuffled 5,000 times. The bar spans the 5th to 95th percentile, 1.6% to 3.9%; the tick is what actually happened, 2.1%. Order luck alone moves it this much.
All 57 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 16 Feb 2024 51,880.00 | 20 Mar 2024 61,937.40 | 33 | $10,057.40 |
| long | 21 Mar 2024 67,840.50 | 23 Mar 2024 63,796.60 | 2 | −$4,043.87 |
| long | 25 Mar 2024 67,210.00 | 3 Apr 2024 65,464.00 | 9 | −$1,746.01 |
| long | 5 Apr 2024 68,487.80 | 13 Apr 2024 67,116.50 | 8 | −$1,371.28 |
| long | 23 Apr 2024 66,819.30 | 25 Apr 2024 64,289.60 | 2 | −$2,529.74 |
| long | 5 May 2024 63,892.00 | 7 May 2024 63,165.20 | 2 | −$726.85 |
| long | 14 May 2024 62,940.10 | 15 May 2024 61,577.50 | 1 | −$1,362.60 |
| long | 16 May 2024 66,206.50 | 12 Jun 2024 67,314.20 | 27 | $1,107.72 |
| long | 15 Jul 2024 60,797.90 | 1 Aug 2024 64,628.00 | 17 | $3,830.10 |
| long | 2 Aug 2024 65,354.00 | 3 Aug 2024 61,498.30 | 1 | −$3,855.68 |
| long | 22 Aug 2024 61,156.00 | 28 Aug 2024 59,415.00 | 6 | −$1,741.03 |
| long | 13 Sep 2024 58,132.30 | 17 Sep 2024 58,214.00 | 4 | $81.68 |
| long | 18 Sep 2024 60,314.00 | 2 Oct 2024 60,804.90 | 14 | $490.93 |
| long | 7 Oct 2024 62,819.90 | 9 Oct 2024 62,160.50 | 2 | −$659.41 |
| long | 12 Oct 2024 62,540.00 | 5 Nov 2024 67,850.00 | 24 | $5,310.02 |
| long | 6 Nov 2024 69,372.00 | 20 Dec 2024 97,461.90 | 44 | $28,089.90 |
| long | 25 Dec 2024 98,663.60 | 27 Dec 2024 95,791.60 | 2 | −$2,871.98 |
| long | 3 Jan 2025 96,984.80 | 8 Jan 2025 96,954.60 | 5 | −$30.19 |
| long | 15 Jan 2025 96,560.90 | 2 Feb 2025 100,636.00 | 18 | $4,074.81 |
| long | 21 Feb 2025 98,305.00 | 22 Feb 2025 96,182.00 | 1 | −$2,123.02 |
| long | 3 Mar 2025 94,270.00 | 4 Mar 2025 86,221.20 | 1 | −$8,048.83 |
| long | 20 Mar 2025 86,845.90 | 21 Mar 2025 84,223.40 | 1 | −$2,622.55 |
| long | 24 Mar 2025 86,082.50 | 29 Mar 2025 84,424.40 | 5 | −$1,658.12 |
| long | 2 Apr 2025 85,158.40 | 3 Apr 2025 82,516.30 | 1 | −$2,642.07 |
| long | 12 Apr 2025 83,423.80 | 31 May 2025 103,985.00 | 49 | $20,561.60 |
| long | 2 Jun 2025 105,643.00 | 5 Jun 2025 104,697.00 | 3 | −$946.07 |
| long | 8 Jun 2025 105,552.00 | 13 Jun 2025 105,672.00 | 5 | $119.59 |
| long | 17 Jun 2025 106,795.00 | 18 Jun 2025 104,551.00 | 1 | −$2,243.36 |
| long | 24 Jun 2025 105,334.00 | 2 Jul 2025 105,681.00 | 8 | $347.19 |
| long | 3 Jul 2025 108,850.00 | 1 Aug 2025 115,764.00 | 29 | $6,914.48 |
| long | 8 Aug 2025 117,472.00 | 16 Aug 2025 117,342.00 | 8 | −$129.98 |
| long | 23 Aug 2025 116,936.00 | 24 Aug 2025 115,438.00 | 1 | −$1,497.94 |
| long | 9 Sep 2025 112,065.00 | 10 Sep 2025 111,546.00 | 1 | −$518.85 |
| long | 11 Sep 2025 113,960.00 | 23 Sep 2025 112,651.00 | 12 | −$1,309.01 |
| long | 30 Sep 2025 114,312.00 | 11 Oct 2025 112,774.00 | 11 | −$1,537.48 |
| long | 27 Oct 2025 114,559.00 | 30 Oct 2025 110,021.00 | 3 | −$4,538.11 |
| long | 4 Dec 2025 93,429.90 | 5 Dec 2025 92,078.10 | 1 | −$1,351.89 |
| long | 10 Dec 2025 92,678.80 | 13 Dec 2025 90,268.40 | 3 | −$2,410.38 |
| long | 31 Dec 2025 88,485.50 | 1 Jan 2026 87,648.20 | 1 | −$837.29 |
| long | 2 Jan 2026 88,839.10 | 21 Jan 2026 88,427.70 | 19 | −$411.39 |
| long | 3 Mar 2026 68,830.10 | 4 Mar 2026 68,338.00 | 1 | −$492.05 |
| long | 5 Mar 2026 72,666.80 | 7 Mar 2026 68,114.00 | 2 | −$4,552.75 |
| long | 11 Mar 2026 69,948.60 | 20 Mar 2026 69,930.00 | 9 | −$18.63 |
| long | 24 Mar 2026 70,906.40 | 27 Mar 2026 68,820.30 | 3 | −$2,086.14 |
| long | 6 Apr 2026 69,034.20 | 16 May 2026 79,113.20 | 40 | $10,079.00 |
| long | 4 Jul 2026 62,583.30 | 9 Jul 2026 62,290.00 | 5 | −$293.25 |
| long | 10 Jul 2026 63,230.00 | 14 Jul 2026 62,334.50 | 4 | −$895.49 |
| long | 15 Jul 2026 65,044.00 | 25 Jul 2026 64,140.00 | 10 | −$903.99 |
| long | 26 Jul 2026 64,375.00 | 28 Jul 2026 63,755.90 | 2 | −$619.15 |
| long | 31 Jul 2026 64,780.00 | 1 Aug 2026 62,887.90 | 1 | −$1,892.15 |
| long | 5 Aug 2026 64,106.60 | 11 Aug 2026 63,970.00 | 6 | −$136.54 |
| long | 18 Aug 2026 64,532.10 | 11 Sep 2026 76,568.70 | 24 | $12,036.60 |
| long | 12 Sep 2026 77,225.70 | 14 Sep 2026 76,842.00 | 2 | −$383.70 |
| long | 15 Sep 2026 78,189.20 | 16 Sep 2026 75,644.50 | 1 | −$2,544.72 |
| long | 19 Sep 2026 80,883.90 | 8 Oct 2026 83,321.80 | 19 | $2,437.95 |
| long | 12 Oct 2026 83,773.10 | 13 Oct 2026 81,850.00 | 1 | −$1,923.11 |
| long | 14 Oct 2026 83,169.00 | 15 Oct 2026 81,682.00 | 1 | −$1,486.99 |
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 Strat this page | — | v5 strategy | +3.2% | 57 | 2.2% |
| 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% |
Upgrade and export
Written in Pine Script v5. The clean-v5 step types the settings and moves when = into if blocks, which v6 requires; it is run before and after like any upgrade.
- 2 settings use the untyped input() (line 6, 12). It still compiles in v6; input.int, input.float or input.source state the type.
- No v6-only features. v6 is not needed to run this script.
Same 57 trades and the same P&L on BTCUSDT 1D, 1,000 bars, in Zeal Terminal's engine.
- Line 6: input() → input.int()
- Line 12: input() → input.int()
⋯// EMA Variables-emaInput = input(21, "Length")+emaInput = input.int(21, "Length")ema = ta.ema(close, emaInput)⋯// Start Date-yearInput = input(2000, "Year")+yearInput = input.int(2000, "Year")if (time >= timestamp(2000,01,01,01,01))⋯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: 57 before, 49 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.
⋯// © ericdwyang-//@version=5+//@version=6strategy("EMA Strat", overlay=true, margin_long=100, margin_short=100)⋯Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 10 ms | Yes 1,000 bars in 19 ms | Not measured |
| PineTS | Not tested | Not tested | Not measured |
“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.
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/// © ericdwyang//@version=5strategy("EMA Strat", overlay=true, margin_long=100, margin_short=100)// EMA VariablesemaInput = input(21, "Length")ema = ta.ema(close, emaInput)// Variable Declarationp = 0start = false// Start DateyearInput = input(2000, "Year")if (time >= timestamp(2000,01,01,01,01)) start := true// Check first candle relative to EMAif (close > ema and start == true) p += 1 strategy.entry("Long", strategy.long, comment = "Entry") // Candle close above EMA (p + 1, count reset to 0)above = close[1] > ema[1]if (above) p += 1// Candle close below EMA (reset p to 0, count -1)below = close < emaif (below) p := 0 strategy.close("Long", comment = "Flat")// // Exit Position// if (redCount == -2)// strategy.close("Long", comment = "Flat") // goLong = p[1] == 0 and p == 1// flatten = p == 0 // // Restablish long // if (goLong and start == true)// strategy.entry("Long", strategy.long, comment = "Entry") plot(p)plot(ema)