Scripts: How to Disable/Enable Auto/Algo-Trading in both MT5 and MT4.

 

How to Disable/Enable Auto/Algo-Trading in both MT5 and MT4.:

Just simple codes to Disable/Enable Auto/Algo-Trading in both MT5 and MT4. Of course, DLLs must be allowed, This cannot be done without DLLs

Author: Kailash Bai Mina

 
Works as a charm!
 
Rot Zooi #:
Works as a charm!

did you not get this error message on compiling:


event handling function not found


 

If you getting any error about event handling function or getting an error , use these two codes below 

MT4

//--- 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
    }
}
//======================================================================
input bool EnableAlgoTrading=false;
//--------------------------------------------------------------
int OnInit()
  {
 SetAlgoTradingTo(EnableAlgoTrading);
   return(INIT_SUCCEEDED);
  }
//-------------------------------------------------------------  
void OnTick()
  {
  // You can also call it from the OnTick function , just delete the first two forward slash below :
  
  // SetAlgoTradingTo(EnableAlgoTrading);
  
  }

MT5

//+------------------------------------------------------------------+
//|                                    EnableDisableAlgo_Trading.mq5 |
//|                                  Copyright 2024, MetaQuotes Ltd. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
//--- importing required dll files
#define MT_WMCMD_EXPERTS   32851
#define WM_COMMAND 0x0111
#define GA_ROOT    2
#include <WinAPI\winapi.mqh>
//+------------------------------------------------------------------+
//| Toggle auto-trading button                                       |
//+------------------------------------------------------------------+
void AlgoTradingStatus(bool newStatus_True_Or_False) {
    //--- getting the current status
    bool currentStatus = (bool) TerminalInfoInteger(TERMINAL_TRADE_ALLOWED);
    //--- if the current status is equal to input trueFalse then, no need to toggle auto-trading
    if(currentStatus != newStatus_True_Or_False) {
        //--- Toggle Auto-Trading
        HANDLE hChart = (HANDLE) ChartGetInteger(ChartID(), CHART_WINDOW_HANDLE);
        PostMessageW(GetAncestor(hChart, GA_ROOT), WM_COMMAND, MT_WMCMD_EXPERTS, 0);
    }
}
//-------------------------------------------------------------------------------------
//-------------------------------------------------------------------------------------
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, MetaQuotes Ltd."
#property link      "https://www.mql5.com"
#property version   "1.00"
//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
input bool EnabAlgoTrading=false;
int OnInit()
  {
//---
   AlgoTradingStatus(EnabAlgoTrading);
  
 
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Expert deinitialization function                                 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
  {
//---
   
  }
//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
  {
// You can also call it from the OnTick function , just delete the first two forward slash below :
  
  // AlgoTradingStatus(EnabAlgoTrading);
   
  }
//+------------------------------------------------------------------+
MQL5.community - User Memo
MQL5.community - User Memo
  • www.mql5.com
You have just registered and most likely you have questions such as, "How do I insert a picture to my a message?" "How do I format my MQL5 source code?" "Where are my personal messages kept?" You may have many other questions. In this article, we have prepared some hands-on tips that will help you get accustomed in MQL5.community and take full advantage of its available features.
 
Excellent app
 
I want to enable algo trading and I have mt5 real account, so please help me step wise to start algo trading 🙏
 
Good job 👍🏻
It's helpful, thanks a lot bro! :-)