Indicator

 

I'm returning INIT_FAILED in a indicator OnInit function, but it's created anyway.

If a EA tries to load it it get a valid handle and can't identify that's not working properly.

I also tried to force an error, but it get loaded anyway.

// Option 1
int OnInit()
  {
    return INIT_FAILED;
  }

// Option 2
int OnInit()
  {
    int a=0;
    a=1/a; // Force a failure
    return INIT_FAILED;
  }
// EA
ResetLastError();
int indicator=IndicatorCreate(symbol,timeframe,IND_CUSTOM,ArraySize(params),params);
Print(indicator," | ",GetLastError()); // Result: 10 | 0

Anything wrong with my approach?

 
Henrique Vilela:

I'm returning INIT_FAILED in a indicator OnInit function, but it's created anyway.

If a EA tries to load it it get a valid handle and can't identify that's not working properly.

I also tried to force an error, but it get loaded anyway.

Anything wrong with my approach?


use:

#property strict

than after INIT_FAILED script/indicator/EA will be stopped and removed from chart.

 
Janusz Trojca:

use:

than after INIT_FAILED script/indicator/EA will be stopped and removed from chart.

The topic is about MT5.
 
Henrique Vilela:

I'm returning INIT_FAILED in a indicator OnInit function, but it's created anyway.

If a EA tries to load it it get a valid handle and can't identify that's not working properly.

I also tried to force an error, but it get loaded anyway.

Anything wrong with my approach?

In my opinion it's a bug (at least when used with iCustom) and I reported it to ServiceDesk more than 3 years ago. I never got an answer so I closed the ticket.

Try to add code to remove the indicator from the chart in OnDeinit() :

//+------------------------------------------------------------------+
//| Custom indicator deinitialization function                       |
//+------------------------------------------------------------------+  
void OnDeinit(const int reason)
  {
   if(reason==REASON_INITFAILED)
     {
      int window=ChartWindowFind(ChartID(),"shortname");
      ChartIndicatorDelete(ChartID(),window,"shortname");
     }
  }
Reason: