Best way to code a Consolidation/Noise Filter in MQL5

 

Hi everyone,

I'm currently working on a trend-following EA, and like many of you, I'm struggling with false breakouts during market consolidation (flat markets).

To solve this, I coded a simple filter that checks the dynamic spread between two Moving Averages and ensures the current candle body size is larger than the average body size of the last 10 candles.

Here is the basic logic I am using to filter out the noise:

// Simple check to ensure we are not in a tight flat zone
bool IsMarketActive(string symbol, ENUM_TIMEFRAMES period)
{
    double maFast = iMA(symbol, period, 10, 0, MODE_EMA, PRICE_CLOSE);
    double maSlow = iMA(symbol, period, 50, 0, MODE_EMA, PRICE_CLOSE);
    
    // Calculate the distance between MAs in points
    double maDistance = MathAbs(maFast - maSlow) / SymbolInfoDouble(symbol, POINT);
    
    // Minimum distance required to consider it a trend (e.g., 150 points)
    if(maDistance < 150) 
    {
        return false; // Market is flat
    }
    
    return true; // Trend is active
}


  • My questions for the experts here:

    1. Do you think relying on MA distance is reliable enough for higher timeframes (like H1/H4)?

    2. Would it be more efficient to replace this with an ATR-based filter or candle body ratios (like looking for strong solid candles instead of dojis)?

    Looking forward to your suggestions and code improvements!


 
Sami Eid Fahid Almashaqbeh:

Hi everyone,


IMO once you have an ok system to detect false breakouts, then it is best to decide after the trade is opened -- to close with small loss OR to run with the profits. If your breakout system is reliable, then it will always recover the losses from those false breakouts.

Once you question the strategy and start to add filter after filter, you are heading down the path which leads to curve fitting.