# How to calculate slope of an EMA?

66

Hi,

I've got this code calculating the moving average:

double ma1 =  iMA(NULL, MA1_TTIMEFRAME, MA1_PERIOD, 0, MA1_METHOD, MA1_PRICE, 0);

How do I calculate whether the MA is pointing up or down? above 0 line or below 0 line? How to calculate its slope basically?

Thanks a lot

Moderator
7130

Here are many howto to calculate slopes!

To find out whether the ma goes up or down simply take the difference of two points of the ma. How to do it read the reference (put the cursor on iMA and press F1).

17942

William2050: double ma1 =  iMA(NULL, MA1_TTIMEFRAME, MA1_PERIOD, 0, MA1_METHOD, MA1_PRICE, 0);
1. Why did you post your MT4 question in the Root / MT5 General section instead of the MQL4 section, (bottom of the Root page?)
General rules and best pratices of the Forum. - General - MQL5 programming forum

2. (Y2-Y1)/(X2-X1)
Slope - Wikipedia
You need two points in general. (SMA/WMA.)
EMA always point toward the price (no need for second point.)
Never any reason to use the SMMA(n) it's equivalent to the EMA(2n-1).
The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io

3. On MT4: Unless the current chart is the specific pair/TF referenced, you must handle 4066/4073 errors.
Don't mix apples and oranges.
243

William2050:

Hi,

I've got this code calculating the moving average:

double ma1 =  iMA(NULL, MA1_TTIMEFRAME, MA1_PERIOD, 0, MA1_METHOD, MA1_PRICE, 0);

How do I calculate whether the MA is pointing up or down? above 0 line or below 0 line? How to calculate its slope basically?

Thanks a lot

I am also interested this question and, trying to find the good post for this. Everybody who find the post, please kindly remind me.

66

4420

William2050: So SMMA is the slow MA and WMA is the faster one but they return just doubles e.g. +0.0050 but what I need is the angle e.g. 20 degrees or -20 degrees.

SMMA is Smooth MA, not "slow" MA.

Also, your original query used the word "slope" (which usually infers the gradient of the slope), so you you were given the correct answer. "Angle" and "slope" are two different terms. They are not the same.

To convert a "slope" gradient into "angle" use: "angle = arctan( slope )". In MQL you can use the function MathArctan() to return the angle in "radians" and you will have to convert it into "degrees" using: "degrees = radians * 180 / pi" using the M_PI constant in MQL.

17942

• There is no angle from 2 anchor points. 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. Delta price / delta time. Changing the axes scales changes the apparent angle, the slope is constant. Angle is meaningless.
• You can create a angled line using one point and setting the angle (Object Properties - Objects Constants - Standard Constants, Enumerations and Structures - MQL4 Reference.) The line is drawn that angle in pixels. Changing the axes scales does NOT change the angle (and thus is meaningless.)
9

slope = (Y2-Y1)/(X2-X1)

the (X2-X1) is equal in all point of chart because the time frame is fix in one chart. foe example in 4 Hours time frame all 2 point in the chart will be 4 Hours. So you can ignore this part of above function and just use "Slope = Y2-Y1".

It is so easy. You just need to calculate:

Slope = MA candle[0] - MA candle[1];

the code:

Slope=(iMA(NULL,0,SlopeMAPeriod,0,MODE_SMMA,PRICE_CLOSE,0)-iMA(NULL,0,SlopeMAPeriod,0,MODE_SMMA,PRICE_CLOSE,1))/Point;

you can modify it for yourself and use it in your EA. for example the mode of MA, the period of MA (SlopeMAPeriod), time frame and so on.

In fact, the slope of MA is my trading strategy.