wrong output from iMA()

 

Hello everyone,

I'm trying new code with Moving averages on 3 different periods:

string MAsBuySig;

extern int cx1=2; // MA shift 1
extern int cx2=3; // MA shift 2
extern int cx3=4; // MA shift 3

void OnTick()
  {
   double MA200_cx1 = iMA(_Symbol, _Period, 200, 0, MODE_SMA, PRICE_CLOSE, cx1);
   double MA50_cx1 = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE, cx1);
   double MA21_cx1 = iMA(_Symbol, _Period, 21, 0, MODE_SMA, PRICE_CLOSE, cx1);

   if(MA21_cx1>MA50_cx1 && MA50_cx1>MA200_cx1){MAsBuySig="Buy";}else {MAsBuySig="";}

   Comment(
   "\n",
   "\n   MAs Buy Signal: ",MAsBuySig,
   "\n   200MA candle ", cx1,": ",MA200_cx1,
   "\n   50MA candle ", cx1,": ",MA50_cx1,
   "\n   21MA candle ", cx1,": ",MA21_cx1
   );
   PlaceBlackRectangle();
  }

the problem is, i get wrong output for all 3 of them, as you can see on the below screenshots  (note: the cursor is on candle 2) :


even if the signal is correct, still the numbers are wrong as per "Data Window" (note: the cursor is on candle 2)


 
  1. Younis Alsulaimi: I'm trying new code with Moving averages on 3 different periods:
       double MA200_cx1 = iMA(_Symbol, _Period, 200, 0, MODE_SMA, PRICE_CLOSE, cx1);
       double MA50_cx1 = iMA(_Symbol, _Period, 50, 0, MODE_SMA, PRICE_CLOSE, cx1);
       double MA21_cx1 = iMA(_Symbol, _Period, 21, 0, MODE_SMA, PRICE_CLOSE, cx1);

    Words have meanings, use the correct ones. You are not getting MAs on “different periods,” you are getting MAs of different lengths from the same period.

  2. Younis Alsulaimi: the problem is, i get wrong output for all 3 of them, as you can see on the below screenshots  (note: the cursor is on candle 2) :

    Open each indicator and verify they have the correct lengths and type.

 
@William Roeder #: Words have meanings, use the correct ones. You are not getting MAs on “different periods,” you are getting MAs of different lengths from the same period.

Yes, words have meaning and the OP's words are correct, not incorrect. It is MetaQuotes that messed it up with the words and not the OP.

The OP is getting moving averages of different periods (correct term), of the same timeframe (MetaQuotes incorrectly called this "period").

 
Younis Alsulaimi: Hello everyone, I'm trying new code with Moving averages on 3 different periods: the problem is, i get wrong output for all 3 of them, as you can see on the below screenshots  (note: the cursor is on candle 2) :

even if the signal is correct, still the numbers are wrong as per "Data Window" (note: the cursor is on candle 2)

In your code you are using different shifts for each moving average, but it is unclear to us if you are using the same shift values for the indicators on the chart.

So, please show the parameters of the Indicators that you are displaying on the Chart and in the Data Watch.

 
William Roeder #:
  1. Words have meanings, use the correct ones. You are not getting MAs on “different periods,” you are getting MAs of different lengths from the same period.

  2. Open each indicator and verify they have the correct lengths and type.

its ma periods in the documentation, maybe thats why i confused with the wording.

I tried to fix the TF and shifts and still getting the same result

   double MA200_cx1 = iMA(_Symbol, PERIOD_M15, 200, 0, MODE_SMA, PRICE_CLOSE, 2);
   double MA50_cx1 = iMA(_Symbol, PERIOD_M15, 50, 0, MODE_SMA, PRICE_CLOSE, 2);
   double MA21_cx1 = iMA(_Symbol, PERIOD_M15, 21, 0, MODE_SMA, PRICE_CLOSE, 2);
Fernando Carreiro #:

In your code you are using different shifts for each moving average, but it is unclear to us if you are using the same shift values for the indicators on the chart.

So, please show the parameters of the Indicators that you are displaying on the Chart and in the Data Watch.

actually i'm using same shift for all three, just in the input i have 2 more shifts that i'm not using yet.

from my understanding, MAs on the tester chart template are only graphical, is it!? ... so it doesn't matter if MAs are on the chart or not, values will still caculate.

 
Fernando Carreiro #:

In your code you are using different shifts for each moving average, but it is unclear to us if you are using the same shift values for the indicators on the chart.

So, please show the parameters of the Indicators that you are displaying on the Chart and in the Data Watch.

Oh sorry now i got what you mean... the data watch will get values from the MAs on the tester template.. i'll check it now.

 

These are the settings for the 3 MAs


 
Younis Alsulaimi #: These are the settings for the 3 MAs

Then you have confirmed the problem. In your screenshot all three MAs have a shift of 0 ...


... but your EA is using a shift of "cx1 = 2" ...

extern int cx1=2; // MA shift 1
...
double MA200_cx1 = iMA(_Symbol, _Period, 200, 0, MODE_SMA, PRICE_CLOSE, cx1);

... so the results will be different.

 
Younis Alsulaimi #: These are-he settings for the 3 MA

Your screenshots show the indicators are “Smoothed”. Your code is reading “simple” (SMA). That is the difference.

Never any reason to use the SMMA(L) it's equivalent to the EMA(2L-1).
          The Smoothed Moving Average or SMMA - How to Avoid It - NinjaTrader Programming | futures.io (2019)

 
Fernando Carreiro #:

Then you have confirmed the problem. In your screenshot all three MAs have a shift of 0 ...


... but your EA is using a shift of "cx1 = 2" ...

... so the results will be different.

I think that you have got a bit mixed up Fernando.

double  iMA( 
   string       symbol,           // symbol 
   int          timeframe,        // timeframe 
   int          ma_period,        // MA averaging period 
   int          ma_shift,         // MA shift 
   int          ma_method,        // averaging method 
   int          applied_price,    // applied price 
   int          shift             // shift 
   );

The MA shift is the 4th parameter, the last parameter is the bar shift. Why on earth MQ decided to name them both shift, who knows? MA shift might have been better named  MA offset.

 
Keith Watford #: I think that you have got a bit mixed up Fernando. The MA shift is the 4th parameter, the last parameter is the bar shift. Why on earth MQ decided to name them both shift, who knows? MA shift might have been better named  MA offset.
Yes, you are correct. Sorry about the confusion I caused.
Reason: