mql4mql5mqlmetatrader5

wrong buffer value retrieved from mql5 indicator using iCustom


I just moved from mql4 to mql5 and now i am trying to read buffers values from an indicator , but it only shows me 0 instead of the actual buffer values

enter image description here

Here is my code :

#property version   "1.00"

int handle1 = 0;

double valu1,valu2,valu3,valu4;
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- create timer
   EventSetTimer(60);
   handle1 = iCustom(NULL, NULL, "LTD by KDMfx");
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//--- destroy timer
   EventKillTimer();
   IndicatorRelease(handle1);
  }

//+------------------------------------------------------------------+
//| Get indicator value                                              |
//+------------------------------------------------------------------+
double GetIndicator(int handle, int buffer_num, int index)
  {
 
   double arr[];
 
   if(CopyBuffer(handle, buffer_num, 0, index+1, arr) <= 0)
     {
      Sleep(200);
      for(int i=0; i<100; i++)
        {
         if(BarsCalculated(handle) > 0)
            break;
         Sleep(50);
        }
      int copied = CopyBuffer(handle, buffer_num, 0, index+1, arr);
      if(copied <= 0)
        {
         Print("CopyBuffer failed. Maybe history has not download yet? Error = ", GetLastError());
         return -1;
        }
      else
         return (arr[index]);
     }


   return 0;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void OnTick()
  {
//---
   valu1 = GetIndicator(handle1,0,4);
   valu2 = GetIndicator(handle1,1,4);
   valu3 = GetIndicator(handle1,2,4);
   valu4 = GetIndicator(handle1,3,4);
   
   Comment("b1: ", valu1,
            "\nb2: ", valu2,
               "\nb3: ", valu3,
                  "\nb4: ", valu4 
            
            );
  }

what am i doing wrong here ? the indicator does not appear on the current or the last closed candle , it appears on the current+4 candle , meaning on the 5th candle in past . so i am using "4" as the candle id but still no use , no matter what i try i can get it to work


Solution

  • Try simplifying things a bit. I don't have a copy of the indicator you are trying to read, but the following code should work

    #property version       "1.10"
    #property strict
    
    int handle;
    
    //+------------------------------------------------------------------+
    //| Initialization function of the expert                            |
    //+------------------------------------------------------------------+
    int OnInit()
    {
       handle=iCustom(_Symbol, 0, "LTD by KDMfx");
    return(INIT_SUCCEEDED);
    }
    //+------------------------------------------------------------------+
    //| Deinitialization function of the expert                          |
    //+------------------------------------------------------------------+
    void OnDeinit(const int reason)
    {
       IndicatorRelease(handle);
    }
    //+------------------------------------------------------------------+
    //| "Tick" event handler function                                    |
    //+------------------------------------------------------------------+
    void OnTick()
    {
       double LTDindicator[];
       ArraySetAsSeries(LTDindicator, true);
       CopyBuffer(handle, 0, 0, 25, LTDindicator);
    
       Comment(LTDindicator[0]);
    
    return;
    }