iVolume returning static value for every bar

 

Hi all. Just poking around in MQL5 for the first time.

I thought I'd start with something simple, print to the journal on the first tick of a new bar:


//+------------------------------------------------------------------+
//|                                                     learning.mq5 |
//|                        Copyright 2019, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2019, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
//Var

long cVol; //Current volume handle

bool firstTick; //First tick bool
//+------------------------------------------------------------------+
//| Initialize                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
   cVol = iVolume(Symbol(),Period(), 0);
//---
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Terminate                             |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Run                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
   firstTick = cVol == 1;
   
    if(firstTick == true)
     {
      Print("New bar, tick volume = ", cVol);
      }
      
//---
   
  }
//+------------------------------------------------------------------+


That yielded no output in the terminal so I changed the firstTick bool to 1 > 0 to see what was going on. 


I get results printed for each bar now, but volume is apparently 65 for every bar in the test:



Anyone have any idea what's going on here?

Conversely, requesting a print of iOpen works just as expected, with variable and correct values for each bar printed.


Update: assigning iVolume handle during init seems to be the problem. Assigning during tick works.

Can anyone give me a tl;dr of the thinking here? Init is the correct time to assign indicator handles, but not bar?

 

OnInit is only called once, when the EA is initialised.

You are printing every tick, not every new bar.

 
Keith Watford:

OnInit is only called once, when the EA is initialised.

You are printing every tick, not every new bar.

Hi Keith, thanks for your response. This is where you'd bind indicator handles though correct? How is it that they update with the latest value? 

In the screen it is printing once every 5m bar, though as the tick check wasn't working, that behavior was forced with open price only.

Reason: