Zeal Terminal Open in Zeal Terminal

Pine Script v4 · Strategy

255 EMA Strategy

A Pine Script v4 strategy built on EMA (close, 255), MACD (close, 12*i_MACDmult) and ATR (100). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss and a profit target. Each trade commits 30000 in cash and pays 0.04 commission. 9 settings; defaults include EMA Length 255, EMA Expander 5 and MACD Mult 1.

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 255 EMA Strategy repaint?
No
No repainting patterns were found in the source, and a live tick-by-tick replay matched history on 3 of 3 trades.
How did 255 EMA Strategy do on BTCUSDT 1D with default settings?
−218.42%
From 26 Jan 2024 to 21 Oct 2026, over 11 closed trades, against +102.5% for buy and hold. A backtest, not investment advice.
Can 255 EMA Strategy be converted to Pine Script v5?
Yes
The upgrade takes 1 step, from v4; each was run before and after on the same candles with identical results, and the v5 script can be downloaded below.
Does 255 EMA Strategy use a stop loss?
Yes
strategy.exit sets a stop on line 56, 57.
Does 255 EMA Strategy run in Zeal Terminal?
Yes
It compiles in 23 ms and runs 1,000 bars in 74 ms, in the browser.

BTCUSDT · 1D · default settings

  • EMA
  • EMA Upper Band
  • EMA Lower Band
  • SL
  • TP
  • 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 (9)
40,00060,00080,000100,000120,000AprJulOct2025AprJulOct2026AprJulOct$-11,842$20,253Equity from $10,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 = not i_reverse?true:false, long = not i_reverse?false:true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 52, 53) so the engine trades the way the script says.
Net profit
−218.42%
−$21,841.90
Buy and hold
+102.5%
same period
Closed trades
11
1 won · 10 lost
Profitable
9.1%
of closed trades
Profit factor
0.15
gross profit ÷ gross loss
Max drawdown
186.05%
−$25,603.10

One run with the author's default settings, $10,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.

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

  • Note

    Uses long =, which v5 renamed line 52

    long = on line 52, 53. 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, macd → ta.macd, atr → ta.atr, lowest → ta.lowest, highest → ta.highest, valuewhen → ta.valuewhen, crossover → ta.crossover, crossunder → ta.crossunder.
  • 2 strategy calls use the when = argument (line 52, 53). It is deprecated in v5 and removed in v6, where each call moves inside an if block.

v4 → v5

Verified

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

  • Line 15: //@version=4 → //@version=5
  • Line 24: ema → ta.ema
  • Line 25: macd → ta.macd
  • Line 26: atr → ta.atr
  • Line 27: atr → ta.atr
  • Line 34: lowest → ta.lowest
9 more edits
  • Line 35: highest → ta.highest
  • Line 38: valuewhen → ta.valuewhen
  • Line 39: valuewhen → ta.valuewhen
  • Line 40: valuewhen → ta.valuewhen
  • Line 41: valuewhen → ta.valuewhen
  • Line 49: crossover → ta.crossover
  • Line 50: crossunder → ta.crossunder
  • Line 52: long = not i_reverse?true:false → direction = not i_reverse?strategy.long:strategy.short
  • Line 53: long = not i_reverse?false:true → direction = not i_reverse?strategy.short:strategy.long
