MACD_FlatMarketDetector

Download script
Author: Copyright � Pointzero-indicator.com
MACD_FlatMarketDetector

The MACD Flat Market Detector is a specialized technical analysis tool designed to help traders identify periods of market consolidation and measure the intensity of price momentum. Unlike standard MACD indicators that focus solely on trend direction, this script adds a layer of logic to detect when the market is "flattening out" or moving with low volatility.

How the Logic Works

The indicator operates through a multi-step analytical process to filter out market noise and highlight specific price behaviors:

1. Momentum Calculation The tool begins by analyzing the MACD (Moving Average Convergence Divergence) based on two distinct timeframes (Fast and Slow). It calculates the difference between the current price movement and the previous movement. By looking at these changes, the script establishes a baseline for how much "velocity" the market currently possesses.

2. Volatility & Flatness Detection To determine if the market is in a "flat" state, the script evaluates the absolute magnitude of these momentum changes over a long historical window (the Average Period). It calculates an average trendline of this momentum. By applying a specific multiplier (Gamma), it creates a visual boundary. When the actual market activity falls within or near these boundaries, it indicates that the price is oscillating without significant growth—essentially identifying a "flat" market.

3. Acceleration vs. Deceleration The script further distinguishes between different types of market movement by comparing the current momentum value against the previous one:

  • Acceleration: When the current momentum is higher than the previous bar, the tool identifies this as increasing strength and highlights it with a specific visual cue (Blue).
  • Deceleration: When the current momentum is lower than the previous bar, the tool identifies this as weakening strength and highlights it with a different visual cue (Red).

Key Features for Traders

  • Visual Clarity: The script uses color-coded histograms and lines to separate accelerating trends from decelerating ones, allowing you to see at a glance whether a move is gaining steam or losing energy.
  • Consolidation Alerts: By identifying "flat" zones, the tool helps traders avoid entering trades during sideways markets where price action may be stagnant and unprofitable.
  • Real-Time Filtering: The logic can be configured to update only when a candle closes, ensuring that your analysis remains stable and isn't affected by temporary price flickers during a live trading session.
Indicators Used
MACD HistogramMoving average indicator
Miscellaneous
Implements a curve of type %1
11 Views
0 Downloads
0 Favorites
MACD_FlatMarketDetector
//+------------------------------------------------------------------+
//| MACD_FlatMarketDetector.mq4
//| Copyright http://www.pointzero-indicator.com
//+------------------------------------------------------------------+
#property copyright "Copyright © Pointzero-indicator.com"
#property link      "http://www.pointzero-indicator.com"

//---- Properties
#property indicator_separate_window
#property indicator_buffers 4
#property indicator_color1 DodgerBlue
#property indicator_color2 Red
#property indicator_color3 DarkSlateGray
#property indicator_color4 DarkSlateGray
#property indicator_width1 2
#property indicator_width2 2
#property indicator_width3 1
#property indicator_width4 1
#property indicator_style3 STYLE_DOT
#property indicator_style4 STYLE_DOT

//---- External variables
extern bool   CalculateOnBarClose = true;
extern int    FastEMA             = 9;
extern int    SlowEMA             = 26;
extern int    AvPeriod            = 400;
extern double Gamma               = 0.75;

//---- Buffers
double FextMapBuffer1[];
double FextMapBuffer2[];
double FextMapBuffer3[];
double FextMapBuffer4[];
double FextMapBuffer5[];
double FextMapBuffer6[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//|------------------------------------------------------------------|
int init()
{
   // Total buffers
   IndicatorBuffers(6);
   
   // Visible buffers
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexBuffer(0, FextMapBuffer1);
   SetIndexStyle(1,DRAW_HISTOGRAM);
   SetIndexBuffer(1, FextMapBuffer2);
   SetIndexStyle(2,DRAW_LINE);
   SetIndexBuffer(2,FextMapBuffer3);    
   SetIndexStyle(3,DRAW_LINE);
   SetIndexBuffer(3,FextMapBuffer4); 
   
   // Internal  
   SetIndexBuffer(4,FextMapBuffer5);  
   SetIndexBuffer(5,FextMapBuffer6);       
   
   // My name
   IndicatorShortName("MACD + Flat Market Detector ("+ FastEMA +","+ SlowEMA +")");
   Comment("Copyright © http://www.pointzero-indicator.com");
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
{
   return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
   // Start, limit1, etc..
   int start = 0;
   int limit;
   int counted_bars = IndicatorCounted();
   
   // nothing else to do?
   if(counted_bars < 0) 
       return(-1);

   // do not check repeated bars
   limit = Bars - 1 - counted_bars;
   
   // Check if ignore bar 0
   if(CalculateOnBarClose == true) { start = 1; }
   
   // Iteration from past to present
   for(int pos = limit; pos >= start; pos--)
   {  
      // Median
      double median  = iMACD(NULL,0, FastEMA, SlowEMA, 4, PRICE_MEDIAN, MODE_MAIN, pos);
      double median1 = iMACD(NULL,0, SlowEMA, SlowEMA, 4, PRICE_MEDIAN, MODE_MAIN, pos+1);
     
      // Difference
      FextMapBuffer6[pos] = median - median1;
      
      // Save current absolute value
      FextMapBuffer5[pos] = MathAbs(FextMapBuffer6[pos]);
   }
      
   for(pos = limit; pos >= start; pos--)
   {  
      // Flat lines
      double flat = iMAOnArray(FextMapBuffer5, Bars, AvPeriod, 0, MODE_EMA, pos);
      FextMapBuffer3[pos] = (flat * Gamma);
      FextMapBuffer4[pos] = 0 - (flat * Gamma);
      
      //---- Deceleration
      if(FextMapBuffer6[pos] < FextMapBuffer6[pos+1])
      {
         // This is a red line
         FextMapBuffer2[pos] = FextMapBuffer6[pos];
         FextMapBuffer1[pos] = 0;
         
      //---- Acceleration
      } else if(FextMapBuffer6[pos] > FextMapBuffer6[pos+1]){
         
         // This is a blue line
         FextMapBuffer1[pos] = FextMapBuffer6[pos];
         FextMapBuffer2[pos] = 0;
      }
   }
   return(0);
}

Comments