Custom indicator gives correct values only if manually resfreshed

 

Hi everyone, it probably is a very dum8 mistake but I'm new to coding,

I've created an indicator that calculates the difference between two consecutive candles tick volume but for some reason, unless i refresh it manually in mt5 , after first loading it it will display wrong values


I'll post my code here


//+------------------------------------------------------------------+
//|                                                     Vol_diff.mq5 |
//|                                  Copyright 2025, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property indicator_separate_window
#property indicator_label1  "Vol_diff2"
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrTomato
#property indicator_style1  STYLE_SOLID

#property indicator_width1  2


#property indicator_buffers 1
#property indicator_plots   1

double Vol_diff[];
//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   SetIndexBuffer(0,Vol_diff,INDICATOR_DATA);

//---
   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[])
  {
//---
   for (int i = prev_calculated; i<rates_total; i++)
   {
      Vol_diff[i] = iTickVolume(NULL,PERIOD_CURRENT,rates_total-i-1) - iTickVolume(NULL,PERIOD_CURRENT,rates_total-i);
   }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+

thanks for your time

Files:
 
Egom96:

The issue you're seeing happens because you're using "TickVolume()" inside the "OnCalculate()" function. This function makes external data requests, which can cause the indicator to show incorrect values when it first loads, or update only after a manual refresh.

You don’t need to use "iTickVolume()" here because MetaTrader already gives you all the tick volume data in the "tick_volume[]" array, which is passed directly into "OnCalculate()".

To fix this, just replace this line:

Vol_diff[i] = iTickVolume(NULL, PERIOD_CURRENT, rates_total - i - 1) - iTickVolume(NULL, PERIOD_CURRENT, rates_total - i);

With this:

Vol_diff[i] = tick_volume[i] - tick_volume[i + 1];

Also, make sure your loop stops at "rates_total - 1", since the last bar doesn't have a "next" one to compare with.

Once you make these changes, the indicator will work as expected and update automatically with every new tick, no need to refresh it manually anymore.