need help with a piece of code

 

I am trying to generate alerts for moving average crossovers. At the end of every bar, I am trying to print the moving average at the end of the current period and at the end of the previous period. However, the values are not consistent. Can someone help me what I am doing wrong. here is the code that I have.

double short_ema=5, long_ema=25;
double longema, shortema, shortemaprev,longemaprev;
shortema=iMA(NULL,0,short_ema,0,0,0,0);
longema=iMA(NULL,0,long_ema,0,0,0,0);
shortemaprev=iMA(NULL,0,short_ema,0,0,0,1);
longemaprev=iMA(NULL,0,long_ema,0,0,0,1);
Print(shortema, "-", longema, "--", shortemaprev, "-", longemaprev);

The issue for instance is that the shortemaprev for the current period does not always match with the shortema for the previous period.

 
sanju:

The issue for instance is that the shortemaprev for the current period does not always match with the shortema for the previous period.


What do you mean they don't match?
shortema calculated with close prices on the current bar is subject to change until the bar is complete as the close price changes.