Art12900_Structs/TestMqlRates.mq5

253 行
23 KiB
MQL5

2026-03-01 22:12:41 +07:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| TestMqlRates.mq5 |
//| Copyright 2023, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2023, MetaQuotes Ltd."
#property link "https://www.mql5.com"
#property version "1.00"
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
//---
MqlRates array[];
if(CopyRates(Symbol(),PERIOD_CURRENT,0,4,array)!=4)
{
Print("CopyRates failed, error: ",(string)GetLastError());
return;
}
2026-03-01 22:12:41 +07:00
//--- #AB0=02;8205< 8=45:A0F8N <0AA820 :0: C B09<A5@88
ArraySetAsSeries(array,true);
//---  F8:;5 ?> <0AA82C A ?>;CG5==K<8 40==K<8 10@>2 @0A?5G0BK205< 2 6C@=0;5 :@0B:85 70?8A8
for(int i=0;i<(int)array.Size();i++)
MqlRatesPrint(Symbol(),PERIOD_CURRENT,array[i],false,14,2,i);
/* @8<5@ 2K2>40:
GBPUSD H1[0]:
Time: 2023.07.21 14:00:00
Open: 1.28451
High: 1.28541
Low: 1.28451
Close: 1.28491
Tick Volume: 1098
Spread: 4
Real Volume: 0
GBPUSD H1[1]:
Time: 2023.07.21 13:00:00
Open: 1.28678
High: 1.28685
Low: 1.28418
Close: 1.28452
Tick Volume: 3602
Spread: 1
Real Volume: 0
GBPUSD H1[2]:
Time: 2023.07.21 12:00:00
Open: 1.28581
High: 1.28696
Low: 1.28557
Close: 1.28678
Tick Volume: 4807
Spread: 1
Real Volume: 0
GBPUSD H1[3]:
Time: 2023.07.21 11:00:00
Open: 1.28695
High: 1.28745
Low: 1.28401
Close: 1.28581
Tick Volume: 7440
Spread: 1
Real Volume: 0
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 2@5<O >B:@KB8O 10@0 |
//+------------------------------------------------------------------+
string MqlRatesTime(const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Time:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-s",indent,"",w,header,(string)rates.time);
/* @8<5@ 2K2>40:
Time: 2023.07.21 06:00:00
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 F5=C >B:@KB8O 10@0 |
//+------------------------------------------------------------------+
string MqlRatesOpen(const string symbol,const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Open:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >;CG05< :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9
int dg=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-.*f",indent,"",w,header,dg,rates.open);
/* @8<5@ 2K2>40:
Open: 1.28812
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 F5=C High 10@0 |
//+------------------------------------------------------------------+
string MqlRatesHigh(const string symbol,const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="High:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >;CG05< :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9
int dg=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-.*f",indent,"",w,header,dg,rates.high);
/* @8<5@ 2K2>40:
High: 1.28859
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 F5=C Low 10@0 |
//+------------------------------------------------------------------+
string MqlRatesLow(const string symbol,const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Low:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >;CG05< :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9
int dg=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-.*f",indent,"",w,header,dg,rates.low);
/* @8<5@ 2K2>40:
Low: 1.28757
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 F5=C 70:@KB8O 10@0 |
//+------------------------------------------------------------------+
string MqlRatesClose(const string symbol,const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Close:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >;CG05< :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9
int dg=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-.*f",indent,"",w,header,dg,rates.close);
/* @8<5@ 2K2>40:
Close: 1.28770
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 B8:>2K9 >1JQ< 10@0 |
//+------------------------------------------------------------------+
string MqlRatesTickVolume(const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Tick Volume:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-lld",indent,"",w,header,rates.tick_volume);
/* @8<5@ 2K2>40:
Tick Volume: 963
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 A?@54 =0 10@5 |
//+------------------------------------------------------------------+
string MqlRatesSpread(const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Spread:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-ld",indent,"",w,header,rates.spread);
/* @8<5@ 2K2>40:
Spread: 4
*/
}
//+------------------------------------------------------------------+
//| >72@0I05B 2 2845 AB@>:8 18@652>9 >1JQ< 10@0 |
//+------------------------------------------------------------------+
string MqlRatesRealVolume(const MqlRates &rates,const uint header_width=0,const uint indent=0)
{
//--- ?@545;O5< B5:AB 703>;>2:0 8 H8@8=C ?>;O 703>;>2:0
//--- A;8 H8@8=0 703>;>2:0 ?5@540=0 2 DC=:F8N @02=>9 =C;N, B> H8@8=>9 1C45B @07<5@ AB@>:8 703>;>2:0 + 1
string header="Real Volume:";
uint w=(header_width==0 ? header.Length()+1 : header_width);
//--- >72@0I05< 7=0G5=85 A2>9AB20 A 703>;>2:>< A =C6=>9 H8@8=>9 8 >BABC?><
return StringFormat("%*s%-*s%-lld",indent,"",w,header,rates.real_volume);
/* @8<5@ 2K2>40:
Real Volume: 0
*/
}
//+------------------------------------------------------------------+
//| K2>48B 2 6C@=0; >?8A0=85 2A5E ?>;59 AB@C:BC@K MqlRates |
//+------------------------------------------------------------------+
void MqlRatesPrint(const string symbol,const ENUM_TIMEFRAMES timeframe,const MqlRates &rates,
const bool short_entry=true,const uint header_width=0,const uint indent=0,int index=WRONG_VALUE)
{
//--- 1JO2;O5< ?5@5<5==CN 4;O E@0=5=8O @57C;LB0B0
string res="";
//--- >;CG05< :>;8G5AB2> 7=0:>2 ?>A;5 70?OB>9 8 AB@>:>2>5 7=0G5=85 B09<D@59<0
int dg=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
string tf=StringSubstr(EnumToString(timeframe==PERIOD_CURRENT ? Period() : timeframe),7);
string num=(index==WRONG_VALUE ? ":" : StringFormat("[%ld]:",index));
//--- A;8 :@0B:0O 70?8AL - 2K2>48< 2 6C@=0; 40==K5 10@0 2 D>@<0B5 Symbol TF[index]: Time, O, H, L, C, S, V/VR
if(short_entry)
{
res=StringFormat("%s %s%s %s, O: %.*f, H: %.*f, L: %.*f, C: %.*f, S: %2ld, V: %5lld, RV: %lld",
symbol,tf,num,(string)rates.time,dg,rates.open,dg,rates.high,dg,rates.low,dg,rates.close,rates.spread,rates.tick_volume,rates.real_volume);
Print(res);
}
/* @8<5@ 2K2>40:
GBPUSD H1: 2023.07.21 08:00:00, O: 1.28794, H: 1.28848, L: 1.28772, C: 1.28772, S: 4, V: 448, RV: 0
*/
//--- =0G5
else
{
//--- A>740Q< AB@>:C A >?8A0=85< 2A5E 40==KE AB@C:BC@K A >BABC?0<8 8 7040==>9 H8@8=>9 ?>;O 703>;>2:0
res=StringFormat("%s %s%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s\n%s",symbol,tf,num,
MqlRatesTime(rates,header_width,indent),
MqlRatesOpen(symbol,rates,header_width,indent),
MqlRatesHigh(symbol,rates,header_width,indent),
MqlRatesLow(symbol,rates,header_width,indent),
MqlRatesClose(symbol,rates,header_width,indent),
MqlRatesTickVolume(rates,header_width,indent),
MqlRatesSpread(rates,header_width,indent),
MqlRatesRealVolume(rates,header_width,indent)
);
//--- K2>48< 2 6C@=0; ?>;CG5==CN AB@>:C
Print(res);
}
/* @8<5@ 2K2>40 (5A;8 Last =5 =C;52>9):
Time: 2023.07.20 14:42:33
Bid: 1.28958
Ask: 1.28962
Last: 1.28947
Volume: 33
Time msc: 2023.07.20 14:42:33.401
Flags: BID|ASK
Volume Real: 33.45
@8<5@ 2K2>40 (5A;8 Last =C;52>9):
Time: 2023.07.20 14:42:33
Bid: 1.28958
Ask: 1.28962
Time msc: 2023.07.20 14:42:33.401
Flags: BID|ASK
*/
}
//+------------------------------------------------------------------+