Download MetaTrader 5

Help with a very small part of code please

To add comments, please log in or register
Nicolas Zouein
1063
Nicolas Zouein  

Please can anyone point out what is wrong in this part of code below, it is showing "array out of range" and pointing that the error is on this line: datetime time = Time[0];

datetime getLastTradeTime(int type)
{
    datetime time = Time[0];
    for(int cnt = OrdersTotal()-1 ;cnt>=0;cnt--)
    {
      if(!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))continue;
      if(OrderMagicNumber() == MagicNumber && (OrderType() == type || type == -1)) 
      {
         if(time<OrderOpenTime())time = OrderOpenTime();
      }
    }
    
    for(int cnt = OrdersHistoryTotal()-1 ;cnt>=0;cnt--)
    {
      if(!OrderSelect(cnt, SELECT_BY_POS, MODE_HISTORY))continue;
      if(OrderMagicNumber() == MagicNumber && (OrderType() == type || type == -1)) 
      {
         if(time < OrderCloseTime())time = OrderCloseTime();
      }
    } 
   return(time);
} 
Yohana Parmi
4041
Yohana Parmi  
Nicolas Zouein:

Please can anyone point out what is wrong in this part of code below, it is showing "array out of range" and pointing that the error is on this line: datetime time = Time[0];

Hi,

Please try  this :)

datetime time = iTime(_Symbol,PERIOD_D1,1);
Nicolas Zouein
1063
Nicolas Zouein  
Yohana Parmi:

Hi,

Please try  this :)

Thank You so much, you're a queen !

whroeder1
17951
whroeder1  
Nicolas Zouein: it is showing "array out of range" and pointing that the error is on this line: datetime time = Time[0];
Don't try to use any price or server related functions in OnInit (or on load,) as there may be no connection/chart yet:
  1. Terminal starts.
  2. Indicators/EAs are loaded. Static and globally declared variables are initialized. (Do not depend on a specific order.)
  3. OnInit is called.
  4. For indicators OnCalculate is called with any existing history.
  5. Human may have to enter password, connection to server begins.
  6. New history is received, OnCalculate called again.
  7. New tick is received, OnCalculate/OnTick is called. Now TickValue, TimeCurrent and prices are valid.
To add comments, please log in or register