Bars

为指定交易品种和周期返回历史计算中的字节数量。有两种变量函数可以调用。

要求所有历史字节

int  Bars(
   string           symbol_name,     // 交易品种名称
   ENUM_TIMEFRAMES  timeframe        // 周期
   );

为所选时间间隔要求历史字节

int  Bars(
   string           symbol_name,     // 交易品种名称
   ENUM_TIMEFRAMES  timeframe,       // 周期
   datetime         start_time,      // 启动日期和时间
   datetime         stop_time        // 结束日期和时间
   );

参量

symbol_name

[in]  交易品种名称。

timeframe

[in]  周期。

start_time

[in]  与第一元素相一致的字节时间。

stop_time

[in]  与最后一个元素相一致的字节时间。

返回值

如果定义start_time 和 stop_time参量,函数返回指定时间间隔的自己数量,否则返回全部字节数量。

注释

如果指定参量的时序列数据通过Bars()函数调用在终端形成,或者时序列数据在函数调用时不在synchronized交易服务器中,函数返回0值。

当请求指定时间间隔的柱形图数量,只有在开盘时间间隔内的柱形图才被考虑。例如,如果一周当前日是星期六,请求就是W1周期的柱数,start_time=last_tuesday 和 stop_time=last_friday,由于W1时间周期的开盘时间始终是星期日并且在指定间隔内没有一个单独的W1柱形图,所以函数将返回0。

所有历史柱形图数量的示例请求:

   int bars=Bars(_Symbol,_Period);
   if(bars>0)
     {
      Print("Number of bars in the terminal history for the symbol-period at the moment = ",bars);
     }
   else  //无有效柱
     {
      //--- 交易品种数据不能和服务器数据同步
      bool synchronized=false;
      //--- 循环计数器
      int attempts=0;
      // 进行5次尝试等候同步进行
      while(attempts<5)
        {
         if(SeriesInfoInteger(Symbol(),0,SERIES_SYNCHRONIZED))
           {
            //--- 同步化完成,退出
            synchronized=true;
            break;
           }
         //--- 增加计数器
         attempts++;
         //--- 等候10毫秒直至嵌套反复
         Sleep(10);
        }
      //--- 同步化后退出循环
      if(synchronized)
        {
         Print("Number of bars in the terminal history for the symbol-period at the moment = ",bars);
         Print("The first date in the terminal history for the symbol-period at the moment = ",
               (datetime)SeriesInfoInteger(Symbol(),0,SERIES_FIRSTDATE));
         Print("The first date in the history for the symbol on the server = ",
               (datetime)SeriesInfoInteger(Symbol(),0,SERIES_SERVER_FIRSTDATE));
        }
      //--- 不发生数据同步
      else
        {
         Print("Failed to get number of bars for ",_Symbol);
        }
     }

指定间隔中柱形图数量的示例请求:

   int n;
   datetime date1 = D'2016.09.02 23:55'// 星期五
   datetime date2 = D'2016.09.05 00:00'// 星期一
   datetime date3 = D'2016.09.08 00:00'// 星期四
   //---
   n=Bars(_Symbol,PERIOD_H1,D'2016.09.02 02:05',D'2016.09.02 10:55');
   Print("Number of bars: ",n); // 输出:"柱形图数:8",计算中考虑H2柱形图,而非H11柱形图
   n=Bars(_Symbol,PERIOD_D1,date1,date2);
   Print("Number of bars: ",n); // 输出:"柱形图数:1",由于单独D1(星期一)柱形图开盘时间在间隔内
   n=Bars(_Symbol,PERIOD_W1,date2,date3);
   Print("Number of bars: ",n); // 输出:"柱形图数:0",由于没有一个W1柱形图开盘时间在指定间隔内

另见

事件处理函数