ObjectGetValueByTime:Always 0 as returned value

Back to topics list To post a new topic, please log in or register
avatar
18
kei2tahn 2015.12.07 16:50
 

Hi,

I am faceing the following problem: I create a regression channel with the following code

if(RegressionCreate(0,"Regression",0,mindate,maxdate,clrBlue,STYLE_SOLID,1,false,false,true,true,true,0)==true)
   {
      Regression_Channel=true;
      testprice=ObjectGetValueByTime(0,"Regression",TimeCurrent(),1);
    }else
    {
       Regression_Channel=false;

     }

where

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxx Create Linear Regression Channel by the given coordinates xxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

bool RegressionCreate(const long            chart_ID=0,        // chart's ID
                      const string          name="Regression", // channel name
                      const int             sub_window=0,      // subwindow index
                      datetime              time1=0,           // first point time
                      datetime              time2=0,           // second point time
                      const color           clr=clrRed,        // channel color
                      const ENUM_LINE_STYLE style=STYLE_SOLID, // style of channel lines
                      const int             width=1,           // width of channel lines
                      const bool            fill=false,        // filling the channel with color
                      const bool            back=false,        // in the background
                      const bool            selection=true,    // highlight to move
                      const bool            ray_right=false,   // channel's continuation to the right
                      const bool            hidden=true,       // hidden in the object list
                      const long            z_order=0)         // priority for mouse click
  {
//--- set anchor points' coordinates if they are not set
   //ChangeRegressionEmptyPoints(time1,time2);
//--- reset the error value
   ResetLastError();
//--- create a channel by the given coordinates
   if(!ObjectCreate(chart_ID,name,OBJ_REGRESSION,sub_window,time1,0,time2,0))
     {
      Print(__FUNCTION__,
            ": failed to create linear regression channel! Error code = ",GetLastError());
      return(false);
     }
//--- set channel color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set style of the channel lines
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set width of the channel lines
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of highlighting the channel for moving
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- enable (true) or disable (false) the mode of continuation of the channel's display to the right
   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


though the testprice is always 0 despite the fact that the Regression Channel is successfully created. Do I make some mistake in the ObjectGetValueByTime section?


Thanking you in advance,


Panos Kourtesis

avatar
12983
avatar
18
kei2tahn 2015.12.08 00:48 #
 
if(RegressionCreate(0,"Regression",0,mindate,maxdate,clrBlue,STYLE_SOLID,1,false,false,true,true,true,0)==true)
{
  Regression_Channel=true;
  testprice=ObjectGetValueByTime(0,"Regression",TimeCurrent(),1);
}else
{
  Regression_Channel=false;
}


//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxx Create Linear Regression Channel by the given coordinates xxxxxxxxxxxxxxxxxxxxx
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

bool RegressionCreate(const long            chart_ID=0,        // chart's ID
                      const string          name="Regression", // channel name
                      const int             sub_window=0,      // subwindow index 
                      datetime              time1=0,           // first point time
                      datetime              time2=0,           // second point time
                      const color           clr=clrRed,        // channel color
                      const ENUM_LINE_STYLE style=STYLE_SOLID, // style of channel lines
                      const int             width=1,           // width of channel lines
                      const bool            fill=false,        // filling the channel with color
                      const bool            back=false,        // in the background
                      const bool            selection=true,    // highlight to move
                      const bool            ray_right=false,   // channel's continuation to the right
                      const bool            hidden=true,       // hidden in the object list
                      const long            z_order=0)         // priority for mouse click
  {
//--- set anchor points' coordinates if they are not set
   //ChangeRegressionEmptyPoints(time1,time2);
//--- reset the error value
   ResetLastError();
//--- create a channel by the given coordinates
   if(!ObjectCreate(chart_ID,name,OBJ_REGRESSION,sub_window,time1,0,time2,0))
     {
      Print(__FUNCTION__,
            ": failed to create linear regression channel! Error code = ",GetLastError());
      return(false);
     }
//--- set channel color
   ObjectSetInteger(chart_ID,name,OBJPROP_COLOR,clr);
//--- set style of the channel lines
   ObjectSetInteger(chart_ID,name,OBJPROP_STYLE,style);
//--- set width of the channel lines
   ObjectSetInteger(chart_ID,name,OBJPROP_WIDTH,width);
//--- display in the foreground (false) or background (true)
   ObjectSetInteger(chart_ID,name,OBJPROP_BACK,back);
//--- enable (true) or disable (false) the mode of highlighting the channel for moving
//--- when creating a graphical object using ObjectCreate function, the object cannot be
//--- highlighted and moved by default. Inside this method, selection parameter
//--- is true by default making it possible to highlight and move the object
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTABLE,selection);
   ObjectSetInteger(chart_ID,name,OBJPROP_SELECTED,selection);
//--- enable (true) or disable (false) the mode of continuation of the channel's display to the right
   ObjectSetInteger(chart_ID,name,OBJPROP_RAY_RIGHT,ray_right);
//--- hide (true) or display (false) graphical object name in the object list
   ObjectSetInteger(chart_ID,name,OBJPROP_HIDDEN,hidden);
//--- set the priority for receiving the event of a mouse click in the chart
   ObjectSetInteger(chart_ID,name,OBJPROP_ZORDER,z_order);
//--- successful execution
   return(true);
  }
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx



