iMA value does not update

 

Hello friends,

I am trying to determine wether a trend is in the same direction on the (5) minute chart and hourly.

When I do a function call often one value has not been updated for example the hourly says it on short and the minute on long but the actual value is different. (values should be long and long)

But when I run the script once more it always does show correct directions, so apparently the code is functional and does work properly but does not update the values first attempt.

I tried using a for loop with two iterations but this does not fix the problem either?


Can someone please explain what goes wrong and how I can fix this?


The code:

string MinutesDirection;
string HourlyDirection;

void OnStart()
  {
   CheckHourly();
   CheckMinutes();

   Alert("Symbol: " + Symbol() + " Hourly: " + HourlyDirection + ", Minutes: " + MinutesDirection);

  }

void CheckHourly()
  {

   float MAfast = iMA(NULL,60,5,0,MODE_EMA,PRICE_CLOSE,0);
   float MAslow = iMA(NULL,60,10,0,MODE_SMA,PRICE_CLOSE,0);


   if(MAfast > MAslow)
     {
      HourlyDirection = "Long";
     }

   if(MAfast < MAslow)
     {
      HourlyDirection = "Short";
     }

  }

void CheckMinutes()
  {

   float MAfast = iMA(NULL,5,10,0,MODE_EMA,PRICE_CLOSE,0);
   float MAslow = iMA(NULL,5,20,0,MODE_EMA,PRICE_CLOSE,0);


   if(MAfast > MAslow)
     {
      MinutesDirection = "Long";
     }

   if(MAfast < MAslow)
     {
      MinutesDirection = "Short";
     }
  }
 

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

 
William Roeder #:

On MT4: Unless the current chart is that specific symbol(s)/TF(s) referenced, you must handle 4066/4073 errors before accessing candle/indicator values.
          Download history in MQL4 EA - MQL4 programming forum - Page 3 #26.4 (2019)

Do you mean with the current chart the one currently selected? And also how do you handle these error codes?

Regards, David

 
Magus I #: Do you mean with the current chart the one currently selected? And also how do you handle these error codes?

No! The current chart is the one on which the Script, Indicator or Expert Advisor is running on.

To handle the errors, click the links provided and read-up on what is written there.

Reason: