<Rant>. MQL4 includes no tools to directly detect trading events. Event-driven programming has been around for over twenty years and, like in-line debugging tools, MQL4 is sadly lacking. I’m happy to say that MetaQuotes has gotten their act together and included all this stuff in MQL5. But that doesn’t help much right now, since many brokers that have provided MT4 for some time, have not yet adopted MT5 (which has been around for a couple of years now.) </Rant>
Event detection is the act of noticing programmatically the opening and closing of trades or orders and the movement of stop loss and take profit prices. Any events that occur either by direct manipulation of the order from the MT4 platform or the action of the broker on the account, for example, the closing of an order due to the hitting a stop loss price, are detectable. Events that you must detect may also include movement of lines and graphics on the MT4 platform. We’ll cover the trading events first.
Since there are no tools to directly detect trading events, we have to detect them by inference. We have to keep track of open trades so we can notice when they are closed. This is tricky, since we also have to make sure that open/closed trade information survives chart time-frame changes, which re-initializes the EA, and other crashes and shut downs that could occur. To do so will require that we either write the data in the form of a Global Variable (GV) or a file.
We just spent a few installments describing the saving of data in a file. We’ve talked about using GVs as well. So I won’t cover those issues here. The decision to use one or the other becomes a decision of convenience and aesthetics. For one or two variables, GVs are much more convenient. If you are not going to run your EA on very many pairs, the proliferation of GVs won’t be a problem either. On the other hand, if you must store many variables and/or will be running your EA on many pairs, the list of GVs may get very clogged. Plus, if you are going to market your EA, having a lot of GVs is not very appealing (did I mention that programming was an art?). The end users can tinker with them and possibly cause problems with the EA operation. If using GVs, be sure to completely identify your EA and your pair in your GV name.
For the purposes of event detection, at a minimum you will need to store the fact that there is an active trade. It can be stored in the form of a boolean. If you are checking for a change on every market tick (which I recommend), that may be enough. If it is only necessary to check for a change at the close of a bar, you may also want to store the entry time in a datetime variable. That way you will be able to distinguish the trade you are tracking from a new trade in the situation that the old trade was closed and a new one opened during the course of the bar.
Next installment we’ll look at the code to use for watching for events. Thanks for your attention and please follow me on Twitter and LinkedIn.
Winner’s Edge Trading, as seen on: