uchar ExtCharArray[];
MqlRates ExtRates[1];
//+--------------------------------------------------------------------------------+
//| Script program start function |
//+--------------------------------------------------------------------------------+
void OnStart()
{
//--- copia i dati di una barra corrente nella struttura MqlRates
if(CopyRates(Symbol(), PERIOD_CURRENT, 0, 1, ExtRates)!=1)
{
Print("CopyRates() failed. Error code: ", GetLastError());
return;
}
//--- stampa i dati ricevuti nella struttura MqlRates nel journal
Print("Data in the structure immediately after receiving it:");
MqlRatesPrint(ExtRates[0]);
//--- copia la struttura nell'array di tipo uchar
ResetLastError();
if(!StructToCharArray(ExtRates[0], ExtCharArray))
{
Print("StructToCharArray() failed. Error code: ", GetLastError());
return;
}
//--- cancella la struttura
ZeroMemory(ExtRates[0]);
//--- stampa i dati nella struttura dopo la cancellazione
Print("\nData in the structure after ZeroMemory():");
MqlRatesPrint(ExtRates[0]);
//--- ora copia i dati dall'array uchar alla struttura MqlRates
if(!CharArrayToStruct(ExtRates[0], ExtCharArray))
{
Print("CharArrayToStruct() failed. Error code: ", GetLastError());
return;
}
//--- stampa i dati nella struttura MqlRates dopo averlo copiato dall'array uchar
Print("\nData in the MqlRates structure after copying it from a uchar array:");
MqlRatesPrint(ExtRates[0]);
/*
risultato:
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
*/
}
//+--------------------------------------------------------------------------------+
//| Stampa i campi della struttura MqlRates nel journal |
//+--------------------------------------------------------------------------------+
void MqlRatesPrint(MqlRates &rates)
{
//--- crea la stringa di output
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);
//-- stampa l'intestazione e la stringa di output nel journal
Print("MqlRates data:\n", text);
}
|