Indicator Causes EA to fail

 

I found this indicator on github. 

It loads in terminal just fine. When called via EA it fails. 

Alert: Error 0 while getting handle on Price_Density

I replicated the error by using a test EA. 

Any help would be much appreciated. 

This is sample code of EA getting handle in OnInit, that generates an error. Now if I don't use OnInit and move everything to OnTick it works. Code is attached for that as well. I am just curious what am I missing here?

// TEST EA FOR PRICE DENSITY Using OnInit() //

#include<Trade\Trade.mqh>


#define NOISE_INDICATOR "Price_Density"

input string  Noise_Settings = "Noise Filter Settings"; 
input int     Noise_Filtering_Period   = 20.0;                                         
input double  Noise_Filtering_Level    = 5.0;   
                                     
int Noise_Handle=INVALID_HANDLE;

double            Noise_Value_Array[];  

int OnInit()
  {

   Noise_Handle=iCustom(_Symbol,_Period,NOISE_INDICATOR,Noise_Filtering_Period,Noise_Filtering_Level);     
      
      {
      Alert(StringFormat("Error %i while getting handle on %s",_LastError,NOISE_INDICATOR));
      return(INIT_FAILED);
      }
   
   ArraySetAsSeries(Noise_Value_Array,true);

   return(INIT_SUCCEEDED);
  }

void OnTick()
  {

      if(CopyBuffer(Noise_Handle,0,0,3,Noise_Value_Array)==-1)                              
      return; 

      Comment(
               "NOISE BUFFER 0 : ",Noise_Value_Array[0],"\n"
               "NOISE BUFFER 1 : ",Noise_Value_Array[1],"\n"
             );
   
  }
//+------------------------------------------------------------------+


// ----- 2022.05.22 19:00:27.492        Price_Density_Check (EPM22,M1)  Alert: Error 0 while getting handle on Price_Density   -------//

OnTick Code Below

// TEST EA FOR PRICE DENSITY using Only OnTick()//

#include<Trade\Trade.mqh>

#define NOISE_INDICATOR "Price_Density"

input int     Noise_Filtering_Period   = 20.0;                                         
input double  Noise_Filtering_Level    = 5.0;   

void OnTick()

 {
                                     
      int Noise_Handle=INVALID_HANDLE;

      double Noise_Value_Array[];  


      Noise_Handle=iCustom(_Symbol,_Period,NOISE_INDICATOR,Noise_Filtering_Period,Noise_Filtering_Level);     
      
      if(Noise_Handle==INVALID_HANDLE)
      {
      Alert(StringFormat("Error %i while getting handle on %s",_LastError,NOISE_INDICATOR));
      return;
      }
   
      ArraySetAsSeries(Noise_Value_Array,true);

      if(CopyBuffer(Noise_Handle,0,0,3,Noise_Value_Array)==-1)                              
      return; 

      Comment(
               "NOISE BUFFER 0 : ",Noise_Value_Array[0],"\n"
               "NOISE BUFFER 1 : ",Noise_Value_Array[1],"\n"
             );
   
  }


This is the Indicator Code. 

#property version   "1.01"
#property strict

#property indicator_separate_window
#property indicator_buffers 4
#property indicator_plots   1
#property indicator_type1   DRAW_LINE
#property indicator_color1  clrDarkOrange
#property indicator_level1 5              
#property indicator_levelwidth 1          
#property indicator_levelstyle STYLE_DOT  
   
//Input parameters
input int    InpPeriods                = 20;      //Indicator Periods
input double InpNoiseThresholdLevel    = 5.0;     //Visual Noise Threshold Level

double PriceDensityBuffer[];      
double SumOfIndHighLowsBuffer[]; 
double HighestHighBuffer[];
double LowestLowBuffer[];
   
int OnInit()
{
   SetIndexBuffer(0, PriceDensityBuffer, INDICATOR_DATA);              
   SetIndexBuffer(1, SumOfIndHighLowsBuffer, INDICATOR_CALCULATIONS);  
   SetIndexBuffer(2, HighestHighBuffer, INDICATOR_CALCULATIONS);       
   SetIndexBuffer(3, LowestLowBuffer, INDICATOR_CALCULATIONS);        
   
   IndicatorSetInteger(INDICATOR_DIGITS, 5);
   IndicatorSetString(INDICATOR_SHORTNAME, "Noise Price Density (" + IntegerToString(InpPeriods) + ")");
   PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, InpPeriods);
   IndicatorSetDouble(INDICATOR_LEVELVALUE, 0, InpNoiseThresholdLevel);
   IndicatorSetInteger(INDICATOR_LEVELCOLOR, 0, clrYellow);
   IndicatorSetString(INDICATOR_LEVELTEXT, 0, "HIGH VALUES = HIGH NOISE"); 

   return(INIT_SUCCEEDED);
}

int OnCalculate(const int currCountOfPriceBars,   
                const int prevCountOfPriceBars,  
                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[])
{
   if(currCountOfPriceBars <= InpPeriods)
      return(0);    
   
   int currentPosition = prevCountOfPriceBars - 1; 
   
   if(currentPosition < InpPeriods)
      currentPosition = InpPeriods;
   
   for(int i = currentPosition; i < currCountOfPriceBars && !IsStopped(); i++)
   {    
      if(currCountOfPriceBars != prevCountOfPriceBars) 
      {
         SumOfIndHighLowsBuffer[i] = 0;
         HighestHighBuffer[i] = DBL_MIN;
         LowestLowBuffer[i] = DBL_MAX;
         
         for(int iLoop = i - 1; iLoop > i - InpPeriods; iLoop--)      
         {
            SumOfIndHighLowsBuffer[i] += high[iLoop] - low[iLoop]; 
         
            if(high[iLoop] > HighestHighBuffer[i])
               HighestHighBuffer[i] = high[iLoop];
               
            if(low[iLoop] < LowestLowBuffer[i])
               LowestLowBuffer[i] = low[iLoop];
         }
      }
      
      double highestHigh = MathMax(HighestHighBuffer[i], high[i]);
      double lowestLow = MathMin(LowestLowBuffer[i], low[i]);
      
      if(highestHigh - lowestLow != 0) 
         PriceDensityBuffer[i] = (SumOfIndHighLowsBuffer[i] + (high[i] - low[i])) / (highestHigh - lowestLow);
      else                     
         PriceDensityBuffer[i] = 0.0;
   }

   return(currCountOfPriceBars);
}
 
  1. Adnan S: It loads in terminal just fine. When called via EA it fails. 

    Alert: Error 0 while getting handle on Price_Density

    It has not failed. Zero mean no error.

    int OnInit()
      {
    
       Noise_Handle=iCustom(_Symbol,_Period,NOISE_INDICATOR,Noise_Filtering_Period,Noise_Filtering_Level);     
          
          {
          Alert(StringFormat("Error %i while getting handle on %s",_LastError,NOISE_INDICATOR));
          return(INIT_FAILED);
          }
    

    You are issuing the message and killing the EA always.


  2. void OnTick()
     {
          int Noise_Handle=INVALID_HANDLE;
          Noise_Handle=iCustom(_Symbol,_Period,NOISE_INDICATOR,Noise_Filtering_Period,Noise_Filtering_Level); 

    Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              How to call indicators in MQL5 - MQL5 Articles (2010)

 
William Roeder #:
  1. It has not failed. Zero mean no error.

    You are issuing the message and killing the EA always.


  2. Perhaps you should read the manual, especially the examples.
       How To Ask Questions The Smart Way. (2004)
          How To Interpret Answers.
             RTFM and STFW: How To Tell You've Seriously Screwed Up.

    They all (including iCustom) return a handle (an int). You get that in OnInit. In OnTick/OnCalculate (after the indicator has updated its buffers), you use the handle, shift and count to get the data.
              Technical Indicators - Reference on algorithmic/automated trading language for MetaTrader 5
              Timeseries and Indicators Access / CopyBuffer - Reference on algorithmic/automated trading language for MetaTrader 5
              How to start with MQL5 - General - MQL5 programming forum - Page 3 #22 (2020)
              How to start with MQL5 - MetaTrader 5 - General - MQL5 programming forum - Page 7 #61 (2020)
              How to call indicators in MQL5 - MQL5 Articles (2010)

Thanks William I figured out what the issue was,

if(Noise_Handle==INVALID_HANDLE) is what I forgot to add above {Alert.........,

But I do sincerely appreciate the help.  

Reason: