Zeal Terminal Open in Zeal Terminal

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
Prerendered with the defaults. Change anything and run it here. The script runs in your browser.
Settings (2)
0.0050,000100,000AprJulOct2025AprJulOct2026AprJulOct$1,033,220$2,025,273Equity from $1,000,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.
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.

SettingDefaultType and rangeFeeds
Length21intema
Year2000intunused

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.

closecloseprice dataemaInputLengthsetting · 21emaEMA→ emabelowbelowconditionaboveaboveconditionentry:LongOpen Longstrategy.entryclose:LongClose Longstrategy.closeplot:40plotplotplot:41plotplot

Order rules

OrderFires whenBy defaultLine
Open Long long(close > ema and start == true)On18
Close Long(below)On28

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.

19/19trades 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+3.2%+102.5%5726%1.432.2%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−0.2%+5.5%6221%0.941.2%8 May 2026 – 21 Oct 2026
ETHUSDT 1D+0.2%+9.8%4838%1.760.1%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−0.0%+8.4%7515%0.840.0%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+0.0%+16.7%6227%1.080.0%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+0.0%+21.5%6715%1.090.0%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+0.0%+148.3%5934%1.530.0%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h+0.0%+17.1%6728%1.040.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 barsTradesWonP&L
Rising market2236%$28,938.50
Sideways market2223%−$4,700.44
Falling market1315%$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.

0%4%
All 57 trades
SideEntryExitBarsP&L
long16 Feb 2024 51,880.0020 Mar 2024 61,937.4033$10,057.40
long21 Mar 2024 67,840.5023 Mar 2024 63,796.602−$4,043.87
long25 Mar 2024 67,210.003 Apr 2024 65,464.009−$1,746.01
long5 Apr 2024 68,487.8013 Apr 2024 67,116.508−$1,371.28
long23 Apr 2024 66,819.3025 Apr 2024 64,289.602−$2,529.74
long5 May 2024 63,892.007 May 2024 63,165.202−$726.85
long14 May 2024 62,940.1015 May 2024 61,577.501−$1,362.60
long16 May 2024 66,206.5012 Jun 2024 67,314.2027$1,107.72
long15 Jul 2024 60,797.901 Aug 2024 64,628.0017$3,830.10
long2 Aug 2024 65,354.003 Aug 2024 61,498.301−$3,855.68
long22 Aug 2024 61,156.0028 Aug 2024 59,415.006−$1,741.03
long13 Sep 2024 58,132.3017 Sep 2024 58,214.004$81.68
long18 Sep 2024 60,314.002 Oct 2024 60,804.9014$490.93
long7 Oct 2024 62,819.909 Oct 2024 62,160.502−$659.41
long12 Oct 2024 62,540.005 Nov 2024 67,850.0024$5,310.02
long6 Nov 2024 69,372.0020 Dec 2024 97,461.9044$28,089.90
long25 Dec 2024 98,663.6027 Dec 2024 95,791.602−$2,871.98
long3 Jan 2025 96,984.808 Jan 2025 96,954.605−$30.19
long15 Jan 2025 96,560.902 Feb 2025 100,636.0018$4,074.81
long21 Feb 2025 98,305.0022 Feb 2025 96,182.001−$2,123.02
long3 Mar 2025 94,270.004 Mar 2025 86,221.201−$8,048.83
long20 Mar 2025 86,845.9021 Mar 2025 84,223.401−$2,622.55
long24 Mar 2025 86,082.5029 Mar 2025 84,424.405−$1,658.12
long2 Apr 2025 85,158.403 Apr 2025 82,516.301−$2,642.07
long12 Apr 2025 83,423.8031 May 2025 103,985.0049$20,561.60
long2 Jun 2025 105,643.005 Jun 2025 104,697.003−$946.07
long8 Jun 2025 105,552.0013 Jun 2025 105,672.005$119.59
long17 Jun 2025 106,795.0018 Jun 2025 104,551.001−$2,243.36
long24 Jun 2025 105,334.002 Jul 2025 105,681.008$347.19
long3 Jul 2025 108,850.001 Aug 2025 115,764.0029$6,914.48
long8 Aug 2025 117,472.0016 Aug 2025 117,342.008−$129.98
long23 Aug 2025 116,936.0024 Aug 2025 115,438.001−$1,497.94
long9 Sep 2025 112,065.0010 Sep 2025 111,546.001−$518.85
long11 Sep 2025 113,960.0023 Sep 2025 112,651.0012−$1,309.01
long30 Sep 2025 114,312.0011 Oct 2025 112,774.0011−$1,537.48
long27 Oct 2025 114,559.0030 Oct 2025 110,021.003−$4,538.11
long4 Dec 2025 93,429.905 Dec 2025 92,078.101−$1,351.89
long10 Dec 2025 92,678.8013 Dec 2025 90,268.403−$2,410.38
long31 Dec 2025 88,485.501 Jan 2026 87,648.201−$837.29
long2 Jan 2026 88,839.1021 Jan 2026 88,427.7019−$411.39
long3 Mar 2026 68,830.104 Mar 2026 68,338.001−$492.05
long5 Mar 2026 72,666.807 Mar 2026 68,114.002−$4,552.75
long11 Mar 2026 69,948.6020 Mar 2026 69,930.009−$18.63
long24 Mar 2026 70,906.4027 Mar 2026 68,820.303−$2,086.14
long6 Apr 2026 69,034.2016 May 2026 79,113.2040$10,079.00
long4 Jul 2026 62,583.309 Jul 2026 62,290.005−$293.25
long10 Jul 2026 63,230.0014 Jul 2026 62,334.504−$895.49
long15 Jul 2026 65,044.0025 Jul 2026 64,140.0010−$903.99
long26 Jul 2026 64,375.0028 Jul 2026 63,755.902−$619.15
long31 Jul 2026 64,780.001 Aug 2026 62,887.901−$1,892.15
long5 Aug 2026 64,106.6011 Aug 2026 63,970.006−$136.54
long18 Aug 2026 64,532.1011 Sep 2026 76,568.7024$12,036.60
long12 Sep 2026 77,225.7014 Sep 2026 76,842.002−$383.70
long15 Sep 2026 78,189.2016 Sep 2026 75,644.501−$2,544.72
long19 Sep 2026 80,883.908 Oct 2026 83,321.8019$2,437.95
long12 Oct 2026 83,773.1013 Oct 2026 81,850.001−$1,923.11
long14 Oct 2026 83,169.0015 Oct 2026 81,682.001−$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.

ScriptSharesTypeNetTradesMax drawdown
EMA Strat this page—v5 strategy+3.2%572.2%
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%

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.

Clean v5

VerifiedDownload the v5 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

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 10 msYes 1,000 bars in 19 msNot measured
PineTSNot testedNot testedNot 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)