iCustom returns 0 when used on different timeframe

 

Hi, I'm using MT5 build 1627 and I noticed a potential bug.

When using the iCustom-function on a different timeframe within an indicator, the result is always 0.

For demonstration, i put the following code into a script:

//+------------------------------------------------------------------+
#property copyright "@2017, osfx"
#property link      "https://www.forexfactory.com/osfx"
#property version   "1.00"
//---
#include <@osfx\osfx.StandardLib.v04.mqh>
//---
//+------------------------------------------------------------------+
//| Script program start function                                    |
//+------------------------------------------------------------------+
void OnStart()
  {
   int RMIHandle=iCustom(_Symbol,PERIOD_M4,"@osfx\\osfx.RMI.v02.ex5",
                         "",12,3,PRICE_CLOSE,
                         "",false,
                         "",0.0,0.0,
                         "",0,0,1,clrNONE,clrNONE,clrNONE,
                         "",true);

   double RMI[];
   ArraySetAsSeries(RMI,true);
   CopyBuffer(RMIHandle,0,0,2,RMI);

   Print("RMI[1]: ",DoubleToString(RMI[1],2));
  }
//+------------------------------------------------------------------+

Running the script on a M1-timeframe, it returns the correct result:

Runnning the same code within an indicator:

//+------------------------------------------------------------------+
//|                                                         Test.mq5 |
//|                                                      @2017, osfx |
//|                                https://www.forexfactory.com/osfx |
//+------------------------------------------------------------------+
#property copyright "@2017, osfx"
#property link      "https://www.forexfactory.com/osfx"
#property version   "1.00"
#property indicator_chart_window
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
   static bool Test=true;

   if(Test)
     {
      int RMIHandle=iCustom(_Symbol,PERIOD_M4,"@osfx\\osfx.RMI.v02.ex5",
                            "",12,3,PRICE_CLOSE,
                            "",false,
                            "",0.0,0.0,
                            "",0,0,1,clrNONE,clrNONE,clrNONE,
                            "",true);

      double RMI[];
      ArraySetAsSeries(RMI,true);
      CopyBuffer(RMIHandle,0,0,2,RMI);

      Print("RMI[1]: ",DoubleToString(RMI[1],2));

      Test=false;
     }

   return(rates_total);
  }
//+------------------------------------------------------------------+

returns:

Do I miss anything or is this a bug ?

Thanks, Oliver

 
Oliver Schroeer:

Hi, I'm using MT5 build 1627 and I noticed a potential bug.

When using the iCustom-function on a different timeframe within an indicator, the result is always 0.

For demonstration, i put the following code into a script:

Running the script on a M1-timeframe, it returns the correct result:

Runnning the same code within an indicator:

returns:

Do I miss anything or is this a bug ?

Thanks, Oliver


This is a bug (feature) in MQL5. All indicators run on a single thread so you can't call to a different indicator from within a running indicator. You have to trick it by first instantiating the second indicator and then setting a timer so that you can exit out of the OnCalculate function - run the second ind - and then return back to the OnTimer function of the first indicator to capture the data from the second. 

 
nicholishen:

This is a bug (feature) in MQL5. All indicators run on a single thread so you can't call to a different indicator from within a running indicator. You have to trick it by first instantiating the second indicator and then setting a timer so that you can exit out of the OnCalculate function - run the second ind - and then return back to the OnTimer function of the first indicator to capture the data from the second. 


I just tested it successfully - many thanks for your help !!

 
nicholishen:

This is a bug (feature) in MQL5. All indicators run on a single thread so you can't call to a different indicator from within a running indicator.

Actually there is 1 thread by symbol, not for all indicators. And there is no problem to call a different indicator within a running one.
 
Oliver Schroeer:

Hi, I'm using MT5 build 1627 and I noticed a potential bug.

When using the iCustom-function on a different timeframe within an indicator, the result is always 0.

For demonstration, i put the following code into a script:

Running the script on a M1-timeframe, it returns the correct result:

Runnning the same code within an indicator:

returns:

Do I miss anything or is this a bug ?

Thanks, Oliver

The problem is on the first call, the "iCustom" indicator is not ready yet. You need to check the return value of CopyBuffer(), you will probably get an error 4806. In this case just use return(0) and on next tick all should be ok.

Also I suggest you to initiliaze your handle in OnInit().

 
Alain Verleyen:

The problem is on the first call, the "iCustom" indicator is not ready yet. You need to check the return value of CopyBuffer(), you will probably get an error 4806. In this case just use return(0) and on next tick all should be ok.

Also I suggest you to initiliaze your handle in OnInit().


Thanks Alain, tested it successfully !

Reason: