ObjectGetValueByTime

Retourne la valeur du prix pour le temps indiqué de l'objet indiqué.

double  ObjectGetValueByTime(
   long      chart_id,     // identificateur du graphique
   string    name,         // nom de l'objet
   datetime  time,         // temps 
   int       line_id       // ligne
   );

Paramètres

chart_id

[in]  L'identificateur du graphique. 0 signifie le graphique courant.

name

[in]  Le nom de l'objet.

time

[in] La valeur du temps.

line_id

[in]  L'identificateur de la ligne.

Valeur de Retour

La valeur du prix pour le temps indiqué de l'objet indiqué.

Note

La fonction utilise un appel synchrone, ce qui signifie que la fonction attend la fin de l'exécution de toutes les commandes présentes dans la queue de ce graphique avant cet appel. C'est pourquoi cette fonction peut être consommatrice en terme de temps. Cette caractéristique doit être prise en compte lors de l'utilisation d'un grand nombre d'objets sur un graphique.

Puisqu'un objet à une coordonnée de prix peut avoir plusieurs valeurs, il est nécessaire d'indiquer le numéro de la ligne. Cette fonction est employée seulement pour les objets suivants :

  • La ligne de tendance (OBJ_TREND)
  • La ligne de tendance selon un angle (OBJ_TRENDBYANGLE)
  • Le ligne de Gann (OBJ_GANNLINE)
  • Le canal équidistant (OBJ_CHANNEL) - 2 lignes
  • Le canal de régression linéaire (OBJ_REGRESSION) - 3 lignes
  • Le canal d'écart type (OBJ_STDDEVCHANNEL) - 3 lignes
  • La ligne flêchée (OBJ_ARROWED_LINE)

 

Exemple :

#property copyright "Copyright 2025, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
 
#define   OBJ_NAME   "TestObjectGetValueByTime" // nom de l'objet graphique
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
//--- identifiant du graphique, symbole
   long   chart_id=ChartID();
   string symbol=ChartSymbol(chart_id);
 
   long bar1=0bar2=0visible=0;
//--- récupère la première barre du graphique visible à gauche
   ResetLastError();
   if(!ChartGetInteger(chart_idCHART_FIRST_VISIBLE_BAR0bar1))
     {
      Print("ChartGetInteger() failed. Error "GetLastError());
      return;
     }
//--- nombre de barres visibles sur le graphique
   if(!ChartGetInteger(chart_idCHART_VISIBLE_BARS0visible))
     {
      Print("ChartGetInteger() failed. Error "GetLastError());
      return;
     }
 
//--- ajuste les valeurs obtenues et calcule l'indice de la première barre visible à droite.
   bar1-=1;
   visible-=2;
   bar2=bar1-visible;
 
//--- crée un canal équidistant entre le plus haut de la barre visible de gauche et le plus bas de celle de droite.
   if(!CreateChannel(chart_id, (int)bar1, (int)bar2))
      return;
   
   int digits=(int)SymbolInfoInteger(symbol,SYMBOL_DIGITS);
   
//--- en boucle, de la barre visible de gauche à la barre visible de droite sur le graphique
//--- récupère la valeur du prix pour la barre en cours dans la boucle de chaque ligne du canal équidistante.
//--- affiche le prix reçu pour chaque ligne du journal
   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);
     }
   
//--- attends 5 secondes et nettoie tout
   Sleep(5000);
   ObjectDelete(chart_idOBJ_NAME);
   ChartRedraw(chart_id);
   /*
   résultat :
   [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
   */
  }
//+------------------------------------------------------------------+
//| Retourne l'heure de la barre spécifiée par l'index               |
//+------------------------------------------------------------------+
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]);
  }
//+--------------------------------------------------------------------------------------------+
//| Construit un canal équidistant du +haut de la barre de gauche au +bas de celle de droite |
//+--------------------------------------------------------------------------------------------+
bool CreateChannel(const long chart_idconst int bar1const int bar2)
  {
   long     visible=0;
   datetime time1 =0time2 =0;
   double   price1=0price2=0;
 
//--- symbole du graphique
   string symbol=ChartSymbol(chart_id);
   
//--- récupère l'heure de la première barre visible sur la gauche du graphique
   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];
   
//--- récupère l'heure de la première barre du graphique visible sur la droite
   if(CopyTime(symbolPERIOD_CURRENT, (int)bar21time_array)!=1)
     {
      PrintFormat("%s: CopyTime() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   time2=time_array[0];
   
//--- récupère le prix le Plus Haut de la première barre visible sur la gauche du graphique
   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];
   
//--- récupère le prix le Plus Bas de la première barre visible sur la droite du graphique
   if(CopyLow(symbolPERIOD_CURRENT, (int)bar21price_array)!=1)
     {
      PrintFormat("%s: CopyLow() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
   price2=price_array[0];
   
//--- calcule la plage de prix du graphique en points
//--- pour un canal équidistant, la distance de la deuxième ligne sera égale à 1/3 de la plage de prix
   double range=price1-price2;
   double distance=range*0.3;
   
//--- aux coordonnées calculées, crée un objet graphique : le canal équidistant
   if(!ObjectCreate(chart_idOBJ_NAMEOBJ_CHANNEL0time1price1time2price2time1price1-distance))
     {
      PrintFormat("%s: ObjectCreate() failed. Error %d",__FUNCTION__GetLastError());
      return(false);
     }
     
//--- met à jour le graphique et retourne 'true'
   ChartRedraw(chart_id);
   return(true);
  }

 

Voir aussi

Les types des objets