Download MetaTrader 5

Global variable to use in different function

To add comments, please log in or register
Having troubles understanding the code? Perhaps, you can find an article on that topic!
fx_ta
60
fx_ta 2012.09.23 22:43 

Hi all

 I've a global variable defined like this:

MqlRates ga_rates[];

Then I've a function in my EA defined as follows.

void getBarInfo()
{
   // fill array with bar information to use in functions
   CopyRates(Symbol(), PERIOD_CURRENT, 0,10, ga_rates);
}

Now I would like to use the variable ga_rates in the section onTick().

   // only execute if bar is new
   if (lv_isNewBar == true)
   {
      getBarInfo();
      Print("D", "001", ga_rates[0].open);
      Print("D", "002", ga_rates[0].close);
      Print("D", "003", ga_rates[0].high);
      Print("D", "004", ga_rates[0].low);
   }   

I do expect, that I receive 4 different values, but I only get one. It's always the open-value.

Anybody an idea?

 

Thanks for any help in advance. 

FX_TA 

Miguel Chambel
441
Miguel Chambel 2012.09.23 23:31  
fx_ta:

Hi all

 I've a global variable defined like this:

Then I've a function in my EA defined as follows.

Now I would like to use the variable ga_rates in the section onTick().

I do expect, that I receive 4 different values, but I only get one. It's always the open-value.

Anybody an idea?

 

Thanks for any help in advance. 

FX_TA 

  

 

 

Hello FX_TA!

It's easy, you're calling getBarInfo() when new bar shows up so naturaly you only got the first price of that bar which is the open.

When the first bar arrives Open=Close=High=Low, then they will all change except the open, of course.

Try geting the previous bar rates, ga_rates[1].open; ga_rates[1].close,... You will get all 4 prices correctly, or, remove the new_bar function.

  

Hope it helps, regards 

 

Helghast 

 

 

fx_ta
60
fx_ta 2012.09.24 17:30  

Hi Helghast

 Thank you! Problem solved.

 Best Regards

 FX_TA 

To add comments, please log in or register