⋯// when price was trading AWAY from the 255EMA"-//@version=4+//@version=5strategy("255 EMA Strategy", overlay=true, pyramiding=1, default_qty_type=strategy.cash, default_qty_value=30000, commission_value = 0.04, initial_capital=10000)⋯//Strategy Variables-EMA=ema(close,i_EMAlength)+EMA=ta.ema(close,i_EMAlength)-[macdLine, signalLine, histLine]=macd(close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmult)+[macdLine, signalLine, histLine]=ta.macd(close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmult)-EMAupper=EMA+((atr(100))*i_EMAexpander)+EMAupper=EMA+((ta.atr(100))*i_EMAexpander)-EMAlower=EMA-((atr(100))*i_EMAexpander)+EMAlower=EMA-((ta.atr(100))*i_EMAexpander)//SL & TP Inputs⋯//SL & TP Variables-SwingLow=lowest(i_SwingLookback)+SwingLow=ta.lowest(i_SwingLookback)-SwingHigh=highest(i_SwingLookback)+SwingHigh=ta.highest(i_SwingLookback)//SL & TP Calculationsbought=strategy.position_size != strategy.position_size[1]-lSL=valuewhen(bought, SwingLow, 0)*(1-i_SLExpander)+lSL=ta.valuewhen(bought, SwingLow, 0)*(1-i_SLExpander)-sSL=valuewhen(bought, SwingHigh, 0)*(1+i_SLExpander)+sSL=ta.valuewhen(bought, SwingHigh, 0)*(1+i_SLExpander)-lTP=strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0))*(1-i_TPExpander))+lTP=strategy.position_avg_price + (strategy.position_avg_price-(ta.valuewhen(bought, SwingLow, 0))*(1-i_TPExpander))-sTP=strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price)*(1+i_TPExpander*100)+sTP=strategy.position_avg_price - (ta.valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price)*(1+i_TPExpander*100)islong=strategy.position_size > 0⋯EMAsell=i_EMAreverse ? close < EMAlower : close > EMAupper-MACDbuy=crossover(macdLine, signalLine)+MACDbuy=ta.crossover(macdLine, signalLine)-MACDsell=crossunder(macdLine, signalLine)+MACDsell=ta.crossunder(macdLine, signalLine)//Entries-strategy.entry("long", long=not i_reverse?true:false, when=EMAbuy and MACDbuy)+strategy.entry("long", direction = not i_reverse?strategy.long:strategy.short, when=EMAbuy and MACDbuy)-strategy.entry("short", long=not i_reverse?false:true, when=EMAsell and MACDsell)+strategy.entry("short", direction = not i_reverse?strategy.short:strategy.long, when=EMAsell and MACDsell)//Exits⋯

Clean v5

VerifiedDownload the v5 script

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

  • Line 18: input() → input.bool()
  • Line 19: input() → input.bool()
  • Line 20: input() → input.int()
  • Line 21: input() → input.int()
  • Line 22: input() → input.int()
  • Line 29: input() → input.bool()
5 more edits
  • Line 30: input() → input.int()
  • Line 31: input() → input.float()
  • Line 32: input() → input.float()
  • Line 52: when = EMAbuy and MACDbuy → if block
  • Line 53: when = EMAsell and MACDsell → if block
⋯//Inputs-i_reverse=input(false, title="Trade Reverse")+i_reverse=input.bool(false, title="Trade Reverse")-i_EMAreverse=input(true, title="EMA Reverse Entry")+i_EMAreverse=input.bool(true, title="EMA Reverse Entry")-i_EMAlength=input(defval=255, title="EMA Length")+i_EMAlength=input.int(defval=255, title="EMA Length")-i_EMAexpander=input(defval=5, title="EMA Expander")+i_EMAexpander=input.int(defval=5, title="EMA Expander")-i_MACDmult=input(defval=1, minval=1, title="MACD Mult")+i_MACDmult=input.int(defval=1, minval=1, title="MACD Mult")//Strategy Variables⋯//SL & TP Inputs-i_SL=input(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")+i_SL=input.bool(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")-i_SwingLookback=input(10, title="Swing Lo/Hi Lookback")+i_SwingLookback=input.int(10, title="Swing Lo/Hi Lookback")-i_SLExpander=input(defval=0.6, step=.2, title="SL Expander")*.01+i_SLExpander=input.float(defval=0.6, step=.2, title="SL Expander")*.01-i_TPExpander=input(defval=1.2, step=.2, title="TP Expander")*.01+i_TPExpander=input.float(defval=1.2, step=.2, title="TP Expander")*.01//SL & TP Variables⋯//Entries-strategy.entry("long", direction = not i_reverse?strategy.long:strategy.short, when=EMAbuy and MACDbuy)+if EMAbuy and MACDbuy+    strategy.entry("long", direction = not i_reverse?strategy.long:strategy.short)-strategy.entry("short", direction = not i_reverse?strategy.short:strategy.long, when=EMAsell and MACDsell)+if EMAsell and MACDsell+    strategy.entry("short", direction = not i_reverse?strategy.short:strategy.long)//Exits⋯

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: 11 before, 0 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.

