How can I get moving average every minute

 

Hello, I am coding my first EA to get moving average calculated every minutes. When I initialised my EA it would return the correct value, but then it would return the same original value constantly.

So it seems that the iMA function reads the value on the chart only at initialisation and does not update afterwards. How can I solve this? Thanks for your support.

I use the following code (partial display):

int start()
{
double MovingPeriodLong = 14;
double MovingShiftLong = 0;
bool run =true;
double MACD_long_period;

while (run==TRUE)
{
MACD_long_period=functionMACDreturnLong(MovingPeriodLong,MovingShiftLong);

// Display MACD calculation value

Print( "MACD long =", MACD_long_period);

Sleep(60000);
}
return(0);
}

double functionMACDreturnLong(double MovingPeriodLongParam,double MovingShiftLongParam)
{
double MACD_long_periodLast;
MACD_long_periodLast=iMA(NULL,0,MovingPeriodLongParam,MovingShiftLongParam,MODE_EMA,PRICE_CLOSE,0);

return(MACD_long_periodLast);

}

 

A)

B) You are completely misunderstanding that nature of MT4 processing and how start() works ...

- start() is called for each tick (typically every fews seconds or so)

- the idea is to let start() finish processing as soon as possible - do NOT use Sleep() ! - do NOT loop forever ! (this is my view, others may differ)

C) to calc MA every minute in an EA, with a 1 minute chart, try something like the following in start():

static datetime LastTickBarTime = 0;
bool FirstTickOfBar = false;
if(LastTickBarTime != Time[0])
{
 LastTickBarTime = Time[0];
 FirstTickOfBar = true;
}
if(FirstTickOfBar)
{
 CheckMA_AndProcessIt();
}
else
{
 // do no work
 return(0);
}

NB code just typed, not compiled or checked