Discussing the article: "From Basic to Intermediate: Objects and Subwindows (II)"

Check out the new article: From Basic to Intermediate: Objects and Subwindows (II).

This article explains how to track and handle the removal of objects from the chart in MQL5 using MetaTrader 5 events. When the code detects that an object created by an indicator has been deleted, it deletes the corresponding instance to avoid inconsistencies and prevent subwindows from being left behind. This approach uses unique names and a header file, prioritizing simplicity and predictable behavior.

If you’ve practiced and studied each of these articles, then you’ve probably already realized that the purpose of the MQL5 language is precisely to allow programmers to control, to some extent, the behavior and operation of MetaTrader 5 so that the platform produces predictable results. This is the easy part, although few people actually realize it.

The tricky part is understanding that you won't always be able to do something without first getting a handle on some of the mechanisms and concepts used by MetaTrader 5. One of these concepts has already been explained: we CANNOT add two instances of the same indicator to a single chart. This is a fact, and it is guaranteed by MetaTrader 5's internal mechanisms. However, if we change any argument or input parameter of the indicator, we can add a second instance to the same chart, since in that case MetaTrader 5 will assume that this second—that is, new—instance is not yet present on the chart.

Okay, but why, on what occasion, or under what circumstances would you bring this up again? So, dear reader, the fact is that the code we've looked at so far has some issues that could significantly complicate matters. And these problems arise precisely when we try to improve the code itself. That’s why I want you to clearly understand the following: although the code we’ve looked at so far works—which you can verify in each article using animations that accurately show how the code works—changing it can lead to some rather confusing results. This is precisely because each of these code examples is missing certain details.

To make this clearer and easier to understand, let's use one of the code examples included in the attachment to the previous article. If we use one of these code examples, we'll get the result shown in the following animation.


Author: CODE X