Init() and DeInit() execution sequence

 

Hi all !

I am facing the following problem

I have an indicator or Expert Advisor.

There are two functions in it - Init() and DeInit()

What is the sequence of execution of these functions when I change TF (timeframe)?

I understand that when I start or attach the indicator, the Init() should work.

When deleting it from the chart it should runDeInit()

When changing TF, at firstDeInit() from the current TF should work and thenInit() of the new TF should work

However, it is not always executed in this sequence, which spoils the logic of writing the program.

I attached a sample of indicator and logs.

Please advise who solved this problem or just forget about it!

Files:
ERROR.mq5  2 kb
Log.txt  1 kb
 

What kind of logic is spoiled?

When you change timeframe, a new copy of the indicator is created that knows nothing about the previous copy. For a certain period of time (very short) both copies of the indicator exist in parallel. Then the previous copy is unloaded.

Read documentation https://www.mql5.com/ru/docs/runtime/running

Документация по MQL5: Программы MQL5 / Выполнение программ
Документация по MQL5: Программы MQL5 / Выполнение программ
  • www.mql5.com
Программы MQL5 / Выполнение программ - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
When the timeframe is changed, OnInit is called every time and creates some inconvenience.
 
Slawa:

What kind of logic is spoiled?

When you change timeframe, a new copy of the indicator is created that knows nothing about the previous copy. For a certain period of time (very short) both copies of the indicator exist in parallel. Then the previous copy is excluded.

Read documentation https://www.mql5.com/ru/docs/runtime/running


Thank you for your feedback

About "logic spoiling"

I wrote an indicator that replaces the main chart (candlesticks) with its own type of drawing DRAW_CANDLES

The point is to remove the main price chart and display only my chart.

- When Inite set the colour of the main chart to transparent.

I draw my own chart (according to my parameters).


Since I want to restore the colour of the main chart after removal of my indicator

- In DeInit I restore the colour of the main chart


When changing the TF I mean first DeInit (restore the colour), and then Init (go back to being transparent)


The execution of commands is not sequential; periodically, when changing the TF

periodically the main chart (with restored colour) is overlaid on my indicator.

Here's one "logic screw up" for example.


PS: (((Read documentationhttps://www.mql5.com/ru/docs/runtime/running )))

If you change symbol or timeframe of the chart, to which the Expert Advisor is attached, the Expert Advisor is not unloaded and loaded. In this case , handlersOnDeinit() on the old symbol/timeline andOnInit() on the new symbol/timeline (if available) areconsistently called, values of global variables andstatic variables are not reset. All events received for an EA before completion of initialization (OnInit()) are skipped.


Документация по MQL5: Программы MQL5 / Выполнение программ
Документация по MQL5: Программы MQL5 / Выполнение программ
  • www.mql5.com
Программы MQL5 / Выполнение программ - справочник по языку алгоритмического/автоматического трейдинга для MetaTrader 5
 
nmaratr:


Regarding.

"a new copy of the indicator is createdthat knows nothing about the previous copy"

Why then DeInit deletes objects that were created in the new Init copy. If it is just a copy.



 
Petros Shatakhtsyan:
When timeframe is changed, OnInit is called each time and causes some inconvenience.


What does it mean to call OnInit every time

What is OnInit for - to initialise all variables and parameters once

For example

I want to write an indicator that creates a text marker on a chart with current timeframe.

When deleting the indicator it removes this text mark from the chart.

And when changing the TF it would update its content to a new one (by removing the old one and creating a new one).

What we get

A couple of times we changed the TF and the text mark has disappeared. (So at first OnInit and then DeInit has acted, which removed this label.)

Then few more times changed TF and it appeared.

This is not right.

 
nmaratr:

if they are graphical objects, they belong to the chat room and can be accessed by any software running on that chat room
 
Alexander Bereznyak:

if they are graphical objects, they belong to the chat and accessible by any program running on this chat.


So how, can this problem be solved ??? (Has anyone got it ???)

Or accept as is. That in order to speed up calculations, it is possible to execute commands in a non-sequential manner.

What is contrary to documentation


When changing a symbol or a timeframe of a chart, to which the Expert Advisor is attached, the Expert Advisor will not be unloaded and loaded. In this case

sequentially

HandlersOnDeinit() on the old symbol/timeline andOnInit() on the new symbol/timeline (if available) are called

 
nmaratr:


So, can this problem be solved ??? (Has it worked for anyone ???)



I join the author of this thread. The question really isn't idle. And very specific.
I discovered this problem a long time ago. I contacted servicedesk back in June 2016 with an example absolutely similar to the author of this thread. They ignored it - it's still an open topic:

Application to servicedesk

Of course I have made a parameter transfer in indicator when changing TF, having spent a lot of time on it. But I do not want to make a simple task with diamonds.
But developers, please implement the possibility to create special global variables in indicators that won't reinitialize during timeframe changes. In the Expert Advisors the re-initialization does not happen at the time of TF change, in the indicator it does. If there were a possibility not to reinitialize some variables and arrays in indicators, the synchronization of OnInit and OnDeinit would not be an issue, and new interesting opportunities for programmers would appear. I don't think that this possibility of variable uninitialization will affect the safety of programs.

What's it for?
There are a lot of situations.

For example:
-When I start the indicator, I perform calculations of variables, index arrays, data arrays, that are independent of TF, and they can be quite long and voluminous. The question is why I have to do it each time I change the TF.
- I cannot change the indicator parameters through the parameters window, but for convenience and clarity right from the chart with a mouse (you can see and use this indicator, which achieves it in a most complicated way). And the settings are not lost every time you change the TF.

 

This issue is solved like two fingers... You know what...

In OnDeinit it is necessary to condition the reason for deinitialisation before deleting the object... If it's not a period change, then the object is deleted. AND THAT'S IT...

 
Alexey Viktorov:

This issue is solved like two fingers... You know what...

In OnDeinit it is necessary to condition the reason for deinitialisation before deleting the object... If it is NOT a period change, then the object is deleted. AND THAT'S IT...

So that's it!?
I've been experimenting and using this reason code (REASON_CHARTCHANGE) to the fullest extent. What's the use if all variables are set to their original state again and OnDeinit can be executed after OnInit of the new TF
Reason: