以下 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();
知道哪里出错了?
提前致谢 / koul。
答案 0 :(得分:0)
您需要执行以下任一操作:
声明数组时修复数组大小。
价格arr [0];
或者,在OnStart()中,使用:
调整其大小ArrayResize(arr,0)