**MPW Capital:**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()".

- docs.mql4.com

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?