Handle release

 

Hello!

I have this function in my include file :

double ATR(int index)
  {

   double atr[];

   int handle = 0;

   handle = iATR(_Symbol,PERIOD_CURRENT,14);
   CopyBuffer(handle,0,0,10,atr);
   ArraySetAsSeries(atr,true);


   return NormalizeToTickSize(atr[index]);
  }

So at any time in my code I need ATR, I call this function with index that I require.

My question is : Since am not releasing this handle on OnDeinit() ,What can happen ? Thank you!

 
Daniel Cioca:

Hello!

I have this function in my include file :

So at any time in my code I need ATR, I call this function with index that I require.

My question is : Since am not releasing this handle on OnDeinit() ,What can happen ? Thank you!

Nothing will happen. In Oninit the calculation of handle is loaded, so it should be unloaded, but no problem if you dont unload it, however its considered good practice to unload/release handle because its not in used. 

Lets suppose you turn off TV but you dont remove the power cord from the main supply? Nothing will happen. You may save some power if you remove power cord after turning off tv, same way you release some memory if you release handle.

 
double ATR(int index)
  {

   double atr[];
double res=0;

   int handle = 0;

   handle = iATR(_Symbol,PERIOD_CURRENT,14);
   CopyBuffer(handle,0,0,10,atr);
   ArraySetAsSeries(atr,true);
    res=atr[index];
IndicatorRelease(handle);


   return res;
  }

How about if I release it like this? 

 
Daniel Cioca #:

How about if I release it like this? 

A handle should be loaded only once in OnInit but only in some special cases its loaded on either calculate or ontimer

what i see is you are adding handle on a function, I cant see where its executing.

so i am assuming you execute it to OnCalculate function

so if your job is done then you can release the handle

otherwise you should be releasing it when OnDeinit() is called.


 if(handle!=INVALID_HANDLE){
     if(!IndicatorRelease(handle))
                  Print("IndicatorRelease() failed. Error ",GetLastError());
               else handle=INVALID_HANDLE;
}
 
Arpit T #:

A handle should be loaded only once in OnInit but only in some special cases its loaded on either calculate or ontimer

what i see is you are adding handle on a function, I cant see where its executing.

so i am assuming you execute it to OnCalculate function

so if your job is done then you can release the handle

otherwise you should be releasing it when OnDeinit() is called.


I am using it on OnTick()

 
Daniel Cioca #:

I am using it on OnTick()

You  can use handle anywhere as there is no restriction. You can use it on OnTick(), OnChartEvent() OnInit() etc

But first try to know what is the meaning of using handle so you will know when its required to load / release

Consider a generator you start it once, and it provides you power supply constantly. Would you like to restart it every second or every tick? Well it may be required if generator is faulty.

Here 

handle = iATR(_Symbol,PERIOD_CURRENT,14);

it returns  INVALID_HANDLE if its fail to load. So if your handle is successful in executing and not returning INVALID_HANDLE then it means your handle is loaded on memory. Now when your task is finished You can release it from memory.


So now you may decide when to load and when to release

Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
Documentation on MQL5: Constants, Enumerations and Structures / Named Constants / Other Constants
  • www.mql5.com
Other Constants - Named Constants - Constants, Enumerations and Structures - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Arpit T #:

You  can use handle anywhere as there is no restriction. You can use it on OnTick(), OnChartEvent() OnInit() etc

But first try to know what is the meaning of using handle so you will know when its required to load / release

Consider a generator you start it once, and it provides you power supply constantly. Would you like to restart it every second or every tick? Well it may be required if generator is faulty.

Here 

it returns  INVALID_HANDLE if its fail to load. So if your handle is successful in executing and not returning INVALID_HANDLE then it means your handle is loaded on memory. Now when your task is finished You can release it from memory.


So now you may decide when to load and when to release

Thank you! 
Reason: