ObjectsTotal

在指定类型的指定子窗口的指定图表物件中,函数返回物件数量。

int  ObjectsTotal(
   long  chart_id,           // 图表标识符
   int   sub_window=-1,      // 窗口索引
   int   type=-1             // 物件类型
   );

参量

chart_id

[in]  图表标识符。0表示当前图表。

sub_window=-1

[in]  图表子窗口的数量。0代表主图表窗口,-1代表图表的所有子窗口,包括主窗口。

type=-1

[in]  物件类型。值可以是 ENUM_OBJECT 值中一个,-1表示全部类型。

返回值

物件数量。

注意

该函数使用同步调用,这意味着这个函数等待执行在调用之前已入列图表的所有命令,这就是该函数耗费时间的原因。当处理图表上的大量对象时应该考虑这个特性。

 

示例:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 图表 ID
   long chart_id=ChartID();
   
//---获取图表子窗口包括主窗口的数量
   long wnd=0;
   ResetLastError();
   if(!ChartGetInteger(chart_idCHART_WINDOWS_TOTAL0wnd))
     {
      Print("ChartGetInteger() failed. Error "GetLastError());
      return;
     }
   
//--- 获取并在日志中显示每个子窗口中图形对象的数量
   for(int i=0i<(int)wndi++)
     {
      int objects=ObjectsTotal(chart_idi);
      string wnd_head=(i==0 ? "The main chart window" : StringFormat("The window with index %d of the chart"i));
      PrintFormat("%s contains %d graphic objects"wnd_headobjects);
     }
   /*
   具有两个子窗口的主窗口的结果,
   其中主窗口包含交易标签,
   而子窗口中有两个图形对象:
   The main chart window contains 656 graphic objects
   The window with index 1 of the chart contains 2 graphic objects 
   The window with index 2 of the chart contains 2 graphic objects
   */
  }