How to identify current trend of MA through code?

 

Hello,

I am writing an EA but I am beginner in MQL4. Please suggest me how could I identify a DOWN TREND or an UP TREND of moving average in my EA programmatically?

Please advise me as I have not so familiar with MQL4.

 
kumaillakhani:

Hello,

I am writing an EA but I am beginner in MQL4. Please suggest me how could I identify a DOWN TREND or an UP TREND of moving average in my EA programmatically?

Please advise me as I have not so familiar with MQL4.

By the cross of MA.

Have a look at the following links:

I hope it help.
MA Cross
MA Cross
  • www.mql5.com
Indicator MACD TrackTrend The indicator that shows the direction of the trend on all timeframes in one window of the chart. Bull vs Medved The EA for H4, it's shooted in for GBPUSD. It works with pending orders...
 

I am not using cross of MA.

Actually I am trying to used Heiken Ashi & Moving Average for my EA. First I want to know about the Trend of MA. So I would like to know how could I get this identify that currently MA is in up or down trend or it is moving in horizontal direction.

Please review attach image in that image MA line is moving horizontal almost straight not is up direction not in down direction how could identify in mql4 up, down and this trend that image is showing?

I exactly want know in EA that weather it is a up trend or down trend. I am beginner please let me know.

Thank you in advance.

 

Heiken Ashi == Moving average.

So if you want the trend direction then it's:

if(Moving_average_bar_0 > Moving_average_bar_1)
 {
  // Direction = Long;
 }

else if(Moving_average_bar_0 < Moving_average_bar_1)
 {
  // Direction = Short;
 }
 
Marco vd Heijden:

Heiken Ashi == Moving average.

So if you want the trend direction then it's:

You mean to say like this:

//MA
double H4EMA = iMA(_Symbol, PERIOD_H4, 10, 0, MODE_EMA, PRICE_CLOSE, 0);

//Heiken Ashi
double HALow = iCustom(NULL,0, "Heiken Ashi", color1, color2, color3, color4, HALOW, 0);
double HAHigh = iCustom(NULL,0, "Heiken Ashi", color1, color2, color3, color4, HAHIGH, 0);
double HAOpen = iCustom(NULL,0, "Heiken Ashi", color1, color2, color3, color4, HAOPEN, 0);
double HAClose = iCustom(NULL,0, "Heiken Ashi", color1, color2, color3, color4, HACLOSE, 0);


if(H4EMA > HAClose)
 {
  // Direction = Long;
  // THIS MEANS UP TREND?????
 }

else if(H4EMA < HAClose)
 {
  // Direction = Short;
  // THIS MEAN DOWN TREND????
 }

Did understand you correctly? Please verify.

thanks in advance.

 

No i did not mean that.

Heiken ashi is a visualization technique to 'cheat' your eyes.

There is no use in trying to automate it as a strategy it will be similar to a moving average:

if(Moving_average_bar_0 > Moving_average_bar_1)
 {
  // Direction = Long;
 }

else if(Moving_average_bar_0 < Moving_average_bar_1)
 {
  // Direction = Short;
 }

If the last bar is above the previous last bar your direction is long.

If the last bar is below the previous last bar, your direction is short.

You only need one moving average for that, and no Heken Ashi.

 
kumaillakhani:

Hello,

I am writing an EA but I am beginner in MQL4. Please suggest me how could I identify a DOWN TREND or an UP TREND of moving average in my EA programmatically?

Please advise me as I have not so familiar with MQL4.

How about Moving Average angle?   I don't know much about it, though.

https://www.mql5.com/en/forum/179710

 
An angle requires distance divided by distance; a unitless number. A chart has price/time. What is the angle of going 30 miles in 45 minutes? Meaningless!

You can get the slope from a trendline or moving average. Delta price / delta time. Changing the axes scales changes the apparent angle, the slope is constant. Angle is meaningless.

 
whroeder1:
An angle requires distance divided by distance; a unitless number. A chart has price/time. What is the angle of going 30 miles in 45 minutes? Meaningless!

You can get the slope from a trendline or moving average. Delta price / delta time. Changing the axes scales changes the apparent angle, the slope is constant. Angle is meaningless.

I found you posted a nice code that is very helpful to OP here (Slope of a moving average) :

https://www.mql5.com/en/forum/152085

OP should check it :)

Slope of a moving average
Slope of a moving average
  • 2014.06.23
  • www.mql5.com
I want to calculate the slope of a moving average of 200 periods...
 
Marco vd Heijden:

No i did not mean that.

Heiken ashi is a visualization technique to 'cheat' your eyes.

There is no use in trying to automate it as a strategy it will be similar to a moving average:

If the last bar is above the previous last bar your direction is long.

If the last bar is below the previous last bar, your direction is short.

You only need one moving average for that, and no Heken Ashi.

I love the colors on your Heikin Ashi example chart, haha. 

Reason: