Trendline in my EA

 

Hi i was just wondering if there is any way to draw a trend line with an ea and use it for comparison just like a moving average.The method would be the thumb rule for a trend line:

  • a straight line if touches 3 lows of the price & if the lows are higher than eachother =uptrend
  • a straight line if touches 3 highs of the price & if the highs are lower than eachother =downtrend

Now i dont really care about the visual part so there is no need for an indicator, i just want that the EA could work with this.A simple trendline to be drawn (not necessarly visual only created the way so that the ea can work with it) and use it as comparison for example like somehow storing it in a variable lets say "trendline" (or an array of the trendline variable with all data if otherwise not possible) and just compare it

if(trendline<Close[0])
//blablabla

Is this possible?

 

If you mean computed trendlines (as opposed to manually drawn ones)...


Identifying the 3 highs and lows programatically, whilst not rocket science, can give different results depending on

a) the exact definition of "a high" (i's visually obvious but not a simple to code),

b) the timeframe you are using (low timeframes have a lot of noise etc).


Once you've worked that out, your 3 points will rarely be in a perfectly straight line, so you will have to define more precisely how to draw the line. As different traders can draw different lines the precise definition depends on your strategy.


This is the reason I think a lot of EA's are indicator based rather than price action/trend line/SR based (which are the cornerstones of trading), because to to trend lines, and SR are subjective and precise location open to interpretation.


For trendline a simple approach is to use least squares, but you then have to check how close all your points are to the line, to determine how valid the line is.

 
ydrol:

If you mean computed trendlines (as opposed to manually drawn ones)...


Identifying the 3 highs and lows programatically, whilst not rocket science, can give different results depending on

a) the exact definition of "a high" (i's visually obvious but not a simple to code),

b) the timeframe you are using (low timeframes have a lot of noise etc).


Once you've worked that out, your 3 points will rarely be in a perfectly straight line, so you will have to define more precisely how to draw the line. As different traders can draw different lines the precise definition depends on your strategy.


This is the reason I think a lot of EA's are indicator based rather than price action/trend line/SR based (which are the cornerstones of trading), because to to trend lines, and SR are subjective and precise location open to interpretation.


For trendline a simple approach is to use least squares, but you then have to check how close all your points are to the line, to determine how valid the line is.


a) High[barnumber] just that simple

b) I dont think it matters what TF i use.I just want to connect 3 highs or 3 lows and "draw" a trendline on them.And i want the EA to use that for calculations just like a moving average which was plotted to the graph.You can use a moving average to check if the price is over or under it, why not a straight trendline too?

And yes i want it to be a computed one, dont really care if it is visual,because the EA will handle all calculations, no human interference will be here.If you say it is possible to do this may i ask how to do it?

 
Proximus:

a) High[barnumber] just that simple

b) I dont think it matters what TF i use.I just want to connect 3 highs or 3 lows and "draw" a trendline on them.And i want the EA to use that for calculations just like a moving average which was plotted to the graph.You can use a moving average to check if the price is over or under it, why not a straight trendline too?

And yes i want it to be a computed one, dont really care if it is visual,because the EA will handle all calculations, no human interference will be here.If you say it is possible to do this may i ask how to do it?



So how to draw your line....

can you show it with a picture ???

To me a line is a connection from one point to a second point connecting all points like y = n*x + m

And you want to make a line connecting 3 highs or 3 lows ..... make it clear so we understand.....

.... what you call a line

 
deVries:


So how to draw your line....

can you show it with a picture ???

To me a line is a connection from one point to a second point connecting all points like y = n*x + m

And you want to make a line connecting 3 highs or 3 lows ..... make it clear so we understand.....

.... what you call a line

I thought the definition of a straight line is obvious.


Uptrend: 3 or more lows hitting a line which connects them,but and at the end it is broken with a small retracement



Downtrend: 3 or more highs hitting the trendline and connects it,but at the end it is broken by a reversal


Now my request is that is there any way for an EA do draw the trend line, even it is not visual since i saw that the indicators used in an ea are not visible, so i dont care if its visible or not, but i want the EA to do work with trend lines.Can you use it like a moving average for testing situations like testing if the price is above or below trendline.A moving average can be done easily but the problem with an MA is that it follows the price, i want a straight line only which should not follow it because this way the signal will not lag that much.Is there any way to do this?

 
ydrol:

If you mean computed trendlines (as opposed to manually drawn ones)...

Yes you got the definition of it, now please show me how to program this?
 
Proximus:
Yes you got the definition of it, now please show me how to program this?


walk though the chart, get the swing highs/lows (or fractal points - with Fractals indicator), connect the lower swing highs, you have the upper trendline.

connect higher swing lows, you have the lower trendline. probably the 3 upper or lower points will not match on one lline, then you have to walk on through other points.

as new bars, swing highs are created trendlines will change their position, so you have to keep this in mind, define for yourself what is a valid (strong) trendline.

also, on your charts the lines will be different few bars earlier or later.

look at the images here: http://forex-strategies-revealed.com/advanced/trendlines-breakout/cpage-11

 

Might be this is giving you also an idea

Name:
QuickFib
Author:nirgle (2010.09.20 15:37)



Download:
QuickFib.mq4 (6.4 Kb) View
 

Proximus:
Hi i was just wondering if there is any way to draw a trend line with an ea...Is this possible?

The short answer is yes it is possible. However, there is a disconnect between what looks simple visually on a chart, and what you want coded. I tried to allude to this in my first response.

I thought the definition of a straight line is obvious.

A line is

- the shortest distance between two points on a flat surface.

- Or for mathematically inclined (eg an EA) a line is the equation y = mx + c.

What a line isn't is a way to arbitrarily connect 3 points even if they appear to be in a line, which is, indirectly, what you are asking for.


If you look very closely at the downtrend in your chart you can see the trendline you drew in fact doesn't exactly touch 3 highs at all. So if you want to translate your description into code you need to be precise. That is why the responses are trying to tease more detail out of you.

The fact that any two traders can draw different trendlines on the same chart, should give you a clue that your request is not as simple as it first appears, given the amount of detail you have provided.

a) High[barnumber] just that simple

Again, how do you select exactly which bars to use for the highs - this is a partially rhetorical question, I'm asking so that it may start you thinking about how this apparently 'simple', 'obvious' task needs more qualification if you want it coded.

This is all possible, its not trivial, but it is not advanced either. However it's only simple once you start realising there are gaps in your description instead of telling people how simple and obvious it is.

Having said that there are plenty of trendline indicators around to download. Maybe try some and one will draw trendlines to your liking, then use the source in your EA (with permission etc.)

Good luck with it!

Establishing Support/Resistance also has similar issues as trendlines. Fundamental to successful trading, but no precise value (two traders can draw different SR) and thus not simple to code as first appears. And again there are plenty of different implementations to play with, or roll your own.

 
deVries:

Might be this is giving you also an idea

Name:
QuickFib
Author:nirgle (2010.09.20 15:37)



Download:
QuickFib.mq4 (6.4 Kb) View

I dont think this is the one, this needs manual line to be drawn, while my EA is fully automated.


ydrol:

The short answer is yes it is possible. However, there is a disconnect between what looks simple visually on a chart, and what you want coded. I tried to allude to this in my first response.

A line is

- the shortest distance between two points on a flat surface.

- Or for mathematically inclined (eg an EA) a line is the equation y = mx + c.

What a line isn't is a way to arbitrarily connect 3 points even if they appear to be in a line, which is, indirectly, what you are asking for.


If you look very closely at the downtrend in your chart you can see the trendline you drew in fact doesn't exactly touch 3 highs at all. So if you want to translate your description into code you need to be precise. That is why the responses are trying to tease more detail out of you.

The fact that any two traders can draw different trendlines on the same chart, should give you a clue that your request is not as simple as it first appears, given the amount of detail you have provided.

Again, how do you select exactly which bars to use for the highs - this is a partially rhetorical question, I'm asking so that it may start you thinking about how this apparently 'simple', 'obvious' task needs more qualification if you want it coded.

This is all possible, its not trivial, but it is not advanced either. However it's only simple once you start realising there are gaps in your description instead of telling people how simple and obvious it is.

Having said that there are plenty of trendline indicators around to download. Maybe try some and one will draw trendlines to your liking, then use the source in your EA (with permission etc.)

Good luck with it!

Establishing Support/Resistance also has similar issues as trendlines. Fundamental to successful trading, but no precise value (two traders can draw different SR) and thus not simple to code as first appears. And again there are plenty of different implementations to play with, or roll your own.

1) Well yes a line connects 2 points, but a trendline is only a valid one with 3 or more touches, so i guess there would need to be a test wether the 3rd point is on the line.Saying that the think i was trying to avoid is math.Although i am good in mathematical statistics what i hate the most is geometry.Isn't there a build in function in MQL4 which can handle trendlines, just as normal moving averages,but straight ones to keep it simple?

2) The next question i believe how i select high/lows.Well thats easy, i will have a variable called "CHECKBARS" which will be an int and can be set to check as many bars back in history as i want.Lets say it is set to 30 then we will check back 30 bars excluding the current bar.The entire "trendline code" will be inside a for() .Which will go from CHECKBARS down to 1, exluding 0 the current bar.So we will go through 30 bars and pick the every high that comes along and test if there is any other 2 highs in the 30 bars that could form a line, so for example if it finds High[CHECKBARS] and High[CHECKBARS-5] to align perfectly with High[CHECKBARS-7] then you got a nice downtrend.But we must test all combinations available to be accurate.

So pick High/Lows in sets of 2 and compare it with all other High/Lows on the list.So it would be a decent algorithm and it would not consume that much computing power.I believe in this 30 bar example the number of testings required by the CPU to do is combination of 30 by 2 which is =30! /( 2! *28!)=435

So the bar selection algorithm would look like this, now i need one to compute the trendlines please,since that is what i`m having trouble with :D


3) Another question about the link that you provided, i can't emphasize it enough, i want this to be fully automated, that means no manual trendline draw, i want the EA to draw/compute his trendline all by itself.I know there are plenty EA/indicators which after you draw a trendline manually will do work with it, but in my case i want the EA to draw/compute the line with 0 human interference.

 

Hi

There is no built in automatic trendline function for exactly the same reasons I've spelt out in my last two posts.

However you're getting there in the detail, but still think you are making some assumptions about things, and ignoring the fact that a line does not often exactly touch the 3 points, if you look very closely.

Some of the indicators you will find are automated, and may even do what you want, if the author was after the same thing.

Other than that you will have to roll your own or get a programmer to help, just be aware that trendlines ( and Support/Resistance) are somewhat subjective and open to interpretation and this is why there are no built in indicators.

But it is possible :)

est if there is any other 2 highs in the 30 bars that could form a line, so for example if it finds High[CHECKBARS] and High[CHECKBARS-5] to align perfectly with High[CHECKBARS-7] then you got a nice downtrend.

So use the first two points for two simulaneous equations price1 = m * bar1 + c and price2 = m * bar2 + c, and solve for m and c. Then, as mentioned before (in fact in almost every post I've made to this thread) you will need a tolerance for the 3rd point. ie. m * bar3 + c is approximately equal to price3.

In fact I think your lines may be better approximations if you first pick the two ends and then look for 3rd point in the middle.

Also you have to make sure no intermediate points significantly breach the line. (ie the line is above all other highs). Tha'ts fairly simple, for all other bars make sure: m * barN + c +some margin <= priceN (for highs - reverse for lows)

[This can be checked at the same time you are looking for the 3rd point, as the equation is the same. IF the price is withing tolerance then its a point 'on the line' otherwise if price is well above expected price then line is broken.]


So the bar selection algorithm would look like this, now i need one to compute the trendlines please,since that is what i`m having trouble with :D

Once you have selected your points you can just assume the line is y=mx + c or recalculate a best fit line for the 3 points (eg using use least squares to draw trendline, )

For 'x' part of the equation, if you use barnumber, You may have to code around weekend bars depending on what broker sends (unless these are intraday trend lines)

Another option is to use the actual time rather than the bar number.

Simple :)

Reason: