MT4 MQL noob: my indicators won't refresh

 

Hi guys, and thanks for taking the time to read this.

I have just started learning & coding in MQL (I have some experience with C). I am setting up an EA that gets moving averages. It loops, getting the moving averages repeatedly -- intending to be getting the most recent averages.

The first time I grab moving averages they are correct. But on every subsequent loop iteration, they return the SAME VALUES -- they don't reflect the new values that are being added to the chart.

Here is my code:

int start()
int errchk;
double iMA_now, iMA_past;
while(1)
{
// get most recent 5-min moving average from current chart
iMA_now = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);
errchk = GetLastError();
if(errchk != ERR_NO_ERROR)
Alert("error getting current average: ", ErrorDescription(errchk));
// get 5-min moving average from 15 minutes ago
iMA_past = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 3);
errchk = GetLastError();
if(errchk != ERR_NO_ERROR)
Alert("error getting past average: ", ErrorDescription(errchk));
Print("Now: ", iMA_now, ", Past:", iMA_past);
}
return(0);
}

What am I missing?

Thank you

 
After sleeping on it, I think I have figured it out: I shouldn't be looping. start() will be called with each incoming tick.
 
leecallen:

Well, the code you posted doesn't actually compile. If yours does you must have mis-copied it when posting.

It helps to use the source code button (SRC) when posting so you retain the syntax coloring.

The GetLastError() function checks seem inappropriate. There is no mention of an error flag being set by the iMA function.

I assume you have

#include <stderror.mqh>
#include <stdlib.mqh>

at the top of the file, but didn't copy them into the post above.

 
#property indicator_chart_window


#include <stderror.mqh>
#include <stdlib.mqh>

int start(){

   // get most recent 5-min moving average from current chart
   double iMA_now = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 0);
   int errchk = GetLastError();   // this probably doesn't do anything
   if( errchk != ERR_NO_ERROR )
      Alert("error getting current average: ", ErrorDescription(errchk));
   
   // get 5-min moving average from 15 minutes ago
   double iMA_past = iMA(NULL, 0, 5, 0, MODE_SMA, PRICE_CLOSE, 3);
   errchk = GetLastError();   // this probably doesn't do anything
   if(errchk != ERR_NO_ERROR)
      Alert("error getting past average: ", ErrorDescription(errchk));
   
   // if you print to the log file on every tick the log could get quite big 
   Print("Now: ", DoubleToStr(iMA_now, Digits), ", Past:", DoubleToStr(iMA_past,Digits));
   Comment( "Now: ", DoubleToStr(iMA_now, Digits), ", Past:", DoubleToStr(iMA_now, Digits) );
   
   return(0);
}