Changing The Source Of A Moving Average

 

Hi guys,


Forgive me I've just started coding in mql4 last week....I have also tried searching the forums and reading the reference materials but am so confused.

My question is:

How do you change the source of a moving average to be something other than 'PRICE_CLOSE'/'PRICE_HIGH' etc.  For example just a moving average of the Volume.

This is super easy in pine script on tradingview.

I believe this cannot be done with 'iMA'.  Am I correct in thinking in my volume example above that you must use iMAOnArray?

I am getting so confused but what I have so far is:

#property copyright "Jim Davies"
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict


void OnStart()
  {
  
  
  double volbuffer[1];
  ArraySetAsSeries(volbuffer,True);
  volbuffer[0] = Volume[0];
  
  
  double mavol = iMAOnArray(volbuffer,0,14,0,MODE_SMA,0);
  
  Print(volbuffer[0]);
  Print(mavol);



The problem here is (volbuffer outputs the volume correctly) but mavol outputs 0.


Any ideas?


Thanks for any help

 

You need to fill the buffer I guess. One value is not enough to form a moving average.

 
lippmaje:

You need to fill the buffer I guess. One value is not enough to form a moving average.

Thanks for your reply.

I changed "double volbuffer[1]" to "double volbuffer[100]" and "mavol" now outputs 314.43533.... instead of 0. This value cant be right as the volume is  between 5000-10,000 over the last 14 candles...… :/. 

I know i'm stupid but this is difficult to understand....

 
Why 100? You need 14 as this is your MA period. And you need to fill that array so it holds all 14 volumes, that's it.
 
lippmaje:
Why 100? You need 14 as this is your MA period. And you need to fill that array so it holds all 14 volumes, that's it.

only because 14 returned a ridiculous number as well. (just testing other numbers)


if the volbuffer is outputting 5000+ and the previous 14 candles are 5000+ how on earth is a 14 period moving average returning 300? gaaaahhhhh

 
You need to copy all 14 volume values to your buffer, do this with a for loop.
 
apinkdog: How do you change the source of a moving average to be something other than 'PRICE_CLOSE'/'PRICE_HIGH' etc.  For example just a moving average of the Volume.
  1. Place a (separate window) volume indicator on the chart.
  2. Drag the Moving Average on to the volume indicator and select previous indicator.
 
lippmaje:
You need to copy all 14 volume values to your buffer, do this with a for loop.
ahh. okay.  I will look into this.  thank you!
 
William Roeder:
  1. Place a (separate window) volume indicator on the chart.
  2. Drag the Moving Average on to the volume indicator and select previous indicator.
that gives me a number to check/reference my code to thank you!
 

If anyone could help further...


I have a moving average(maArray) of a volume moving average(FinalMa)

MaArray is outputting the first index but not any of the others. All the others are 0.

Heres the code:

void OnStart()
   {



 double VolArray[14];
 double MaArray[14];
 

   for(int i=0; i<14;i++)
      {
      VolArray[i] =Volume[i];
     
      }

   for(int j=0;j<14;j++)
   
      {
      MaArray[j] = iMAOnArray(VolArray,0,14,0,MODE_SMA,j);
      }
 
double FinalMa = iMAOnArray(MaArray,0,14,0,MODE_SMA,0);

Print(MaArray[0]);
Print(FinalMa);
   }

Any help/Ideas why?


Thanks again guys

 

For anyone searching the forums with a similar problem....

The solution that works or at least outputs the correct numbers is increasing the VolArray[14] to VolArray[27] and also changing the VolArray 'for' loop to match. This i'm guessing is to allow for the shift of MaArray/iMaOnArray. I also reverse indexed VolArray with ArraySetAsSeries to keep the 0 index as the most recent candle.

FinalMa then outputs a 14 period moving average of a 14 period moving average of the volume.

Reason: