MACD ultimate with EMA override

This Pine Script v5 indicator combines MACD zero-cross signals, SuperTrend trend validation, an EMA(50/200) trend filter and an EMA-crossover override to produce clean, session-constrained entry signals and robust exit logic. It draws labels and lines on the chart (entries, exits, SL lines) and supports alerts. Stop-losses use percentage-based sizing and are evaluated on bar close only to avoid intrabar noise.
Key features
Primary entry rule (MACD zero-cross):
Buy when MACD line crosses above zero (current bar MACD > 0 and previous bar MACD < 0).
Sell when MACD line crosses below zero (current bar MACD < 0 and previous bar MACD > 0).
Session-only entries: Entries are generated only inside a user-defined session (e.g., 09:30-11:30). Exits are evaluated at all times.
SuperTrend validation: Optional SuperTrend filter for entries and exits. Can be configured so exits require both MACD exit and SuperTrend flip (AND mode) or use OR mode.
EMA trend filter for entries: Optional EMA(50) vs EMA(200) filter — when enabled the indicator will only open buys in EMA-up trend and sells in EMA-down trend.
EMA crossover override (priority rule): If EMA fast crosses the slow:
EMA50 crosses above EMA200 → forced BUY override (bypasses session, SuperTrend, MACD). Exits any active short and opens long.
EMA50 crosses below EMA200 → forced SELL override (bypasses other validations). Exits any active long and opens short.
Overrides respect same-direction protection (won’t reopen an existing same-side position).
Opposite-entry immediate exit: When an opposite-direction raw entry (MACD zero-cross) occurs, any active opposite trade is exited immediately (then the script may open the opposite entry subject to entry validation). Same-direction repeated signals do not force an exit.
Stop-Loss (percentage): Parameterized SL (%) applied at entry; SL is checked and triggered only on bar close (e.g., long SL triggers if barstate.isconfirmed and close <= SL).
Labels & SL lines: Single-line, non-repainting labels for entries/exits; SL horizontal line drawn on open positions and greys out after closing.
Plots & visuals:
MACD panel (histogram, MACD, signal) optional.
SuperTrend plotted as a single color-coded line: green for bullish, red for bearish (no dots).
Optional EMA( fast / slow ) plots.
Entry markers (triangles) shown only for session-filtered entries.
Alerts: Entry and exit alerts are included and can be toggled on/off.
Inputs (high level)
MACD: fast, slow, signal lengths.
SL (%) and toggle to enable/disable SL.
SuperTrend: ATR length, multiplier; toggles: require for entry, allow/require for exit, show/hide.
EMA trend: enable/disable filter; fast/slow lengths; show/hide EMAs.
EMA override (built-in) — crossover detection triggers forced entry/exit.
Session: time range (HHMM-HHMM) — applies to entry generation only.
Misc: allow multiple entries flag, enable alerts, show/hide MACD panel.
Behavioral notes & caveats
The indicator is an overlay indicator (not a strategy()), so it draws visual signals and alerts but does not place real trades — use strategy() conversion to backtest trade P&L.
EMA override bypasses all validations by design — it forcibly exits the opposite side and opens the override side immediately (on the same bar). This is intentional to capture major trend flips.
SL is checked on bar close only. That reduces false SL triggers from intrabar spikes but means realized fills can differ in live trading depending on execution and slippage.
Opposite-entry exits are immediate (no SuperTrend/MACD requirement) except when a crossover override is the cause — the script guards so EMA overrides take precedence.
Pine Script runs on bar close for most accurate signals; intrabar behavior depends on your chart settings (realtime vs historical) — expect small differences between indicator labels and broker fills.
Plot/label density: many labels and SL lines can clutter the chart on lower timeframes. Consider hiding SL lines after N bars (optional enhancement) or use higher timeframe charts for less clutter.
Suggested default settings
MACD: 12, 26, 9
SL: 1.0 (%) with Use SL = on
SuperTrend: ATR 10, Multiplier 3.0, require for entry = true, require for exit = true (AND mode)
EMA trend filter: enabled (50/200)
Session: 0930-1130 (adjust to your exchange/timezone)
Alerts: on
How to use
Paste the full Pine v5 script into TradingView’s Pine Editor and add to chart.
Set the trade_session to the market hours you want entries in (chart timezone should match your intended exchange).
Toggle Use EMA trend / Require SuperTrend / Require ST for exit depending on how tight you want validation.
Use strategy() conversion before backtesting to verify the rules produce acceptable historical returns (indicator-only won’t generate P&L).
Recommended next steps
Convert to a strategy() script to backtest and measure win rate, drawdown, profit factor, and to validate the SL-on-close logic with realistic fills.
Add an input to auto-hide SL lines after N bars or compress labels to a compact trade status box.
Consider adding ATR- or volatility-based SL as an alternative to percentage SL.
Skrip hanya-undangan
Hanya pengguna yang disetujui oleh penulis yang dapat mengakses skrip ini. Anda perlu meminta dan mendapatkan izin untuk menggunakannya. Izin ini biasanya diberikan setelah pembayaran. Untuk detail selengkapnya, ikuti petunjuk penulis di bawah ini atau hubungi ebi1405 secara langsung.
TradingView TIDAK menyarankan untuk membayar atau menggunakan skrip kecuali Anda sepenuhnya mempercayai pembuatnya dan memahami cara kerjanya. Anda juga dapat menemukan alternatif yang gratis dan sumber terbuka di skrip komunitas kami.
Instruksi penulis
Pernyataan Penyangkalan
Skrip hanya-undangan
Hanya pengguna yang disetujui oleh penulis yang dapat mengakses skrip ini. Anda perlu meminta dan mendapatkan izin untuk menggunakannya. Izin ini biasanya diberikan setelah pembayaran. Untuk detail selengkapnya, ikuti petunjuk penulis di bawah ini atau hubungi ebi1405 secara langsung.
TradingView TIDAK menyarankan untuk membayar atau menggunakan skrip kecuali Anda sepenuhnya mempercayai pembuatnya dan memahami cara kerjanya. Anda juga dapat menemukan alternatif yang gratis dan sumber terbuka di skrip komunitas kami.