ObjectGetValueByTime - page 2

 
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.



 
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");
    }
  }
 
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. :)
 
The post has now gone, but I agree it is a bit of crude workaround.
 

I'm having a similar issue to this :

I've got the equivalent of above working, I'm able to capture the OBJ_REGRESSION values via ObjectGetValueByTime

But.... this only works using the visual mode strategy test, otherwise it just returns 0 (as in the problem above)

I know that Sleep has no impact on a Strategy Test is ChartRedraw the same?

or is there any way to simulate/force a tick via code to solve the strategy tester issue?

 
iRick:

I'm having a similar issue to this :

I've got the equivalent of above working, I'm able to capture the OBJ_REGRESSION values via ObjectGetValueByTime

But.... this only works using the visual mode strategy test, otherwise it just returns 0 (as in the problem above)

I know that Sleep has no impact on a Strategy Test is ChartRedraw the same?

or is there any way to simulate/force a tick via code to solve the strategy tester issue?

I tried separately adding a horizontal line and then deleting it, hoping that it would somehow refresh, but it seems the chart isn't re-drawn until after the OnTick() has processed

 
iRick:

I'm having a similar issue to this :

I've got the equivalent of above working, I'm able to capture the OBJ_REGRESSION values via ObjectGetValueByTime

But.... this only works using the visual mode strategy test, otherwise it just returns 0 (as in the problem above)

I know that Sleep has no impact on a Strategy Test is ChartRedraw the same?

or is there any way to simulate/force a tick via code to solve the strategy tester issue?

Anyone got any ideas?

Is it impossible? so basically ObjectGetValueByTime won't work via Strategy Tester

Will creating OBJ_REGRESSION lines via an Indicator and then calling from the EA help?

Is it worth putting this question to the Freelance section?

 

I've tried this:

https://www.mql5.com/en/forum/280520

Same issue, values aren't populated until after the tick while using Strategy Tester (in non visual mode) rendering ObjectGetValueByTime useless

May have to resort to looping all the values into an array and then calculating the Intercept/Gradient manually


HELP HELP HELP ::::: How to get Regression Channel value?
HELP HELP HELP ::::: How to get Regression Channel value?
  • 2018.09.21
  • www.mql5.com
Hi guys, I,m trying to get regression channel value with this code below : ObjectCreate("trend_regression",OBJ_REGRESSION,0,Time[200],0,Time[0],0...
 
iRick:

I've tried this:

https://www.mql5.com/en/forum/280520

Same issue, values aren't populated until after the tick while using Strategy Tester (in non visual mode) rendering ObjectGetValueByTime useless

May have to resort to looping all the values into an array and then calculating the Intercept/Gradient manually


iRick,

Did you ever manage to get this sorted? I'm having the exact same problem now. I have tried the link you posted, I have tried using ObjectGetValueByShift as well... It doesn't seem to matter what I do, I get 0 during non-visual backtesting. I appreciate that this is a year since your last message but it's worth a shot


Thanks

 

Thanks. All of you. I had the same problem and couldn't figure this one out.

Although I use ChartRedraw() in my code no matter how long I've waited (several ticks, minutes), ObjectGetValueByTime() always returned 0.0.

Same happened with a direct Sleep(50) statement in the code before using ObjectGetValueByTime().

After that I've done what seems to be unnecessarily complicated - and it works. :)

if ( !ControlFlow.getbObjCreated() )                    //at least one tick has passed since object created (and ChartRedraw() was used at that time)
{
        MqlDateTime dTimeStruc;
        TimeCurrent(dTimeStruc);
        double dPrice1;
        if ( ObjectFind(0, "TrendLine") >= 0 )
        {
                Sleep(50);
                ChartRedraw(0);
                dPrice1 = ObjectGetValueByTime(0, "TrendLine", StructToTime(dTimeStruc), 0);
                Print("TEST: Trend Line Price value for next bar: ", DoubleToString(dPrice1, Digits()));                //returns correct value instead of 0.0!
        }
}