Line:
if(period<=1 || rates_total-begin<period) return(0);
must be a non-strict equality:
if(period<1 || rates_total-begin<period) return(0);Otherwise, if, for example, you compile the MACD.mq5 delivery code and set InpSignalSMA=1, the Signal line will be equal to zero, while it should be equal to the MACD line.
Why do you need the last parameter weightsum in
int LinearWeightedMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,const int period,const double& price[],double& buffer[],int &weightsum)
while the other 3 functions do not have it. If it is still needed, how to use it when initialising the external variable weightsum=0, other functions work crookedly and generate the error of division by zero.
Why do you need the last parameter weightsum in
int LinearWeightedMAOnBuffer(const int rates_total,const int prev_calculated,const int begin,const int period,const double& price[],double& buffer[],int &weightsum)
while the other 3 functions do not have it. If it is still needed, how to use it when initialising the external variable weightsum=0, other functions work crookedly and generate the error of division by zero.
In a similar indicator Custom Moving Average.mq5, supplied with the terminal, it is declared inside the function:
static int weightsum;
as static, i.e. it will not be reset at each new recalculation of the indicator. In indicators static int weightsum - do not cross, because they are different threads and they have their own variables.
But I suppose that if we count 2 or more LWMAs (running from the Expert Advisor) with different periods, then weightsum should be different for each, and not 1 for all by declaring static inside the function.
Thus, for each LWMA, we need to have our own global variable, which we will pass to LinearWeightedMAOnBuffer(),
For example, if there are 2 LWMAs, the following variables should be declared globally
int weightsum1;
int weightsum2;
and then you need to pass them to the function.
If I have misunderstood - please correct me.
Thus, for each LWMA, we need to have a global variable, which we will pass to LinearWeightedMAOnBuffer(),
For example, if there are 2 LWMAs, then globally declared are
int weightsum1;
int weightsum2;
and then you need to pass them to the function.
In the ExponentiaMA function I don't quite understand the meaning of entering the 3rd parameter prev_value. It is clear that in a pure formula for calculating the average you need the value of the previous period, but in my opinion the function would be clearer to the average person if it counted this value internally and only 3 data should be entered into the function as for SimpleMA.
Absolutely agree! It is not clear how to use it. In the end I chose ExponentialMAOnBuffer because I didn't understand where to take prev_value. These functions are necessary not to call an external indicator from the Expert Advisor, but to read everything inside. The developer suggested what?
It reminds me of a quote from the cartoon "Prostokvashino" - "In order to sell something you don't need, you must first buy something you don't need, and we don't have money".
I totally agree! It is not clear how to use it. In the end, I chose ExponentialMAOnBuffer, because I didn't understand where to take prev_value. These functions are necessary not to call an external indicator from the Expert Advisor, but to read everything inside. The developer suggested what?
It reminds me of a quote from the cartoon "Prostokvashino" - "In order to sell something you don't need, you must first buy something you don't need, and we don't have money".
Yes, I agree. The same applies to:
//+------------------------------------------------------------------+ //| Smoothed Moving Average| //+------------------------------------------------------------------+ double SmoothedMA(const int position,const int period,const double prev_value,const double &price[]) { double result=0.0; //--- check period if(period>0 && period<=(position+1)) { if(position==period-1) { for(int i=0; i<period; i++) result+=price[position-i]; result/=period; } result=(prev_value*(period-1)+price[position])/period; } return(result); }
It doesn't matter how the code in yellow is calculated. The result will still be a green string. And this is in the standard MT5 libraries! Yep!
Improve the function, otherwise it is useless. In MQL5, it is very useful when you need to calculate MA values on several instruments. And it is not known in advance which ones. You can't form MA handles of all instruments from the market overview!
I totally agree! It is not clear how to use it. In the end, I chose ExponentialMAOnBuffer, because I didn't understand where to take prev_value. These functions are necessary not to call an external indicator from the Expert Advisor, but to read everything inside. The developer suggested what?
It reminds me of a quote from the cartoon "Prostokvashino" - "In order to sell something you don't need, you must first buy something you don't need, and we don't have money".
Yes, I join you. The same applies to:
It doesn't matter how the code highlighted in yellow is calculated. The result will still be a green string. And this is in the standard MT5 libraries! Yep!
Improve the function, otherwise it is useless. In MQL5, it is very useful when you need to calculate MA values on several instruments. And it is not known in advance which ones. You can't form MA handles of all instruments from the market overview!
Well, you could search in CodeBase and look there. I am not the only one who has used this library...

Excuse me, what's the variable "position"?
double SimpleMA(const int position,const int period,const double &price[])
double SimpleMA( const int position, const int period, const double &price[]) { double result= 0.0 ; //--- check period if (period> 0 && period<=(position+ 1 )) { for ( int i= 0 ; i<period; i++) result+=price[position-i]; result/=period; } return (result); }
Good morning
Is it clearer like this?

- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
MovingAverages:
The MovingAverages library is a part of Standard package of MetaTrader 5 client terminal.
The library contains functions for calculation of different types of moving averages. Totally, there are 8 functions that can be divided into 2 groups of functions of the same type, each containing 4 of them.
The first group contains functions that receive an array and simply return a value of a moving average at a specified position:
These functions are intended for obtaining the value of an average once for an array, and are not optimized for multiple calls. If you need to use a function from this group in a loop (to calculate values of an average and further write each calculated value into an array), you'll have to organize an optimal algorithm.
The second group of functions is intended for filling out the recipient array by values of a moving average based on the array of initial values:
Author: MetaQuotes Software Corp.