Help Please!!! Indicator not drawing AveragedVolulme line...

 

Greetings Fellows

I have created a custom indicator to display tick volume as histogram and average volume as line. The indicator is drawing the histogram but not the line.

Data Window though shows both values are calculated. Screenshot and Code attached.

Any clues what I am doing wrong will highly appreciated.

Regards

//+-----------------------------------------------------------------------------------------------------------------------------+
//| iNVOL.mq5
//| NORMALIAZED VOLUME INDICATOR
//+-----------------------------------------------------------------------------------------------------------------------------+
#property description       "Normalized Volume Indicator [Volume%]"
#property description       "With selection of MA Method / Volume Type defaulted to TickVolume"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   2
#property indicator_color1  clrGreen, clrRed
#include <MovingAverages.mqh>
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Define Input Parameters
//+-----------------------------------------------------------------------------------------------------------------------------+
input int             InpAvgPeriod = 21;            // Averaging Period as recommended in Bollinger Bands
input ENUM_MA_METHOD  InpMAMethod  = MODE_EMA;
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Define Global Variables
//+-----------------------------------------------------------------------------------------------------------------------------+
int       ExtAvgPeriod;
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Define Indicator Buffers for Data & Arrays for Indicator Calculation
//+-----------------------------------------------------------------------------------------------------------------------------+
#define         _bVolume                0
#define         _bClrIndex      1
#define         _bNVolume               2

