iMA function - does not handle the previous value

 

Hello,

I'm trying to write a code using iMA function however I'm very much confused when I start tracing the iMA values.

The below code is supposed to calculate the short and long moving averages of current and one period before values of iMA

ma1 is short period ma, ma2 is long period ma, ma1p is previous short ma and ma2p is previous long ma.

The code is inside Start() function and when you choose 1M period on chart, every minute you get the results on screen.


   ma1=iMA(NULL,0,MovingPeriod_1,0,MODE_LWMA,PRICE_CLOSE,0);
   ma2=iMA(NULL,0,MovingPeriod_2,0,MODE_LWMA,PRICE_CLOSE,0);
  
   ma1p=iMA(NULL,0,MovingPeriod_1,0,MODE_LWMA,PRICE_CLOSE,1);
   ma2p=iMA(NULL,0,MovingPeriod_2,0,MODE_LWMA,PRICE_CLOSE,1);
  

   Alert("ma1=",ma1,"  ma1p=",ma1p,"  ma2=",ma2,"  ma2p=",ma2p);


The problem is ma1 of the previous line must be equal to current ma1p in the Alert dialog box. But it isn't.


I've got the below results from Eur/Usd  M1

04:   ma1=1.304  ma1p=1.3041      ma2=1.3042  ma2p=1.3043

03:   ma1=1.304  ma1p=1.304        ma2=1.3044 ma2p=1.3045

02:   ma1=1.304  ma1p=1.304        ma2=1.3045  ma2p=1.3045

01:   ma1=1.3043  ma1p=1.3044     ma2=1.3047  ma2p=1.3048


For example, In the 3rd tick, ma1p is equal to 2nd tick of ma1 which is fine again ma2p is equal to ma2 but for the other ticks it's not the same and it should be.


Any help is appreciated.

AED.

 

I read somewhere, stating that index 0 might give wrong values since it calculates the current tick.

So I decided to change the code to below, comparing index 1 and index 2 instead of index 0 and index 1.

   ma1=iMA(NULL,0,MovingPeriod_1,0,MODE_LWMA,PRICE_CLOSE,1);
   ma2=iMA(NULL,0,MovingPeriod_2,0,MODE_LWMA,PRICE_CLOSE,1);
  
   ma1p=iMA(NULL,0,MovingPeriod_1,0,MODE_LWMA,PRICE_CLOSE,2);
   ma2p=iMA(NULL,0,MovingPeriod_2,0,MODE_LWMA,PRICE_CLOSE,2);
  

   Alert("ma1=",ma1,"  ma1p=",ma1p,"  ma2=",ma2,"  ma2p=",ma2p);


Most of the values are now printing normal but again with some exceptions. It may come with rounding of 5th decimal but again it shouldn't be. These are past values and each time the past values must be calculated in the same manner.

I don't know if I should use iMAonArray type of function but then I'm asking myself why there is a function called iMA?


Any comments?

AED.

 
Alert("ma1=",DoubleToStr(ma1,Digits),"  ma1p=",ma1p,"  ma2=",ma2,"  ma2p=",ma2p);

Alert

read again how Alert is displaying values

Data of double type output with 4 decimal digits after point. To output with more precision use DoubleToStr() function.

.

with this line mal will be correctly displayed with digits of Symbol( )

 

Yes, thank you deVries;

Now my results are shown better however the results doesn't change. Now I clearly see that index 0 and index 1 values are not calculated correctly.

Any comments for this also?

Thanks.

 
aed71:

Yes, thank you deVries;

Now my results are shown better however the results doesn't change. Now I clearly see that index 0 and index 1 values are not calculated correctly.

How do you know they are not correct ?
 
aed71:

Yes, thank you deVries;

Now my results are shown better however the results doesn't change. Now I clearly see that index 0 and index 1 values are not calculated correctly.

Any comments for this also?

Thanks.



Use Data Window      CTRL+D

and hold pc mouse pointed to the bar you wanna check

if values incorrect then settings indicator are different with the settings of your EA 

 

I did it, the current ma value is equal to the one in Data Window. This is fine. The 5 digit output is also very fine.

The problem is I think I cannot hold the previous value of ma_short and ma_long properly with the formula.

ma1=iMA(NULL,0,MovingPeriod_1,0,MODE_LWMA,PRICE_CLOSE,1);
   ma2=iMA(NULL,0,MovingPeriod_2,0,MODE_LWMA,PRICE_CLOSE,1);
  
   ma1p=iMA(NULL,0,MovingPeriod_1,0,MODE_LWMA,PRICE_CLOSE,2);
   ma2p=iMA(NULL,0,MovingPeriod_2,0,MODE_LWMA,PRICE_CLOSE,2);
  

   Alert("ma1=",ma1,"  ma1p=",ma1p,"  ma2=",ma2,"  ma2p=",ma2p);



Hi RaptorUK,

I print them with Alert on the screen. And ma1p must be equal to the value of previous line ma1. I'm trying to check like that.

Please comment if my logic to tracing is wrong.


Many thanks

AED.

 
aed71:

Hi RaptorUK,

I print them with Alert on the screen. And ma1p must be equal to the value of previous line ma1. I'm trying to check like that.

Please comment if my logic to tracing is wrong.


When you get a new bar the value of ma1p  will be what the value of ma1  was during the last bar and the value of ma2p will be what the value of ma2 was during the last bar.  Do you Alert just at the start oof the new bar ?
 
RaptorUK:

When you get a new bar the value of ma1p  will be what the value of ma1  was during the last bar and the value of ma2p will be what the value of ma2 was during the last bar.  Do you Alert just at the start oof the new bar ?

It works just fine . . .

 

 

Try this Indicator on a M1 chart for a few minutes. 

Files:
 

You are great, thanks for detailed explanation. I think my problem was time intervals. I was choosing 1M time frame and sometimes I realized that there are missing minutes in my chart.

I saw your code much better with time control. I think I have a lot more to learn the structure of writing MQL codes. I'm glad that the MQL is working fine  :-)

I really appreciate your help.

Best Regards

AED.

Reason: