Identifying trends thru code

Hi all,

Just getting back into programming after a long hiatus and doing some coding in mql4, and I’m trying to identify trend kind programatically from ma’s, and wondering if anyone has any ideas.

I may be looking at this in the completely wrong way, but I’m playing around with comparing closes from the current time to the close of say 8 hrs ago, seems a bit of a hack way to do it, is there a more accurate way you guys are doing it?

Thanks

Hi fort,

You are on the wrong track lol.

Remember that to determine trend, you need to compare 2 moving averages. A single moving average by itself doesn’t really tell you much. The most common method is to use 2 moving average…a slow one and a fast one, eg 50 & 100. When the fast MA crosses above the the slow, then we have an uptrend and vice versa

The basic task progamatically is to determine when they cross. You do this by assigning 4 MA values to variables.

MA(50) _1 = moving average last bar;
MA(50)_2 = moving average current bar

MA(100)_1 = moving average last bar
MA(100)_2 = moving average current bar

Now we inspect for the crossover which involves comparing where the 2 moving averages on the last bar were relative to each other(above or below)…the we compare them on the current bar to see if that relationship has changed.

if(MA(50)_1<MA(100)_1) && (MA(50)_2>MA(100)_2) then we have BUY

if(MA(50)_1>MA(100)_1) && (MA(50)_2<MA(100)_2) then we have SELL

Of course you could just use MACD…a negative MACD is downtrend and a positive MACD is uptrend

Hope it helps.

k