avatar
18
kei2tahn 2015.12.08 00:54 #
 

I have tried both with 0,1 and 2 but I still get 0 in the debug mode.

I believe that either in the call of ObjectGetValueByTime I make a mistake or in the initial creation of the Regression Channel.

The Regression Channel is successfully created on the chart. I do not know if this is a clue but when I hover the mouse ove the middle regression line I do get a value on the chart (not in the code) but when the mouse is over the two outer regression lines no price is returned. This might indicate something wrong in the creation code part.


Thanking you in advance,

Panos Kourtesis

 

avatar
Moderator
3404
GumRai 2015.12.08 23:39 #
 

This is one of the strange anomalies with mq4.

The ObjectCreate() function returns true, but the object has not always actually been created. The Object drawing has been placed in a queue.

Because your code tries to read the value immediately after the  ObjectCreate, the object has not actually been drawn, so ObjectGetValueByTime() returns zero.

In a script, this can often be overcome by inserting

      ChartRedraw();

 after the ObjectCreate, or Sleep for a few seconds. In an indicator, you will need to create the object and wait at least 1 tick before trying to access the values

Here is an example of use in an indicator

   datetime mindate=Time[10];
   datetime maxdate=Time[1];
   static bool Regression_Channel=false;
   double testprice;

   if(Regression_Channel)
     {
      testprice=ObjectGetValueByTime(0,"Regression",TimeCurrent(),0);
      Print(DoubleToStr(testprice,Digits));
      testprice=ObjectGetValueByTime(0,"Regression",TimeCurrent(),1);
      Print(DoubleToStr(testprice,Digits));
      testprice=ObjectGetValueByTime(0,"Regression",TimeCurrent(),2);
      Print(DoubleToStr(testprice,Digits));
     }
   else
   if(RegressionCreate(0,"Regression",0,mindate,maxdate,clrBlue,STYLE_SOLID,1,false,false,true,true,true,0))
     {
      ChartRedraw();
      Regression_Channel=true;
     }
   else
     {
      Regression_Channel=false;
     }

 Obviously, this will print the values every tick, but I doubt that you actually want to print the values in your code when it is finished.

avatar
18
kei2tahn 2015.12.09 12:42 #
 
Thank
GumRai:

This is one of the strange anomalies with mq4.

The ObjectCreate() function returns true, but the object has not always actually been created. The Object drawing has been placed in a queue.

Because your code tries to read the value immediately after the  ObjectCreate, the object has not actually been drawn, so ObjectGetValueByTime() returns zero.

In a script, this can often be overcome by inserting

 after the ObjectCreate, or Sleep for a few seconds. In an indicator, you will need to create the object and wait at least 1 tick before trying to access the values

Here is an example of use in an indicator

 Obviously, this will print the values every tick, but I doubt that you actually want to print the values in your code when it is finished.

Thank you so much. I will try it out to see how it goes!
avatar
18
kei2tahn 2015.12.09 14:17 #
 

You were right with the use of ChartRedraw(); it works perfectly.

I would like to ask you another question: How can I delete a specific fibolevel, because ObjectDelete requires chart id and object name and I havent given any names to fibolevels!


Thanking you in advance!

avatar
Moderator
3404
GumRai 2015.12.09 14:30 #
 

You need to be clear whether you want do delete a Fibonacci object or a level within that object. If there is only 1 Fibonacci object on the chart, you can find it by loopng through the objects and checking ObjectType().

I have, as yet, never done any coding with Fibonacci objects, so maybe somebody else can help you. 

Back to topics list  

To add comments, please log in or register