⋯// when price was trading AWAY from the 255EMA"-//@version=5+//@version=6strategy("255 EMA Strategy", overlay=true, pyramiding=1, default_qty_type=strategy.cash, default_qty_value=30000, commission_value = 0.04, initial_capital=10000)⋯

Settings

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

SettingDefaultType and rangeFeeds
Trade Reversefalseboolentry long entry short
EMA Reverse EntrytrueboolEMAbuy EMAsell
EMA Length255intEMA
EMA Expander5intEMAupper EMAlower
MACD Mult1int · min 1macdLine signalLine histLine
Use Swing Lo/Hi Stop Loss & Take Profittrueboolexit longexit exit shortexit line 55 SL TP
Swing Lo/Hi Lookback10intSwingLow SwingHigh
SL Expander0.6float · step .2lSL sSL
TP Expander1.2float · step .2lTP sTP

What it calculates

  • EMA (ema, ta.ema in v5)close, EMA Length (255)EMA · line 24
  • MACD (macd, ta.macd in v5)close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmultmacdLine · line 25
  • MACD (macd, ta.macd in v5)close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmultsignalLine · line 25
  • MACD (macd, ta.macd in v5)close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmulthistLine · line 25
  • ATR (atr, ta.atr in v5)100EMAupper · line 26
  • ATR (atr, ta.atr in v5)100EMAlower · line 27
  • Lowest (lowest, ta.lowest in v5)Swing Lo/Hi Lookback (10)SwingLow · line 34
  • Highest (highest, ta.highest in v5)Swing Lo/Hi Lookback (10)SwingHigh · line 35
  • Valuewhen (valuewhen, ta.valuewhen in v5)bought, SwingLow, 0lSL · line 38
  • Valuewhen (valuewhen, ta.valuewhen in v5)bought, SwingHigh, 0sSL · line 39
  • Valuewhen (valuewhen, ta.valuewhen in v5)bought, SwingLow, 0lTP · line 40
  • Valuewhen (valuewhen, ta.valuewhen in v5)bought, SwingHigh, 0sTP · line 41
  • Crossover (crossover, ta.crossover in v5)macdLine, signalLineMACDbuy · line 49
  • Crossunder (crossunder, ta.crossunder in v5)macdLine, signalLineMACDsell · line 50

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 datai_EMAlengthEMA Lengthsetting · 255i_MACDmultMACD Multsetting · 1high · low · closehigh · low · closeprice datai_EMAexpanderEMA Expandersetting · 5i_SwingLookbackSwing Lo/Hi Lookbacksetting · 10i_SLExpanderSL Expandersetting · 0.6i_TPExpanderTP Expandersetting · 1.2EMAEMA→ EMAmacdLineMACD→ macdLinesignalLineMACD→ signalLinehistLineMACD→ histLineEMAupperATR→ EMAupperEMAlowerATR→ EMAlowerSwingLowLowest→ SwingLowSwingHighHighest→ SwingHighlSLValuewhen→ lSLsSLValuewhen→ sSLlTPValuewhen→ lTPsTPValuewhen→ sTPMACDbuyCrossover→ MACDbuyMACDsellCrossunder→ MACDsellSLSLvalueTPTPvalueEMAbuy, EMAsellEMAbuy · EMAsell2 valuesbought, islong, isshortbought · islong +13 conditionsentry:longOpen longstrategy.entryentry:shortOpen shortstrategy.entryexit:longexitexit longexitstrategy.exitexit:shortexitexit shortexitstrategy.exitplot:59EMAplotplot:60EMA Upper Bandplotplot:61EMA Lower Bandplotplot:62SLplotplot:63TPplot

