Custom Indicator Not Showing in All Time Frames

 
#property indicator_separate_window
#include  <Custom\Function_BBIndicators.mqh>

#property indicator_buffers 1

int    History = 50000;
double Buffer_VolumeIntensity[];

//+---------------------------------------------------------------------------+
//| Initialisation Function
//+---------------------------------------------------------------------------+
int OnInit()
{
  // Set Indicator Levels
  IndicatorSetInteger(INDICATOR_LEVELS,3); 
  IndicatorSetDouble(INDICATOR_LEVELVALUE,0,125);
  IndicatorSetDouble(INDICATOR_LEVELVALUE,1,50);
  IndicatorSetDouble(INDICATOR_LEVELVALUE,1,20);
  // Set Minimum & Maximum Levels for SubWindow
  IndicatorSetDouble(INDICATOR_MINIMUM,0);
  IndicatorSetDouble(INDICATOR_MAXIMUM,250);
  // Set Height & #Digit for accuracy of drawing of indicator values for SubWindow
  IndicatorSetInteger(INDICATOR_DIGITS,Digits);
  IndicatorSetInteger(INDICATOR_HEIGHT,150);
  // Set 
  SetIndexBuffer(0,Buffer_VolumeIntensity);
  SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1,clrDodgerBlue);
  string short_Name = "Volume Intensity (21) ";
  IndicatorShortName(short_Name);
  SetIndexLabel(0,"Vol Intensity (21)");
  return(INIT_SUCCEEDED);
}
//+---------------------------------------------------------------------------+
//| Main Body start() Function
//+---------------------------------------------------------------------------+
void start()
{
  int i;
  int n;
  int countedBars;
  countedBars = IndicatorCounted();         // Number of counted bars
  i = Bars - (countedBars - 1);             // Indext of first uncounted bar
  if(i > (History-1))                       // If more then History Bars
    i = (History-1);
    // While..Loop for getting indicator values
    while(i >= 0)
    {
      Buffer_VolumeIntensity[i] = getBB_VolumeIndicator(i);
      i--;
    } // End of while..loop
} // End of start() function

//+---------------------------------------------------------------------------+
//| Function - getBB_VolumeIndicator
//+---------------------------------------------------------------------------+
double getBB_VolumeIndicator(int barIndex)
{
  double  sum_Intensity       = 0;
  double  volume_Intensity = 0;
  int     count = barIndex +(InpBB_Period-1);         // Average Period taken as period value of BB Main
  int     i = barIndex;                               // Average from BatIndex[]
  int     n = 0;
  for(i; i<=count; i++)
  {
    sum_Intensity = sum_Intensity + ((2*Close[i])-High[i]-Low[i])/(High[i]-Low[i]);
    n++;
    volume_Intensity = (sum_Intensity / n);
  }
  return(NormalizeDouble(volume_Intensity,Digits));
} // END Of getBB_VolumeIndicator() function

  • sum_Intensity = sum_Intensity + ((2*Close[i])-High[i]-Low[i])/(High[i]-Low[i]);
    Shows ZERO Divide Error while running the indicator and don't create line of indicator. However, if I run this in a script, it works fine and shows the values perfectly.
  • I have created two more Indicators on similar lines and they work fine up Time Frame H1, on H4 and higher time frame they don't work.
  • Another problem, even these indicators working on timeframe below H4, but when I run strategy tester they don't work at all in tester.
  • How can I get value on M15 Time Frame and H1 Time Frame ? if I use the function specifying the time Chart, it gives different results then physical results when I see on each chart?
Looking forward to hear from expert to solve the issue. Thanks in advance.
 
#property strict
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_level1  0
//--- plot Line 1
#property indicator_label1  "Line 1"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrYellow
#property indicator_style1  STYLE_SOLID
#property indicator_width1  1
//--- input parameters
input int               InpBB_Period    = 21;
//--- indicator buffers
double         Buffer_VolumeIntensity[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- indicator buffers mapping
   SetIndexBuffer(0, Buffer_VolumeIntensity);

   //---
   return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
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, j, limit;

   if (prev_calculated < 0)
      return(-1);
   if (prev_calculated == 0)
      limit = rates_total - InpBB_Period;
   else
      limit = rates_total - prev_calculated + 1;

   for (i = limit; i >= 0; i--)
   {
      double  sum_Intensity = 0.0;
      int     count = i + (InpBB_Period - 1);
      int     n = 0;

      for(j = i; j <= count; j++)
      {
         if (high[j] - low[j] > 0)
         {
            sum_Intensity += (2 * close[j] - high[j] - low[j]) / (high[j] - low[j]);
            n++;
         }
      }

      Buffer_VolumeIntensity[i] = NormalizeDouble(sum_Intensity / n, Digits);
   }
   //--- return value of prev_calculated for next call
   return(rates_total);
}
//+------------------------------------------------------------------+
 
Nagisa Unada:

Thanks a lot Nagisa, this is perfectly working fine on all charts too. I have not yet tried in Tester, but hope it will working there too.

just quick questions close[] and Close[] are different, and is it necessary to use OnCalculate() ?

I will explore further on how to create a function for this, as I have separate functions file as a single source for these values for indicator as well as my EA.

 
"start()" was used in the old MT4. It does work now, but it should not be used for new system.
 
int OnCalculate(const int rates_total,      // Number of bars available for calculation
                const int prev_calculated,  // Number of bars calculated in previous call
                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[])
{
  // Local variables
  int i;              // Bar index number
  int limit;          // 
  int periodMA = (InpBB_Period * 2);  // volume to be divided by Moving Average prices
  
  if(prev_calculated < 0)
    return(-1);
  if(prev_calculated == 0)
    limit = rates_total - periodMA;
  else
    limit = rates_total - prev_calculated + 1;

  for (i = limit; i >= 0; i--)
  {
    double avg_Price = iMA(NULL,PERIOD_CURRENT,periodMA,0,MODE_SMA,PRICE_CLOSE,i);
    Normalized_Volume[i] = NormalizeDouble((volume[i] / avg_Price)*(100*get_Pips()),0);
  }
// Number of Bars calculated in this call = return value of prev_calculated for next call
  return(rates_total);
}
Nagisa Unada
:

"start()" was used in the old MT4. It does work now, but it should not be used for new system.

Thanks for update. Most examples on MT4 site still uses it, so I was not aware that it is outdated. The VolIndicater suggested by you is working perfectly with Tester too.

I am still unable to understand the error I am making while writing code. As while i am trying to code the other indicators (Normalised Volume, BandWidth and %b) i continue to get the problem of not working on all time frames and in tester.

If you don't mind, may I request you to help me out.

Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Representation
Documentation on MQL5: Constants, Enumerations and Structures / Chart Constants / Chart Representation
  • www.mql5.com
The specific way of displaying the price chart is set by the function ChartSetInteger(chart_handle,CHART_MODE, chart_mode), where chart_mode is one of the values of the ENUM_CHART_MODE enumeration. To specify the mode of...
 
Normalized_Volume[i] = NormalizeDouble(((double)tick_volume[i] / avg_Price) * (100 * get_Pips()), 0);

volume[] doesn't have values.

What's the "get_Pips()" ? I don't know it.

 
Nagisa Unada:

volume[] doesn't have values.

What's the "get_Pips()" ? I don't know it.

Wollaa ...

Thanks so much Nagisa, TickVolume worked well. I have also managed to figure out the PercentB and BandWidth Indicators, so 4 are working now with your support.

get_Pips(), I have created a function to calculate PIP value based on currency system of broker. In this case it is 0.0001.

What is the use of (double)tick_volume[i] ? My Indicator working fine without double, so can I drop it out of code?

 
Anil Varma:

What is the use of (double)tick_volume[i] ? My Indicator working fine without double, so can I drop it out of code?

double Normalized_Volume[]

double avg_Price

long     tick_volume[]

It use a "long" variable to calculate the result of the "double", so I'm matching the variable types as a precaution.

 
Nagisa Unada:

double Normalized_Volume[]

double avg_Price

long     tick_volume[]

It use a "long" variable to calculate the result of the "double", so I'm matching the variable types as a precaution.

sounds reasonable :) thanks