Question about drawing in an object.

 

I'm trying to draw a linear regression channel in an EA I'm working on. The two points I want to use are:

T1 - beginning of the calendar day i.e. 0:00

T2 - 8:00 am on the same day.

However I want the T2 of the channel to constantly update with each new bar forming (that is on the M15 time-frame) while T1 stays the same. Can anyone give me some advice on how to code that properly? 

I managed to get T1 to stay the same, but I can't fix it on 0:00 the way I want it.

Thanks :)

 
datetime T = iTime( NULL, 60, Hour()) ;
ObjectSet( objname,OBJPROP_TIME1, T ) ;//00:00
T = iTime( NULL, 60, Hour() - 8) ;
ObjectSet( objname,OBJPROP_TIME2, T) ;//08:00
 
fxcourt:
datetime T = iTime( NULL, 60, Hour()) ;
ObjectSet( objname,OBJPROP_TIME1, T ) ;//00:00
T = iTime( NULL, 60, Hour() - 8) ;
ObjectSet( objname,OBJPROP_TIME2, T) ;//08:00

You have used "T" for both points here, but in order to use ObjectSet() I must first create the reg channel with ObjectCreate(). In this case what are the two points I should use?

Also how will I redraw the channel when the next bar comes? I want the first point to stay on 00:00 and the second to move with the M15 chart i.e. 08:00, 08:15, 08:30 etc.

 

N

Having created objName use something like below:

if((Minute()%Period())==0)
{
  ObjectMove(objName,1,TimeCurrent(),0);         //"1" selects coordinate pair.2 of 2 for LR object type. NB:selector counts from 0..2
  //any other code needed when on bar boundary
...

each time new bar comes along right time coordinate is updated and channel extends.

attached test EA is way over top but I decided to learn too - never had need for lin regr ch...

run on any periodocity - M1 is fun to watch...


hth

Files:
25076.mq4  2 kb
 
fbj:

N

Having created objName use something like below:

each time new bar comes along right time coordinate is updated and channel extends.

attached test EA is way over top but I decided to learn too - never had need for lin regr ch...

run on any periodocity - M1 is fun to watch...


hth

Perfect! This is exactly what I needed. A nice piece of code, now I can continue with the other stuff and I just learned a new thing :)

Thanks!

 

Ok, in order not to create a different topic I'll write a new thing that came up here. After I created the regression channel the way I wanted (thanks to fbj and all others for the help) I now want to obtain a value from it. I particularly need its value for the last completed bar. 

I've tried using the ObjectGet() function and got some strange results. Although the channel is drawn using 2 points in time, I used ObjectGet("Reg_Channel_Name",OBJPROP_PRICE1) and actually got a double type value for the first drawing point. However I can't figure out how to get the its value for the second point, which is what I'm interested in.

I'm posting a screen shot to better display what I have in mind. You can see there that my cursor is pointing at the regression line on the last completed bar. Is there a way to obtain that value in my EA?

Thanks for all the help in advance!


ScreenShot

 

oh boy... just saw your post ;)

From what I remember you've got to consider that the LR lines move all over the place yes?

Is not like getting a y-axis value from a line object with reference to x-axis/time. The left is static I think(**), as you discover and so value is readily given. The right is not - is moving in y-axis as bar.0 builds.


(**)edit: no it is not, since the 'best-fit' means as price changes so will the fit and hence the lines.. and just put up the LR and the left start points do move up/down in y-axis

this is a trying to hit a moving target issue. Not see how do. Unless YOU draw the LR channel made up of 3 OBJ_TREND lines and then each data tick you move/redraw. The upside to all this is YOU will know what the price/y-axis is for bar.0.... have a think about this for a bit.

Also, google Linear Regression formulas/algorithms (spelling?!!!) and you will then see the HOW TO's for it all. Just a bit of code once get a handle on the calcs!

and have a look at ObjectGet(name,OBJPROP_DEVIATION) which maybe gives you the standard deviation value used in drawing the two outer lines. but can always play with that once you have your LR drawing function :o)

one ref: http://www.stator-afm.com/linear-regression-lines.html

but is too heavy for me. must be more higher level talks about it all....

ALL thots of course.

(**)endEdit

I'll let it all sink in and see what pops up - by then, someone may solve!

Oh yes, first solution tells me to say: look at these... ObjectGetShiftByValue() and ObjectGetValueByShift()

But, since the two coordinates are only time values, I'd suspect above will return funnies - maybe not tho.

.

Second idea is to create an OBJ_TREND which for sure can be interrogated by the above two builtins.

Issue I'd think is mapping the two coordinate pairs (time,price) onto one of the OBJ_REGRESSION lines. Sound like idea?

.

phew... see ya later

 
fbj:

oh boy... just saw your post ;)

From what I remember you've got to consider that the LR lines move all over the place yes?

Is not like getting a y-axis value from a line object with reference to x-axis/time. The left is static I think(**), as you discover and so value is readily given. The right is not - is moving in y-axis as bar.0 builds.


(**)edit: no it is not, since the 'best-fit' means as price changes so will the fit and hence the lines.. and just put up the LR and the left start points do move up/down in y-axis

this is a trying to hit a moving target issue. Not see how do. Unless YOU draw the LR channel made up of 3 OBJ_TREND lines and then each data tick you move/redraw. The upside to all this is YOU will know what the price/y-axis is for bar.0.... have a think about this for a bit.

Also, google Linear Regression formulas/algorithms (spelling?!!!) and you will then see the HOW TO's for it all. Just a bit of code once get a handle on the calcs!

and have a look at ObjectGet(name,OBJPROP_DEVIATION) which maybe gives you the standard deviation value used in drawing the two outer lines. but can always play with that once you have your LR drawing function :o)

one ref: http://www.stator-afm.com/linear-regression-lines.html

but is too heavy for me. must be more higher level talks about it all....

ALL thots of course.

(**)endEdit

I'll let it all sink in and see what pops up - by then, someone may solve!

Oh yes, first solution tells me to say: look at these... ObjectGetShiftByValue() and ObjectGetValueByShift()

But, since the two coordinates are only time values, I'd suspect above will return funnies - maybe not tho.

.

Second idea is to create an OBJ_TREND which for sure can be interrogated by the above two builtins.

Issue I'd think is mapping the two coordinate pairs (time,price) onto one of the OBJ_REGRESSION lines. Sound like idea?

.

phew... see ya later

Yeah, the first point of the regression channel also moves with time, this is the tricky part. Strange how using the ObjectGet() function I managed to get this value, but the "all ove the place" one doesn't show up. I also tried to calculate the regression line myself using the OSL (Ordinary Least Squares method), but I didn't get the correct values so I dropped that. This link you gave me may be of help though. I will try these and report the results :)

 
ObjectGet("Reg_Channel_Name",OBJPROP_PRICE2); ?
 
fxcourt:
ObjectGet("Reg_Channel_Name",OBJPROP_PRICE2); ?

Tried it. Gives me a cold and heartless 0 :/

 

Strange. This is what i used to test it.

int init()

{

ObjectCreate( "reg",OBJ_REGRESSION, 0, iTime(NULL,60,3), 0, iTime(NULL,60,0)) ;

}


int start()

{


ObjectSet("reg",OBJPROP_TIME1,iTime(NULL,60,Hour()));

ObjectSet("reg",OBJPROP_TIME2,iTime(NULL,60,0));

double regression_value = ObjectGet("reg",OBJPROP_TIME2);

Comment(regression_value);

}

Reason: