Discussing the article: "Moving Average in MQL5 from scratch: Plain and simple"

 

Check out the new article: Moving Average in MQL5 from scratch: Plain and simple.

Using simple examples, we will examine the principles of calculating moving averages, as well as learn about the ways to optimize indicator calculations, including moving averages.

We have reviewed the principles of calculating the main types of moving averages available in the settings of the standard Moving Average indicator in the client terminal. The calculations presented in the article can be used both in indicators with calculation optimization (which is also shown in the article), while the presented codes can be used as an independent calculation of average values of a sequential data set in your programs.

The figure above shows the difference between moving averages with the same calculation period (10), but different types:

Red - SMA, green - EMA, golden - SMMA, blue - LWMA.

It is clear that the smoothed moving average is less susceptible to the influence of small price fluctuations than the others, and displays the general trend of price movement more clearly.
Exponential and linear weighted moving averages are more responsive to market fluctuations because they place the greatest weight on current data in their calculations.

Author: Artyom Trishkin

 

Thanks for the good article. But I can't help but criticise it. I'm in such a mood today.

The article pays special attention not so much to coding as to formulas and optimisation of calculations. But I think it will be more useful for novice programmers and those who have recently started to pay attention to the possibility of using different loop operators. I hope that the next articles will reflect this. After all, there are at least 3 loop operators in MQL5. And any of them can be used to build an indicator.

 

useful information, all at once in one place,

and finally the i++ loop

 

A good article for beginners to show the code behind the standard 4 MQ averages.  

You should also discuss importance of simple optimization and its impact as 99% of the ticks occur between the bar changes.  Minimizing the calculations between each bar change will provide much higher efficiency at the cost of a little complexity.  Thus by calculating the base values once on the bar change and saving the values will produce significant reductions in calculation time.   for example with a SAM the standard calculation for N periods is:

Consider

double sm=0;

for(int bar=0;bar<N;bar++) sum+=Close[CurrentBar-bar];

SMA=sum/N;


versus

static double partialsum;

double sum=0;


On the Bar Change{

partialsum=0;

for(int bar=0;bar<N-1;bar++) partialsum+=Close[CurrentBar-bar];

partialsum/=(N-1);

}

SMA =partialsum +Close[CurrentBar]/N;

If there are 1000 ticks in a bar period and N=10 then this optimization saves about 90,000 calculations of sum+=Close[EndingBar-bar] for each bar.  If your chart contains 1,000 bars, then the savings are over 90 Million unneeded calculations.  With modern cpus this savings generated by this example are trivial and probably not noticeable, eventually they will add up as your programs get more complex in Expert Advisors.   

The importance of manual optimization is that you are developing better programming techniques that will become second nature for you in future projects.  

 

Thank you for your article, but it is not clear why, after using the OnCalculate() function, I can no longer use the OrderSend() transaction function, I do not know how the author solved this problem, I have no choice but to use the indicators in the standard library in the following way:

#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#include <Indicators\Trend.mqh>
CiMA ma;

//+------------------------------------------------------------------+
//| Script programme start function &nbsp Script programme start function
//+------------------------------------------------------------------+
int OnInit(){ 
    ma.DeleteFromChart(ChartID(), 0);
    ma.Create(_Symbol, PERIOD_CURRENT, 14, 0, MODE_SMA, PRICE_CLOSE);
    ma.AddToChart(PERIOD_CURRENT, 0);
    
    return INIT_SUCCEEDED; 
}

void OnTick(){ 
    ma.Refresh();
    double curMA = ma.Main(0);
    
    //Print("Current MA value:", maValue);
}