double    BufferVolume[];
double    BufferColorIndex[];
double    BufferNVOL[];                                                                         // Normalized Volume
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+-----------------------------------------------------------------------------------------------------------------------------+
int OnInit() {
        //+---------------------------------------------------------------------------------------------------------------------------+
        //| Check Input Values are correct else set them defualt of the Indicator
        //+---------------------------------------------------------------------------------------------------------------------------+
                if(InpAvgPeriod < 21) {
                  ExtAvgPeriod = 21;
                  PrintFormat("%s: InpAvgPeriod %d is too low. Recommended period %d will be used.",(string)__FILE__,InpAvgPeriod,ExtAvgPeriod);
                }
                else
                        ExtAvgPeriod = InpAvgPeriod;
        //+--------------------------------------------------------------------------------------------------------------------------+
        //| Indicator SubWindow common 'properties' (used instead of #property)
        //+--------------------------------------------------------------------------------------------------------------------------+
                //IndicatorSetString(INDICATOR_SHORTNAME,"iNVOL ("+(string)ExtAvgPeriod+") ["+EnumToString(InpMAMethod)+"] ");
                IndicatorSetString(INDICATOR_SHORTNAME,"NVOL ("+(string)ExtAvgPeriod+")");      // temporarily used as PlotLabel (below) is not working
                IndicatorSetInteger(INDICATOR_DIGITS,0);
                IndicatorSetInteger(INDICATOR_HEIGHT,125);
        //+--------------------------------------------------------------------------------------------------------------------------+
        //| Indicator Buffers mapping / binding
        //+--------------------------------------------------------------------------------------------------------------------------+
                SetIndexBuffer(_bVolume,BufferVolume,INDICATOR_DATA);
                SetIndexBuffer(_bClrIndex,BufferColorIndex,INDICATOR_COLOR_INDEX);
                SetIndexBuffer(_bNVolume,BufferNVOL,INDICATOR_DATA);
//+-----------------------------------------------------------------------------------------------------------------------------+
//| Plot Indicator Lables in SubWindow (used instead of #property)
//+-----------------------------------------------------------------------------------------------------------------------------+
   PlotIndexSetString(_bVolume,PLOT_LABEL,"TickVOL");
   PlotIndexSetInteger(_bVolume,PLOT_DRAW_TYPE,DRAW_COLOR_HISTOGRAM);
   PlotIndexSetInteger(_bVolume,PLOT_LINE_STYLE,STYLE_SOLID);
   PlotIndexSetInteger(_bVolume,PLOT_LINE_WIDTH,3);
   PlotIndexSetDouble(_bVolume,PLOT_EMPTY_VALUE,0.00);
   PlotIndexSetInteger(_bVolume,PLOT_DRAW_BEGIN,ExtAvgPeriod-1);

   PlotIndexSetString(_bNVolume,PLOT_LABEL,"NVOL ("+string(ExtAvgPeriod)+") ");
   PlotIndexSetInteger(_bNVolume,PLOT_DRAW_TYPE,DRAW_LINE);
   PlotIndexSetInteger(_bNVolume,PLOT_LINE_STYLE,STYLE_SOLID);
   PlotIndexSetInteger(_bNVolume,PLOT_LINE_COLOR,clrDodgerBlue);
   PlotIndexSetInteger(_bNVolume,PLOT_LINE_WIDTH,1);
   PlotIndexSetDouble(_bNVolume,PLOT_EMPTY_VALUE,0.00);
   PlotIndexSetInteger(_bNVolume,PLOT_DRAW_BEGIN,ExtAvgPeriod-1);

//---
   return(0);

} // End Of method OnInit()
//+------------------------------------------------------------------------------------------------------------------------------+
//| Custom indicator iteration function
//+------------------------------------------------------------------------------------------------------------------------------+
int OnCalculate(const int       rates_total,        // size of the price[] array;
                const int       prev_calculated,    // number of available bars;
                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[])

  {
//--- if the size of bars on chart is too small
   if(rates_total < ExtAvgPeriod)
      return(0); // do not calculate or draw anything
//--- if it's the first call
   if(prev_calculated == 0)
     {
      BufferVolume[0] = (double)tick_volume[0];  // Set bar open Volume at Index[0]
     }
//--- calculate values of mtm and |mtm|
   int start;
   if(prev_calculated == 0)
      start = 1;                                    // start filling out BufferVolume[] from the 1st index
   else
      start = prev_calculated-1;                    // set start equal to the last index in the arrays
   for(int i = start; i < rates_total && !IsStopped(); i++) {
      BufferVolume[i] = (double)tick_volume[i];
   }
                //--- calculate the moving average on arrays
                int k = 0;                                                        // index, from which data for smoothing available
                switch(InpMAMethod) {
                
                        case MODE_EMA:
                                ExponentialMAOnBuffer(rates_total,prev_calculated,k,ExtAvgPeriod,BufferVolume,BufferNVOL);
                                break;
                        case MODE_LWMA:
                                LinearWeightedMAOnBuffer(rates_total,prev_calculated,k,ExtAvgPeriod,BufferVolume,BufferNVOL);
                                break;
                        default:                                  // none of above then apply default method
                                SimpleMAOnBuffer(rates_total,prev_calculated,k,ExtAvgPeriod,BufferVolume,BufferNVOL);
                                break;
                }
                // Color Index sets Bullish Volume (Green) and Bearish Volume (Red)
                for(int i = start; i < rates_total && !IsStopped(); i++) {
                        BufferColorIndex[i] = close[i] >= open[i] ? 0 : 1;
                }
                //--- return value of prev_calculated for next call
                return(rates_total);

} // End Of method OnCalculate()
//+------------------------------------------------------------------------------------------------------------------------------+
 
The line needs to be plotted first before the histogram because the histogram is based on the values of the line. 

Switch it so that SetIndexBuffer sets up the line first, histogram after.
 
phade #:
The line needs to be plotted first before the histogram because the histogram is based on the values of the line. 

Switch it so that SetIndexBuffer sets up the line first, histogram after.

Thanks @phade for quick reply.

Actually histogram is based on double BufferVolume[] by copying data from long tick_volume[] buffer, as I was not allowed to use long type tick_volume buffer in SimpleMAOnBuffer or other variants.

Line is based on double    BufferNVOL[].

Anyway I have tried to shift buffers as per your suggestion as below:

#define         _bNVolume       0
#define         _bVolume        1
#define         _bClrIndex      2

Now it just draws the Line and not the 'Histogram'

Looking forward to resolve the issue.
 

 
Anil Varma #:

Thanks @phade for quick reply.

Actually histogram is based on double BufferVolume[] by copying data from long tick_volume[] buffer, as I was not allowed to use long type tick_volume buffer in SimpleMAOnBuffer or other variants.

Line is based on double    BufferNVOL[].

Anyway I have tried to shift buffers as per your suggestion as below:

Now it just draws the Line and not the 'Histogram'

Looking forward to resolve the issue.
 

You have 2 plots, they should be indexed 0 and 1. You are using index 2. Don't mix buffers indexes and plot indexes.
 
Anil Varma #:

Now it just draws the Line and not the 'Histogram'

Looking forward to resolve the issue.
 

When making a custom indicator, you need to define properties for all of the plots, also taking in mind what Alain said.


So set the properties like this this:

#property description       "Normalized Volume Indicator [Volume%]"
#property description       "With selection of MA Method / Volume Type defaulted to TickVolume"
#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   2

#property indicator_type1 DRAW_LINE
#property indicator_style1   STYLE_SOLID
#property indicator_color1 clrDodgerBlue 
#property indicator_width1 1

#property indicator_type2 DRAW_HISTOGRAM
#property indicator_color2 clrGreen, clrRed
#property indicator_width2 3


This is correct:

#define         _bNVolume               0
#define         _bVolume                1
#define         _bClrIndex              2
 
phade #:

When making a custom indicator, you need to define properties for all of the plots, also taking in mind what Alain said.


So set the properties like this this:


This is correct:

Thanks a lot @phade and @Alain Verleyen.

the below code finally resolved the issue :)

#property indicator_separate_window
#property indicator_buffers 3
#property indicator_plots   2

#property indicator_type1       DRAW_LINE
#property indicator_style1      STYLE_SOLID
#property indicator_color1      clrDodgerBlue 
#property indicator_width1      1

#property indicator_type2       DRAW_COLOR_HISTOGRAM
#property indicator_color2      clrGreen, clrRed
#property indicator_width2      3

//+-----------------------------------------------------------------------------------------------------------------------------+
//| Define Indicator Buffers for Data & Arrays for Indicator Calculation
//+-----------------------------------------------------------------------------------------------------------------------------+
#define         _bNVolume               0
#define         _bVolume                1
#define         _bClrIndex      2

double    BufferNVOL[];                                                                         // Normalized Volume
double    BufferVolume[];
double    BufferColorIndex[];
        //+-----------------------------------------------------------------------------------------------------------------------------+
        //| Plot Indicator Lables in SubWindow (used instead of #property)
        //+-----------------------------------------------------------------------------------------------------------------------------+
                PlotIndexSetString(_bNVolume,PLOT_LABEL,"NVOL ("+string(ExtAvgPeriod)+") ");
                //PlotIndexSetInteger(_bNVolume,PLOT_DRAW_TYPE,DRAW_LINE);			...NEEDED TO REMOVE THESE, not understood why
                //PlotIndexSetInteger(_bNVolume,PLOT_LINE_STYLE,STYLE_SOLID);
                //PlotIndexSetInteger(_bNVolume,PLOT_LINE_COLOR,clrDodgerBlue);
                //PlotIndexSetInteger(_bNVolume,PLOT_LINE_WIDTH,1);
                PlotIndexSetDouble(_bNVolume,PLOT_EMPTY_VALUE,0.00);
                PlotIndexSetInteger(_bNVolume,PLOT_DRAW_BEGIN,ExtAvgPeriod-1);

                PlotIndexSetString(_bVolume,PLOT_LABEL,"TickVOL");
                //PlotIndexSetInteger(_bVolume,PLOT_DRAW_TYPE,DRAW_COLOR_HISTOGRAM);
                //PlotIndexSetInteger(_bVolume,PLOT_LINE_STYLE,STYLE_SOLID);
                //PlotIndexSetInteger(_bVolume,PLOT_LINE_WIDTH,3);
                PlotIndexSetDouble(_bVolume,PLOT_EMPTY_VALUE,0.00);
                PlotIndexSetInteger(_bVolume,PLOT_DRAW_BEGIN,ExtAvgPeriod-1);
 
Anil Varma #:

Thanks a lot @phade and @Alain Verleyen.

the below code finally resolved the issue :)

You also don't necessarily need to comment out those PlotIndexSetInteger lines, it's just important to include the properties for the plots. I also had the same issue before
 
phade #:
You also don't necessarily need to comment out those PlotIndexSetInteger lines, it's just important to include the properties for the plots. I also had the same issue before

@phade Thanks for reply :)

Well there is no point in keeping double properties for same result.

Hence decided to remove the commented out once (for which #property method used), and

keep the once where #property not used.

Anyway THANKS A LOT again, for helping me out.

Regards