uchar ExtCharArray[];
MqlRates ExtRates[1];
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 将当前柱形图的数据复制到MqlRates结构
if(CopyRates(Symbol(), PERIOD_CURRENT, 0, 1, ExtRates)!=1)
{
Print("CopyRates() failed. Error code: ", GetLastError());
return;
}
//--- 在日志中记录MqlRates结构中接收的数据
Print("Data in the structure immediately after receiving it:");
MqlRatesPrint(ExtRates[0]);
//--- 将该结构复制到uchar类型数组中
ResetLastError();
if(!StructToCharArray(ExtRates[0], ExtCharArray))
{
Print("StructToCharArray() failed. Error code: ", GetLastError());
return;
}
//--- 清空结构
ZeroMemory(ExtRates[0]);
//--- 清空后在结构中记录数据
Print("\nData in the structure after ZeroMemory():");
MqlRatesPrint(ExtRates[0]);
//--- 现在将数据从uchar数组复制到MqlRates结构
if(!CharArrayToStruct(ExtRates[0], ExtCharArray))
{
Print("CharArrayToStruct() failed. Error code: ", GetLastError());
return;
}
//--- 将数据从uchar数组复制后记录到MqlRates结构中
Print("\nData in the MqlRates structure after copying it from a uchar array:");
MqlRatesPrint(ExtRates[0]);
/*
result:
Data in the structure immediately after receiving it:
MqlRates data:
Time = 2024.02.21 07:00;
Open = 1.08143;
High = 1.08158;
Low = 1.08122;
Close = 1.08137;
Tick volume = 1341;
Spread = 4;
Real volume = 0
Data in the structure after ZeroMemory():
MqlRates data:
Time = 0;
Open = 0.00000;
High = 0.00000;
Low = 0.00000;
Close = 0.00000;
Tick volume = 0;
Spread = 0;
Real volume = 0
Data in the MqlRates structure after copying it from a uchar array:
MqlRates data:
Time = 2024.02.21 07:00;
Open = 1.08143;
High = 1.08158;
Low = 1.08122;
Close = 1.08137;
Tick volume = 1341;
Spread = 4;
Real volume = 0
*/
}
//+------------------------------------------------------------------+
//| 在日志中记录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);
}
|