I'm trying to write a custom indicator that relies on finding the average value over a past number of bars, but I'm having trouble figuring out how to write the function, although it should be simple, but I don't see an "average" function prebuilt into mql4 and I don't think I would need to save each value into it's own variable and divide by the number of variables manually. There has to be a better way than that. For instance, if I want to find the average value of the Close price minus the Open prices over the past x number of bars, how would I be able to do that? Any ideas on writing a simple script that doesn't make me save each bar value into it's own variable, which would be cumbersome if I had more than say 10 bars, and then manually divide by the number of bars?
It's called a Simple Moving Average and is the most basic of indicators build into every platform I know (and that includes MetaTrader).
The MQL4 function for it (and several other types of moving averages) is "iMA()". If you need to apply it to custom data, then use "iMAOnArray()".
...I would need to save each value into it's own variable and divide by the number of variables manually. There has to be a better way than that....
I cannot comment on MQL4, but if you look in the MQL5 official library, you will see that's basically how they do it using an array and a loop.
So no harm in copying that way if there is no function in MQL4
double AveP = iMAOnArray(Buf_Par,0, 15, 0,MODE_SMA, i);
Buf_AveP[i] = AveP;
Please enable the necessary setting in your browser, otherwise you will not be able to log in.