Help - Custom MQL Code
-
Does anyone know any MQL code to be included in my project through (Custom MQL Code)?
Any of the 3 functions will serve:
- Turn off Automated Trading.
- Close all graphics windows.
- Or even close the MT4 platform completely.
I have an EA that works on several different pairs at the same time, and if the DD reaches a certain amount of losses (Equity), this protector of mine will close all trades and prevent new trades from being opened.
My Project: I actually copied it from another project here on the forum and I'm modifying it...
-
-
These are standard MQL code, search google and you will find, turning off AUTO trading is more difficult.
-
//--- importing required dll files #include <WinUser32.mqh> #import "user32.dll" int GetAncestor(int, int); #define MT4_WMCMD_EXPERTS 33020 #import //+------------------------------------------------------------------+ //| | //+------------------------------------------------------------------+ void SetAlgoTradingTo(bool trueFalse) { //--- getting the current status bool currentStatus = IsTradeAllowed(); //--- if the current status is equal to input trueFalse then, no need to toggle auto-trading if(currentStatus != trueFalse) { //--- Toggle Auto-Trading int main = GetAncestor(WindowHandle(Symbol(), Period()), 2/*GA_ROOT*/); PostMessageA(main, WM_COMMAND, MT4_WMCMD_EXPERTS, 0 );//Toggle Expert Advisor button } } -
@jonatassantanacaje I'm afraid that goes beyond my capabilities, sorry.
-
Toggling AutoTrading can't be done with a basic mql code block, it requires to be done with a dll. As you can see at the top of the code, it's including files. FXDreema doesn't do this unless you make a custom block so you can include in the global variables. Here is how you would set up a block to use to toggle the AutoTrading button off. You can click it whenever you want to resume trading. Keep in mind this would disable ALL EAs. I tested this with a basic project and it does work but it has to be done correctly.

In the project, I just had it enter a large lot with a very very low equity drawdown since this can't be done in the backtester. Then, used the block after closing trades and the AutoTrading was turned off.
https://fxdreema.com/shared/klNJARKgbHowever, I feel like if you're just trying to prevent new trades after a certain amount of loss, there are other easier ways to do it. Just terminate the EA with the terminate block or something, why do you need to toggle the AutoTrading button? It achieves the same thing pretty much. Hope this helps.
-
@willramsey Hello, thank you for your attention, I need to turn off ALGOTRADING, because I have about 5 robots open at the same time on different charts, and all of them have to stop trading at the same time.
-
@jonatassantanacaje I see, if that's the case you would need to create the custom block. Then wherever you pass and use the block, it will disable autotrading. If you don't know code then it'll probably be quite confusing.
You'll also have to create some code to close all pairs open in the account. I think the close trades block will only close current EA and manual trades. This would work since it loops all orders on any pair. After they close then it gets disabled.

if (OrdersTotal() > 0){
for (int i = OrdersTotal() - 1; i >= 0; i--) {
if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
OrderClose(OrderTicket(), OrderLots(), MarketInfo(OrderSymbol(), MODE_BID), 5 /slippage/, clrRed);
}
}
} -
@willramsey I don't need all the functions working, I just gave examples, whichever is easier to do will be ideal for me.
Turn off automation or close all windows
-
Thank you very much @willramsey
Everything is working properly
-
You get code to do all that, but can take a lot of work, why don't you use a variable to start/stop trading, use a terminal (global) variable to stop and close trades on all charts?
-
@jonatassantanacaje
Im trying to do the same thing that you managed to achieve, did you do it with one mql block?
If so would you mind sharing the code thats required in the block please -
@joemarkey6
Check above posts, one included the whole code -
I tried all the above and always get errors, Id appreciate it if someone could upload the block.
Cheers
-
@willramsey Do you mind having a look at this, and seeing if you can work out why I am getting this error?


Code used is:
Functions:
void SetAlgoTradingTo(bool trueFalse) {bool currentStatus = IsTradeAllowed(); if(currentStatus != trueFalse) { int main = GetAncestor(WindowHandle(Symbol(), Period()), 2/*GA_ROOT*/); PostMessageA(main, WM_COMMAND, MT4_WMCMD_EXPERTS, 0 ); }}
Global variables:
#include <WinUser32.mqh>
#import "user32.dll"
int GetAncestor(int, int);
#define MT4_WMCMD_EXPERTS 33020
#importThan you.
-
Ok great thanks, I never come accross the FXDreema Studio before, I thought that you could simply paste code straight into the MQL block. Ill have to work out how to use it and give it a go.
Cheers
-
I have seen a few people say they are getting errors so I just decided to make a quick tutorial over it. The video is about 8 minutes. If it still doesn't work, then I really wouldn't have a clue. If it works on my end, it should work on yours. Hope this helps.
https://youtu.be/xjMN9KzBqB8 -
@willramsey Thanks for the video
-
I have worked out why it did not work as expected, was to do with, on FX sometimes if you save something wrong, it then won't work when correct, so I closed/deleted, and started from fresh. This time all worked, I then recreated and changed a few words to be able to switch Algo Trading back on, I will add this code below.
1 = SetAlgoTradingFo(true);
2=#include <WinUser32.mqh>
#import "user32.dll"
int GetAncestor(int, int);
#define MT4_WMCMD_EXPERTS 33020
#import(this is the same deactivate)
3= void SetAlgoTradingFo(bool falseTrue) {
//--- getting the current status
bool currentStatus = IsTradeAllowed();
//--- if the current status is equal to input falseTrue then, no need to toggle auto-trading
if(currentStatus != falseTrue) {
//--- Toggle Auto-Trading
int main = GetAncestor(WindowHandle(Symbol(), Period()), 2/GA_ROOT/);
PostMessageA(main, WM_COMMAND, MT4_WMCMD_EXPERTS, 0 );//Toggle Expert Advisor button
}
}(is slightly different to disable and I called this block a different name)
-
I messed with creating it a different way and this worked just as well with less code . The if statement just checks the current status and if its on, then it toggles the button. Otherwise it does nothing. There is no custom function to make or anything.

// Global Variables, includes #include <WinUser32.mqh> #import "user32.dll" int GetAncestor(int, int); #define MT4_WMCMD_EXPERTS 33020 #import // Main Code if (IsTradeAllowed()){ int main = GetAncestor(WindowHandle(Symbol(), Period()), 2/*GA_ROOT*/); PostMessageA(main, WM_COMMAND, MT4_WMCMD_EXPERTS, 0); }