//+------------------------------------------------------------------+
//| 脚本程序起始函数 |
//+------------------------------------------------------------------+
void OnStart()
{
//--- 日期为星期日
datetime time=D'2002.04.25 12:00';
string symbol="GBPUSD";
ENUM_TIMEFRAMES tf=PERIOD_H1;
bool exact=false;
//--- 如果在指定时间没有柱形图,那么iBarShift将返回最近柱形图的指数。
int bar_index=iBarShift(symbol,tf,time,exact);
//--- 调用iBarShift()之后,检查错误代码
int error=GetLastError();
if(error!=0)
{
PrintFormat("iBarShift(): GetLastError=%d - The requested date %s "+
"for %s %s is not found in the available history",
error,TimeToString(time),symbol,EnumToString(tf));
return;
}
//--- iBarShift()函数被成功执行,返回结果exact=false
PrintFormat("1. %s %s %s(%s): bar index is %d (exact=%s)",
symbol,EnumToString(tf),TimeToString(time),
DayOfWeek(time),bar_index,string(exact));
datetime bar_time=iTime(symbol,tf,bar_index);
PrintFormat("Time of bar #%d is %s (%s)",
bar_index,TimeToString(bar_time),DayOfWeek(bar_time));
//--- 请求指定时间的柱形图指数;如果没有柱形图则将返回-1
exact=true;
bar_index=iBarShift(symbol,tf,time,exact);
//--- iBarShift()函数被成功执行,返回结果exact=true
PrintFormat("2. %s %s %s (%s):bar index is %d (exact=%s)",
symbol,EnumToString(tf),TimeToString(time)
,DayOfWeek(time),bar_index,string(exact));
}
//+------------------------------------------------------------------+
//| 返回一周的每日名称 |
//+------------------------------------------------------------------+
string DayOfWeek(const datetime time)
{
MqlDateTime dt;
string day="";
TimeToStruct(time,dt);
switch(dt.day_of_week)
{
case 0: day=EnumToString(SUNDAY);
break;
case 1: day=EnumToString(MONDAY);
break;
case 2: day=EnumToString(TUESDAY);
break;
case 3: day=EnumToString(WEDNESDAY);
break;
case 4: day=EnumToString(THURSDAY);
break;
case 5: day=EnumToString(FRIDAY);
break;
default:day=EnumToString(SATURDAY);
break;
}
//---
return day;
}
//+------------------------------------------------------------------+
/* Execution result
1. GBPUSD PERIOD_H1 2018.06.10 12:00(SUNDAY): bar index is 64 (exact=false)
Time of bar #64 is 2018.06.08 23:00 (FRIDAY)
2. GBPUSD PERIOD_H1 2018.06.10 12:00 (SUNDAY):bar index is -1 (exact=true)
*/
|