iMACD_Advanced_Histo_v1.0

Download script
Author: Copyright 2019, Nikolaos Pantzos

This script is designed as a visual technical analysis tool for the MetaTrader platform. It creates a customized histogram that identifies and highlights market trends based on specific price momentum dynamics.

Core Functionality

The logic revolves around comparing a standard MACD (Moving Average Convergence Divergence) calculation against an Average MACD value. By doing this, the script attempts to filter out "noise" and focus on significant trend shifts rather than minor price fluctuations.

How it Works

  • Dual Indicator Comparison: The script calculates two different types of data simultaneously. One represents the current momentum (the active signal), while the other represents a smoothed average of that same momentum over a larger period.
  • Threshold Filtering: Instead of reacting to every tiny movement, the script uses "Percentage Levels" (user-adjustable settings). It only registers a change in trend if the current indicator value moves significantly higher or lower than the average value by a specific percentage.
    • If the price momentum drops below the negative threshold, it identifies a Downtrend.
    • If the price momentum rises above the positive threshold, it identifies an Uptrend.
  • Visual Feedback: The script then translates these mathematical findings into a visual histogram on your chart:
    • UpTrend Buffer: This highlights instances where the trend is moving upward.
    • DnBuffer (DownTrend): This highlights instances where the trend is moving downward.

Key Features for the User

  • Customizable Sensitivity: You can adjust the "Fast," "Slow," and "Signal" periods to change how quickly the indicator reacts to market changes.
  • Volatility Protection: By using percentage-based levels, the tool helps traders avoid "whipsaws"—situations where the market moves slightly back and forth without establishing a clear direction.
  • Visual Clarity: The script separates these trends into distinct visual buffers (UpTrend vs. DownTrend), making it easier to see at a glance which way the broader momentum is leaning.
Indicators Used
MACD Histogram
Miscellaneous
Implements a curve of type %1
15 Views
0 Downloads
0 Favorites
iMACD_Advanced_Histo_v1.0
ÿþ//======================================================================================================================================================//

#property copyright "Copyright 2019, Nikolaos Pantzos"

#property link      "https://www.mql5.com/en/users/pannik"

#property version   "1.0"

#property strict

//======================================================================================================================================================//

#property indicator_separate_window

#property indicator_buffers 2

#property indicator_color1 clrDodgerBlue

#property indicator_color2 clrRed

//======================================================================================================================================================//

extern int FastPeriod=12;

extern int SlowPeriod=26;

extern int SignalPeriod=9;

extern ENUM_APPLIED_PRICE AppliedPrice=PRICE_CLOSE;

extern int BarsForAveragePrice=140;

extern double PercentageLevelUp=25;

extern double PercentageLevelDn=25;

//======================================================================================================================================================//

double UpBuffer[];

double DnBuffer[];

//======================================================================================================================================================//

int OnInit(void)

  {

//--------------------------------------------------------------------------------

   string iName="iMACD_Histo("+IntegerToString(FastPeriod)+","+IntegerToString(SlowPeriod)+","+IntegerToString(SignalPeriod)+")";

//--------------------------------------------------------------------------------

   IndicatorShortName(iName);

   IndicatorDigits((int)MarketInfo(Symbol(),MODE_DIGITS));

//--------------------------------------------------------------------------------

   SetIndexStyle(0,DRAW_HISTOGRAM,STYLE_SOLID,3);

   SetIndexStyle(1,DRAW_HISTOGRAM,STYLE_SOLID,3);

   SetIndexBuffer(0,UpBuffer);

   SetIndexBuffer(1,DnBuffer);

//--------------------------------------------------------------------------------

   SetIndexLabel(0,"UpTrend");

   SetIndexLabel(1,"DownTrend");

//--------------------------------------------------------------------------------

   SetIndexDrawBegin(0,SlowPeriod);

   SetIndexDrawBegin(1,SlowPeriod);

//--------------------------------------------------------------------------------

   return(INIT_SUCCEEDED);

//--------------------------------------------------------------------------------

  }

//======================================================================================================================================================//

int OnCalculate(const int rates_total,

                const int prev_calculated,

                const datetime &time[],

                const double &open[],

                const double &high[],

                const double &low[],

                const double &close[],

                const long &tick_volume[],

                const long &volume[],

                const int &spread[])

  {

//--------------------------------------------------------------------------------

   int IndicatorShift=0;

   int IndicatorTrend=0;

   double IndicatorValue=0;

   double AvrgValue=0;

//--------------------------------------------------------------------------------

   for(IndicatorShift=Bars-SlowPeriod-1; IndicatorShift>=0; IndicatorShift--)

     {

      AvrgValue=iMACD(NULL,0,FastPeriod,BarsForAveragePrice,SignalPeriod,AppliedPrice,MODE_MAIN,IndicatorShift);

      IndicatorValue=iMACD(NULL,0,FastPeriod,SlowPeriod,SignalPeriod,AppliedPrice,MODE_MAIN,IndicatorShift);

      //---

      if(IndicatorValue>AvrgValue+((AvrgValue*PercentageLevelDn)/100))

         IndicatorTrend=-1;

      if(IndicatorValue<AvrgValue-((AvrgValue*PercentageLevelUp)/100))

         IndicatorTrend=1;

      //---

      if(IndicatorTrend>0)

        {

         if(IndicatorValue>AvrgValue-((AvrgValue*PercentageLevelUp)/100)/2)

            UpBuffer[IndicatorShift]=1.0;

         else

            UpBuffer[IndicatorShift]=1.0;

         DnBuffer[IndicatorShift]=0;

        }

      //---

      if(IndicatorTrend<0)

        {

         if(IndicatorValue<AvrgValue+((AvrgValue*PercentageLevelDn)/100)/2)

            DnBuffer[IndicatorShift]=1.0;

         else

            DnBuffer[IndicatorShift]=1.0;

         UpBuffer[IndicatorShift]=0;

        }

     }

//-----------------------------------------------------------------------------------

   return(rates_total);

//-----------------------------------------------------------------------------------

  }

//======================================================================================================================================================//

Comments