array out of range

 

Hello, I'm new to coding, can you help me solve this code, the EA closes when I change the period to D1 or higher, the error is "array out of range": double open_price = price_open[0]; I use different times for different objects for intraday trading.


//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   // Día de inicio: Fecha y hora actual de la barra diaria
   datetime start_day = iTime(NULL,PERIOD_D1,0);
   datetime final_day = start_day + 86399; // + 23:59:59 Horas
   
   datetime start_time = start_day; // Fecha y hora actual 00:00:00
   datetime final_time = final_day; // Fecha y hora proyectado + 24:00:00

   // Crear una matriz para los datos de precios
   MqlRates PriceInfo[];
   
   // Ordenar la matriz desde la vela actual hacia abajo
   ArraySetAsSeries(PriceInfo,true);
   
   // Rellene la matriz con datos de precios
   int data = CopyRates(NULL,PERIOD_CURRENT,start_time,final_time,PriceInfo);
   
   // Crear matriz para datos de precios
   double   price_open[];

   // Ordenar la matriz desde la vela actual hacia abajo
   ArraySetAsSeries(price_open,false); // Faslo para que empiese del primer elemento y no del actual

   // Llena la matriz con precios altos
   CopyOpen(NULL,PERIOD_CURRENT,start_time,final_time,price_open);

   // Variables para graficación
   double    open_price = price_open[0];
//---
  }
//+------------------------------------------------------------------+
 

You must check the result returned by the Copy*** function ( CopyRates and  CopyOpen ) . Follow the link and read the documentation.


Add: Since you want to receive data from someone else's timeframe, you really need to study the documentation Organizing Data Access.

Documentation on MQL5: Timeseries and Indicators Access / CopyRates
Documentation on MQL5: Timeseries and Indicators Access / CopyRates
  • www.mql5.com
CopyRates - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Gerardo Calvetty: Hello, I'm new to coding, can you help me solve this code, the EA closes when I change the period to D1 or higher, the error is "array out of range": double open_price = price_open[0]; I use different times for different objects for intraday trading.

You are not checking if CopyOpen is returning enough data or if an error occurred. You are just assuming that it worked and that enough data was copied. Check the value returned by the function.

EDIT: The same applies to CopyRates that Vladimir mentioned above.

Documentation on MQL5: Timeseries and Indicators Access / CopyOpen
Documentation on MQL5: Timeseries and Indicators Access / CopyOpen
  • www.mql5.com
CopyOpen - Timeseries and Indicators Access - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
Reason: