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.
this is how I would fix your indicator
//+------------------------------------------------------------------+ //| 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 start = (prev_calculated == 0) ? 1 : prev_calculated - 1; for (int i = start; i<rates_total; i++) { Vol_diff[i] = double(tick_volume[i] - tick_volume[i-1]); } //--- return value of prev_calculated for next call return(rates_total); }
If you only cared about incoming ticks on current bar:
int start = (prev_calculated == 0) ? 1 : prev_calculated;
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
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
thanks for your time