iBarShift

根据时间搜索柱形图。这个函数返回与指定时间对应的柱形图指数。

int  iBarShift(

   const string        symbol,          // 交易品种

   ENUM_TIMEFRAMES     timeframe,       // 周期

   datetime            time,            // 时间

   bool                exact=false      // 模式

   );

参数

交易品种

[in]  金融工具的交易品种名称。NULL表示当前交易品种。

timeframe

[in]  周期。它可以是ENUM_TIMEFRAMES其中一个枚举值。PERIOD_CURRENT 表示当前图表周期。

时间

[in]  要搜索的时间值。

exact=false

[in]  返回值,如果指定时间的柱形图没有找到。如果exact=falseiBarShift返回最近柱形图的指数,其开仓时间小于指定时间(time_open<time)。如果这个柱形图没有找到(没有指定时间之前的历史记录),那么该函数返回 -1。如果exact=true,iBarShift不会搜索最近的柱形图而是立即返回 -1。

返回值

与指定时间对应的柱形图指数。如果与指定时间对应的柱形图没有找到(历史记录中有一个空白),那么函数返回 -1或最近柱形图的指数(取决于'exact'参数)。

例如:

//+------------------------------------------------------------------+

//| 脚本程序起始函数                                                   |

//+------------------------------------------------------------------+

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)

*/