I have this classical MACD with histogram where the histogram gets drawn in front of the MACD and Signal lines. Could someone please show me how to set the Histogram in the back? I have tried to figure it out, but without success.
Thanks,
Laurus
Edit: Got help. Had to switch the order of the buffers . Have attached the indicator file with the histogram in the back if someone would like to have it.
//+---------------------------------------------------------------------------+
//| _MACD.mq4 |
//| |
//| The Classical MACD with Histogram |
//+---------------------------------------------------------------------------+
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Black
#property indicator_color2 Red
#property indicator_color3 SteelBlue
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalEMA=9;
//---- indicator buffers
double MACD_Buffer[];
double SIGNAL_Buffer[];
double HISTOGRAM_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexDrawBegin(1,SignalEMA);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,MACD_Buffer);
SetIndexBuffer(1,SIGNAL_Buffer);
SetIndexBuffer(2,HISTOGRAM_Buffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("_MACD ("+FastEMA+","+SlowEMA+","+SignalEMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
SetIndexLabel(2,"Histogram");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start() {
int i;
int limit;
int counted_bars = IndicatorCounted();
if(counted_bars < 0) return(-1); //---- check for possible errors
if(counted_bars > 0) counted_bars--; //---- the last counted bar will be recounted
limit = Bars - counted_bars;
//---- Draw MACD line
for(i=0; i<limit; i++) { //-- loop from the current bar to the first bar
MACD_Buffer[i] = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
}
//---- Draw Signal line and Histogram
for(i=0; i<limit; i++) { //-- same loop above
SIGNAL_Buffer[i] = iMAOnArray(MACD_Buffer,Bars,SignalEMA,0,MODE_EMA,i );
HISTOGRAM_Buffer[i] = MACD_Buffer[i] - SIGNAL_Buffer[i];
}
//---- Done
return(0);
}
//+-----------------------------------------------------------------+
Thanks,
Laurus
Edit: Got help. Had to switch the order of the buffers . Have attached the indicator file with the histogram in the back if someone would like to have it.
//+---------------------------------------------------------------------------+
//| _MACD.mq4 |
//| |
//| The Classical MACD with Histogram |
//+---------------------------------------------------------------------------+
//---- indicator settings
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_color1 Black
#property indicator_color2 Red
#property indicator_color3 SteelBlue
#property indicator_width1 1
#property indicator_width2 1
#property indicator_width3 2
//---- indicator parameters
extern int FastEMA=12;
extern int SlowEMA=26;
extern int SignalEMA=9;
//---- indicator buffers
double MACD_Buffer[];
double SIGNAL_Buffer[];
double HISTOGRAM_Buffer[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function |
//+------------------------------------------------------------------+
int init() {
//---- drawing settings
SetIndexStyle(0,DRAW_LINE);
SetIndexStyle(1,DRAW_LINE);
SetIndexStyle(2,DRAW_HISTOGRAM);
SetIndexDrawBegin(1,SignalEMA);
IndicatorDigits(Digits+1);
//---- indicator buffers mapping
SetIndexBuffer(0,MACD_Buffer);
SetIndexBuffer(1,SIGNAL_Buffer);
SetIndexBuffer(2,HISTOGRAM_Buffer);
//---- name for DataWindow and indicator subwindow label
IndicatorShortName("_MACD ("+FastEMA+","+SlowEMA+","+SignalEMA+")");
SetIndexLabel(0,"MACD");
SetIndexLabel(1,"Signal");
SetIndexLabel(2,"Histogram");
//---- initialization done
return(0);
}
//+------------------------------------------------------------------+
//| Moving Averages Convergence/Divergence |
//+------------------------------------------------------------------+
int start() {
int i;
int limit;
int counted_bars = IndicatorCounted();
if(counted_bars < 0) return(-1); //---- check for possible errors
if(counted_bars > 0) counted_bars--; //---- the last counted bar will be recounted
limit = Bars - counted_bars;
//---- Draw MACD line
for(i=0; i<limit; i++) { //-- loop from the current bar to the first bar
MACD_Buffer[i] = iMA(NULL,0,FastEMA,0,MODE_EMA,PRICE_CLOSE,i) - iMA(NULL,0,SlowEMA,0,MODE_EMA,PRICE_CLOSE,i);
}
//---- Draw Signal line and Histogram
for(i=0; i<limit; i++) { //-- same loop above
SIGNAL_Buffer[i] = iMAOnArray(MACD_Buffer,Bars,SignalEMA,0,MODE_EMA,i );
HISTOGRAM_Buffer[i] = MACD_Buffer[i] - SIGNAL_Buffer[i];
}
//---- Done
return(0);
}
//+-----------------------------------------------------------------+
Attached File(s)
_MACD.mq4
3 KB
|
210 downloads