Moving average of an indicator

To add comments, please log in or register
Philippe
23
Philippe  

Hi,


I'm new in programming MQL4, I'm trying to calculate the mving average of an indicator in an EA.


Could someone give me a code example of :


- The moving average of x period of the volume of a symbol ?

- The moving average of x period of the slow stochastic(5,10,3) of a symbol ?


Thanks in advance for your help.

Philippe
23
Philippe  

No one to help me ?.


Mark
249
Mark  

I would imagine it would have something to do with re-writing a custom MA indicator that looks at average values of the indicator you want. Should really just be a case of replacing the price values in the current MA indicator with iStochastic values then you can just use iCustom() to call the custom indicator. I think.


here's the simple moving average code from the MA indicator that comes with MT4, should just be a case of replacing the Close[pos] with iStochastic(...,[pos]) values i think


void sma()
{
double sum=0;
int i,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=Close[pos];
//---- main calculation loop
while(pos>=0)
{
sum+=Close[pos];
ExtMapBuffer[pos]=sum/MA_Period;
sum-=Close[pos+MA_Period-1];
pos--;
}
//---- zero initial bars
if(ExtCountedBars<1)
for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;

Philippe
23
Philippe  
mrwobbles:

I would imagine it would have something to do with re-writing a custom MA indicator that looks at average values of the indicator you want. Should really just be a case of replacing the price values in the current MA indicator with iStochastic values then you can just use iCustom() to call the custom indicator. I think.


here's the simple moving average code from the MA indicator that comes with MT4, should just be a case of replacing the Close[pos] with iStochastic(...,[pos]) values i think


void sma()
{
double sum=0;
int i,pos=Bars-ExtCountedBars-1;
//---- initial accumulation
if(pos<MA_Period) pos=MA_Period;
for(i=1;i<MA_Period;i++,pos--)
sum+=Close[pos];
//---- main calculation loop
while(pos>=0)
{
sum+=Close[pos];
ExtMapBuffer[pos]=sum/MA_Period;
sum-=Close[pos+MA_Period-1];
pos--;
}
//---- zero initial bars
if(ExtCountedBars<1)
for(i=1;i<MA_Period;i++) ExtMapBuffer[Bars-i]=0;

Thanks for your help, mrwobbles, but it didn't help me yet.

Imagine simply I would like to calculate the moving average of x periods on volumes, have you got the code for this ?

Mark
249
Mark  
I've done a simple MA of the volume, if you look at the Moving Averages indicator that comes with MT4 you can see what I've done. It's fairly simple. The problem with the Stochastic MA is that stochastics return positive and negative values so the sums arent accurate.
Files:
Mark
249
Mark  
here's one that has an exponential MA in it as well. just change MA method, 0=Simple MA, 1=Exponential MA. Oh and you have to set the colour to red, default is black for some reason...
Files:
ahmetozer10
6
ahmetozer10  
Use iMAOnArray() function. For the data array, use an array you previously populated with the indicator's data.


PS:

Note that

   ArraySetAsSeries(array, true);

is necessary before calling iMAOnArray function.
paul selvan
505
paul selvan  

As said ahmetozer10  you have to use iMAOnArray()

e.g. 

int period = 7;
int history = 100;
double array[];
ArrayResize(array,history);
   
for (int i=0; i<history; i++) array[i]=(double)iVolume(NULL,0,i); 

double result = iMAOnArray(array,0,period,0,MODE_SMA, shift);  // shift = studied bar
ahmetozer10
6
ahmetozer10  

Note that

   ArraySetAsSeries(array, true);

is necessary before calling iMAOnArray function.
Marcin Madrzak
477
Marcin Madrzak  
ahmetozer10:
Use iMAOnArray() function. For the data array, use an array you previously populated with the indicator's data.


PS:

Note that

   ArraySetAsSeries(array, true);

is necessary before calling iMAOnArray function.

You have just answered 10 years old question. I know, theoretically there is possibility they guy still is looking for an answer but the chances are slim ;) 

To add comments, please log in or register