How to calculate slope of an EMA?

 

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

 

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).

slope calculation - Google-Suche
slope calculation - Google-Suche
  • www.google.at
{"clt":"n","id":"lB1hkCPhzUMaLM:","isu":"geokov.com","itg":0,"ity":"png","oh":236,"ou":"http://geokov.com/Images/topography/slope-diagram.png","ow":250,"pt":"Topographic Map Slope (Gradient) Calculation from Contours","rid":"XNASe6PusxRfUM","rmt":0,"rt":0,"ru":"http://geokov.com/education/slope-gradient-topographic.aspx","s":"diagram showing...
 
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.
              Download history in MQL4 EA - MQL4 and MetaTrader 4 - MQL4 programming forum
    Don't mix apples and oranges.
 
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.

 
 
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.

 
  • 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.)
 

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.

 
expertnevis:

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.

Hi expertnevis

Thanks for simplifying the complex issue :)

What is the value of 'Point' you have used in the above code ?

 
  1. Anil Varma - #: What is the value of 'Point' you have used in the above code ?

    Result is in points per bar. Don't use it, you get change in price per bar. If you keep the X2-X1, you get slope per second. You choose what units you need.

  2. Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).
              The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io (March 23rd 2019