change MACD indicator color (long/short) for candles

 

Hello,

just started learning MT4 programming. Would be great if somebody have any idea how to change the color of MACD indicator whilst runtime.

If MACD above Signal Line = MACD for this bar in blue (input color MacdLong) and if MACD below Signal Line = MACD for this bar in red (input color MacdShort).

Below reduced standard MACD from library

Thank you

Mike

#property strict
#include <MovingAverages.mqh>
//--- indicator settings
#property   indicator_separate_window
#property   indicator_buffers 2
#property   indicator_color1  clrGray //MACD Monochrome
#property   indicator_color2  clrBlack //Signal Line
#property   indicator_level1 0.0
#property   indicator_levelcolor   clrBlack
//--- indicator parameters
input int InpFastEMA=5;   // Fast EMA Period
input int InpSlowEMA=34;   // Slow EMA Period
input int InpSignalSMA=20;  // Signal SMA Period
input color MacdLong = clrBlue; // MACD > Signal Line
input color MacdShort = clrRed; // MACD < Signal Line
//--- indicator buffers
double   ExtMacdBuffer[];
double   ExtSignalBuffer[];
//--- right input parameters flag
bool      ExtParameters=false;
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit(void)
  {
   IndicatorDigits(Digits+1);
//--- drawing settings
   SetIndexStyle(0,DRAW_HISTOGRAM);
   SetIndexStyle(1,DRAW_LINE);
   SetIndexDrawBegin(1,InpSignalSMA);
//--- indicator buffers mapping
   SetIndexBuffer(0,ExtMacdBuffer);
   SetIndexBuffer(1,ExtSignalBuffer);
//--- name for DataWindow and indicator subwindow label
   IndicatorShortName("MACD("+IntegerToString(InpFastEMA)+","+IntegerToString(InpSlowEMA)+","+IntegerToString(InpSignalSMA)+")");
   SetIndexLabel(0,"MACD");
   SetIndexLabel(1,"Signal Line");
//--- check for input parameters
   if(InpFastEMA<=1 || InpSlowEMA<=1 || InpSignalSMA<=1 || InpFastEMA>=InpSlowEMA)
     {
      Print("Wrong input parameters");
      ExtParameters=false;
      return(INIT_FAILED);
     }
   else
      ExtParameters=true;
//--- initialization done
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence                           |
//+------------------------------------------------------------------+
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 i,limit;
//---
   if(rates_total<=InpSignalSMA || !ExtParameters)
      return(0);
//--- last counted bar will be recounted
   limit=rates_total-prev_calculated;
   if(prev_calculated>0)
      limit++;
//--- macd counted in 1-st buffer
   for(i=0; i<limit; i++)
      ExtMacdBuffer[i]=iMA(NULL,0,InpFastEMA,0,MODE_EMA,PRICE_CLOSE,i)-
                    iMA(NULL,0,InpSlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
//--- signal line counted in 2-nd buffer
      SimpleMAOnBuffer(rates_total,prev_calculated,0,InpSignalSMA,ExtMacdBuffer,ExtSignalBuffer);
   return(rates_total);
  }
//+------------------------------------------------------------------+
 
Mike Redmer: Would be great if somebody have any idea how to change the color 
  1. There are many people here that can.
         How To Ask Questions The Smart Way. 2004
              Only ask questions with yes/no answers if you want "yes" or "no" as the answer.

  2. Same as any colored line indicator. Look in the codebase. If you assign to one color buffer, make the other color buffer EMPTY_VALUE.
              HOW CAN I hide CONNECTION lines of plots? (ttt) - MQL4 programming forum 2016.07.09

    For MT4 I use:

    1. One buffer has the value, color set to CLR_NONE so not shown on chart, (but in data window and pop up.)
    2. Two buffers, one color each, with SetIndexLabel(i, NULL) so they don't show in data window.
    3. Then you need to connect the lines on color change. downBuffer[i]=value[i]; if(downBuffer[i+1]==EMPTY_VALUE) downBuffer[i+1]=value[i].