Pine Script v4 · Strategy
MACD oscillator with EMA strategy 4H
A Pine Script v4 strategy built on EMA (hl2, 9) and SMA or EMA (hl2, 12). It has 4 order rules, 2 entries among them, and trades long and short with the default settings. Exits wait for a signal; there is no stop loss. Each trade is one contract and pays no commission. 16 settings; defaults include From Day 1, From Month 1 and From Year 2020.
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 MACD oscillator with EMA strategy 4H repaint?
- No
- No repainting patterns were found in the source. The live replay was not measured: it had no trades to compare.
- How did MACD oscillator with EMA strategy 4H 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 code limits trading to a date window, which can leave this period out. It closed no trades on the other markets either. A backtest, not investment advice.
- Can MACD oscillator with EMA strategy 4H 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 MACD oscillator with EMA strategy 4H use a stop loss?
- No
- Every exit waits for a signal; nothing limits a losing trade before it fires.
- Does MACD oscillator with EMA strategy 4H run in Zeal Terminal?
- Yes
- It compiles in 15 ms and runs 1,000 bars in 56 ms, in the browser.
BTCUSDT · 1D · default settings
- EMA
- SMA
- SMA
- SMA
- 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.
-
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
2 settings have no label
In the settings dialog longEntry (line 63), shortEntry (line 64) show 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 an argument v6 removed
when = on line 66, 67, 69, 70. The upgraded version below moves each call inside an if block.
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 security → request.security, heikinashi → ticker.heikinashi, ema → ta.ema, sma → ta.sma.
- 4 strategy calls use the when = argument (line 66, 67, 69, 70). It is deprecated in v5 and removed in v6, where each call moves inside an if block.
v4 → v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 3: //@version=4 → //@version=5
- Line 10: security → request.security
- Line 10: heikinashi → ticker.heikinashi
- Line 11: security → request.security
- Line 11: heikinashi → ticker.heikinashi
- Line 12: security → request.security
15 more edits
- Line 12: heikinashi → ticker.heikinashi
- Line 13: security → request.security
- Line 13: heikinashi → ticker.heikinashi
- Line 30: ema → ta.ema
- Line 33: input(type = input.integer) → input.int()
- Line 34: input(type = input.integer) → input.int()
- Line 35: input(type = input.integer) → input.int()
- Line 36: input(type = input.bool) → input.bool()
- Line 37: input(type = input.bool) → input.bool()
- Line 39: sma → ta.sma
- Line 39: ema → ta.ema
- Line 40: sma → ta.sma
- Line 40: ema → ta.ema
- Line 42: sma → ta.sma
- Line 42: ema → ta.ema
⋯// © SoftKill21-//@version=4+//@version=5strategy("MACD+EMA 4H STRATEGY", overlay=true)⋯// === BASE FUNCTIONS ===-haClose = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, close) : close+haClose = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, close) : close-haOpen = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, open) : open+haOpen = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, open) : open-haHigh = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, high) : high+haHigh = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, high) : high-haLow = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, low) : low+haLow = UseHAcandles ? request.security(ticker.heikinashi(syminfo.tickerid), timeframe.period, low) : low//timecondition⋯src = input(hl2, title="Source")-out = ema(src, len)+out = ta.ema(src, len)//plot(out, title="EMA", color=color.blue)//histogram-fast_length = input(title="Fast Length", type=input.integer, defval=12)+fast_length = input.int(title="Fast Length", defval=12)-slow_length = input(title="Slow Length", type=input.integer, defval=26)+slow_length = input.int(title="Slow Length", defval=26)-signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)+signal_length = input.int(title="Signal Smoothing", minval = 1, maxval = 50, defval = 9)-sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)+sma_source = input.bool(title="Simple MA (Oscillator)", defval=false)-sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)+sma_signal = input.bool(title="Simple MA (Signal Line)", defval=false)// Calculating-fast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)+fast_ma = sma_source ? ta.sma(src, fast_length) : ta.ema(src, fast_length)-slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)+slow_ma = sma_source ? ta.sma(src, slow_length) : ta.ema(src, slow_length)macd = fast_ma - slow_ma-signal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)+signal = sma_signal ? ta.sma(macd, signal_length) : ta.ema(macd, signal_length)hist = macd - signal⋯Clean v5
Not verifiedNeither version placed a trade on any of the 8 markets, so there was nothing to compare.
- Line 6: input() → input.bool()
- Line 15: input() → input.int()
- Line 16: input() → input.int()
- Line 17: input() → input.int()
- Line 21: input() → input.int()
- Line 22: input() → input.int()
9 more edits
- Line 23: input() → input.int()
- Line 28: input() → input.int()
- Line 29: input() → input.source()
- Line 63: input() → input.bool()
- Line 64: input() → input.bool()
- Line 66: when = longCondition → if block
- Line 67: when = exitLongCondition → if block
- Line 69: when = exitLongCondition → if block
- Line 70: when = longCondition → if block
⋯//heiking ashi calculation-UseHAcandles = input(false, title="Use Heikin Ashi Candles in Algo Calculations")+UseHAcandles = input.bool(false, title="Use Heikin Ashi Candles in Algo Calculations")////⋯//timecondition-fromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)+fromDay = input.int(defval = 1, title = "From Day", minval = 1, maxval = 31)-fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)+fromMonth = input.int(defval = 1, title = "From Month", minval = 1, maxval = 12)-fromYear = input(defval = 2020, title = "From Year", minval = 1970)+fromYear = input.int(defval = 2020, title = "From Year", minval = 1970) //monday and session ⋯// To Date Inputs-toDay = input(defval = 31, title = "To Day", minval = 1, maxval = 31)+toDay = input.int(defval = 31, title = "To Day", minval = 1, maxval = 31)-toMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)+toMonth = input.int(defval = 12, title = "To Month", minval = 1, maxval = 12)-toYear = input(defval = 2021, title = "To Year", minval = 1970)+toYear = input.int(defval = 2021, title = "To Year", minval = 1970)startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)⋯//ema data -- moving average-len = input(9, minval=1, title="Length")+len = input.int(9, minval=1, title="Length")-src = input(hl2, title="Source")+src = input.source(hl2, title="Source")out = ta.ema(src, len)⋯//automatization-longEntry= input(true)+longEntry= input.bool(true)-shortEntry=input(false)+shortEntry=input.bool(false)if(longEntry)- strategy.entry("long",strategy.long,when=longCondition)+ if longCondition+ strategy.entry("long",strategy.long)- strategy.close("long",when=exitLongCondition)+ if exitLongCondition+ strategy.close("long")if(shortEntry)- strategy.entry("short",strategy.short,when=exitLongCondition)+ if exitLongCondition+ strategy.entry("short",strategy.short)- strategy.close("short",when=longCondition)+ if longCondition+ strategy.close("short")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.
⋯// © SoftKill21-//@version=5+//@version=6strategy("MACD+EMA 4H STRATEGY", overlay=true)⋯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 |
|---|---|---|---|
| Use Heikin Ashi Candles in Algo Calculations | false | bool | haClose haOpen haHigh haLow |
| From Day | 1 | int · min 1 · max 31 | startDate |
| From Month | 1 | int · min 1 · max 12 | startDate |
| From Year | 2020 | int · min 1970 | startDate |
| To Day | 31 | int · min 1 · max 31 | finishDate |
| To Month | 12 | int · min 1 · max 12 | finishDate |
| To Year | 2021 | int · min 1970 | finishDate |
| Length | 9 | int · min 1 | out |
| Source | hl2 | source | out fast_ma slow_ma |
| Fast Length | 12 | int (written input.integer) | fast_ma |
| Slow Length | 26 | int (written input.integer) | slow_ma |
| Signal Smoothing | 9 | int (written input.integer) · min 1 · max 50 | signal |
| Simple MA (Oscillator) | false | bool | fast_ma slow_ma |
| Simple MA (Signal Line) | false | bool | signal |
| Long Entry (no label in the script) | true | bool | entry long close long line 65 |
| Short Entry (no label in the script) | false | bool | entry short close short line 68 |
What it calculates
- EMA (
ema,ta.emain v5)Source (hl2), Length (9)out · line 30 - SMA / EMA (
sma,ta.smain v5)Source (hl2), Fast Length (12)fast_ma · line 39 - SMA / EMA (
sma,ta.smain v5)Source (hl2), Slow Length (26)slow_ma · line 40 - SMA / EMA (
sma,ta.smain v5)macd, Signal Smoothing (9)signal · line 42
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 | longConditionlong and not longOpeneda | On | 66 |
| Close long | exitLongConditionlongOpeneda[1] and longExitSignala | On | 67 |
| Open short short | exitLongConditionlongOpeneda[1] and longExitSignala | On | 69 |
| Close short | longConditionlong and not longOpeneda | On | 70 |
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 data4 security call(s), none with lookahead on.
- 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 | 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 |
|---|---|---|---|---|---|
| MACD oscillator with EMA strategy 4H this page | — | v4 strategy | 0.0% | 0 | 0.0% |
| MACD Long Strat by TheGrindToday | ema sma | v4 strategy | +24.0% | 79 | 20.1% |
| Stock trending strategy by exlux99 | ema sma | v4 strategy | +35.3% | 21 | 14.7% |
| 4-Hour Stochastic EMA Trend by slymnturkoglu | ema sma | v4 strategy | No trades on this market | ||
| 4X EMA and volume strategy by SoftKill21 | ema sma | v4 strategy | +1.8% | 3 | 25.8% |
| Simple Momentum Strategy Based on SMA, EMA and Volume by slip_stream | sma ema | v4 strategy | +7.4% | 18 | 27.4% |
Where it runs
| Engine | Compiles | Runs | Matches TradingView |
|---|---|---|---|
| Zeal Terminal · OpenPineScript v2 | Yes 15 ms | With a workaround 1,000 bars in 56 ms | Not measured |
| PineTS | No v5 and v6 only | — | Not measured |
The engine's host API cannot yet declare a chart's symbol and timeframe, so this page sets them on the engine directly for each market.
“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/// © SoftKill21//@version=4strategy("MACD+EMA 4H STRATEGY", overlay=true)//heiking ashi calculationUseHAcandles = input(false, title="Use Heikin Ashi Candles in Algo Calculations")////// === /INPUTS ===// === BASE FUNCTIONS ===haClose = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, close) : closehaOpen = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, open) : openhaHigh = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, high) : highhaLow = UseHAcandles ? security(heikinashi(syminfo.tickerid), timeframe.period, low) : low//timeconditionfromDay = input(defval = 1, title = "From Day", minval = 1, maxval = 31)fromMonth = input(defval = 1, title = "From Month", minval = 1, maxval = 12)fromYear = input(defval = 2020, title = "From Year", minval = 1970) //monday and session // To Date InputstoDay = input(defval = 31, title = "To Day", minval = 1, maxval = 31)toMonth = input(defval = 12, title = "To Month", minval = 1, maxval = 12)toYear = input(defval = 2021, title = "To Year", minval = 1970)startDate = timestamp(fromYear, fromMonth, fromDay, 00, 00)finishDate = timestamp(toYear, toMonth, toDay, 00, 00)time_cond = time >= startDate and time <= finishDate//ema data -- moving averagelen = input(9, minval=1, title="Length")src = input(hl2, title="Source")out = ema(src, len)//plot(out, title="EMA", color=color.blue)//histogramfast_length = input(title="Fast Length", type=input.integer, defval=12)slow_length = input(title="Slow Length", type=input.integer, defval=26)signal_length = input(title="Signal Smoothing", type=input.integer, minval = 1, maxval = 50, defval = 9)sma_source = input(title="Simple MA (Oscillator)", type=input.bool, defval=false)sma_signal = input(title="Simple MA (Signal Line)", type=input.bool, defval=false)// Calculatingfast_ma = sma_source ? sma(src, fast_length) : ema(src, fast_length)slow_ma = sma_source ? sma(src, slow_length) : ema(src, slow_length)macd = fast_ma - slow_masignal = sma_signal ? sma(macd, signal_length) : ema(macd, signal_length)hist = macd - signal//main variables to apply conditions are going to be out(moving avg) and hist(macd)long = haClose > out and haClose > haClose[1] and out > out[1] and hist> 0 and hist[1] < 0 and time_condshort = haClose < out and haClose < haClose[1] and out < out[1] and hist < 0 and hist[1] > 0 and time_cond//limit to 1 entryvar longOpeneda = falsevar shortOpeneda = falsevar int timeOfBuya = nalongCondition= long and not longOpeneda if longCondition longOpeneda := true timeOfBuya := timelongExitSignala = shortexitLongCondition = longOpeneda[1] and longExitSignalaif exitLongCondition longOpeneda := false timeOfBuya := naplotshape(longCondition, style=shape.labelup, location=location.belowbar, color=color.green, size=size.tiny, title="BUY", text="BUY", textcolor=color.white)plotshape(exitLongCondition, style=shape.labeldown, location=location.abovebar, color=color.red, size=size.tiny, title="SELL", text="SELL", textcolor=color.white)//automatizationlongEntry= input(true)shortEntry=input(false)if(longEntry) strategy.entry("long",strategy.long,when=longCondition) strategy.close("long",when=exitLongCondition)if(shortEntry) strategy.entry("short",strategy.short,when=exitLongCondition) strategy.close("short",when=longCondition)