IndicatorRelease WHEN needed?

 
Hi

I wonder when i must release Indicator and when not?

i have a custom RSI so sometimes the EA load "myRSI"
and sometime he removing the indicator.

No i am worrying if i do this maybe 200 times that there 
will be buffers every time and i may run in memory troubles.

OR is MetaTrader clever and reuses the indicator buffers 
when i reload "myRSI" on a later time??

Seams to be so technically that i did not found any infos 
out there... ..  .

HOW KNOWS ABOUT?
 
Get the handle in OnInit. Use it in OnCalculate. Release it in OnDeinit.
 

When using the same indicator parameters, the previous handle is used. 

Not sure if this is best practice. The documentation recommends @whroeder1's approach.

I too face the same situation where my EA trades 28 pairs, but only 2 at the same time. In my mind it is overkill to use indicator for 28 pairs all the time, when only 2 are actively used at a time.

 

Форум по трейдингу, автоматическим торговым системам и тестированию торговых стратегий

Последовательность выполнение Init() и DeInit()

fxsaber, 2017.04.17 09:03

int GetShortNames( string &ShortNames[], const long Chart_ID = 0, const int SubWindow = 0 )
{    
  const int Total = ChartIndicatorsTotal(Chart_ID, SubWindow);

  ArrayResize(ShortNames, Total);
  
  for (int i = 0; i < Total; i++)
    ShortNames[i] = ChartIndicatorName(Chart_ID, SubWindow, i);
    
  return(Total);

}

// Возвращает свое "Короткое имя" - ShortName
string GetMyShortName( void )
{  
  string Res = "";
  
  const int SubWindow = ChartWindowFind();
  
  string ShortNames[];

  GetShortNames(ShortNames, 0, SubWindow);
  
  const string TmpShortName = __FUNCSIG__ + (string)MathRand();

  IndicatorSetString(INDICATOR_SHORTNAME, TmpShortName);    

  string NewShortNames[];

  const int Total = GetShortNames(NewShortNames, 0, SubWindow);
  
  for (int i = 0; i < Total; i++)
    if (NewShortNames[i] == TmpShortName)
    {
      Res = ShortNames[i];
      
      IndicatorSetString(INDICATOR_SHORTNAME, Res);
      
      break;
    }
  
  return(Res);
}

// Возвращает свой хэндл
int GetMyHandle( void )
{
  const string ShortName = GetMyShortName();
  
  const string TmpShortName = __FUNCSIG__ + (string)MathRand();  
  
  IndicatorSetString(INDICATOR_SHORTNAME, TmpShortName);

  const int Res = ChartIndicatorGet(0, ChartWindowFind(), TmpShortName);
  
  IndicatorSetString(INDICATOR_SHORTNAME, ShortName);  

  return(Res);
}

It is very important to make the IndicatorRelease of your handle before OnDeinit.


Example

int GetMyParameters( MqlParam &Params[] )
{
  const int handle = GetMyHandle();
  ENUM_INDICATOR Type;

  const int Res = ::IndicatorParameters(handle, Type, Params);
  IndicatorRelease(handle);

  return(Res);
}
 
Enrique Dangeroux:

When using the same indicator parameters, the previous handle is used. 

Not sure if this is best practice. The documentation recommends @whroeder1's approach.

Can you provide the link to this documentation ?

I too face the same situation where my EA trades 28 pairs, but only 2 at the same time. In my mind it is overkill to use indicator for 28 pairs all the time, when only 2 are actively used at a time.

How is this related to IndicatorRelease() usage ? Could you be more precise please.

The topic is interesting but covers a lot of different things.

 
Alain ";-)"
 
Baumann Andrea:
Alain ";-)"
?
 
Alain Verleyen:
Can you provide the link to this documentation ?

Note. Repeated call of the indicator function with the same parameters within one mql5-program does not lead to a multiple increase of the reference counter; the counter will be increased only once by 1. However, it's recommended to get the indicators handles in function OnInit() or in the class constructor, and further use these handles in other functions.

https://www.mql5.com/en/docs/indicators

Alain Verleyen:

How is this related to IndicatorRelease() usage ? Could you be more precise please.

The topic is interesting but covers a lot of different things.

I use the eventsspy indicator, which is does not really indicate anything but allows to pass events, i am interested in tick's only. So my first method was to create the indicator handle only when entering a trade, because no trade, no ticks needed. Similar to Andreas approach.

Now considering my EA loads 28 charts without doing anything it is a relative memory hog per definition. IndicatorRelease() does not work in tester, so it can not be tested. The continues creation and releasing was giving me the same worries as Andreas. 

Files:
eventsspy.mq5  8 kb
 
Alain Verleyen:
?

Nice to read u again.


Enrique Dangeroux 2018.04.03 14:36      DE

When using the same indicator parameters, the previous handle is used. 


Are you shure - where to read this??

 
Baumann Andrea:

Nice to read u again.


Enrique Dangeroux 2018.04.03 14:36      DE

When using the same indicator parameters, the previous handle is used. 


Are you shure - where to read this??

See link and or quote from my previous posts.

Now, when the documentation states something, it does not mean it also works like described. We are dealing with MQ here.

 

Yes it said to get the handle OnInit(), and to use it in OnTick(), OnTimer(), etc...

It doesn't say to use IndicatorRelease(), and certainly not in OnDeinit() which is useless.

so :

whroeder1:
Get the handle in OnInit. Use it in OnCalculate. Release it in OnDeinit.

In general, no need to use this function in IndicatorRelease().

The function has to be use when an handle is not needed anymore, to realease resource. Not needed in OnDeinit() as either it's a "close" of the program, so it's done automatically, or it's a unload/load sequence and you don't need to release the handle at all.

Of course they are exception as always (as in your case about eventsspy).