OnCalculate() bug? TickVolume returns 1 on OnCalculate()

 

My VWAP indicator stopped to work recently, after an update. I noticed that the volume_tick array returns 1 in the last iteration. In the next iteration the before values are right.


I coded the follow indicator to show the problem:

#property copyright "Copyright 2018, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_chart_window

int OnInit(){return(INIT_SUCCEEDED);}

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[]){
   if(prev_calculated == 0){ return 1; }
  
   for(int i=prev_calculated; i<rates_total; i++){
      PrintFormat("idx: %d, tickVol [%.1f -> %.1f], vol [%.1f -> %.1f]", i, tick_volume[i-1], tick_volume[i], volume[i-1], volume[i]);
   }
   
   return(rates_total);
}

A return fragment is:

2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11734, tickVol [8972.0 -> 4208.0], vol [25938.0 -> 12486.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11735, tickVol [4208.0 -> 2263.0], vol [12486.0 -> 6637.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11736, tickVol [2263.0 -> 3816.0], vol [6637.0 -> 11453.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11737, tickVol [3816.0 -> 2680.0], vol [11453.0 -> 7885.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11738, tickVol [2680.0 -> 4378.0], vol [7885.0 -> 13220.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11739, tickVol [4378.0 -> 3187.0], vol [13220.0 -> 10437.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11740, tickVol [3187.0 -> 1590.0], vol [10437.0 -> 5110.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11741, tickVol [1590.0 -> 1457.0], vol [5110.0 -> 4229.0]
2018.11.14 11:09:32.981 TestVolume (WINZ18,M1)  idx: 11742, tickVol [1457.0 -> 1002.0], vol [4229.0 -> 3008.0]

2018.11.14 11:09:57.368 TestVolume (WINZ18,M1)  idx: 11743, tickVol [1427.0 -> 1.0], vol [4290.0 -> 2.0]
2018.11.14 11:10:57.561 TestVolume (WINZ18,M1)  idx: 11744, tickVol [3508.0 -> 1.0], vol [9726.0 -> 4.0]
2018.11.14 11:11:57.473 TestVolume (WINZ18,M1)  idx: 11745, tickVol [5003.0 -> 1.0], vol [14169.0 -> 5.0]
2018.11.14 11:12:57.550 TestVolume (WINZ18,M1)  idx: 11746, tickVol [3531.0 -> 1.0], vol [11182.0 -> 2.0]
2018.11.14 11:13:57.948 TestVolume (WINZ18,M1)  idx: 11747, tickVol [2314.0 -> 1.0], vol [6641.0 -> 2.0]
2018.11.14 11:14:57.380 TestVolume (WINZ18,M1)  idx: 11748, tickVol [2665.0 -> 1.0], vol [8406.0 -> 2.0]
2018.11.14 11:15:57.507 TestVolume (WINZ18,M1)  idx: 11749, tickVol [1683.0 -> 1.0], vol [5604.0 -> 2.0]


Notice that I put the indicator at 11:09:32.

The history data are loaded correctly. But the iterate data returns 1 to tick_volume. 

The tick_volume returns the correct value in the next iteration: idx-1 is always correct, but idx is always 1.


I tested this code in real data with two brokers and the behavior was the same.

Build 1940


What can I do to fix that?

 
Rafael Caetano Pinto:

My VWAP indicator stopped to work recently, after an update. I noticed that the volume_tick array returns 1 in the last iteration. In the next iteration the before values are right.


I coded the follow indicator to show the problem:

A return fragment is:


Notice that I put the indicator at 11:09:32.

The history data are loaded correctly. But the iterate data returns 1 to tick_volume. 

The tick_volume returns the correct value in the next iteration: idx-1 is always correct, but idx is always 1.


I tested this code in real data with two brokers and the behavior was the same.

Build 1940


What can I do to fix that?

After the first iteration (when prev_calculated is 1), your code only run on each new M1 bar, so the tick volume is always 1.
 

Tks Alain,

To fix that I skipped when prev_calculated=zero and I always calculate i-1 values.