Sometimes zero value from iMA when running 32 EA on virtual server

 

Hello all,

I'm facing a strange problem with my automated trading system, which is running 32 EA (for 32 currency pairs) on a virtual server. In every EA I'm calculating the exponential moving average, but sometimes for some EA, the function iMA is returning 0. Here is the code snippet I use for all scripts:

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {

  if (isNewBar()){    
    hEMA = iMA(NULL,Timeframe,EMA_period,0,MODE_EMA,PRICE_CLOSE);
Does anyone has ever experienced something like this?

thank you in advance,

Kind regards

Christoph

 
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2.  hEMA = iMA(NULL,CalcPeriodInMin,EMA_period,0,MODE_EMA,PRICE_CLOSE);
    CalcPeriodInMin is not in minutes. It's an enumeration. On MT5 ENUM_TIMEFRAMES values are not the period in minutes.
    Use EnumToString(TF[i]) to see the string representation of the time frame.
    Use PeriodSeconds(TF[i]) to see the duration of the time frame.
              Why calculations of period()_value are different? (over 30 minutes) What's Burg? - Indices - Technical Indicators - MQL5 programming forum
  3. On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5
 
whroeder1:
  1. When you post code please use the CODE button (Alt-S)! (For large amounts of code, attach it.) Please edit your post.
              General rules and best pratices of the Forum. - General - MQL5 programming forum
              Messages Editor

  2. CalcPeriodInMin is not in minutes. It's an enumeration. On MT5 ENUM_TIMEFRAMES values are not the period in minutes.
    Use EnumToString(TF[i]) to see the string representation of the time frame.
    Use PeriodSeconds(TF[i]) to see the duration of the time frame.
              Why calculations of period()_value are different? (over 30 minutes) What's Burg? - Indices - Technical Indicators - MQL5 programming forum
  3. On MT5: Unless the chart is that specific pair/TF, you must Synchronize the terminal Data from the Server.
              Timeseries and Indicators Access /  Data Access - Reference on algorithmic/automated trading language for MetaTrader 5

Hello Whroeder,

thank you very much for the fast reply, sorry for the wrong formatation I have corrected it (1.).

For 2. in fact the variable name is misleading, it is of type ENUM_TIMEFRAMES.

3. I think this could be my problem, do you mean that i got zero value for certain pairs because the data of iMA is not ready?

Just for completion, attached is the terminal output of the experts if I put a printf right after the iMA call, you see the last one is zero.

Thank you in advance,

Kind regards

Christoph

Files:
Capture.PNG  46 kb
 

I saw some threads about calculation data not being ready, so i tryed to repeat the CopyBuffer instruction, but it seems not helping:

    hEMA=iMA(NULL,Timeframe,EMA_period,0,MODE_EMA,PRICE_CLOSE);
    CopyBuffer(hEMA,0,0,3,pEMA);
    ArraySetAsSeries(pEMA,true);

    int cnt = 0;

    while((hEMA==INVALID_HANDLE || pEMA[1] < 0.0001) && cnt < 100)
      {
      //--- tell about the failure and output the error code
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d",
                  Symbol(),
                  EnumToString(Timeframe),
                  GetLastError());
      CopyBuffer(hEMA,0,0,3,pEMA);
      ArraySetAsSeries(pEMA,true);
      cnt += 1;  
      }    

The output is continuosly repeating the CopyBuffer instruction without sucess:

...

2018.03.08 18:32:11.703    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:33:02.564    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:33:53.423    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:34:44.283    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:35:35.143    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:36:26.002    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:37:16.862    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:38:07.722    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:38:58.582    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:39:49.441    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:40:40.303    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
...

Can someone help?

Thanks in advance,

Kind regards

Christoph

 
Christoph Pedretti:

I saw some threads about calculation data not being ready, so i tryed to repeat the CopyBuffer instruction, but it seems not helping:

The output is continuosly repeating the CopyBuffer instruction without sucess:

...

2018.03.08 18:32:11.703    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:33:02.564    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:33:53.423    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:34:44.283    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:35:35.143    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:36:26.002    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:37:16.862    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:38:07.722    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:38:58.582    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:39:49.441    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
2018.03.08 18:40:40.303    GBPDKK_SuperSafeCross_H3 (GBPDKK,H3)    Failed to create handle of the iMA indicator for the symbol GBPDKK/PERIOD_H3, error code 4806
...

Can someone help?

Thanks in advance,

Kind regards

Christoph

It will never work this way. On each tick you are trying to create an handle (launch an indicator), this is done asynchronously, the data are not ready for your CopyBuffer.

Initialize your handle in OnInit(), then use CopyBuffer() in OnTick(). And check your handle right after, before using it.

 
Christoph Pedretti:

I saw some threads about calculation data not being ready, so i tryed to repeat the CopyBuffer instruction, but it seems not helping:


At some point you are passing bad data into the iMA function. Don't use "NULL" for symbols in Mql5 functions, and always specify the symbol and timeframe. You'll also want to check for errors when using CopyBuffer. Here is an example that will check to make sure you correctly called iMA and then loop CopyBuffer if the handle is valid but there was an error in copying the buffer. 

void OnStart()
  {
//---
   double ma_buffer[];
   int total = myMA(ma_buffer,20,"GBPUSD",PERIOD_H2);
   if(total > 0)
      printf("No errors in MA, size of buffer = %d",total);

  }
//+------------------------------------------------------------------+

int myMA(double                 &buffer[], 
         const int               ema_period,
         string                  symbol              = NULL,
         const ENUM_TIMEFRAMES   timeframe           = PERIOD_CURRENT, 
         const int               start               = 0,
         int                     count               = NULL,
         const bool              as_series           = true,
         const uint              millisecond_timeout = 250)
{
   ResetLastError();
   symbol = symbol == NULL ? _Symbol : symbol;
   int ma_handle = iMA(symbol,timeframe,ema_period,0,MODE_EMA,PRICE_CLOSE);
   if(ma_handle == INVALID_HANDLE)
   {
      printf("[%s]::Invalid indicator handle, code = %d",__FUNCSIG__,_LastError);
      return (int)false;
   }
   uint milliseconds = GetTickCount();
   ArraySetAsSeries(buffer,as_series);
   int result=0;
   
   do
   {
      ResetLastError();
      count = count == NULL ? Bars(symbol,timeframe) : count;
      result = CopyBuffer(ma_handle,0,start,count,buffer);  
   }
   while(_LastError != ERR_SUCCESS && GetTickCount()-milliseconds < millisecond_timeout);
   
   if(_LastError != ERR_SUCCESS || result < 1)
   {
      printf("[%s]::CopyBuffer error, code = %d",__FUNCSIG__,_LastError);
      return (int)false;
   } 
   return result;  
} 
 
aslkdjf:

At some point you are passing bad data into the iMA function. Don't use "NULL" for symbols in Mql5 functions, and always specify the symbol and timeframe. You'll also want to check for errors when using CopyBuffer. Here is an example that will check to make sure you correctly called iMA and then loop CopyBuffer if the handle is valid but there was an error in copying the buffer. 

There is no problem to use NULL with iMA. It means current chart symbol as documented.

Using a loop to manage asynchronous function is really a poor coding practice. If you really can't wait for a tick (why???), use a timer.

 
Alain Verleyen:

There is no problem to use NULL with iMA. It means current chart symbol as documented.

Using a loop to manage asynchronous function is really a poor coding practice. If you really can't wait for a tick (why???), use a timer.

What asynchronous function
 
aslkdjf:
What asynchronous function

iMA.

It returns an handle, but the underlying indicator is not yet ready (most of the time).

 
Alain Verleyen:

iMA.

It returns an handle, but the underlying indicator is not yet ready (most of the time).

That's exactly why the example function has a timeout feature, to keep calling CopyBuffer until either it's ready within an acceptable delay or it times-out and returns false. 
 
aslkdjf:
That's exactly why the example function has a timeout feature, to keep calling CopyBuffer until either it's ready within an acceptable delay or it times-out and returns false. 
I know. What I am saying is it's not a good approach to loop...at least you should add a Sleep() inside your loop, as you are keeping the thread busy 'all' the time, so in my opinion a timer is nicer.