# ObjectCreate Trendline

92

Is it possible to set a Trendline with the ObjectCreate function, that has an ending?

Otherwise my Chart is full with ray's, if the program sets various Trendlines.

1863

tak88js:

Is it possible to set a Trendline with the ObjectCreate function, that has an ending?

Otherwise my Chart is full with ray's, if the program sets various Trendline

Write this below ObjectCreate () function.

ObjectSet ("Object name", OBJPROP_RAY, False);

1091

tak88js:

Is it possible to set a Trendline with the ObjectCreate function, that has an ending?

Otherwise my Chart is full with ray's, if the program sets various Trendlines.

This is a snip from my code, where this section is in a for loop, indexed by n.

```      ObjectCreate( "trendLine"+n, OBJ_TREND,window, tStart, priceStart, tStop, priceStop);
ObjectSet( "trendLine"+n, OBJPROP_RAY, false);  // now a point to point line not a ray```
92

dabbler:

This is a snip from my code, where this section is in a for loop, indexed by n.

Is there a way to figure out if a Trendline got crossed by the chart or an indicator?
17660

tak88js:

Is there a way to figure out if a Trendline got crossed by the chart or an indicator?
Yes, do a linear interpolation or use ObjectGetShiftByValue() and/or ObjectGetValueByShift()
1091

tak88js:

Is there a way to figure out if a Trendline got crossed by the chart or an indicator?

Clearly. But it is not one line of code. You are going to have to calculate at every point on the line if the price is above or below the trendline. This is a problem in maths and computing. You know the equation of a straight line: y=mx+c, but you can probably use a simpler form since you know the two co-ordinates of the end points of the line. So you will iterate in bars from the start bar (time) to the end bar. And the slope of the line is simply the price difference between then ends divided by the time difference in bars.

EDIT: Raptor's method above seems simpler. I hadn't even seen those functions before!

92

I've seen the ObjectGetShiftByValue already for Fibonacci-lines. I thought it would be a solution, but didnt know how to use it for the Trend-line.
1091

I thought I would have a play with REGRESSION LINES. So I wrote a little script and the regression line looks good. The outer bands seem close to, but not exactly, 2 sigma deviations. I tested this by using the object type OBJ_STDDEVCHANNEL and set the deviation to 2.0 then 2.1

The print out from my script is bizarre as the first 15 values are 0 (WTF?) and then the rest are correct. At 31 they are zero again as the regression line ends (so that is fair enough).

```int start(){
Comment("");
ObjectsDeleteAll();
ObjectCreate( "regress",OBJ_REGRESSION,0, Time[30], 0,Time[1],0);
ObjectSet( "regress", OBJPROP_COLOR, Yellow);

string str="";
for( int n=0; n<41; n++ )
str = str + "n= " + n + ": " + DoubleToStr( ObjectGetValueByShift("regress",n), Digits ) + "\n";

Comment( str );
return(0);
}```

It is also not clear how to get the price of the outer bands of the regression channel (or the standard deviation channel). That would be even more interesting.

1091

To answer my own question I experimented with this regression line to find out why the first 15 or so bars gave null data.

```int first = 0;

int start(){
Comment("");
ObjectsDeleteAll();
ObjectCreate( "regress",OBJ_STDDEVCHANNEL,0, Time[40], Open[40],Time[first],Open[first]);
ObjectSet( "regress", OBJPROP_COLOR, Yellow);

string str="";
for( int n=0; n<51; n++ )
str = str + "n= " + n + ": " + DoubleToStr( ObjectGetValueByShift("regress",n), Digits ) + "\n";

Comment( str );
return(0);
}```

(EDIT: this answer is revised in the post below) The answer is that in order to get the script to work correctly you must not put 0 in for the price values of the regression line. Who knows why? Probably some divide by zero somewhere. Anyway by using sensible price values (which are then ignored according to the documentation) the line values are then readable. With zero for the prices the lines do draw correctly but their values do not read out correctly.

1091

I've done some more experimentation. Using non-zero values for the price fields masks the problem. You can use any price values, including 0, but if you use something close to the correct value such as Bid, the readings can appear correct when they are still actually wrong. Here is my new test script.

```// Test script to show reading from a regression line
// Using NON-ZERO price values, even though they are "not used",
// makes the readings appear correct BUT THEY ARE NOT until
// the second read through. You can use Bid, for example, but
// the errors are more obvious if you use 0 for the prices.

// MT4 VERSION 4.00 build 409
int start(){
ObjectsDeleteAll();
ObjectCreate( "regress",OBJ_REGRESSION,0, Time[40],0,Time[0],0);
ObjectSet( "regress", OBJPROP_COLOR, Yellow);

int n=0;

string str="";
for( int p=0; p<3; p++ ){  // notice on the print out that the first run through gives invalid data
str = str + "\n\nStart= " + DoubleToStr( ObjectGet("regress",OBJPROP_PRICE2), Digits );
str = str + "  :: End= "  + DoubleToStr( ObjectGet("regress",OBJPROP_PRICE1), Digits );
str = str + "\n";
for( n=0; n<51; n++ ){
str= str + "n=" + n + "->" + DoubleToStr( ObjectGetValueByShift("regress",n), Digits ) + "   ";
if( n % 7 == 6 )
str = str + "\n";
}
}
Comment( str );
return(0);
}```

Reading the regression line twice gives a correct read on the second time through.

1091

Ok, now I'm seriously pissed off with this regression channel stuff. I have spent hours working on it and it is just unreliable and inconsistent. I can run the same EA several times in the strategy tester in quick succession and get wildly different answers.

Here's the EA code.

```extern int length=40;
int initial=0;

//---------------------------------------------------------------------------------------+
int init(){
Comment("");
}
//---------------------------------------------------------------------------------------+
int deinit(){
}
//---------------------------------------------------------------------------------------+
int start(){

static datetime newBar = 0;

if( newBar==Time[0] )
return( 0 );

newBar= Time[0];
initial++;

if( initial==2*length ){
Regress();
}

return( 0 );
}
//---------------------------------------------------------------------------------------+
void Regress(){

ObjectDelete("regress");
ObjectCreate("regress",OBJ_REGRESSION,0,Time[length],0,Time[0],0);
ObjectSet   ("regress",OBJPROP_COLOR, Yellow);

string str="";
for( int p=0; p<3; p++ ){
str = str + "\n\nStart= " + DoubleToStr( ObjectGet("regress",OBJPROP_PRICE2), Digits );
str = str + "  :: End= "  + DoubleToStr( ObjectGet("regress",OBJPROP_PRICE1), Digits );
str = str + "\n";
for( int n=0; n<51; n++ ){
str= str + "n=" + n + "->" + DoubleToStr( ObjectGetValueByShift("regress",n), Digits ) + "   ";
if( n % 7 == 6 )
str = str + "\n";
}
}
Comment( str );

return;
}```

The code is very similar to the script code in the previous post. But as an EA, sometimes the second block of data is mostly zeros and sometimes it is valid.

This is just not reliable so I am not going to use it at all. Switching to code from first principles like this