How do I disable autotrading globally in MetaTrader 4/5 from within MQL4/5 code without using DLLs?
3 Answers
Here you go, (Author is Tiago Praxedes)
#define MT_WMCMD_EXPERTS 32851
#define WM_COMMAND 0x0111
#define GA_ROOT 2
#include <WinAPI\winapi.mqh>
void SetAlgoTradingStatus(bool Enable)
{
bool Status = (bool) TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);
if(Enable != Status)
{
HANDLE hChart = (HANDLE) ChartGetInteger(ChartID(), CHART_WINDOW_HANDLE);
PostMessageW(GetAncestor(hChart, GA_ROOT), WM_COMMAND, MT_WMCMD_EXPERTS, 0);
}
}
void OnTick()
{
SetAlgoTradingStatus(false);
}
Lifted it from here: Source

- 58,075
- 31
- 238
- 265
-
1Thank you, but this solution involves DLLs too (imported via winapi.mqh). – Enivid Jan 21 '23 at 10:38
-
@Enivid I guess no such way exist for stopping the auto-trading feature without resorting to winapi DLL. At least using the winapi seems possible. I'm wondering if this code works exactly the same way for MT4? – Gerard Bosch Jan 23 '23 at 17:51
-
See this also for an MT4 solution using DLL `user32.dll`: https://www.mql5.com/en/code/39068 – Gerard Bosch Jan 31 '23 at 19:05
Yes, MQL4/5
Expert Adviser may locally forbid self to trade this way:
if ( IsTradeAllowed() )
{ Comment( __FILE__, " [EA] Trading is allowed, will disable self." );
...
}
else
{ Comment( __FILE__, " [EA] Trading is not allowed, will disable self." );
...
}
// ---------------------------------------// GRACEFULLY RELEASE ALL RESOURCES BEFORE FIN'd
// ********
// FINALLY: EXPERT-ADVISOR SIG_TERM -> self ( MT4 )
ExpertRemove(); /* MT4 The Expert Advisor
is not stopped immediately
as you call ExpertRemove();
just a flag to stop the EA operation is set.
That is:
- any next event won't be processed,
- OnDeinit() will be called
and
- the Expert Advisor will be unloaded and removed from the chart.
*/

- 1
- 6
- 50
- 92
-
1But it will not disable autotrading globally in the platform, it will just remove the calling EA. – Enivid Jun 04 '16 at 19:35
-
**Sure**, as was noted right in the very headline. There is no [MetaTrader Terminal] interface exposed towards the embedded MQL4/5 code-execution engine, that would allow a code-driven change of the [MetaTrader Terminal] settings. There are many reasons for such sub-ordination. Such **Kamikadze**-code would be ultimately more dangerous than beneficial. – user3666197 Jun 04 '16 at 20:30
If the idea is that you have several EA's on different pairs and you want to disable them all at the same time you could place a specific trade, that I would call an information trade and not meant to be used for trading.
Choose a price very far away from current price. For this example, we can use 9,999.000 as price and we place the trade. Loop through the trades and look for a price = 9999. If you find it use that to disable trading.
If you want it to start again, you can have a button or manually delete that trade that is 9999. Now the block is not there.
All the EA's you may have going can see this. Also, an offsite computer will be able to see it as well.
This was used as a way to hand off trading control to a team of traders. The new owner would send the trade that bumps everyone off. Remove that trade but the other EA's are asleep until a human turns them back on or a "Wake Up" trade is sent.
You can send values in the Comment, TP and SL values as prices. There are many ways to use this method. For this to work though you would have to have your own code and make the changes. If you buy an EA and want to turn it on and off without the source code then this method won't work.