My moving average calculations aren't working right

 

Help!

I have an EA written in MQL4. It is attached to one currency/chart window, but actually performs automatic trades in 6 different currency pairs.

My problem is with 2 lines of code:

// get 5-minute moving averages: current and 4 periods ago
// the +1 offset for the Shift argument was determined through experimentation

iMA_now = iMA(currency, PERIOD_M5, 1, 0, MODE_SMA, PRICE_CLOSE, 0);
iMA_past = iMA(currency, PERIOD_M5, 1, 0, MODE_SMA, PRICE_CLOSE, 4+1);

My intention is for the first line of code to obtain the current 5-minute average for the currency, and the second line of code should obtain the 5-minute average for exactly 20 minutes ago.

This is not working right. When I output the values obtained, and compare them to the values obtained from a real chart window (with an indicator set for a 5-minute moving average), they don't match.

Am I missing or misunderstanding something in this iMA() function?

Is there something I need to be doing so that my EA attached to (eg) EURUSD can call iMA() to get 5-minute moving averages from AUDUSD?

Many thanks in advance.

 
You don't have much of a MA taking an average of 1 bar ;-)
 
RaptorUK:
You don't have much of a MA taking an average of 1 bar ;-)


I bet that's a terriffic answer to my question but I don't understand it!

I find the documentation for these indicators to be very cryptic. I have decoded the iMA() function the best I can from the documentation, but there is a very good chance I completely misunderstand it.

Would you elaborate?

 

Indeed RaptorUK's humorous answer is correct :-)

You are using two moving averages, both with a period of 1 bar. That means each MA is an MA of the close price of only 1 bar - it is just an average of itself! In other words, you are just comparing the current bar close to the close of the bar 20 mins ago.

Check this page https://docs.mql4.com/indicators/iMA

You need to change the "period" parameter (unless you actually WANT just the unadulterated close prices, in which case you could just use iClose instead of iMA - unless of course you want a median, typical or weighted price in which case using iMA is quite clever).

If you want more info on how moving averages are calculated then try here http://stockcharts.com/school/doku.php?id=chart_school:technical_indicators:moving_averages or here https://www.mql5.com/en/code/7534

 
  1. Your code:
    iMA_now = iMA(currency, PERIOD_M5, 1, 0, MODE_SMA, PRICE_CLOSE, 0);

  2. From iMA - MQL4 Documentation
    double iMA( string symbol, int timeframe, int period, int ma_shift, int ma_method, int applied_price, int shift)
  3. What part of period = 1 was unclear?
 
leecallen:


I bet that's a terriffic answer to my question but I don't understand it!

I find the documentation for these indicators to be very cryptic. I have decoded the iMA() function the best I can from the documentation, but there is a very good chance I completely misunderstand it.

Would you elaborate?

Sure . . . from https://docs.mql4.com/indicators/iMA

period - Averaging period for calculation.

If you want your MA to be the average of 5 bars you use 5 for the Period, you have 1, so your average is from 1 bar.

 
RaptorUK:

Sure . . . from https://docs.mql4.com/indicators/iMA

period - Averaging period for calculation.

If you want your MA to be the average of 5 bars you use 5 for the Period, you have 1, so your average is from 1 bar.


Do the period and the timeframe interact? Ie, if I set the period argument to 5, do I get a 5-minute moving average, irrespective of the timeframe setting?

And what about my shift argument: if I want the 5-minute moving average from 20 minutes ago, do I set shift to 4 (4 * 5-minute periods) or 20 ?

Thanks guys.

 
leecallen:


Do the period and the timeframe interact? Ie, if I set the period argument to 5, do I get a 5-minute moving average, irrespective of the timeframe setting?

And what about my shift argument: if I want the 5-minute moving average from 20 minutes ago, do I set shift to 4 (4 * 5-minute periods) or 20 ?

Thanks guys.

If you set a Period of 20 and a timeframe of 5 you will get the average of 20 bars from the M5 timefrane . . . the shift is where these 20 bars start from.
 
RaptorUK:
If you set a Period of 20 and a timeframe of 5 you will get the average of 20 bars from the M5 timefrane . . . the shift is where these 20 bars start from.

Thank you, it's now abundantly clear.
Reason: