Disable automated trading, but only for specific EAs - MetaTrader 4 Library | Trading Script Download - MT4/MT5 Resources

This script is used to disable the Expert Advisor on the chart, automatically clicking on the following button on the image:

In the code below it gets the handle to the main Metatrader and opens the Expert tab (command 33048 in mt4, is different for mt5)
Integer hMetaTrader = GetAncestor(WindowHandle( symbol (), period ()), 2 ); PostMessageA (hMetaTrader, WM_COMMAND, 33048 , 0 ); sleep ( 200 ); //It is important to hold for at least 0.2 seconds for the window to open correctly
Then we need to check which tab we are currently on, we get the name of the tab and autocorrect it to go to the correct tab:
Integer children = get(hMetaTrader); stringtop = name(child); if (top == "common" ) { // If the window is already the correct window (public section tab), just find the button, click it, and click "OK" child = advance(child); integer is_checked=check_state(child); if (checked){ print ( "Button is currently selected" );} else { print ( "No button is currently selected" );} sleep ( 10 ); PostMessageW(child,WM_LBUTTONDOWN, 1 , 0 );//Press the left mouse button to sleep ( 10 ); PostMessageW(child,WM_LBUTTONUP, 1 , 0 );//Release it to sleep ( 10 ); PostMessageA(child,WM_KEYDOWN, 0x0D , 0 );//Press the Enter key to confirm the selection and return 0 ; } Else if (top== "" ){//If the tab is the "About" section, press ctrl+tab (move forward 1 tab) 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 ( No. 17 , 0 , KEYEVENTF_KEYUP, 0 ); sleep ( 10 ); } Else {//If the tab is "Input", press ctrl+maj+tab (returns to 1 tab) 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 ( No. 17 , 0 , KEYEVENTF_KEYUP, 0 ); sleep ( 10 ); }
Finally, if the tab is incorrect, find the "Allow automated trading" button and press it to disable it:
sleep ( 100 ); child=get(hMetaTrader); child = advance(child); sleep ( 100 ); Boolean value is_checked=check_state(child); if (checked){ print ( "Button is currently selected" );} else { print ( "No button is currently selected" );} print (name(child)); PostMessageW(child,WM_LBUTTONDOWN, 1 , 0 ); sleep ( 10 ); PostMessageW(child,WM_LBUTTONUP, 1 , 0 ); sleep ( 10 ); PostMessageA(child,WM_KEYDOWN, 0x0D , 0 );
I forgot to mention that the code will click this particular button even if the auto trade is off so it will activate it, disable it, activate it.. that's why I made a special function to get the current state of the button, you can modify the code you want to change using this function to get the button state:
Integer Kid=<Button handle> Boolean value is_checked=check_state(child); Boolean value check status( integer processing){ Return ( Boolean )SendMessageA(handle, 0x00F0 , 0 , 0 ); }
Attachment download
📎 disable_current.mq4 (8.26 KB)
📎 winuser32.mqh (37.64 KB)
Source: MQL5 #29022
💡 Featured Recommendations
✍️ Latest by the author
- •
- •
- •
- •
- •
- •
📌 Popular topics
- •
- •
- •
- •
- •
- •
- •
- •
🔗 You May Be Interested In
- •
- •
- •
- •
- •
- •