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);
}
|