MQL5 custom indicator turns to EA

 

Hi there,

I am very new to MQL5 and trying to create custom indicator


//+------------------------------------------------------------------+
//|                                                    Hull_info.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "Info"

ENUM_TIMEFRAMES timeFrame        = 0;        
int trendPeriod                  = 20;        
double speed                     = 2.0;         
int priceMode                    = 6;        
string indicator                 = "Hull average 2";
int handle;

double HullBull[]; 
double HullBear[]; 

int OnInit()
  {
   handle = iCustom(_Symbol, timeFrame, indicator, trendPeriod, speed, priceMode);
   if(handle==INVALID_HANDLE) { Print("invalid handle error ",GetLastError()); return INIT_FAILED; }
   return INIT_SUCCEEDED;
  }

void OnDeinit(const int reason)
{
   ChartSetString(0, CHART_COMMENT, "");
}


  
void OnTick()
  {
  
   HullProcessor();
}


void HullProcessor()
   {
      ArraySetAsSeries(HullBull,true);
      ArraySetAsSeries(HullBear,true);
      CopyBuffer(handle,0,0,3,HullBull);
      CopyBuffer(handle,1,0,3,HullBear);
      
      Print( "Bulls is ", HullBull[0],       
               "\n", "Bears is ", HullBear[0]  
               
             );
      
   }


With above code - it become EA (not indicator) when attached to chart - but works perfectly which print buffer values in MT5 Expert Tab

If changed with bellow code - it become indicator - but not working - nothing print


//+------------------------------------------------------------------+
//|                                                    Hull_info.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"

#property indicator_chart_window
#property indicator_buffers 1
#property indicator_plots   1
#property indicator_label1  "Hull"

ENUM_TIMEFRAMES timeFrame        = 0;        
int trendPeriod                  = 20;        
double speed                     = 2.0;         
int priceMode                    = 6;        
string indicator                 = "Hull average 2";
int handle;

double HullBull[]; 
double HullBear[]; 

int OnInit()
  {
   handle = iCustom(_Symbol, timeFrame, indicator, trendPeriod, speed, priceMode);
   if(handle==INVALID_HANDLE) { Print("invalid handle error ",GetLastError()); return INIT_FAILED; }
   return INIT_SUCCEEDED;
  }

void OnDeinit(const int reason)
{
   ChartSetString(0, CHART_COMMENT, "");
}

int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
  {
//---
   
//--- return value of prev_calculated for next call
   return(rates_total);
  }
  
void OnTick()
  {
  
   HullProcessor();
}


void HullProcessor()
   {
      ArraySetAsSeries(HullBull,true);
      ArraySetAsSeries(HullBear,true);
      CopyBuffer(handle,0,0,3,HullBull);
      CopyBuffer(handle,1,0,3,HullBear);
      
      Print( "Bulls is ", HullBull[0],       
               "\n", "Bears is ", HullBear[0]  
               
             );
      
   }


How to make it an indicator which print buffer values in MT5 Expert Tab?

Please help - thank you

 
Your topic has been moved to the section: Technical Indicators
Please consider which section is most appropriate — https://www.mql5.com/en/forum/172166/page6#comment_49114893
 
Anton Wibowo: With above code - it become EA (not indicator) when attached to chart - but works perfectly which print buffer values in MT5 Expert Tab

The OnTick() event handler is reserved for Expert Advisors (EAs). You should not be using it for Indicators.

For indicators, you should be using OnCalculate(), not OnTick(). If you are missing the OnCalculate() event handler, then it will not consider it an indicator.

Don't use OnTick() at all for Indicators, and don't use OnCalculate() at all for Expert Advisors (EAs). Don't mix them up.

Documentation on MQL5: Event Handling / OnCalculate
Documentation on MQL5: Event Handling / OnCalculate
  • www.mql5.com
The function is called in the indicators when the Calculate event occurs for processing price data changes. There are two function types. Only one...
 

Thanks a lot   @Fernando Carreiro for the guidance


Now it works perfectly