ObjectGetValueByTime

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

double  ObjectGetValueByTime(
   long      chart_id,     // 图表标识符
   string    name,         // 物件名称
   datetime  time,         // 时间
   int       line_id       // 线
   );

参量

chart_id

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

name

[in]  物件名称。

time

[in]  时间值。

line_id

[in]  线形ID。

返回值

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

注释

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

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

  • 趋势线(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   "TestObjectGetValueByTime" // 图形对象的名称
//+------------------------------------------------------------------+
//| 脚本程序起始函数                                                   |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- 图表 ID, 交易品种
   long   chart_id=ChartID();
   string symbol=ChartSymbol(chart_id);
 
   long bar1=0bar2=0visible=0;
//--- 取得图表左侧可见的第一个柱形
   ResetLastError();
   if(!ChartGetInteger(chart_idCHART_FIRST_VISIBLE_BAR0bar1))
     {
      Print("ChartGetInteger() failed. Error "GetLastError());
      return;
     }
//--- 图表上可见的柱数
   if(!ChartGetInteger(chart_idCHART_VISIBLE_BARS0visible))
     {
      Print("ChartGetInteger() failed. Error "GetLastError());
      return;
     }
 
//--- 调整获得的数值并计算右侧可见的第一个柱的索引
   bar1-=1;
   visible-=2;
   bar2=bar1-visible;
 
//---从左侧可见柱形的高点到右侧可见柱形的低点,构建一个等距的通道
   if(!CreateChannel(chart_id, (int)bar1, (int)bar2))
      return;
   
   int digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
   
//--- 在图表上左侧可见柱形到右侧可见柱形的循环中
//--- 循环取得每条等距通道线的柱形时间对应的价格值
//--- 在日志中显示获取的每条线上的价格值
   for(int i=(int)bar1i>=bar2 && !IsStopped(); i--)
     {
      datetime time=GetTime(symboli);
      if(time==0)
         continue;
      
      string time_str=TimeToString(time);
      double value0=ObjectGetValueByTime(chart_idOBJ_NAMEtime0);
      double value1=ObjectGetValueByTime(chart_idOBJ_NAMEtime1);
      string idx=StringFormat("%03d"i);
      PrintFormat("[%s] For time %s the price value at 0 line of the object: %.*f, at line 1: %.*f",
                  idxTimeToString(time), digitsvalue0digitsvalue1);
     }
   
//--- 等待5秒并清理
   Sleep(5000);
   ObjectDelete(chart_idOBJ_NAME);
   ChartRedraw(chart_id);
   /*
   结果:
   [114For time 2025.01.02 05:00 the price value at 0 line of the object1.03732at line 11.03393
   [113For time 2025.01.02 05:30 the price value at 0 line of the object1.03694at line 11.03355
   [112For time 2025.01.02 06:00 the price value at 0 line of the object1.03657at line 11.03318
   [111For time 2025.01.02 06:30 the price value at 0 line of the object1.03619at line 11.03280
   [110For time 2025.01.02 07:00 the price value at 0 line of the object1.03581at line 11.03242
   [109For time 2025.01.02 07:30 the price value at 0 line of the object1.03544at line 11.03205
   [108For time 2025.01.02 08:00 the price value at 0 line of the object1.03506at line 11.03167
   [107For time 2025.01.02 08:30 the price value at 0 line of the object1.03468at line 11.03129
   [106For time 2025.01.02 09:00 the price value at 0 line of the object1.03431at line 11.03092
   [105For time 2025.01.02 09:30 the price value at 0 line of the object1.03393at line 11.03054
   [104For time 2025.01.02 10:00 the price value at 0 line of the object1.03355at line 11.03016
   [103For time 2025.01.02 10:30 the price value at 0 line of the object1.03318at line 11.02979
   [102For time 2025.01.02 11:00 the price value at 0 line of the object1.03280at line 11.02941
   [101For time 2025.01.02 11:30 the price value at 0 line of the object1.03242at line 11.02903
   [100For time 2025.01.02 12:00 the price value at 0 line of the object1.03205at line 11.02866
   [099For time 2025.01.02 12:30 the price value at 0 line of the object1.03167at line 11.02828
   [098For time 2025.01.02 13:00 the price value at 0 line of the object1.03129at line 11.02790
   [097For time 2025.01.02 13:30 the price value at 0 line of the object1.03092at line 11.02753
   [096For time 2025.01.02 14:00 the price value at 0 line of the object1.03054at line 11.02715
   [095For time 2025.01.02 14:30 the price value at 0 line of the object1.03016at line 11.02677
   [094For time 2025.01.02 15:00 the price value at 0 line of the object1.02979at line 11.02640
   [093For time 2025.01.02 15:30 the price value at 0 line of the object1.02941at line 11.02602
   [092For time 2025.01.02 16:00 the price value at 0 line of the object1.02903at line 11.02564
   [091For time 2025.01.02 16:30 the price value at 0 line of the object1.02866at line 11.02527
   [090For time 2025.01.02 17:00 the price value at 0 line of the object1.02828at line 11.02489
   [089For time 2025.01.02 17:30 the price value at 0 line of the object1.02790at line 11.02451
   [088For time 2025.01.02 18:00 the price value at 0 line of the object1.02753at line 11.02414
   [087For time 2025.01.02 18:30 the price value at 0 line of the object1.02715at line 11.02376
   [086For time 2025.01.02 19:00 the price value at 0 line of the object1.02677at line 11.02338
   [085For time 2025.01.02 19:30 the price value at 0 line of the object1.02640at line 11.02301
   [084For time 2025.01.02 20:00 the price value at 0 line of the object1.02602at line 11.02263
   */
  }
//+------------------------------------------------------------------+
//| 返回指定索引的柱形的时间                                            |
//+------------------------------------------------------------------+
datetime GetTime(const string symbol_nameconst int index)
  {
   if(index<0)
      return(0);
   datetime array[1];
   ResetLastError();
   if(CopyTime(symbol_namePERIOD_CURRENTindex1array)!=1)
     {
      PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__GetLastError());
      return(0);
     }
   return(array[0]);
  }
//+--------------------------------------------------------------------------------------------+
//| 从左侧柱形的高点到右侧柱形的低点构建一个等距通道                                                  |
//+--------------------------------------------------------------------------------------------+
bool CreateChannel(const long chart_idconst int bar1const int bar2)
  {
   long     visible=0;
   datetime time1 =0time2 =0;
   double   price1=0price2=0;
 
//--- 图表交易品种
   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);
  }

 

另见

对象类型