跳至内容

ObjectCreate

ObjectCreate

该函数用于创建具有指定名称、类型以及指定图表子窗口中初始坐标的对象。该函数有两种实现方式:

bool  ObjectCreate(
   long          chart_id,      // chart ID
   string        object_name,   // object name
   ENUM_OBJECT   object_type,   // object type
   int           sub_window,    // window index
   datetime      time1,         // time of the first anchor point
   double        price1,        // price of the first anchor point
   ...
   datetime      timeN=0,       // time of the N-th anchor point
   double        priceN=0       // price of the N-th anchor point
   );

该函数用于创建具有指定名称、类型以及指定图表子窗口中初始坐标的对象:

bool  ObjectCreate(
   string        object_name,   // object name
   ENUM_OBJECT   object_type,   // object type
   int           sub_window,    // window index
   datetime      time1,         // time of the first anchor point
   double        price1,        // price of the first anchor point
   datetime      time2=0,       // time of the second anchor point
   double        price2=0,      // price of the second anchor point
   datetime      time3=0,       // time of the third anchor point
   double        price3=0       // price of the third anchor point
   );

参数

chart_id

[in] 图表标识符。

object_name

[in] 对象名称。该名称必须在图表及其子窗口内唯一。

object_type

[in] 对象类型。值可以是ENUM_OBJECT枚举中的某个值。

sub_window

[in] 图表子窗口的编号。0表示主图表窗口。指定的子窗口必须存在(窗口索引必须大于等于0且小于WindowsTotal()),否则函数返回false。

time1

[in] 第一个锚点的时间坐标。

price1

[in] 第一个锚点的价格坐标。

time2=0

[in] 第二个锚点的时间坐标。

price2=0

[in] 第二个锚点的价格坐标。

time3=0

[in] 第三个锚点的时间坐标。

price3=0

[in] 第三个锚点的价格坐标。

timeN=0

[in] N个锚点的时间坐标。

priceN=0

[in] N个锚点的价格坐标。

返回值

当该函数应用于当前图表时,会直接访问该图表(这是MQL4中的常见行为),返回true表示对象创建成功;否则返回false。在这种情况下,应调用GetLastError()以获取有关错误的更多信息。

如果要创建不同大小的对象,则使用异步函数调用,ObjectCreate()仅返回命令添加到图表队列的结果。在这种情况下,true表示对象创建命令已成功入队,但命令执行结果未知。要检查命令执行结果,可以使用ObjectFind函数或任何请求对象属性的其他函数,如ObjectGetXXX。然而,请注意,这些函数被添加到该图表的队列末尾,它们等待执行结果(由于同步调用),因此可能会很耗时。

注意

对象名称不应超过63个字符。不属于当前代码页的字符不允许使用(无法从Unicode转换为ANSI的字符将被替换為’?’)。如果程序要分发给具有不同代码页的用户,我们强烈建议使用拉丁字符作为对象名称。

OBJ_LABEL类型的对象忽略坐标。使用ObjectSet()函数设置OBJPROP_XDISTANCE和OBJPROP_YDISTANCE属性。图表子窗口(如果有带有图表中的指示器的子窗口)从1开始编号。图表主窗口始终存在,索引为0。坐标必须成对传递:时间和价格。例如,OBJ_VLINE对象只需要时间,但价格(任何值)也必须传递。

示例:

//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
int start()
  {
   int i;
   string obj_name="label_object";
   long current_chart_id=ChartID();
//--- creating label object (it does not have time/price coordinates)
   if(!ObjectCreate(current_chart_id,obj_name,OBJ_LABEL,0,0,0))
     {
      Print("Error: can't create label! code #",GetLastError());
      return(0);
     }
//--- set color to Red
   ObjectSetInteger(current_chart_id,obj_name,OBJPROP_COLOR,clrRed);
//--- move object down and change its text
   for(i=0; i<200; i++)
     {
      //--- set text property
      ObjectSetString(current_chart_id,obj_name,OBJPROP_TEXT,StringFormat("Simple Label at y= %d",i));
      //--- set distance property
      ObjectSet(obj_name,OBJPROP_YDISTANCE,i);
      //--- forced chart redraw
      ChartRedraw(current_chart_id);
      Sleep(10);
     }
//--- set color to Blue
   ObjectSetInteger(current_chart_id,obj_name,OBJPROP_COLOR,clrBlue);
//--- move object up and change its text
   for(i=200; i>0; i--)
     {
      //--- set text property
      ObjectSetString(current_chart_id,obj_name,OBJPROP_TEXT,StringFormat("Simple Label at y= %d",i));
      //--- set distance property
      ObjectSet(obj_name,OBJPROP_YDISTANCE,i);
      //--- forced chart redraw
      ChartRedraw(current_chart_id);
      Sleep(10);
     }
//--- delete object
   ObjectDelete(obj_name);
   return(0);
  }

另请参阅

对象类型

最后更新于