Pine Script v4 · Strategy
Mathias & Christer EMA
A Pine Script v4 strategy built on EMA (close, 9), ATR (1) and Barssince (BuyFirstTrigger). It has 8 order rules, 2 entries among them, and trades long and short with the default settings. Exits use a stop loss. Each trade commits 100% of equity and pays no commission. 3 settings; defaults include Length 9, Source close and Max Loss 3000.
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 Mathias & Christer EMA repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did Mathias & Christer EMA 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 entry conditions never all held on this data. It closed no trades on the other markets either. A backtest, not investment advice.
- Can Mathias & Christer EMA 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 Mathias & Christer EMA use a stop loss?
- Yes
- strategy.exit sets a stop on line 55, 62.
- Does Mathias & Christer EMA run in Zeal Terminal?
- Yes
- It compiles in 40 ms and runs 1,000 bars in 61 ms, in the browser.
BTCUSDT · 1D · default settings
- buyLow
- myATR
- difference
- emaCurrent
- ema4h
- BuySignalOutBarssince
- 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.
-
Likely bug
ema(src, 1) ignores the settings line 11
Line 11 hard-codes 1 where line 10 uses “Length”. Changing those settings moves one band but not this one.
ema(src, 1)ema(src, len) -
Check
No commission
The backtest assumes free trading. Results on any real exchange will be lower.
-
Check
Every trade uses all the equity
default_qty_value is 100% of equity.
-
Note
1 setting has no label
In the settings dialog maxLoss (line 8) shows an empty name. The labels on this page come from the variable names.
-
Note
No slippage
Fills are assumed at the exact open price. Add slippage in the properties for a more realistic test.
-
Note
Uses transp =, which v5 removed line 21
transp = on line 21, 22, 28, 29. v5 folds the transparency into the colour with color.new(c, transp).
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, atr → ta.atr, barssince → ta.barssince, crossover → ta.crossover, crossunder → ta.crossunder.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 1: //@version=4 → //@version=5
- Line 5: input(type = input.integer) → input.int()
- Line 6: input(type = input.source) → input.source()
- Line 10: ema → ta.ema
- Line 11: ema → ta.ema
- Line 15: atr → ta.atr
20 more edits
- Line 17: barssince → ta.barssince
- Line 19: crossover → ta.crossover
- Line 20: barssince → ta.barssince
- Line 21: transp = 20 → color.new(BuySignalTest and BuySignalTestBarssince[1]>BuyTriggerLineBarssince[1]? color.green : na, 20)
- Line 22: transp = 80 → color.new(BuyFirstTrigger and not BuyFirstTrigger[1]? color.green : na, 80)
- Line 24: barssince → ta.barssince
- Line 26: crossunder → ta.crossunder
- Line 27: barssince → ta.barssince
- Line 28: transp = 20 → color.new(SellSignalTest and SellSignalTestBarssince[1]>SellTriggerLineBarssince? color.red : na, 20)
- Line 29: transp = 80 → color.new(SellFirstTrigger and not SellFirstTrigger[1] and SellTriggerLineBarssince[1]>SellTriggerLineBarssince? color.red : na, 80)
- Line 44: barssince → ta.barssince
- Line 45: barssince → ta.barssince
- Line 45: crossunder → ta.crossunder
- Line 46: crossunder → ta.crossunder
- Line 49: barssince → ta.barssince
- Line 50: barssince → ta.barssince
- Line 50: crossover → ta.crossover
- Line 51: crossover → ta.crossover
- Line 54: strategy.entry(…, true) → strategy.entry(…, strategy.long)
- Line 61: strategy.entry(…, false) → strategy.entry(…, strategy.short)
-//@version=4+//@version=5// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯strategy("Mathias & Christer EMA", shorttitle="M&C_EMA",overlay=true, process_orders_on_close = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)-len = input(9, title="Length", type=input.integer)+len = input.int(9, title="Length")-src = input(close, title="Source", type=input.source)+src = input.source(close, title="Source")//show4h = input(true, title="show 4h", type=input.bool)⋯//ema4h = security(syminfo.ticker, "240", rsi(src, len))-ema4h = ema(src, len)+ema4h = ta.ema(src, len)-emaCurrent = ema(src, 1)+emaCurrent = ta.ema(src, 1)difference = (emaCurrent - ema4h)/ema4h⋯plot(buyLow?1:0,title="buyLow",display=display.none)-myATR = atr(1)+myATR = ta.atr(1)BuyFirstTrigger = (difference>myATR) and (emaCurrent>ema4h) and (difference[1]<myATR[1])-BuyTriggerLineBarssince = barssince(BuyFirstTrigger)+BuyTriggerLineBarssince = ta.barssince(BuyFirstTrigger)BuyTriggerLine = close[BuyTriggerLineBarssince]-BuySignalTest = crossover(close[1],BuyTriggerLine) and (BuyTriggerLineBarssince > 6)+BuySignalTest = ta.crossover(close[1],BuyTriggerLine) and (BuyTriggerLineBarssince > 6)-BuySignalTestBarssince = barssince(BuySignalTest)+BuySignalTestBarssince = ta.barssince(BuySignalTest)-bgcolor(BuySignalTest and BuySignalTestBarssince[1]>BuyTriggerLineBarssince[1]? color.green : na, transp=20)+bgcolor(color.new(BuySignalTest and BuySignalTestBarssince[1]>BuyTriggerLineBarssince[1]? color.green : na, 20))-bgcolor(BuyFirstTrigger and not BuyFirstTrigger[1]? color.green : na, transp=80,title="BuyFirstTrigger")+bgcolor(color.new(BuyFirstTrigger and not BuyFirstTrigger[1]? color.green : na, 80),title="BuyFirstTrigger")SellFirstTrigger = ((difference*-1)>myATR) and (emaCurrent<ema4h)-SellTriggerLineBarssince = barssince(SellFirstTrigger)+SellTriggerLineBarssince = ta.barssince(SellFirstTrigger)SellTriggerLine = close[SellTriggerLineBarssince]-SellSignalTest = crossunder(close[1],SellTriggerLine) and (SellTriggerLineBarssince > 6)+SellSignalTest = ta.crossunder(close[1],SellTriggerLine) and (SellTriggerLineBarssince > 6)-SellSignalTestBarssince = barssince(SellSignalTest)+SellSignalTestBarssince = ta.barssince(SellSignalTest)-bgcolor(SellSignalTest and SellSignalTestBarssince[1]>SellTriggerLineBarssince? color.red : na, transp=20)+bgcolor(color.new(SellSignalTest and SellSignalTestBarssince[1]>SellTriggerLineBarssince? color.red : na, 20))-bgcolor(SellFirstTrigger and not SellFirstTrigger[1] and SellTriggerLineBarssince[1]>SellTriggerLineBarssince? color.red : na, transp=80)+bgcolor(color.new(SellFirstTrigger and not SellFirstTrigger[1] and SellTriggerLineBarssince[1]>SellTriggerLineBarssince? color.red : na, 80))plot(myATR,title="myATR",display=display.none)⋯BuySignal = BuySignalTest and BuySignalTestBarssince[1]>BuyTriggerLineBarssince-BuySignalBarssince = barssince(BuySignal)+BuySignalBarssince = ta.barssince(BuySignal)-BuySignalOutBarssince = barssince(crossunder(close[1],ema4h[1]))+BuySignalOutBarssince = ta.barssince(ta.crossunder(close[1],ema4h[1]))-BuySignalOut = crossunder(close[1],ema4h[1]) and BuySignalOutBarssince[1]>BuySignalTestBarssince[1]+BuySignalOut = ta.crossunder(close[1],ema4h[1]) and BuySignalOutBarssince[1]>BuySignalTestBarssince[1]plot(BuySignalOutBarssince,title="BuySignalOutBarssince",display=display.none)SellSignal = SellSignalTest and SellSignalTestBarssince[1]>SellTriggerLineBarssince-SellSignalBarssince = barssince(ema4h[1]>ema4h[0] and ema4h[1]>80)+SellSignalBarssince = ta.barssince(ema4h[1]>ema4h[0] and ema4h[1]>80)-SellSignalOutBarssince = barssince(crossover(close[1],ema4h[1]))+SellSignalOutBarssince = ta.barssince(ta.crossover(close[1],ema4h[1]))-SellSignalOut = crossover(close[1],ema4h[1]) and SellSignalOutBarssince[1]>SellSignalTestBarssince[1]+SellSignalOut = ta.crossover(close[1],ema4h[1]) and SellSignalOutBarssince[1]>SellSignalTestBarssince[1]if BuySignal strategy.close("short", comment = "Exit short")- strategy.entry("long", true)+ strategy.entry("long", strategy.long) strategy.exit("Max Loss", "long", loss = maxLoss)⋯ strategy.close("long", comment = "Exit Long")- strategy.entry("short", false)+ strategy.entry("short", strategy.short) strategy.exit("Max Loss", "short", loss = maxLoss)⋯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()
⋯//show4h = input(true, title="show 4h", type=input.bool)-maxLoss = input(3000)+maxLoss = input.int(3000)//ema4h = security(syminfo.ticker, "240", rsi(src, len))⋯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.
-//@version=5+//@version=6// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/⋯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 | 9 | int (written input.integer) | ema4h |
| Source | close | source | ema4h emaCurrent |
| Max Loss (no label in the script) | 3000 | int | exit Max Loss |
What it calculates
- EMA (
ema,ta.emain v5)Source (close), Length (9)ema4h · line 10 - EMA (
ema,ta.emain v5)Source (close), 1emaCurrent · line 11 - ATR (
atr,ta.atrin v5)1myATR · line 15 - Barssince (
barssince,ta.barssincein v5)BuyFirstTriggerBuyTriggerLineBarssince · line 17 - Barssince (
barssince,ta.barssincein v5)BuySignalTestBuySignalTestBarssince · line 20 - Barssince (
barssince,ta.barssincein v5)SellFirstTriggerSellTriggerLineBarssince · line 24 - Barssince (
barssince,ta.barssincein v5)SellSignalTestSellSignalTestBarssince · line 27 - Barssince (
barssince,ta.barssincein v5)BuySignalBuySignalBarssince · line 44 - Barssince / Crossunder (
barssince,ta.barssincein v5)crossunder(close[1],ema4h[1])BuySignalOutBarssince · line 45 - Barssince / Crossover (
barssince,ta.barssincein v5)crossover(close[1],ema4h[1])SellSignalOutBarssince · 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 |
|---|---|---|---|
| Close short | BuySignal | On | 53 |
| Open long long | BuySignal | On | 54 |
| Exit Max Loss stop | BuySignal | On | 55 |
| Close long | BuySignalOut | On | 57 |
| Close long | SellSignal | On | 60 |
| Open short short | SellSignal | On | 61 |
| Exit Max Loss stop | SellSignal | On | 62 |
| Close short | SellSignalOut | On | 65 |
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 timingprocess_orders_on_close = true: orders fill at the signal bar's close.
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
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 |
|---|---|---|---|---|---|
| Mathias & Christer EMA this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| Dazzling Bolts by OrcChieftain | ema atr barssince | v4 strategy | +8.5% | 14 | 13.0% |
| Twin Range Filter Algo by OrcChieftain | ema atr barssince | v4 strategy | +48.4% | 18 | 6.8% |
| Trend Reversal - Potential pressure by BardiaB2 | atr ema | v4 strategy | +133,960.0% | 5 | 1,504.8% |
| Simple Ema_ATR Strategy HulkTrading by TheHulkTrading | ema atr | v4 strategy | +2.2% | 14 | 15.6% |
| Keltner Breakout Strategy by Zeal Terminal | ema atr | v5 strategy | +3.5% | 21 | 2.3% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 40 ms | With a workaround 1,000 bars in 61 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
Zeal Terminal's engine currently reads an order side written as true, false as a short, whatever it says; this page passes the same side as strategy.long or strategy.short (lines 54, 61) 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.
//@version=4// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/// © mattehalenstrategy("Mathias & Christer EMA", shorttitle="M&C_EMA",overlay=true, process_orders_on_close = true, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)len = input(9, title="Length", type=input.integer)src = input(close, title="Source", type=input.source)//show4h = input(true, title="show 4h", type=input.bool)maxLoss = input(3000)//ema4h = security(syminfo.ticker, "240", rsi(src, len))ema4h = ema(src, len)emaCurrent = ema(src, 1)difference = (emaCurrent - ema4h)/ema4hbuyLow = low>ema4hplot(buyLow?1:0,title="buyLow",display=display.none)myATR = atr(1)BuyFirstTrigger = (difference>myATR) and (emaCurrent>ema4h) and (difference[1]<myATR[1])BuyTriggerLineBarssince = barssince(BuyFirstTrigger)BuyTriggerLine = close[BuyTriggerLineBarssince]BuySignalTest = crossover(close[1],BuyTriggerLine) and (BuyTriggerLineBarssince > 6)BuySignalTestBarssince = barssince(BuySignalTest)bgcolor(BuySignalTest and BuySignalTestBarssince[1]>BuyTriggerLineBarssince[1]? color.green : na, transp=20)bgcolor(BuyFirstTrigger and not BuyFirstTrigger[1]? color.green : na, transp=80,title="BuyFirstTrigger")SellFirstTrigger = ((difference*-1)>myATR) and (emaCurrent<ema4h)SellTriggerLineBarssince = barssince(SellFirstTrigger)SellTriggerLine = close[SellTriggerLineBarssince]SellSignalTest = crossunder(close[1],SellTriggerLine) and (SellTriggerLineBarssince > 6)SellSignalTestBarssince = barssince(SellSignalTest)bgcolor(SellSignalTest and SellSignalTestBarssince[1]>SellTriggerLineBarssince? color.red : na, transp=20)bgcolor(SellFirstTrigger and not SellFirstTrigger[1] and SellTriggerLineBarssince[1]>SellTriggerLineBarssince? color.red : na, transp=80)plot(myATR,title="myATR",display=display.none)plot(difference,title="difference",display=display.none)plot(emaCurrent,title="emaCurrent")plot(ema4h,title="ema4h", color=color.purple, linewidth=5)plot(BuyTriggerLineBarssince,title="BuyTriggerLineBarssince",display=display.none)plot(BuySignalTestBarssince,title="BuySignalTestBarssince",display=display.none)plot(BuyTriggerLine,"BuyTriggerLine",color.green,linewidth=5)plot(SellTriggerLineBarssince,title="SellTriggerLineBarssince",display=display.none)plot(SellSignalTestBarssince,title="SellSignalTestBarssince",display=display.none)plot(SellTriggerLine,"SellTriggerLine",color.red,linewidth=5)//--------------------------------------------------//--------------------------------------------------//BuySignal = BuySignalTest and BuySignalTestBarssince[1]>BuyTriggerLineBarssinceBuySignalBarssince = barssince(BuySignal)BuySignalOutBarssince = barssince(crossunder(close[1],ema4h[1]))BuySignalOut = crossunder(close[1],ema4h[1]) and BuySignalOutBarssince[1]>BuySignalTestBarssince[1]plot(BuySignalOutBarssince,title="BuySignalOutBarssince",display=display.none)SellSignal = SellSignalTest and SellSignalTestBarssince[1]>SellTriggerLineBarssinceSellSignalBarssince = barssince(ema4h[1]>ema4h[0] and ema4h[1]>80)SellSignalOutBarssince = barssince(crossover(close[1],ema4h[1]))SellSignalOut = crossover(close[1],ema4h[1]) and SellSignalOutBarssince[1]>SellSignalTestBarssince[1]if BuySignal strategy.close("short", comment = "Exit short") strategy.entry("long", true) strategy.exit("Max Loss", "long", loss = maxLoss)if BuySignalOut strategy.close("long", comment = "Exit Long")if SellSignal // Enter trade and issue exit order on max loss. strategy.close("long", comment = "Exit Long") strategy.entry("short", false) strategy.exit("Max Loss", "short", loss = maxLoss)if SellSignalOut // Force trade exit. strategy.close("short", comment = "Exit short") plotchar(BuySignal, "BuySignal", "⬆", location.belowbar, color.lime,size =size.huge )plotchar(BuySignalOut, "BuySignalOut", "█", location.belowbar, color.lime,size =size.small)plotchar(SellSignal, "SellSignal", "⬇", location.abovebar ,color.red,size =size.huge)plotchar(SellSignalOut, "SellSignalOut", "█", location.abovebar, color.red,size =size.small)