In this post, I will introduce an MQL4 code I use to control remotely the auto trading button on/off status, this code is part of the RemoteControl library product available for purchase here.
There are some scenarios where we would like to stop trading completely such as:
- Extreme market conditions or
- Below a certain equity threshold.
- Negative P&L below a certain threshold.
But when we are not near the PC, this could be a challenging task to do. We can either close all of our positions and take some loss, or, let the positions run and hope for the best…
In this post, I will introduce a code snippet which you can plant in your code, which upon certain conditions can mimic halt trading by toggling the auto trading button on / off:
So, upon executing this code, the button will be toggled between on and off, and you will get notified about the new state.
First of all, we need to modify the WinUser32.mqh which is shipped as part of the MetaEditor installation, by adding the following two lines:
- int GetAncestor(int hWnd,int uCmd); – A function to get the parent of the current window, will be used later on to get the handler of the chart parent, the MetaTrader workstation.
- #define WM_AUTOTRADING 0x80FC – A constant holding the toggle auto trading
button action. Will be used later on as part of the call for an action message.
The code is as follows:
SendNotification(StringConcatenate("Toggling automated trading button succeeded, ",OriginalTradeAllowedFlag==false?"trading enabled":"trading disabled"));
SendNotification ("Toggling automated trading button failed messaging the button");
SendNotification ("Toggling automated trading button failed getting the ancestor window");
First, I reset the last error to make sure, that if something goes wrong, we have the latest error code.
Then, using the call to GetAncestor, I getting a window handler to the parent window of the current chart. In our case, this is the MetaTrader application itself.
If the call was successful, and hWnd isn’t -1, rather a handler to the root window. We post a message to that window using the function PostMessageA, which mimic the behavior of clicking the automated trading button. The WM_ AUTOTRADING constant holds the value of the button clicking event.
If successful, the function will return a non-zero value and the button will be clicked to look as:
At this point, no trading is possible and the following functions will fail – OrderSend(), OrderClose(), OrderCloseBy(), OrderModify(), OrderDelete(). We can always call this function again once the risk condition was finished and we are interested to start trading again.
I am using this logic across my expert advisers in the following manners:
if((IsTradeAllowed()==true && AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)>0 && AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)<200) || (IsTradeAllowed()==false && AccountInfoDouble(ACCOUNT_MARGIN_LEVEL)>400))
“TOGGLE AUTOMATED TRADING BUTTON”
Meaning, I set certain threshold levels, in this case 200 and 400. If the account margin level in % goes below 200%, I will disable the trading by firing this event. Then, if the account margin level goes above 400%, I will switch trading back again. I am using the IsTradeAllowed function to verify the current state before firing this event. Normally, I put this logic in the OnTimer() function which I call on an hourly basis, but you may plant anywhere you’d like.
Using my remote-control framework to fire a message based on a OP_BUYSTOP message with the value of 1011 as explained in this post.