Determining the slope of an exponential moving average

 

Hello community. I am currently coding an ea which will allow me to know if the market base line of the TDI indicator (trading dynamic index) which is an exponential moving average is inclined downwards or upwards. In my research I saw that I could use the slope which, if positive, means that the MA is upwards, and if negative, that it is downwards. Here's the code:

double CalculateMAslope(const double& ma[], const int period)
{
    double sum_x = 0.0;
    double sum_y = 0.0;
    double sum_x2 = 0.0;
    double sum_xy = 0.0;

    for (int i = period - 1; i >= 0; i--)
    {
        double x = i;
        double y = ma[i];
        sum_x += x;
        sum_y += y;
        sum_x2 += x * x;
        sum_xy += x * y;
    }

    double slope = (period * sum_xy - sum_x * sum_y) / (period * sum_x2 - sum_x * sum_x);

    return slope;

}

Improperly formatted code edited by moderator.

After testing, I've found that this isn't at all the case in practice. I have positive slopes but visually the Market Base line is tilted downwards. I need your help to explain to me how to go about determining the slopes so that the results given in the terminal reflect those seen on the graph. Thank you 

Moving Average - Trend Indicators - Technical Indicators - Price Charts, Technical and Fundamental Analysis - MetaTrader 5 Help
  • www.metatrader5.com
The Moving Average Technical Indicator shows the mean instrument price value for a certain period of time. When one calculates the moving average...
 
Herbert20: Hello community. I am currently coding an ea which will allow me to know if the market base line of the TDI indicator (trading dynamic index) which is an exponential moving average is inclined downwards or upwards. In my research I saw that I could use the slope which, if positive, means that the MA is upwards, and if negative, that it is downwards. Here's the code: After testing, I've found that this isn't at all the case in practice. I have positive slopes but visually the Market Base line is tilted downwards. I need your help to explain to me how to go about determining the slopes so that the results given in the terminal reflect those seen on the graph. Thank you 
Find your answer here: https://www.mql5.com/en/forum/219495
How to calculate slope of an EMA?
How to calculate slope of an EMA?
  • 2017.11.11
  • www.mql5.com
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 ca...
 
Herbert20

Improperly formatted code edited by moderator.

In the future, please use the CODE button (Alt-S) when inserting code.

Code button in editor

MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Your topic has been moved to the section: Expert Advisors and Automated Trading
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Dominik Egert #:
Find your answer here: https://www.mql5.com/en/forum/219495
okay thanks