Please help on iMA function !

 

To test the function iMA, I wrote the code below


      int start()
         {
          
          
          double x=iMA(NULL,0,10,0,MODE_SMA,MODE_CLOSE,1);
          double y=0;
   
          for (int i=1; i<=10; i++)
               
               y = y + Close[i]/10;
          
          
          Comment("x = ",x," / y = ",y);
         }
  

And I see the results of x and y are different.

Please show what is wrong with the way that I use iMA function. Thank you.

 
I dont know what you are attempting but ive spotted a problem.
 

I need the average close-price value of 10 periods from bar 1 to bar 10 // ( Close[1]+...+Close[10]) / 10.

So I write the code:

//-----------------------------------------------------------------

average=0;

for (int i=1;1<=10;i++) average = average+Close[i]/10;

//------------------------------------------------------------------

I wonder if we can use, equivalently, iMA() to get the average mentioned above or not ?

In other words, Can we get the average just by an assignment ? Like this:

average = iMA(NULL,0,10,0,MODE_SMA,MODE_CLOSE,1);

Is this assignment correct ? If not, where is the error?

 
dahumeovn:

To test the function iMA, I wrote the code below

And I see the results of x and y are different.

Please show what is wrong with the way that I use iMA function. Thank you.

Hi dahumeovn,

You wrote the wrong formula for SMA. There's Moving Average Indicator in MetaEditor which you can read MA formula from there.

:D

  int start()
     {
      double x = iMA(NULL, 0, 10, 0, MODE_SMA, MODE_CLOSE, 1);
      double y = 0;
      for (int i = 1; i <= 10; i++)
           {
           y = y + Close[i];
           }
      
      y = y/10;
      Comment("x = ",x," / y = ",y);
      }
 
dahumeovn:


I need the average close-price value of 10 periods from bar 1 to bar 10 // ( Close[1]+...+Close[10]) / 10.



Your code wont work because you are dividing each value by 10. Try something like below where only the total sum is divided by 10:

int start()
         {
          
          
          double x=iMA(NULL,0,10,0,MODE_SMA,MODE_CLOSE,1);
          double y=0;
   
           for (int i=1; i<=10; i++){
               
               y = y + Close[i];
             }

         y=y/10;
         Comment("x = ",x," / y = ",y);
   
         }
      
 
onewithzachy looks like we were typing at the same time but you clicked add first lol
 
onewithzachy:

Hi dahumeovn,

You wrote the wrong formula for SMA. There's Moving Average Indicator in MetaEditor which you can read MA formula from there.

:D



 for (int i=1; i<=10; i++)

       {
      
        y = y + Close[i];
       }

 y=y/10;


Above is the same as below:

 for (int i=1; i<=10; i++)
     y = y + Close[i]/10;
   

I insist the above two codings are 100% the same !

What I inquire is that whether or not we can alternate the for() statement by an assignment statement using iMA() function.

 
dahumeovn:



Above is the same as below:

I insist the above two codings are 100% the same !

What I inquire is that whether or not we can alternate the for() statement by an assignment statement using iMA() function.



They are not the same your code is like saying
Close[1]/10 + Close[2]/10 + Close[3]/10 +...+ Close[10]/10 

//the later is like 

(Close[1] + Close[2] + Close[3] +...+ Close[10] )/10
 
tonny:
They are not the same your code is like saying


You should check your maths, they are the same, the division happens before the addition, like this . . .

 for (int i=1; i<=10; i++)
     y = y + ( Close[i]/10 ) ;
 

dahumeovn:

double x=iMA(NULL,0,10,0,MODE_SMA,MODE_CLOSE,1); 
double y=0; 
for (int i=1; i<=10; i++) y = y + Close[i]/10;
And I see the results of x and y are different.
Please show what is wrong with the way that I use iMA function.
  1. Why didn't you post the results. Had you, I would have seen the error immediately instead of the 1/10 side track.
    #include <stdlib.mqh>
    int     start(){                        
      double x = iMA(NULL,0,10,0,MODE_SMA, MODE_CLOSE, 1);
      double y=0, z=0;
      for(int i=1; i<=10; i++){ y = y + Close[i]/10; z=z + Close[i]; }
      z = z / 10;
      Print("x="+DoubleToStrMorePrecision(x,16), 
            " y="+DoubleToStrMorePrecision(y,16), 
            " z="+DoubleToStrMorePrecision(z,16));
    // x=1.5718599999999996 y=1.5730100000000000 z=1.5730100000000000
    Since 1/10 can not be exactly represented by floating point numbers, 1/10.+1/10.+1/10.+1/10.+1/10.+1/10.+1/10.+1/10.+1/10.+1/10. is not equal 1.000000, so your Sum(C/10) will not be exactly the same as Sum(C)/10. But since you are only summing 10 numbers, the results are within 16 decimals places. (Sum a million numbers and you'll be off notably.)
  2. You must read the manual better. Ima( sym, tf, period, shift, method, applied price, iBar) applied price NOT Series arrays.
    // double x = iMA(NULL,0,10,0,MODE_SMA,MODE_CLOSE,1); // MODE_CLOSE = 3 = PRICE_LOW
       double x = iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1);

  3. tonny: Is a troll and spammer and spammer and spammer

    Please do not feed the troll.

    When you respond, you give the troll power. When you ignore the troll, he starves for attention and eventually dies.

 
RaptorUK:


You should check your maths, they are the same, the division happens before the addition, like this . . .

True.
Reason: