Download MetaTrader 5

ObjectGetValueByTime - page 2

To add comments, please log in or register
Manage agents in your profile. It is easy!
Mohamad Zulhairi Baba
677
Mohamad Zulhairi Baba 2017.03.20 13:32  
honest_knave:

Who would have thought that would be the solution, eh? 


I agree on your solutions earlier @honest_knave
just want clarifications in OnTick handle, for double value keep switching from 0 it it's double value.



honest_knave
Moderator
2195
honest_knave 2017.03.20 14:02  
Mohamad Zulhairi Baba:


I agree on your solutions earlier @honest_knave
just want clarifications in OnTick handle, for double value keep switching from 0 it it's double value.




Yes, as I mentioned earlier your code creates the object every single tick (if it already exists, your code deletes it then recreates it).

If you don't wait long enough, you will get 0.

If you do wait long enough, you will get the correct value.

TBH, there is a lot of unnecessary code. This will give you the correct value right from the first tick:

string      ObjIdentifier = "ABC_";

int OnInit()
  {
   return(INIT_SUCCEEDED);
  }

void OnDeinit(const int reason)
  {
   Comment("");
   ObjectsDeleteAll(0,ObjIdentifier);
  }

void OnTick()
  {  
   DrawRegressionChannel("Regression",Time[10],High[10],Time[5],Low[5],STYLE_SOLID,clrRed,2,true);
   ChartRedraw();
   Sleep(50);
   double Line0 = ObjectGetValueByTime(0,ObjIdentifier+"Regression",Time[1],0);
   double Line1 = ObjectGetValueByTime(0,ObjIdentifier+"Regression",Time[1],1);
   double Line2 = ObjectGetValueByTime(0,ObjIdentifier+"Regression",Time[1],2);
   Comment( "\nTime Now : ",TimeToString(TimeCurrent()),
            "\n\nTest Value Regression 0 : ",Line0,
            "\n\nTest Value Regression 1 : ",Line1,
            "\n\nTest Value Regression 2 : ",Line2);
  }

void DrawRegressionChannel(string name, datetime T0, double P0, datetime T1, double P1, int style, color clr, int width, bool ray)
  {
   if(ObjectFind(0,ObjIdentifier+name) < 0)
     {
      ObjectCreate(ObjIdentifier+name,OBJ_REGRESSION, 0, T0, P0, T1, P1 );
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_STYLE,style);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_COLOR,clr);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_WIDTH, width);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_RAY, ray);
      ObjectSetInteger(0,ObjIdentifier+name,OBJPROP_BACK,false);
      ObjectSetString (0,ObjIdentifier+name,OBJPROP_TEXT,name+" Channel");
    }
  }
Mohamad Zulhairi Baba
677
Mohamad Zulhairi Baba 2017.03.20 14:31  
honest_knave:


Yes, as I mentioned earlier your code creates the object every single tick (if it already exists, your code deletes it then recreates it).

If you don't wait long enough, you will get 0.

If you do wait long enough, you will get the correct value.

TBH, there is a lot of unnecessary code. This will give you the correct value right from the first tick:

 

Thanks @honest_knave, really appreciate it. :)
honest_knave
Moderator
2195
honest_knave 2017.03.20 14:51  
The post has now gone, but I agree it is a bit of crude workaround.
12
To add comments, please log in or register