create one EA to writes the Ticks-data, but always reports array out of range when debugging, thank you for your help!

 

The following code is relatively simple, but in the mt5 Testing Visualization linear array out of range an error message.

I'm a novices, and I had searched for a long time and have not found the reason for the error, but I think it might be caused by the caching problem in the OnTick() function.

Please give guidance from experts, thank you!


#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

MqlTick myticks[];
double tick_bid[];
double tick_ask[];

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
         ArraySetAsSeries(myticks,true);  
         CopyTicks("XAUUSD",myticks,COPY_TICKS_INFO,0,50);   
                 

         ArrayInitialize(tick_bid,0);
         ArrayInitialize(tick_ask,0);
         ArrayResize(tick_bid,50);
         ArrayResize(tick_ask,50); 
  
         for(int i=0;i<50;i++)  
         {  
         
            tick_bid[i]=myticks[i].bid;
            tick_ask[i]=myticks[i].ask;
   
         }


}
通过MQL5社区和服务探索MetaTrader 5的新机遇
通过MQL5社区和服务探索MetaTrader 5的新机遇
  • www.mql5.com
MQL5:MetaTrader 5客户端内置的交易策略语言。语言允许编写您自己的自动交易系统,技术指标,脚本和函数程序库
 
kakaprince:

The following code is relatively simple, but in the mt5 Testing Visualization linear array out of range an error message.

I'm a novices, and I had searched for a long time and have not found the reason for the error, but I think it might be caused by the caching problem in the OnTick() function.

Please give guidance from experts, thank you!

***

Please insert the code correctly: when editing a message, press the button   Codeand paste your code into the pop-up window

 
Vladimir Karputov #:

Please insert the code correctly: when editing a message, press the button   and paste your code into the pop-up window

OK,thank you for your kindness :)

 
#property copyright "Copyright 2021, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

MqlTick myticks[];
double tick_bid[];
double tick_ask[];

int OnInit()
  {

   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {

   
  }

void OnTick()
  {
         ArraySetAsSeries(myticks,true);  
         ArrayResize(myticks,50);
         CopyTicks("XAUUSD",myticks,COPY_TICKS_INFO,0,50);   
                 

         ArrayInitialize(tick_bid,0);
         ArrayInitialize(tick_ask,0);
         ArrayResize(tick_bid,50);
         ArrayResize(tick_ask,50); 
  
         for(int i=0;i<50;i++)  
         {  
         
            tick_bid[i]=myticks[i].bid;
            tick_ask[i]=myticks[i].ask;
   
         }


}

you have to resize the myticks list too

 
Khuman Bakhramirad # :

you have to resize the myticks list too

Thank you!

I had try it,But at runtime, the previous error will still appear。

 
kakaprince #:

Thank you!

I had try it,But at runtime, the previous error will still appear。

copy the code i write for you and compile it and it will be ok i did test it 

 
Blur Darkness:

The following code is relatively simple, but in the mt5 Testing Visualization linear array out of range an error message.

I'm a novices, and I had searched for a long time and have not found the reason for the error, but I think it might be caused by the caching problem in the OnTick() function.

Please give guidance from experts, thank you!


Please try this code

CopyTicks(_Symbol,tick,COPY_TICKS_INFO,0,iBars(_Symbol,PERIOD_CURRENT));

Reason: