I am a brand new member of this forum and wondered if I could get a little shove in the right direction with event handling such as OnTick and OnChartEvent etc and their precedence.
I have been playing with chart events to make a button that I can click on and get the program to deviate from its normal action by running some alternative code when I make a standalone button with no other code it works fine each time registering the mouse click on it. However, if I include the code in another expert advisor it seems as the OnTick takes precedence and the button activation never gets acknowledged.
My basic question is what are the rules for the Event Handling I have searched many topics but none seem to discuss how the terminal discriminates between OnTick and OnChartEvent.
I hope someone can point me towards some guidance on this topic.
Yes I have it actually says nothing about the precedence of event handling, if the chart event has taken place and a tick also takes place which one is actioned the tick takes the control to OnTick the chart event takes control to the OnChartEvent function. In my case even though a chart event has happened the program continuously runs the OnTick.
A client terminal sends new events to the corresponding open charts. Events can also be generated by charts (chart events) or mql5-programs (custom events). Generation of events of creation or deletion of graphical objects on a chart can be enabled or disabled by setting CHART_EVENT_OBJECT_CREATE and CHART_EVENT_OBJECT_DELETE chart properties. Each MQL5 program and each chart has its own queue of events, where all new incoming events are added.
A program receives only events
from the chart it runs on. All events are processed one after another in
the order they are received. If a queue already has a NewTick
event, or this event is currently being processed, then the new NewTick
event is not placed in the queue of the MQL5 program. Similarly, if ChartEvent
is already enqueued, or this event is being processed, no new event of
this kind is enqueued. The timer events are handled the same way — if
the Timer event is in the queue or being handled, the new timer event is not enqueued.
Event queues have a limited but
sufficient size, so that the queue overflow for well written programs is
unlikely. In case of queue overflow, new events are discarded without
It is not recommended to use
infinite loops to handle events. The exception to this rule may be only
scripts that process only a single Start event.
Thanks again Alain
I have read this, However, it is not clear about which queued event is handled on completion of the current pass, the tick or the chart event or timer etc. Also, the text says that events are not enqueued if a current event is being processed. I realize that it must be my interpretation of this text as this is counter intuitive, what is the point of a queue if nothing is placed in it when the program is in mid-event processing.
I find this a lot with the MQL notes they are very difficult to interpretate.
Kind Regards and thank you for your time with this issue of my understanding, I would appreciate if you could summarise in your words how the new events are handled from different sources whils a current event is being processed.
Events queue is used to register the different events. But OnTick, OnChartEvent and OnTimer can only by queued once (or not if the same kind of event is currently processed).
The events are processed in the order they are queued.
So if an OnTick event is processed and you made some click the chart event is queued. It will executed once OnTick() (and eventual previously queued events) is done.
If you have click events not processed you are maybe falling in the case :
Event queues have a limited but sufficient size, so
that the queue overflow for well written programs is unlikely. In case
of queue overflow, new events are discarded without queuing.
Difficult to say more without any code available.
Many Many thanks Alain your explanation is clear and now that I understand this I can monitor my program flow to check this and establish where my error is.
I agree with William...
I have used sample code of Metatrader... I made lot of testing with mouse click events (clicking button or viewlist) and I found that :
1) time of processing ChartEvent is very long... sometimes few seconds
2) sometimes events are not processed at all.
Or the components and event handling is not working fine... or I have issue in the code.
If you have a simple application or code showing ChartEvents with buttons working fine.. I would be interested to test.
For now I am not satisfied the way events are processed.
... or I have issue in the code.
In comparison to C++ the OnChartEvent() is comparable to the action signal that executes clicked() function, however there are also pressed() and release() functions, and a toggled().
The close resemblance to these other functions is:
Button state (pressed / depressed)
This function can be used inside the OnTick() flow to capture the press of a button to circumvent the OnChartEvent() function so this can never result in delayed execution unless there is a long interval between incoming ticks, nor can the button change state be ignored because it's a part of the primary or main runtime.
One additional feature is that this method also works in the tester.