Strange division problem

 

Hi All


I am coding the following, the code returns no error but doesn't seem to do the calculation. 

      double Top =(iClose(Symbol(),0,i)-iOpen(Symbol(),0,i));

      double Bottom = (iHigh(Symbol(),0,i)-iLow(Symbol(),0,i));

      WVAD[i]=Top*iVolume(Symbol(),0,i)/Bottom;

I tried to debug it by adding the following alert, it doesn't run. However, if I change the WVAD[] = Top or Bottom, it runs.

      if (i % 1000 == 0)

         Alert(WVAD[i]);

The code just doesn't do the division. Why?

#property indicator_separate_window
#property indicator_buffers 2 
#property indicator_color1 Blue 
#property indicator_color2 Red  

extern string separator1 = "*** WVAD Settings ***";

double WVAD[];       //arrow buffer

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int init()
  {
   //---- indicator
   
   SetIndexBuffer(0,WVAD);
   SetIndexLabel(0,"WVAD");
   
   SetIndexStyle(0,DRAW_LINE,STYLE_SOLID,1);

//----
   return(0);
  }
//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+
int deinit()
  {
//---- 

//----
   return(0);
  }


//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start() {
   int i, limit, Counted_bars;     
   double sum;                   
 //--------------------------------------------------------------------
   //Counted_bars=IndicatorCounted(); // Number of counted bars
   //i=Bars-Counted_bars-1;           // Index of the first uncounted
   int counted_bars=IndicatorCounted();
//---- check for possible errors
   if(counted_bars<0) return(-1);
//---- last counted bar will be recounted
   if(counted_bars>0) counted_bars--;

   limit=Bars-counted_bars;
   
   for(i = limit; i >= 0; i--) 
   {
      sum = 0;
      double Top =(iClose(Symbol(),0,i)-iOpen(Symbol(),0,i);
      double Bottom = (iHigh(Symbol(),0,i)-iLow(Symbol(),0,i);
      WVAD[i]=Top*iVolume(Symbol(),0,i)/Bottom;
      WVAD[i]=Top;
      //if (i % 1000 == 0)
         //Alert(WVAD[i]);

   }
   return(0);
}

  
 
Why aren't you using the args passed into the OnCalculate function? Don't use iTimeSeries funcs unless you need MTF. 
 
nicholi shen:
Why aren't you using the args passed into the OnCalculate function? Don't use iTimeSeries funcs unless you need MTF. 

I have never used OnCalculate before, so how should I change my code? and is this the reason why it doesn't do the calculation? or whatever error it has?

 
luckyvictor:

Hi All


I am coding the following, the code returns no error but doesn't seem to do the calculation. 

I tried to debug it by adding the following alert, it doesn't run. However, if I change the WVAD[] = Top or Bottom, it runs.

The code just doesn't do the division. Why?

Please post full code or attach source file.

Warning: seems your double Top returns negative values.

It also seems you want to recreate Williams Variable Accumulation Distribution (WVAD), developed by Larry Williams. If so, WVAD = (( Close – Open ) / ( High – Low )) * Volume.

You should read the following:

https://www.mql5.com/en/articles/10

MQL5: Create Your Own Indicator
MQL5: Create Your Own Indicator
  • www.mql5.com
What is an indicator? It is a set of calculated values that we want to be displayed on the screen in a convenient way. Sets of values are represented in programs as arrays. Thus, creation of an indicator means writing an algorithm that handles some arrays (price arrays) and records results of handling to other arrays (indicator values). Despite...
 
luckyvictor:

I have never used OnCalculate before, so how should I change my code? and is this the reason why it doesn't do the calculation? or whatever error it has?

MT4 and MT5 come with many example indicators. Look in Indicators\Examples\.

I recommend you study these examples and read the "OnCalculate" section of this page :

https://www.mql5.com/en/docs/basis/function/events

 
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots   1
//--- input parameters
input int                inpPeriod = 15;          // Period
//--- indicator buffers
double iVWAD[]; // William's Variable Accumulation Distribution
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
void OnInit()
  {
   SetIndexBuffer(0,iWVAD,INDICATOR_DATA);
  }

int  OnCalculate(
   const int        rates_total,       // size of input time series
   const int        prev_calculated,   // number of handled bars at the previous call
   const datetime&  time[],            // Time array
   const double&    open[],            // Open array
   const double&    high[],            // High array
   const double&    low[],             // Low array
   const double&    close[],           // Close array
   const long&      tick_volume[],     // Tick Volume array
   const long&      volume[],          // Real Volume array
   const long&      spread[])          // Spread array
  {
   if (Bars(_Symbol,_Period)<rates_total) return(0);
   for (int i=(int)MathMax(prev_calculated-1,0); i<rates_total && !_StopFlag; i++)
     {
      iVWAD[i] = ((close[i] – open[i]) / (high[i] – low[i])) * volume[i];
     }                  
   return(rates_total);
  }
I did not test it.
 
Arthur Albano:

Please post full code or attach source file.

Warning: seems your double Top returns negative values.

It also seems you want to recreate Williams Variable Accumulation Distribution (WVAD), developed by Larry Williams. If so, WVAD = (( Close – Open ) / ( High – Low )) * Volume.

You should read the following:

https://www.mql5.com/en/articles/10

Yes, I am trying to code this one. I have also uploaded my source code. 

I tried to follow the tutorial, as well as compiling the example that you share (thanks for this), but I couldnt get it running, it returns me error: oncalculate function not found in custom indicator

 
luckyvictor:

Yes, I am trying to code this one. I have also uploaded my source code. 

I tried to follow the tutorial, as well as compiling the example that you share (thanks for this), but I couldnt get it running, it returns me error: oncalculate function not found in custom indicator

I believe I have fixed it. Please try to compile again.

 
Arthur Albano:

I believe I have fixed it. Please try to compile again.

Same error: oncalculate function not found in custom indicator

 
luckyvictor:

Same error: oncalculate function not found in custom indicator

Fixed. There was typos and wrong characters.
Files:
289246.mq5  3 kb
 
Alain Verleyen:
Fixed. There was typos and wrong characters.

I was using notepad and copied OnCalculate() from Documentation, and other code samples too. Thanks again @Alain Verleyen.

Documentation on MQL5: Event Handling / OnCalculate
Documentation on MQL5: Event Handling / OnCalculate
  • www.mql5.com
The function is called in the indicators when the Calculate event occurs for processing price data changes. There are two function types. Only one of them can be used within a single indicator. [in]  Size of the price[] array or input series available to the indicator for calculation. In the second function type, the parameter value corresponds...
Reason: