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
- 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
VerifiedSame 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⋯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.
| Setting | Default | Type and range | Feeds |
|---|---|---|---|
| Trade Reverse | false | bool | entry long entry short |
| EMA Reverse Entry | true | bool | EMAbuy EMAsell |
| EMA Length | 255 | int | EMA |
| EMA Expander | 5 | int | EMAupper EMAlower |
| MACD Mult | 1 | int · min 1 | macdLine signalLine histLine |
| Use Swing Lo/Hi Stop Loss & Take Profit | true | bool | exit longexit exit shortexit line 55 SL TP |
| Swing Lo/Hi Lookback | 10 | int | SwingLow SwingHigh |
| SL Expander | 0.6 | float · step .2 | lSL sSL |
| TP Expander | 1.2 | float · step .2 | lTP sTP |
What it calculates
- EMA (
ema,ta.emain v5)close, EMA Length (255)EMA · line 24 - MACD (
macd,ta.macdin v5)close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmultmacdLine · line 25 - MACD (
macd,ta.macdin v5)close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmultsignalLine · line 25 - MACD (
macd,ta.macdin v5)close, 12*i_MACDmult, 26*i_MACDmult, 9*i_MACDmulthistLine · line 25 - ATR (
atr,ta.atrin v5)100EMAupper · line 26 - ATR (
atr,ta.atrin v5)100EMAlower · line 27 - Lowest (
lowest,ta.lowestin v5)Swing Lo/Hi Lookback (10)SwingLow · line 34 - Highest (
highest,ta.highestin v5)Swing Lo/Hi Lookback (10)SwingHigh · line 35 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0lSL · line 38 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0sSL · line 39 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, SwingLow, 0lTP · line 40 - Valuewhen (
valuewhen,ta.valuewhenin v5)bought, SwingHigh, 0sTP · line 41 - Crossover (
crossover,ta.crossoverin v5)macdLine, signalLineMACDbuy · line 49 - Crossunder (
crossunder,ta.crossunderin 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.
Order rules
| Order | Fires when | By default | Line |
|---|---|---|---|
| Open long not i_reverse?true:false | EMAbuy and MACDbuy | On | 52 |
| Open short not i_reverse?false:true | EMAsell and MACDsell | On | 53 |
| Exit longexit stop and limit | i_SL | On “Use Swing Lo/Hi Stop Loss & Take Profit” | 56 |
| Exit shortexit stop and limit | i_SL | On “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.
- 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 | −218.4% | +102.5% | 11 | 9% | 0.15 | 186.1% | 26 Jan 2024 – 21 Oct 2026 |
| BTCUSDT 4h | −11.7% | +5.5% | 3 | 33% | 0.52 | 29.7% | 8 May 2026 – 21 Oct 2026 |
| ETHUSDT 1D | −225.4% | +9.8% | 4 | 0% | 0.00 | 225.4% | 26 Jan 2024 – 21 Oct 2026 |
| ETHUSDT 4h | −31.5% | +8.4% | 5 | 20% | 0.41 | 54.3% | 8 May 2026 – 21 Oct 2026 |
| SOLUSDT 1D | +20.9% | +16.7% | 3 | 33% | 1.30 | 44.9% | 26 Jan 2024 – 21 Oct 2026 |
| SOLUSDT 4h | +1.7% | +21.5% | 2 | 50% | 1.06 | 20.4% | 8 May 2026 – 21 Oct 2026 |
| BNBUSDT 1D | +80.8% | +148.3% | 2 | 100% | — | 7.3% | 26 Jan 2024 – 21 Oct 2026 |
| BNBUSDT 4h | −20.3% | +17.1% | 5 | 40% | 0.51 | 28.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 bars | Trades | Won | P&L |
|---|---|---|---|
| Rising market | 6 | 17% | −$8,757.46 |
| Sideways market | 3 | 0% | −$7,538.62 |
| Falling market | 2 | 0% | −$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.
All 11 trades
| Side | Entry | Exit | Bars | P&L |
|---|---|---|---|---|
| long | 7 Nov 2024 75,572.00 | 11 Nov 2024 85,111.00 | 4 | $3,761.21 |
| long | 17 Dec 2024 106,059.00 | 20 Dec 2024 93,585.10 | 3 | −$3,550.87 |
| long | 6 Jan 2025 98,363.60 | 13 Jan 2025 90,981.30 | 7 | −$2,274.65 |
| long | 16 Jan 2025 100,497.00 | 25 Feb 2025 88,721.10 | 40 | −$3,537.97 |
| long | 19 May 2025 106,454.00 | 22 Jun 2025 100,114.00 | 34 | −$1,810.03 |
| long | 23 Jul 2025 119,954.00 | 25 Jul 2025 115,042.00 | 2 | −$1,251.96 |
| long | 12 Aug 2025 118,686.00 | 24 Aug 2025 111,248.00 | 12 | −$1,903.21 |
| long | 2 Oct 2025 118,595.00 | 10 Oct 2025 107,968.00 | 8 | −$2,711.06 |
| short | 23 Mar 2026 67,859.00 | 17 Apr 2026 76,456.00 | 25 | −$3,826.20 |
| short | 28 Jun 2026 60,029.00 | 21 Jul 2026 66,016.60 | 23 | −$3,017.53 |
| short | 28 Jul 2026 63,755.90 | 19 Aug 2026 67,357.90 | 22 | −$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.
| Script | Shares | Type | Net | Trades | Max drawdown |
|---|---|---|---|---|---|
| 255 EMA Strategy this page | — | v4 strategy | −218.4% | 11 | 186.1% |
| Modulo Logic + EMA Strat by tweakerID | lowest highest valuewhen atr ema | v4 strategy | −50.8% | 16 | 54.7% |
| [2022] MACD Cross Strategy (version 1) by spuckles12 | macd ema atr lowest highest | v5 strategy | does not run in Zeal Terminal | ||
| [DuDu95] SSL 4C MACD Laugerre RSI Strategy by DuDu95 | atr ema highest lowest macd valuewhen | v5 strategy | No trades on this market | ||
| Richard's Strategy by Deepak8369 | highest lowest atr ema | v4 strategy | +34.6% | 70 | 26.3% |
| Classic Long Term Trend Following System by Robrecht99 | ema highest lowest atr | v5 strategy | +2.9% | 5 | 1.1% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 23 ms | With a workaround 1,000 bars in 74 ms | Not measured |
| PineTS | No 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")