How to enable disable EA in MT4 indicator ?

 

Hi all :


I try to enable or disable EA in my code , and I use the code from here .

As a script file , it works fine for me, but when I try to convert it into my indicator with a just very simple modify,  it wont work,
the setup popup window just stay there and the "Allow Live Trading" checkbox stay the same without any change 


Any help will appreciated !

Thanks in advance.


//+------------------------------------------------------------------+
//|                                             !Disable current.mq4 |
//|                        Copyright 2021, MetaQuotes Software Corp. |
//|                                             https://www.mql5.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2021, MetaQuotes Software Corp."
#property link      "https://www.mql5.com"
#property version   "1.00"
#property strict
#property indicator_chart_window

#define WM_LBUTTONDOWN 0x0201
#define WM_LBUTTONUP  0x0202
#define WM_KEYDOWN                     0x0100
#define KEYEVENTF_KEYUP                0x0002
#define WM_COMMAND                     0x0111

//#include <WinUser32.mqh>
#import "user32.dll"
int      PostMessageW(int hWnd,int Msg,int wParam,int lParam);
int      UpdateWindow(int hWnd);
int GetAncestor(int,int);
int  GetDlgItem(int hDlg,int nIDDlgItem);
int  GetWindowTextLengthA(int hWnd);
int  GetWindowTextA(int    hWnd,char &lpString[],int    nMaxCount);
int  PostMessageA(int  hWnd,int  Msg,int  wParam,int lParam);
int  GetLastActivePopup(int hWndOwner);
int  GetWindow(int hWnd, int uCmd);
int  GetParent(int hWnd);
void keybd_event(int bVk,int bScan,int dwFlags,int dwExtraInfo);
int  SendMessageA(int  hWnd,int  Msg,int  wParam,int lParam);
#import


//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {
//--- indicator buffers mapping
   
   // EA_On_Off(); not work here 
   
//---
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {
//---
   static datetime timestamp = TimeCurrent();
   static bool FirstTime = true ;
   if(TimeCurrent()-timestamp > 5 && FirstTime)
     {
      EA_On_Off();      // not work here either 
      FirstTime = false ;
     }

//--- return value of prev_calculated for next call
   return(rates_total);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int EA_On_Off()
  {
   int hMetaTrader = GetAncestor(WindowHandle(Symbol(),Period()),2);
   PostMessageA(hMetaTrader, WM_COMMAND, 33048, 0);
   Sleep(200);
   int kid=get(hMetaTrader);
   string top=name(kid);
   if(top=="Common ")
     {
      kid=avance(kid);
      int is_checked=check_state(kid);
      if(is_checked)
        {
         Print("button is currently checked");
         Comment("EA is now [OFF]");
        }
      else
        {
         Print("button is currently NOT checked");
         Comment("EA is now [ON]");
        }

      PostMessageW(kid,WM_LBUTTONDOWN,1,0);
      Sleep(10);
      PostMessageW(kid,WM_LBUTTONUP,1,0);
      Sleep(10);
      PostMessageA(kid,WM_KEYDOWN,0x0D,0);
      return 0;
     }
   else
      if(top=="")
        {
         Print("first");
         Sleep(10);
         keybd_event(17,0,0,0);
         Sleep(10);
         keybd_event(9,0,0,0);
         Sleep(10);
         keybd_event(9,0,KEYEVENTF_KEYUP,0);
         Sleep(10);
         keybd_event(17,0,KEYEVENTF_KEYUP,0);
         Sleep(10);
        }
      else
        {
         keybd_event(17,0,0,0);
         Sleep(10);
         keybd_event(16,0,0,0);
         Sleep(10);
         keybd_event(9,0,0,0);
         Sleep(10);
         keybd_event(9,0,KEYEVENTF_KEYUP,0);
         Sleep(10);
         keybd_event(16,0,KEYEVENTF_KEYUP,0);
         Sleep(10);
         keybd_event(17,0,KEYEVENTF_KEYUP,0);
         Sleep(10);
        }

   Sleep(100);
   kid=get(hMetaTrader);
   kid=avance(kid);
   Sleep(100);
   bool is_checked=check_state(kid);

   if(is_checked)
     {
      Print("button is currently checked");
      Comment("EA is now [OFF]");
     }
   else
     {
      Print("button is currently NOT checked");
      Comment("EA is now [ON]");
     }

   PostMessageW(kid,WM_LBUTTONDOWN,1,0);
   Sleep(10);
   PostMessageW(kid,WM_LBUTTONUP,1,0);
   Sleep(10);
   PostMessageA(kid,WM_KEYDOWN,0x0D,0);

   return 0;

  }


//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int get(int basic)
  {

   UpdateWindow(basic);
   int hInner = GetLastActivePopup(basic);
   int popup=GetWindow(hInner,5);
   int kid=GetWindow(popup,5);


   return kid;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int avance(int kid)
  {
   Sleep(10);
   kid=GetWindow(kid,2);
   kid=GetWindow(kid,2);
   kid=GetWindow(kid,2);
   kid=GetWindow(kid,2);
   kid=GetWindow(kid,2);
   kid=GetWindow(kid,2);
   Sleep(10);
   return kid;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
bool check_state(int handle)
  {
   return (bool)SendMessageA(handle, 0x00F0, 0, 0);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
string name(int handle)
  {
   char e[];
   StringToCharArray("012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234",e) ;
//string p1;
   GetWindowTextA(handle,e,GetWindowTextLengthA(handle)+1);

   return CharArrayToString(e);


  }
//+------------------------------------------------------------------+
Disable auto trading but ONLY for a specific EA
Disable auto trading but ONLY for a specific EA
  • www.mql5.com
Simple Script to disable the Autotrading for the EA on the chart. If you just want to use the DLL, check the code and comment/uncomment the necessary