Order rules

OrderFires whenBy defaultLine
Open long not i_reverse?true:falseEMAbuy and MACDbuyOn52
Open short not i_reverse?false:trueEMAsell and MACDsellOn53
Exit longexit stop and limiti_SLOn “Use Swing Lo/Hi Stop Loss & Take Profit”56
Exit shortexit stop and limiti_SLOn “Use Swing Lo/Hi Stop Loss & Take Profit”57

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.

3/3trades 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−218.4%+102.5%119%0.15186.1%26 Jan 2024 – 21 Oct 2026
BTCUSDT 4h−11.7%+5.5%333%0.5229.7%8 May 2026 – 21 Oct 2026
ETHUSDT 1D−225.4%+9.8%40%0.00225.4%26 Jan 2024 – 21 Oct 2026
ETHUSDT 4h−31.5%+8.4%520%0.4154.3%8 May 2026 – 21 Oct 2026
SOLUSDT 1D+20.9%+16.7%333%1.3044.9%26 Jan 2024 – 21 Oct 2026
SOLUSDT 4h+1.7%+21.5%250%1.0620.4%8 May 2026 – 21 Oct 2026
BNBUSDT 1D+80.8%+148.3%2100%—7.3%26 Jan 2024 – 21 Oct 2026
BNBUSDT 4h−20.3%+17.1%540%0.5128.5%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 market617%−$8,757.46
Sideways market30%−$7,538.62
Falling market20%−$5,545.79

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

How bad could the drawdown be?

449.0%

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

0%516%
All 11 trades
SideEntryExitBarsP&L
long7 Nov 2024 75,572.0011 Nov 2024 85,111.004$3,761.21
long17 Dec 2024 106,059.0020 Dec 2024 93,585.103−$3,550.87
long6 Jan 2025 98,363.6013 Jan 2025 90,981.307−$2,274.65
long16 Jan 2025 100,497.0025 Feb 2025 88,721.1040−$3,537.97
long19 May 2025 106,454.0022 Jun 2025 100,114.0034−$1,810.03
long23 Jul 2025 119,954.0025 Jul 2025 115,042.002−$1,251.96
long12 Aug 2025 118,686.0024 Aug 2025 111,248.0012−$1,903.21
long2 Oct 2025 118,595.0010 Oct 2025 107,968.008−$2,711.06
short23 Mar 2026 67,859.0017 Apr 2026 76,456.0025−$3,826.20
short28 Jun 2026 60,029.0021 Jul 2026 66,016.6023−$3,017.53
short28 Jul 2026 63,755.9019 Aug 2026 67,357.9022−$1,719.60

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
255 EMA Strategy this page—v4 strategy−218.4%11186.1%
Modulo Logic + EMA Strat by tweakerIDlowest highest valuewhen atr emav4 strategy−50.8%1654.7%
[2022] MACD Cross Strategy (version 1) by spuckles12macd ema atr lowest highestv5 strategydoes not run in Zeal Terminal
[DuDu95] SSL 4C MACD Laugerre RSI Strategy by DuDu95atr ema highest lowest macd valuewhenv5 strategyNo trades on this market
Richard's Strategy by Deepak8369highest lowest atr emav4 strategy+34.6%7026.3%
Classic Long Term Trend Following System by Robrecht99ema highest lowest atrv5 strategy+2.9%51.1%

Where it runs

EngineCompilesRunsMatches TradingView
Zeal Terminal · OpenPineScript v2Yes 23 msWith a workaround 1,000 bars in 74 msNot measured
PineTSNo v5 and v6 only—Not measured

Zeal Terminal's engine currently reads an order side written as long = not i_reverse?true:false, long = not i_reverse?false:true as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 52, 53) 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/// © bufirolas//--- From 15 Trading Examples by Trader Alyx ---// Seems like this strategy works better if we reverse the EMA filter logic.// "Description: This basic scalping strategy allows you to enter the market based upon sentiment// provided by the EMA, set at 255 periods. When price is trading below the 255 EMA, you would// look to enter a LONG BUY positions, and when price is trading above the 255 EMA, you would// look to enter a SELL SHORT position. The MACD lagging indicator will show you clear signals for// when to do this. When the MACD lines cross in a bullish manner and price is below the 255// EMA, buy. When the MACD lines cross in a bearish manner and price is above the 255 EMA,// sell.// NOTE: Make sure that price is trading away from the 255EMA before entering a LONG or SHORT// position. As you can see in the chart below, the clearest signs for trade entry were presented// when price was trading AWAY from the 255EMA"//@version=4strategy("255 EMA Strategy", overlay=true, pyramiding=1, default_qty_type=strategy.cash, default_qty_value=30000, commission_value = 0.04, initial_capital=10000)//Inputsi_reverse=input(false, title="Trade Reverse")i_EMAreverse=input(true, title="EMA Reverse Entry")i_EMAlength=input(defval=255, title="EMA Length")i_EMAexpander=input(defval=5, title="EMA Expander")i_MACDmult=input(defval=1, minval=1, title="MACD Mult")//Strategy VariablesEMA=ema(close,i_EMAlength)[macdLine, signalLine, histLine]=macd(close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmult)EMAupper=EMA+((atr(100))*i_EMAexpander)EMAlower=EMA-((atr(100))*i_EMAexpander)//SL & TP Inputsi_SL=input(true, title="Use Swing Lo/Hi Stop Loss & Take Profit")i_SwingLookback=input(10, title="Swing Lo/Hi Lookback")i_SLExpander=input(defval=0.6, step=.2, title="SL Expander")*.01i_TPExpander=input(defval=1.2, step=.2, title="TP Expander")*.01//SL & TP VariablesSwingLow=lowest(i_SwingLookback)SwingHigh=highest(i_SwingLookback)//SL & TP Calculationsbought=strategy.position_size != strategy.position_size[1]lSL=valuewhen(bought, SwingLow, 0)*(1-i_SLExpander)sSL=valuewhen(bought, SwingHigh, 0)*(1+i_SLExpander)lTP=strategy.position_avg_price + (strategy.position_avg_price-(valuewhen(bought, SwingLow, 0))*(1-i_TPExpander))sTP=strategy.position_avg_price - (valuewhen(bought, SwingHigh, 0) - strategy.position_avg_price)*(1+i_TPExpander*100)islong=strategy.position_size > 0isshort=strategy.position_size < 0SL= islong ? lSL : isshort ? sSL : naTP= islong ? lTP : isshort ? sTP : na//CalculationsEMAbuy=i_EMAreverse ? close > EMAupper : close < EMAlowerEMAsell=i_EMAreverse ? close < EMAlower : close > EMAupperMACDbuy=crossover(macdLine, signalLine)MACDsell=crossunder(macdLine, signalLine)//Entriesstrategy.entry("long", long=not i_reverse?true:false, when=EMAbuy and MACDbuy)strategy.entry("short", long=not i_reverse?false:true, when=EMAsell and MACDsell)//Exitsif i_SL    strategy.exit("longexit", "long", stop=SL, limit=TP)    strategy.exit("shortexit", "short", stop=SL, limit=TP)//Plotsplot(EMA, "EMA", color=color.white, linewidth=2)plot(EMAupper, "EMA Upper Band")plot(EMAlower, "EMA Lower Band")plot(i_SL ? SL : na, color=color.red, style=plot.style_cross, title="SL")plot(i_SL ? TP : na, color=color.green, style=plot.style_cross, title="TP")