ObjectGetTimeByValue

指定物件的指定价格值,函数返回时间值。

datetime  ObjectGetTimeByValue(
   long    chart_id,     // 图表标识符
   string  name,         // 物件名称
   double  value,        // 价格
   int     line_id       // 线
   );

参量

chart_id

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

name

[in]  物件名称。

value

[in]  价格值。

line_id

[in]  线路标识符。

返回值

为指定物件的指定价格值设定时间值。

注释

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

一个对象在一个价格坐标内可以有多个值,因此有必要指明行号。这个函数仅应用于以下对象:

  • 趋势线(OBJ_TREND)
  • 角度趋势线(OBJ_TRENDBYANGLE)
  • 江恩线(OBJ_GANNLINE)
  • 等距通道(OBJ_CHANNEL) - 2行
  • 线性回归通道 (OBJ_REGRESSION) - 3行
  • 标准偏差通道(OBJ_STDDEVCHANNEL) - 3行
  • 箭头线 (OBJ_ARROWED_LINE)

 

示例:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   OBJ_NAME   "TestObjectGetTimeByValue" // 图形对象名称
#define   STEP       100                        // 价格步长
 
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- ID, 交易品种
   long   chart_id=ChartID();
   string chart_symbol=ChartSymbol(chart_id);
   
//--- 获取图表交易品种的点值
   double point=SymbolInfoDouble(chart_symbolSYMBOL_POINT);
   if(point==0)
     {
      PrintFormat("Failed to get the Point value of the \"%s\" symbol. Error %d"chart_symbolGetLastError());
      return;
     }
     
//---从左侧可见柱形的高点到右侧可见柱形的低点,构建一个等距的通道
   if(!CreateChannel(chart_id))
      return;
   
//--- 图表上的最大值和最小值, 图表交易品种小数位数
   double chart_max=ChartGetDouble(chart_idCHART_PRICE_MAX);
   double chart_min=ChartGetDouble(chart_idCHART_PRICE_MIN);
   int    digits=(int)SymbolInfoInteger(chart_symbolSYMBOL_DIGITS);
 
//--- 当计算出的价格大于图表的最小价格值时,
//--- 使用 STEP 循环查看图表价格并获得时间值
//--- 对于等距通道的每条线的计算价格值。
//---将每行的接收时间输出到日志
   int index=0;
   double price=chart_max;
   do
     {
      price=chart_max-STEP*index*point;
      datetime time0=ObjectGetTimeByValue(chart_idOBJ_NAMEprice0);
      datetime time1=ObjectGetTimeByValue(chart_idOBJ_NAMEprice1);
      string   time0_str=(time0>0 ? TimeToString(time0) : "No value at this price");
      string   time1_str=(time1>0 ? TimeToString(time1) : "No value at this price");
      string   idx=StringFormat("%02d"index);
      PrintFormat("[%s] For price %.*f the time value at line 0: %s, at line 1: %s"idxdigitspricetime0_strtime1_str);
      index++;
     }
   while(!IsStopped() && price>=chart_min);
   
//--- 等待5秒并清理
   Sleep(5000);
   ObjectDelete(chart_idOBJ_NAME);
   ChartRedraw(chart_id);
   /*
   结果:
   [00For price 1.26110 the time value at line 0No value at this price,  at line 1No value at this price
   [01For price 1.26010 the time value at line 02024.12.30 17:00at line 1No value at this price
   [02For price 1.25910 the time value at line 02024.12.30 22:30at line 1No value at this price
   [03For price 1.25810 the time value at line 02024.12.31 04:00at line 12024.12.30 16:30
   [04For price 1.25710 the time value at line 02024.12.31 10:00at line 12024.12.30 22:00
   [05For price 1.25610 the time value at line 02024.12.31 15:30at line 12024.12.31 03:30
   [06For price 1.25510 the time value at line 02024.12.31 21:00at line 12024.12.31 09:00
   [07For price 1.25410 the time value at line 02025.01.02 03:30at line 12024.12.31 14:30
   [08For price 1.25310 the time value at line 0No value at this priceat line 12024.12.31 20:30
   [09For price 1.25210 the time value at line 0No value at this priceat line 12025.01.02 03:00
   [10For price 1.25110 the time value at line 0No value at this priceat line 1No value at this price
   [11For price 1.25010 the time value at line 0No value at this priceat line 1No value at this price
   [12For price 1.24910 the time value at line 0No value at this priceat line 1No value at this price
   [13For price 1.24810 the time value at line 0No value at this priceat line 1No value at this price
   */
  }
//+--------------------------------------------------------------------------------------------+
//| 从左侧柱形的高点到右侧柱形的低点构建一个等距通道                                                  |
//+--------------------------------------------------------------------------------------------+
bool CreateChannel(const long chart_id=0)
  {
   long     bar1  =0bar2  =0visible=0;
   datetime time1 =0time2 =0;
   double   price1=0price2=0;
 
//--- 取得图表左侧可见的第一个柱形
   ResetLastError();
   if(!ChartGetInteger(chart_idCHART_FIRST_VISIBLE_BAR0bar1))
     {
      PrintFormat("%s: ChartGetInteger() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
//--- 图表上可见的柱数
   if(!ChartGetInteger(chart_idCHART_VISIBLE_BARS0visible))
     {
      PrintFormat("%s: ChartGetInteger() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
 
//--- 调整获得的数值并计算右侧可见的第一个柱的索引
   bar1-=1;
   visible-=2;
   bar2=bar1-visible;
   
//--- 图表交易品种
   string symbol=ChartSymbol(chart_id);
   
//--- 取得图表左侧可见的第一个柱的时间
   ResetLastError();
   datetime time_array[1];
   if(CopyTime(symbolPERIOD_CURRENT, (int)bar11time_array)!=1)
     {
      PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   time1=time_array[0];
   
//--- 取得图表右侧可见的第一个柱的时间
   if(CopyTime(symbolPERIOD_CURRENT, (int)bar21time_array)!=1)
     {
      PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   time2=time_array[0];
   
//--- 取得图表左侧第一个可见柱形的最高价
   double price_array[];
   if(CopyHigh(symbolPERIOD_CURRENT, (int)bar11price_array)!=1)
     {
      PrintFormat("%s: CopyHigh() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   price1=price_array[0];
   
//--- 取得图表右侧第一个可见柱形的最低价
   if(CopyLow(symbolPERIOD_CURRENT, (int)bar21price_array)!=1)
     {
      PrintFormat("%s: CopyLow() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   price2=price_array[0];
   
//--- 计算图表上的价格范围点数
//--- 对于等距通道, 第二条线的距离将是价格范围的 1/3
   double range=price1-price2;
   double distance=range*0.3;
   
//--- 在计算的坐标处创建一个图形对象 - 一个等距通道
   if(!ObjectCreate(chart_idOBJ_NAMEOBJ_CHANNEL0time1price1time2price2time1price1-distance))
     {
      PrintFormat("%s: ObjectCreate() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
     
//--- 更新图表并返回 'true'
   ChartRedraw(chart_id);
   return(true);
  }

 

另见

对象类型