结构数组到 mql4 上的文件

struct to array to File on mql4

以下MQL4代码出现错误:

#property version   "1.00"
#property strict

struct prices
{
    datetime          date; // date
    double            bid;  // bid price
    double            ask;  // ask price
};

prices arr[];
string path ;
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
{
   //---
    path = "script.log";
   //--- save data to array
    arr[0].date = TimeCurrent();
    arr[0].bid  = SymbolInfoDouble(Symbol(),SYMBOL_BID);
    arr[0].ask  = SymbolInfoDouble(Symbol(),SYMBOL_ASK);
   //--- show current data
   Print("Date = ",arr[0].date," Bid = ",arr[0].bid," Ask = ",arr[0].ask);
   //--- increase the counter
   //--- if the array is filled, write data to the file and zero it out
   WriteData(1);
}
//+------------------------------------------------------------------+
//| Write n elements of the array to file                            |
//+------------------------------------------------------------------+
void WriteData(const int n)
{
    //--- open the file
    ResetLastError();
    int handle=FileOpen(path,FILE_READ|FILE_WRITE|FILE_BIN|FILE_COMMON);
    if(handle!=INVALID_HANDLE)
      {
       //--- write array data to the end of the file
       FileSeek(handle,0,SEEK_END);
       FileWriteArray(handle,arr,0,n);
       //--- close the file
       FileClose(handle);
      }
    else
       Print("Failed to open the file, error ",GetLastError());
}
//+------------------------------------------------------------------+

错误是:

2015.06.17 15:39:25.561 array out of range in 'Struct2Array.mq4' (28,8)

第 28 行如下:arr[0].date = TimeCurrent();

知道错在哪里吗?

提前致谢 /库尔。

您需要执行以下任一操作:

  1. 声明时固定数组大小。

    价格到达[0];

  2. 或者,在 OnStart() 中,调整大小:

    ArrayResize( arr, 0 )