#define DATA_TOTAL 4
MqlRates ExtDataRates[DATA_TOTAL];
uchar ExtCharArray[];
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 将最后四个柱形图的数据复制到uchar数组
ResetLastError();
for(int i=0; i<DATA_TOTAL; i++)
{
if(!CopyRatesToCharArray(i, ExtCharArray))
return;
}
//--- 按照uchar数组中的数据量,将循环中的所有可用数据复制到MqlRates结构数组中
for(int i=0; i<DATA_TOTAL; i++)
{
ResetLastError();
if(!CharArrayToStruct(ExtDataRates[i], ExtCharArray, sizeof(MqlRates)*i))
{
Print("CharArrayToStruct() failed. Error code: ", GetLastError());
return;
}
//--- 在成功地将数据从uchar数组复制到MqlRates结构后,
//--- 在日志中记录复制到结构中的数据
MqlRatesPrint(ExtDataRates[i]);
}
/*
result:
MqlRates data:
Time = 2024.02.21 09:00;
Open = 1.08116;
High = 1.08141;
Low = 1.08062;
Close = 1.08124;
Tick volume = 5344;
Spread = 1;
Real volume = 0
MqlRates data:
Time = 2024.02.21 08:00;
Open = 1.08149;
High = 1.08153;
Low = 1.08073;
Close = 1.08114;
Tick volume = 3607;
Spread = 2;
Real volume = 0
MqlRates data:
Time = 2024.02.21 07:00;
Open = 1.08143;
High = 1.08165;
Low = 1.08122;
Close = 1.08150;
Tick volume = 2083;
Spread = 0;
Real volume = 0
MqlRates data:
Time = 2024.02.21 06:00;
Open = 1.08178;
High = 1.08190;
Low = 1.08132;
Close = 1.08142;
Tick volume = 1733;
Spread = 0;
Real volume = 0
*/
}
//+------------------------------------------------------------------+
//| 将柱形图数据复制到uchar数组 |
//| 通过MqlRates结构的指定索引 |
//+------------------------------------------------------------------+
bool CopyRatesToCharArray(const int index, uchar &data_array[])
{
MqlRates rates[1];
uint start=sizeof(MqlRates);
ResetLastError();
if(CopyRates(Symbol(), PERIOD_CURRENT, index, 1, rates)!=1)
{
Print("CopyRates() failed. Error code: ", GetLastError());
return(false);
}
if(!StructToCharArray(rates[0], data_array, start*index))
{
Print("StructToCharArray() failed. Error code: ", GetLastError());
return(false);
}
return(true);
}
//+------------------------------------------------------------------+
//| 在日志中记录MqlRates结构的字段 |
//+------------------------------------------------------------------+
void MqlRatesPrint(MqlRates &rates)
{
//--- 创建输出字符串
string text=StringFormat(" Time = %s;\n"
" Open = %.*f;\n"
" High = %.*f;\n"
" Low = %.*f;\n"
" Close = %.*f;\n"
" Tick volume = %I64u;\n"
" Spread = %d;\n"
" Real volume = %I64u",
TimeToString(rates.time),
_Digits, rates.open,
_Digits, rates.high,
_Digits, rates.low,
_Digits, rates.close,
rates.tick_volume,
rates.spread,
rates.real_volume);
//--- 在日志中记录标题和输出字符串
Print("MqlRates data:\n", text, "\n");
}
|