Moving Average not updating

 

Good day

 

I'm using the following code showing the result of the current 2 moving averages but it is not updating even if the MA Chart is moving...

 

int start()
  {
   int ctr=0;
   while(IsStopped()==false)
   {
      double MAa=iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,0);
      double MAb=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0);
      
      TxtDsply("MAa",20,10,"Moving Average 1: "+DoubleToStr(MAa,Digits),8,"Arial",White);
      TxtDsply("MAb",30,10,"Moving Average 2: "+DoubleToStr(MAb,Digits),8,"Arial",White);
          
   }
   return(0);
  }

 Thank you very much

 
int start()
  {

  
      double MAa=iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,0);
      double MAb=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0);
      
      TxtDsply("MAa",20,10,"Moving Average 1: "+DoubleToStr(MAa,Digits),8,"Arial",White);
      TxtDsply("MAb",30,10,"Moving Average 2: "+DoubleToStr(MAb,Digits),8,"Arial",White);
          
   
   return(0);
  }

why not this way???

this works

 
deVries:

why not this way???

this works


tnx man....

 

i have put it on a script that's why it is not working. I put it on an expert adviser and it works... 

 
and one more thing. How can i check if the 2 MA's crossed. I mean if the 2 MA's crossed it will give an alert... Could i simply do this code?
if(MAa==MAb)
{
     Alert("Moving Averages Crossed");
}
 
raven_chrono:
and one more thing. How can i check if the 2 MA's crossed. I mean if the 2 MA's crossed it will give an alert... Could i simply do this code?

You should read this thread:  Can price != price ? 

MAs don't always cross at a bar,  sometimes they cross between bars. 

 
make use of boolean if direction has changed change value boolean and give Alert
 

i mean if both MA's (MAa, MAb) have the same value it will give an alert. I have tried this code but no success at all even though in the output they have the same result.

 

if((MAa-MAb)==0.0)
{
     Alert("Moving Averages Crossed");
}
 
raven_chrono:

i mean if both MA's (MAa, MAb) have the same value it will give an alert. I have tried this code but no success at all even though in the output they have the same result.

 

 


A line is a connection from point to point of a collection points making a line you see

if there are small gaps in that collection of points you do not see them

that makes you have a situation like   Can price != price ?    click on that link

It is not always that values get equal when one is going bigger then the other

maybe this can work but there will be situations you miss the cross

 

if(DoubleToStr(MAa,Digits)==DoubleToStr(MAb,Digits))
     {
     Alert("Moving Averages Crossed");
     }
 
  1. The == operand. - MQL4 forum
  2. Same problem. If the values were x.xxx10 < x.xxx15 and then x.xxx13 > x.xxx12 they crossed but they were never equal. Don't check for equality, check for a cross.
 

how do i check for the cross. Is this code right?

 

double MAa=iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,0);
double MAb=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,0);
double tempA=iMA(NULL,0,3,0,MODE_SMA,PRICE_CLOSE,1);
double tempB=iMA(NULL,0,10,0,MODE_SMA,PRICE_CLOSE,1);

if((MAa-MAb)*(tempA-tempB)<0)
{
     Alert(......);
} 
Reason: