OnDeinit() function, does execute on close MT5?

 

Hi guys,

I hace several questions about OnDeinit function

When close MT5 does this function works?

I mean, I use 100 charts, each one with the same indicator. If I create a function that deletes all indicators of all those 100 charts into OnInit, It will work?


My problem is that I use the same 3 indicators on 100 charts, but when I  close MT5 and openit again, it loads the same indicators each time I close it.

If I close it one time I have 2 duplicated MACD, 2 times I ll have 3 MACD, and soo


Thats beacuse I use OnInit handles... and loads every time I open it.


How would be the code for solve this?


Thanks.


   long total=ChartGetInteger(ChartID(),CHART_WINDOWS_TOTAL);
   for(int h=0;h<total;h++)
      for(int i=0;i<ChartIndicatorsTotal(0,h);i++)
        {
         if(ChartIndicatorGet(0,h,ChartIndicatorName(0,h,i))==handle_MACD2)
           {
            if(!ChartIndicatorDelete(0,h,ChartIndicatorName(0,h,i)))
              {
               Print("error indicator delete, rc=",GetLastError());
              }
            else
              {
               IndicatorRelease(handle_MACD2);
               handle_MACD2=INVALID_HANDLE;
               //+------------------------------------------------------------------+
               //|Indicador MTF                                         |
               //+------------------------------------------------------------------+
               handle_MACD2=iCustom(m_symbol.Name(),Period(),"Examples\\MACD2");
               //--- if the handle is not created
                if(handle_MACD2==INVALID_HANDLE)
               {
               //--- tell about the failure and output the error code
               PrintFormat("Failed to create handle of the MACD2 indicator for the symbol %s/%s, error code %d",
                  m_symbol.Name(),
                  EnumToString(Period()),
                  GetLastError());
               //--- the indicator is stopped early

               }
               ChartIndicatorAdd(0,0,handle_MACD2);
              }
           }
        }

I ve tryed this into OnInit and no success.


also tryed the same function into OnDeinit and the same...

 
Valerio Fuiorea:
...
         if(ChartIndicatorGet(0,h,ChartIndicatorName(0,h,i))==handle_MACD2)

This will never work believe me. You can never assume the handle will be the same for an indicator with the same parameters (it was explained to me by a Metaquotes developer).

The only working approach to what you are trying is to compare the shortname, then you will probably face other issues.

If your purpose is only to deal with a MACD indicator I am strongly suggesting you to find an other simple approach, depends of what you trying to do exactly.

 
Alain Verleyen:

This will never work believe me. You can never assume the handle will be the same for an indicator with the same parameters (it was explained to me by a Metaquotes developer).

The only working approach to what you are trying is to compare the shortname, then you will probably face other issues.

If your purpose is only to deal with a MACD indicator I am strongly suggesting you to find an other simple approach, depends of what you trying to do exactly.

Hi, first thanks for the answer.

I'll try to write a code that deletes all indicators if Autotrading is desactivated, so every time before closing MT5 will switch it. So when open it again it will load indicators only one time. Hope will work.

 
That's because I use OnInit handles... and loads every time I open it.

Let me sum up what I believe you have said.

  • You have 100 charts open under normal MT5 use.
  • 3-indicators are created on each of these 100 charts using the OnInit() event of some EA.
  • Therefore, when you close MT5, then re-open MT5, the OnInit() event adds the 3-indicators again; but this creates duplicates since each chart already has these 3 indicators from the last time.


I have two thoughts:

1. Create a template with each of the 3-indicators already on the template. Then don't have your EA OnInit() event create the indicators.

2. Have your OnInit() event code detect if the indicator is already present by its "shortname." Then your EA can capture the handle of the existing Indicator and not re-create it.

 
For future viewers, if you do create a Indicator Handle and them plot it in the Chart, you gotta delete them at OnDeinit() and also Release. You also need to Release any handles that you created, or else it will leak for a indefinite time (by my tests, the platform does release handles after some time of not being used, but it is not guaranteed).


// OnInit
        iVolHandle = iVolumes(Symbol(), Period(), ENUM_APPLIED_VOLUMEInp); // Get the Handle
        if(iVolHandle == INVALID_HANDLE || iVolHandle < 0) return INIT_FAILED; // Check if the Handle is valid
        // Start plotting the Handle in the Chart
        // Receive the number of a new subwindow, to which we will try to add the indicator - this is the only safe way to guaranteed Remove of a Added Indicator, because you can't Remove by the Handle (only Release).
        subwindow = (int) ChartGetInteger(ChartID(), CHART_WINDOWS_TOTAL); // Get a new subwindow
        if(!ChartIndicatorAdd(ChartID(), subwindow, iVolHandle)) return INIT_FAILED; // Check if failed to Add
        iVolName = ChartIndicatorName(ChartID(), subwindow, 0); // Save the name so we can delete at OnDeinit()

// OnDeinit
        ChartIndicatorDelete(ChartID(), subwindow, iVolName); // Delete (from the chart)
        IndicatorRelease(iVolHandle); // Release (the handler)
Reason: