MP Divergence Never Lie
- Experten
- Alexandru Chirila
- Version: 1.0
- Aktivierungen: 10
MACD Divergence Expert Advisor (EA)
Overview
The MACD Divergence Expert Advisor (EA) automates the MACD Divergence Trading Strategy, identifying and trading both the validation and invalidation of divergence patterns. The EA utilizes the Moving Average Convergence Divergence (MACD) indicator to detect price-momentum discrepancies, providing early signals for potential trend reversals.
With customizable thresholds, trendline visualization, flexible entry mechanisms, and risk management features, this EA executes trades based on bullish and bearish MACD divergences while allowing for precise risk allocation and automated position sizing based on a fixed risk amount.
MACD Divergence Trading Strategy
1. Understanding Divergence
Divergence occurs when price action and the MACD histogram or signal line move in opposite directions, signaling a possible trend reversal:
-
Bullish Divergence:
-
Price forms a lower low, but the MACD forms a higher low.
-
This suggests weakening downward momentum and a potential bullish reversal.
-
Bearish Divergence:
-
Price forms a higher high, but the MACD forms a lower high.
-
This suggests weakening upward momentum and a potential bearish reversal.
2. Trading the Divergence and Its Invalidation
The EA trades both the validation and invalidation of divergence patterns:
-
Validation:
-
A bearish divergence triggers a sell trade when price breaks the previous low.
-
A bullish divergence triggers a buy trade when price breaks the previous high.
-
Invalidation:
-
A bearish divergence is invalidated if price breaks the previous high, triggering a buy trade.
-
A bullish divergence is invalidated if price breaks the previous low, triggering a sell trade.
This ensures that the EA adapts dynamically to both divergence confirmations and their potential failures, capturing profitable opportunities in both cases.
EA Input Parameters
1. General Trading Parameters
-
EntryTimeframe (ENUM_TIMEFRAMES) – Defines the candle close timeframe for trade execution.
-
InvalidationEntry (bool) – If enabled, the EA trades both the validation and invalidation of a divergence:
-
For bearish divergence: Executes a sell when price breaks the previous low and a buy if price breaks the previous high.
-
For bullish divergence: Executes a buy when price breaks the previous high and a sell if price breaks the previous low.
-
Magic (int) – Unique magic number to differentiate EA trades from other trades.
2. Risk Management Parameters
-
RiskAmount (int) – The amount of risk per trade, defined in account currency (cash).
-
The EA calculates position size based on this risk amount.
-
RiskReward (double) – The risk-reward ratio of the trade.
-
Trade Execution Logic:
-
For bearish divergence:
-
Entry: Previous low breakout
-
Stop Loss (SL): Previous high
-
Take Profit (TP): Based on RiskReward ratio (e.g., if Risk = 100 pips and RR = 2, TP = 200 pips).
-
For bullish divergence:
-
Entry: Previous high breakout
-
Stop Loss (SL): Previous low
-
Take Profit (TP): Based on RiskReward ratio.
3. MACD Calculation Parameters
-
FastEMA (int) – The period of the fast-moving average in MACD calculation.
-
SlowEMA (int) – The period of the slow-moving average in MACD calculation.
-
SignalEMA (int) – The period of the signal line in MACD calculation.
-
AppliedPrice (ENUM_APPLIED_PRICE) – The applied price for calculating MACD (e.g., Close, Open, High, Low, etc.).
4. Divergence Identification Parameters
-
HighLowThreshold (int) – Defines how many candles are required for a high/low to be considered significant on price.
-
If no valid high/low is found, the threshold is gradually reduced to identify the most relevant extremum.
-
DivergenceThreshold (int) – Defines how many candles are required for a high/low to be considered significant on MACD.
-
Unlike the HighLowThreshold, this threshold is strictly enforced.
-
DivergenceInvalidationThreshold (double) – A filter to determine when a divergence is invalidated:
-
Example: If set to 0.01, a bearish divergence is invalidated if the MACD signal line touches 0.01.
-
If the MACD signal line touches -0.01, a bullish divergence is invalidated.
5. Visualization & Alerts
-
DrawPriceTrendLines (int) – Enables drawing divergence lines on price to visually highlight divergence setups.
-
DrawIndicatorTrendLines (int) – Enables drawing divergence lines on MACD to confirm divergence signals.
-
DrawIndicatorEntryLines (int) – Enables drawing entry confirmation lines based on divergence signals.
-
DisplayAlert (int) – Placeholder for alert functionality (not applicable in this version).
How the EA Works
-
Detect Divergence:
-
The EA scans price and MACD to identify valid bullish or bearish divergences based on user-defined thresholds.
-
Validate Entry Conditions:
-
The EA enters a trade when the divergence is confirmed OR when it is invalidated.
-
Calculate Risk & Position Size:
-
The EA calculates the lot size based on the RiskAmount and RiskReward ratio.
-
Set Stop Loss and Take Profit:
-
SL and TP are set based on previous price extremes and RiskReward.
-
Draw Visual Aids:
-
The EA draws trendlines on the price chart and MACD indicator to help visualize detected divergences.
-
Trade Execution & Management:
-
Trades are placed based on divergence validation or invalidation, with automated SL and TP placement.
This EA combines the power of the MACD Divergence Indicator with robust automation and risk control, offering a reliable and systematic approach to trading divergences.