ObjectGetTimeByValue

The function returns the time value for the specified price value of the specified object.

datetime  ObjectGetTimeByValue(
   long    chart_id,     // chart identifier
   string  name,         // object name
   double  value,        // Price
   int     line_id       // Line number
   );

Parameters

chart_id

[in]  Chart identifier. 0 means the current chart.

name

[in]  Name of the object.

value

[in]  Price value.

line_id

[in]  Line identifier.

Return Value

The time value for the specified price value of the specified object.

Note

The function uses a synchronous call, which means that the function waits for the execution of all commands that have been enqueued for this chart prior to its call, that is why this function can be time consuming. This feature should be taken into account when working with a large number of objects on a chart.

An object can have several values in one price coordinate, therefore it is necessary to specify the line number. This function applies only to the following objects:

  • Trendline (OBJ_TREND)
  • Trendline by angle (OBJ_TRENDBYANGLE)
  • Gann line (OBJ_GANNLINE)
  • Equidistant channel (OBJ_CHANNEL) - 2 lines
  • Linear regression channel (OBJ_REGRESSION) - 3 lines
  • Standard deviation channel (OBJ_STDDEVCHANNEL) - 3 lines
  • Arrowed line (OBJ_ARROWED_LINE)

 

Example:

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   OBJ_NAME   "TestObjectGetTimeByValue" // graphical object name
#define   STEP       100                        // price step
 
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- ID, symbol
   long   chart_id=ChartID();
   string chart_symbol=ChartSymbol(chart_id);
   
//--- get the Point value of the chart symbol
   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;
     }
     
//--- build an equidistant channel from the High of the left visible bar to the Low of the right one
   if(!CreateChannel(chart_id))
      return;
   
//--- chart maximum and minimum, chart symbol Digits
   double chart_max=ChartGetDouble(chart_idCHART_PRICE_MAX);
   double chart_min=ChartGetDouble(chart_idCHART_PRICE_MIN);
   int    digits=(int)SymbolInfoInteger(chart_symbolSYMBOL_DIGITS);
 
//--- while the calculated price is greater than the minimum price value of the chart,
//--- go in a loop through the chart prices with STEP and get the time value
//--- for the calculated price value of each line of the equidistant channel.
//--- output the received time for each line to the journal
   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);
   
//--- wait 5 seconds and clean up
   Sleep(5000);
   ObjectDelete(chart_idOBJ_NAME);
   ChartRedraw(chart_id);
   /*
   result:
   [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
   */
  }
//+--------------------------------------------------------------------------------------------+
//| Construct an equidistant channel from the High of the left bar to the Low of the right bar |
//+--------------------------------------------------------------------------------------------+
bool CreateChannel(const long chart_id=0)
  {
   long     bar1  =0bar2  =0visible=0;
   datetime time1 =0time2 =0;
   double   price1=0price2=0;
 
//--- get the first bar of the chart visible on the left
   ResetLastError();
   if(!ChartGetInteger(chart_idCHART_FIRST_VISIBLE_BAR0bar1))
     {
      PrintFormat("%s: ChartGetInteger() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
//--- number of visible bars on the chart
   if(!ChartGetInteger(chart_idCHART_VISIBLE_BARS0visible))
     {
      PrintFormat("%s: ChartGetInteger() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
 
//--- adjust the obtained values and calculate the index of the first bar visible on the right
   bar1-=1;
   visible-=2;
   bar2=bar1-visible;
   
//--- chart symbol
   string symbol=ChartSymbol(chart_id);
   
//--- get the time of the first bar visible on the left of the chart
   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];
   
//--- get the time of the first bar of the chart visible on the right
   if(CopyTime(symbolPERIOD_CURRENT, (int)bar21time_array)!=1)
     {
      PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   time2=time_array[0];
   
//--- get the High price of the first bar visible on the left of the chart
   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];
   
//--- get the Low price of the first bar visible on the right of the chart
   if(CopyLow(symbolPERIOD_CURRENT, (int)bar21price_array)!=1)
     {
      PrintFormat("%s: CopyLow() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   price2=price_array[0];
   
//--- calculate the price range of the chart in points
//--- for an equidistant channel, the distance of the second line will be 1/3 of the price range
   double range=price1-price2;
   double distance=range*0.3;
   
//--- at the calculated coordinates, create a graphical object - an equidistant channel
   if(!ObjectCreate(chart_idOBJ_NAMEOBJ_CHANNEL0time1price1time2price2time1price1-distance))
     {
      PrintFormat("%s: ObjectCreate() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
     
//--- update the chart and return 'true'
   ChartRedraw(chart_id);
   return(true);
  }

 
See also

Object Types