ObjectGetValueByTime() Problem

 

Hi . i create Regression Line with code :

void OnTick()
  {

   datetime time_0= iTime(_Symbol,PERIOD_H1,1);
   datetime time_1= iTime(_Symbol,PERIOD_H1,29);


   ObjectDelete(0,"a");
   ObjectCreate(0,"a",OBJ_REGRESSION,0,time_1,0,time_0,0);

   ChartRedraw();

  Sleep(50);
  double val0=ObjectGetValueByTime(0,"a",time_0,0);
  Sleep(50);
  double val1=ObjectGetValueByTime(0,"a",time_1,0);

   Print("Val_0 : ",val0);
   Print("Val_1 : ",val1);
   }

The regression line between candles 1 and 29 is correctly drawn.

But when I want to get the values of the middle line of regression in candles 1 and 29 using the ObjectGetValueByTime() function, the value 0 is printed.


In the  strategy tester , the program works correctly and variable values are printed! But in real time 0 values are printed!


To solve the problem, I used the Sleep function and the problem was solved to some extent, but the value 0 is printed from time to time!


Thank you for your guidance.


please see below Gif file

gif

 
Print not only the values but also the all the relevant timestamps and other values: time_0, time_1,_Period, and the results of the ObjectCreation and of course _LastError!
 
Carl Schreiber #:
Print not only the values but also the all the relevant timestamps and other values: time_0, time_1,_Period, and the results of the ObjectCreation and of course _LastError!


   Print("Val_0 : ",val0);
   Print("Val_1 : ",val1);
   Print("time_0 : ",time_0);
   Print("time_1 : ",time_1);
   Print("Period : ",_Period);
   Print("Last error : ", GetLastError());
   



 

Read the text below the title: Object functions

It says: 

Thus, do not expect an immediate visual update of graphical objects after calling these functions. Generally, the graphical objects on the chart are updated automatically by the terminal following the change events - a new quote arrival, resizing the chart window, etc. Use ChartRedraw() function to forcefully update the graphical objects.


Although it claims that ChartRedraw() will/should update chart, in my experience, it still does not work every time, no matter how long sleep you use. 

Try to verify that object exists with ObjectFind().


Also, you are deleting and creating an object on every tick. Don't do that. You need to move the channel online when the new bar starts.

  • First, detect if you are on a new bar.
  • If it is a new bar, use ObjectFind() to detect if you already created an object, 
  • if it does not exist, create it, 
  • else use ObjectMove() to move it to the new position

This probably won't solve your problem, you won't be able to modify the channel object and immediately read new values. 

Documentation on MQL5: Object Functions
Documentation on MQL5: Object Functions
  • www.mql5.com
Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 

Strategy tester works differently, it draws only one chart and it does it synchronously.

That is why you see values in a strategy tester.

 
Drazen Penic #:

Read the text below the title: Object functions

It says: 

Although it claims that ChartRedraw() will/should update chart, in my experience, it still does not work every time, no matter how long sleep you use. 

Try to verify that object exists with ObjectFind().


Also, you are deleting and creating an object on every tick. Don't do that. You need to move the channel online when the new bar starts.

  • First, detect if you are on a new bar.
  • If it is a new bar, use ObjectFind() to detect if you already created an object, 
  • if it does not exist, create it, 
  • else use ObjectMove() to move it to the new position

This probably won't solve your problem, you won't be able to modify the channel object and immediately read new values. 

Thank you for the guidance. I will definitely use your words.

i test ObjectFind and newbar . also print 0 .

I think using the while loop will solve my problem to some extent


while(val0==0)
{
val0=ObjectGetValueByTime(0,"a",time_0,0);
   Sleep(1);
}
    
while(val1==0)
{
 val1=ObjectGetValueByTime(0,"a",time_1,0);
 Sleep(1);
 }
 

When you create the object you define the two times but for both the price value is 0:

ObjectCreate(0,"a",OBJ_REGRESSION,0,time_1, 0,time_0, 0);

Read the ref. for this: https://www.mql5.com/en/docs/objects/objectcreate

Documentation on MQL5: Object Functions / ObjectCreate
Documentation on MQL5: Object Functions / ObjectCreate
  • www.mql5.com
ObjectCreate - Object Functions - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Carl Schreiber #:

When you create the object you define the two times but for both the price value is 0:

ObjectCreate(0,"a",OBJ_REGRESSION,0,time_1, 0,time_0, 0);

Read the ref. for this: https://www.mql5.com/en/docs/objects/objectcreate

Based on the search I did and i saw example , for create REGRESSION line we need only 2 times , No need for a price (If it is contrary to this, please advise). REGRESSION Line create successfully and with while loop my problem to some extent solved .