uchar ExtCharArray[];
MqlRates ExtRates[1];
//+------------------------------------------------------------------+
//| スクリプトプログラム開始関数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 現在の1つの足のデータを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]);
/*
結果:
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);
}
|