Constructing a trendline EA

 

There are a lot of trendline EAs already out there. Usually they are so badly commented and with variables that are so indescriptive, it takes a long time for me to figure out what's going on. Could anybody give a brief refresher course on the math involved in creating a trendline?

It's been a while since I took an algebra courses, but if I remember right, I'll end up with a simple x=y formula, like 1x=0.85Y. But I don't remember how to find the formula. Also, how would I average out the noise? That is of course I want to find the "most touched" trendline. What I mean is that a trendline that price has bounced off of say 6 times, is a lot more valuable than a trendline that the price has only bounced of 2 times.

I suppose I would check every single close of every single 1 minute bar over the period of time I wish to examine, then I could use a for loop to go through every single iteration of formula possible, so something like:

for(double i=0.001;i<5;i+=0.001)
{
  //find how many times bar closes come close to this formula
  //if #times > previous, keep this equation
}

But this seems very ineffiecent to me. Doing this over say, the last 2 hours, would result in 600,000 calculations.

 

you dont need to itarate trough all closes, for trendlines i mostly use iHigh/iLow of fraktals. once a array is builded and filled you have already filtred out about 5/6 of all closes. the bigger problem is the difference between a touch and a breaktrouht. (for me).


as for noise reduction i rely mostly on a nice indicator made by Jurik, JMA. i like this indicator a lot.

here is the website of jurik, i hope this is not treated as adertisement since i have nothing to do with Jurik and his site.http://www.jurikres.com/

 

Yeah I was thinking of using the fractal indicator to get readings from since it does a pretty good job of picking the most relevent highs and lows. This still doesn't give me any idea how actually do the math once I have those readings though.

 

well i depends on which trendline you want to create,

if you want one that is never has been broken the math is quite simple. one simple if() condition does that job.

if you want the one where all lower fraktals are as nearest as possible. my statistic knowledge is also not the best but i think the term here is:

standart deviation. you have to minimice this value.

the math for the standart deviation should be

st += MathPow(LOW(x)-ValueOfTrendlineAt(x),2);

a brute force method would be drawing all trendlines from the fraktals, calculating all the standart deviation, and then keeping the best and deleting all other.

since all trendlines are simple linear functions with the form

x=ay+b

where:
b=Low() from TrendlinePoint1
and:
a=(b+(Low() from TrendlinePoint2))/(number of Candles between TLP1 and TLP2) 

//edit:
if you insert the position of the candle realtive to the startpoint TLP1 as y and solve the equation you get the value of the trendline at that specific candle

you dont have to draw them all (which saves a lot of cpu)

i hope i wrote this understandable, it's hard to explain somebody Math in a for me foreign language ;)

if you want a better method maybe https://en.wikipedia.org/wiki/Linear_regression is something for you.

//z