OPEN-SOURCE SCRIPT

Darren - Engulfing + MACD Cross

209
Darren – Engulfing + MACD Cross

Overall Behavior
  1. Identify an engulfing candle (bullish or bearish).
  2. Wait up to windowBars bars for the corresponding MACD crossover (bullish engulfing → MACD cross up; bearish engulfing → MACD cross down).
  3. If the crossover occurs within that window, trigger an entry (long or short) and close any opposite open trade.


Inputs
• macdFast (default 12): length of MACD fast EMA
• macdSlow (default 26): length of MACD slow EMA
• macdSignal (default 9): length of MACD signal line
• windowBars (default 3): maximum bars allowed between an engulfing candle and a MACD crossover

Indicators
• macdLine and signalLine are calculated using ta.macd(close, macdFast, macdSlow, macdSignal)
• macdHist = macdLine – signalLine, plotted as columns (green when ≥ 0, red when < 0)

Engulfing Pattern Detection
• Bullish engulfing (bullEngulfing) is true when the previous candle is bearish (close[1] < open[1]), the current candle is bullish (close > open), and the current body fully engulfs the previous body (open < close[1] and close > open[1]).
• Bearish engulfing (bearEngulfing) is the inverse: previous candle bullish, current candle bearish, and current body fully engulfs the prior body.

MACD Crossover Detection
• macdCrossUp is true when macdLine crosses above signalLine.
• macdCrossDown is true when macdLine crosses below signalLine.

Timing Logic
• barsSinceBull = ta.barssince(bullEngulfing) returns number of bars since the last bullish engulfing.
• barsSinceBear = ta.barssince(bearEngulfing) returns number of bars since the last bearish engulfing.

• longCondition occurs if a MACD cross up happens within windowBars bars of a bullish engulfing (barsSinceBull ≤ windowBars and macdCrossUp).
• shortCondition occurs if a MACD cross down happens within windowBars bars of a bearish engulfing (barsSinceBear ≤ windowBars and macdCrossDown).

Chart Markers
• “Bull” label below bar whenever bullEngulfing is true.
• “Bear” label above bar whenever bearEngulfing is true.
• Small “Up” ▲ below bar when macdCrossUp is true.
• Small “Down” ▼ above bar when macdCrossDown is true.
• Triangle ▲ below bar for Long Entry (longCondition).
• Triangle ▼ above bar for Short Entry (shortCondition).

Entry & Exit Rules
• On longCondition: enter “Long”, and close any existing “Short” position.
• On shortCondition: enter “Short”, and close any existing “Long” position.

Penafian

Maklumat dan penerbitan adalah tidak dimaksudkan untuk menjadi, dan tidak membentuk, nasihat untuk kewangan, pelaburan, perdagangan dan jenis-jenis lain atau cadangan yang dibekalkan atau disahkan oleh TradingView. Baca dengan lebih lanjut di Terma Penggunaan.