Attachments forums

List of attachments posted on this forum.


All files on forums: 136855

Re: Indicators with ON/OFF buttons

pips4life, Sun May 29, 2022 7:14 am

I like the feature of having an on/off button on various indicators. When I took a look at some of the examples, I was shocked at some of the problems!

I hate to step on anyone's toes here, BUT, the fact is that the rough template used to implement many of these on/off buttons is absolutely terrible coding! At best, it is slow, does not entirely work as intended, and has some glitches even when "working". At worst, when used in combination with some other indicators these may HANG MT4, forcing the user to kill it to recover.

I made a post of this same issue at www.mql5.com/en/forum/426017, and perhaps someone will offer some useful feedback, but I'll post a few attachments here that illustrate the wrong way, a better way, and a simple demonstration of how the wrong way, in combination with another indicator, can hang MT4!

Elsewhere in this thread is the original code, but I've added some comments for why this code is so flawed:
Here is an improved "v3" version of the same, still with a lot of comments, including the old (bad) commented code, just for reference. Unlike my "v2", I went ahead and renamed "start()" to "mystart()". The normal "start()" just calls "mystart()", and now the OnChartEvent() also calls "mystart()". Maybe it doesn't matter, but by using a different name, it might lesson the chance of a collision when both incoming ticks AND the OnChartEvent each try to directly run "start()". Most likely it was ok before, and the events and ticks just queued up to run sequentially, but who knows for sure.
If you add the following indicator to a chart, the indicator does nothing more than turn on various events if objects are deleted or created. When your chart has any indicator with these chart events enabled, when you then add the bad original version, just try to change TF's and/or toggle the on/off button then change TF's again. It won't be long until it hangs your MT4 session! (Or, it may take MANY minutes of thrashing until it again is controllable).
I offer my improved v3 code as a demonstration of a better way, but also invite anyone to comment on ways my code could be further improved.

P.S. I don't offer coding services, and I've no interest to port these changes to other indicators, but by comparing the differences of these two, someone with any basic understanding of programming should be able to make similar changes to other indicators.
All files in topic