Transcript

Use with ChatGPT / Claude Auto-scroll A

1. Introduction and Script Demonstration

Introduces the goal of programmatically toggling the auto/algo trading button and demonstrates the working scripts on both MT4 and MT5.

0:07 - 1:02
hello today i'm looking at something that's not terribly complex but it does seem to cause problems for people and it's a question that keeps popping up and that is how to programmatically turn on or off this auto tradingbutton for metatrader 4 or the algo trading button for metatrader5 same thing they just have different names now in these tutorials i try very hard to avoid using dlls but in this case there is no choice i'll haveto use a dll to achieve this i'll also point out that this button the algo trading or the auto trading button on mt4 will enable or disable auto trading for all experts so this tutorial doesn't help with enablingor disabling individual experts it turns the trading on or off for the entire platform it's also worth noting that this simply stops the experts from trading when this is turned off the experts can still run they just won't
be able to place any trades now i've already created some scripts to do this so that i've correctly created two scripts and auto trading often and auto trading on so that it's easy to see that i'm running oneor the other just to demonstrate what i'm talking about at the moment this algo trading is off i can toggle that to on off here so now that it's off i can run my script for auto trading onand it's opening up here it says allow dlnl imports i need to do that as i said you have to enable dlls for this to run so this is the auto trading on and if i now click okyou can see that button's on if i run the auto trading off allow dlls okay and it goes off and if i run off again it actually has no impact because it's already off if i now do the
same thing on metatrader4 my auto trading is turned on at the moment so i'll run the auto trading off dialog looks a little bit different but here is the allow dll imports ok and auto trading goes off irun the auto trading on allow dlnl imports and auto trading is turned back on so we'll go into the code and i'll show you the function that i'm using to do this i'm doing for both metatrader 4 and

2. Script Structure and File Setup

Shows the on/off scripts for both platforms and explains the shared include file containing the set auto trading function.

5.there are a couple of differences but i'll handle those through the compiler directives to compile for metatrader 4 or 5.so i've started here with the metatrader4 editor and i've opened up the scripts to begin with the auto trading off auto trading off for mq5 on for mq4 and on for mq5 and you can see that the scriptsare identical between metatrader 4 and 5 the only thing i've changed is this heading in the description all i'm doing in the onstart is i'm calling a function set auto trading and in the on function i'm call orin the onscript i'm calling that with a true i look at the mq4 version set auto trading true and for the off scripts i'm calling set auto trading faults so all the work's going to be in this setauto trading and i've called that in with this include statement i'm including orchard toggle auto trading as i've called it and a file called toggle includes this set auto trading function so now we'll go and we'll actually create
this toggleautotrading.mqh file and write the code for that

3. Writing the Toggle Function Code

Walks through the DLL imports, PostMessageW call, platform-specific declarations, and getting the main window handle, then compiles and tests.

so here's my function set auto trading takes one boolean which is the state that's the state that i want to set the auto trading to so a true will turn on auto trading and false will turn it offas i said i need dlls i'm going to use the windows apis and this is where we see the differences between metatrader4 and metatrader5 there aren't many of them but it's important
so i'm starting with the ifdef mql4 where i can write the mql4 specific code
and i'm including the win user 32.qh file that includes the definitions for the api that i need
but for mql5 these definitions have been moved into a different file they're in a subfolder under win api and it's called winapi dot mqh so with this file loaded for mql4 and this for mql5 i now have accessto a function that will allow me to set the state of that button
so this is the function post message w it takes a window handle passes a command which we all need to declare and then a sub command which is to toggle the experts button and the third argument is notneeded so that's just a zero so this postmessage w is declared in the windows api it's a you user32.dll inclusion window handle will need to get i'll just define this wm underscore command and the mt underscore wmcmd experts
so starting with the wmcmd experts this value is actually different between metatrader4 and metatrader5 and it's different because this identifies the particular object on screen that we're trying to address and because they have different user interfaces the objectnumbers are different or the address of those objects is different
so the numbers are different between the two so i've made two different declarations and that's why i've placed them in the mql4 or mql5 sections i've used the same name so it doesn't matter which you compile under you'llget the appropriate number for each of those so that takes care of this second argument wmt underscore wmcmd underscore experts wm command is also a value it is already declared in wind user 32 but it is not declaredin win api so i'm going to add a declaration in here in the mql5 section for wm underscore commandnow the value of this is the same for metatrader 4 and 5 it's just that this is already declared inside this wind user 32 and it's not declared in win api so i just need to add the declarationhere so the next thing i need is this main window handle
so main window handle is an integer and i'm calling the get ancestor function which i'll get to in a moment and that returns a long so i need to cast that back to an integer the argument to getancestor uses the chart get integer function which is an inbuilt function in metatrader both four and five zero for the current chart and the argument to that is chart underscore window underscore handle so the chart get integer willget me a number which is the handle of the current chart and i'm casting that also back to an integer because the get ancestor needs an integer argument get ancestor also needs a second argument number two which tellsit that i'm trying to find the ultimate window that holds this chart now get ancestor is also a windows api function and a little bit like wm underscore command except in reverse it is already declared in win api
but it is not declared in win user 32 so i'll need to add a declaration for get ancestor
so to do that hash import user32.dlland then i'm declaring it's an integer type it's called getancester and it takes two arguments integer and integer types and then i finish that with hash import with no further values here so when you're importing from dlls thisis the syntax hash import address or name of the dll the functions that you want to import and then finish that off with another hash import and as i said it's already declared in when api so i don'tneed to declare it here in the mql5 section just in the mql4 now i think that's everything let me do a quick test that worked if i now compile the auto trading off and the auto trading on formql4 i'll just go to the editor for mql5 and do the same so in mql5 i will now compile a typing mistake there so just fix that include let's try again auto trading off compile that worked and auto
trading on compile and that worked all right let's go back to the metatrader 4 and 5 terminals and test this here we are in metatrader4 again auto trading is already on so i'm going to run the auto tradingoff allow dll okay auto trading goes off but now i'm going to run auto trading off again allow dlls and it comes back on so let's fix that problem here in the toggle auto trading the problem is that

4. Fixing Toggle Logic and Final Testing

Fixes the toggle issue by checking terminal trade allowed state, retests on both platforms, and summarizes key differences between MT4 and MT5.

this post message w actually toggles the auto trading or algo trading buttons to the opposite of whatever they are so simply calling this will set them on if they're already off and off if they're already on so ineed to add some extra code in here that will only call this for the appropriate state
and that's a simple test there is already a function built in called terminal info integer with an argument of terminal trade allowed and that gives me the state of that algo trading or auto trading button it comes backas an integer because this is terminal info integer i'm going to cast that to a boolean and then compare that to the state so if this function tells me that the button is already off and i've passed ina state of false then state will be equal to the current state of the button and i just return and the same if i'm trying to turn the button on and it is already on if state is equalto the current state of the button i just return and that avoids calling this postmessage w again so let me recompile these now back to metatrader4 auto trading is on allow dlls and turning it off with auto trading
off that goes off if i run auto trading off again
no changes so that's doing the right thing now i'll turn on again with auto trading onrun on again allow dlls that stays on now you wouldn't normally put this into a script you would call this from your expert or indicator depending whether you want to turn auto trading on or off i'm just usinga script here as a demonstration but it's a simple call to that one function and let me just quickly go to metatrader5 and demonstrate that it's also working there so here in metatrader5 we have algo trading off atthe moment so i'm going to run the on allow dlls and now i'll go trading is on i'll run auto trading on again allow dlls and that stays on and if i run auto trading off allow dll okayand again off it should stay there and it does and that's all there is to this i think where people get caught up and have problems are that this experts flag has changed values between metatrader 4 and 5
so they're sometimes trying to copy their metatrader 4 code and use it in 5 or going the other way around this wm command doesn't exist in win api and the get ancestor doesn't exist in wind user 32 sothere are some differences between 4 and five i'm not quite sure why those differences exist other than that the experts is there simply because it's a different user interface but i don't want to modify this win user32 orwin api to correct those because they could be overridden at any time if there's a new release from meta quotes and then that's all there is if you want to toggle that switch from within your indicators or expertsi hope you found this useful if you did click the like button and if you want to see more of our videos click subscribe and click the bell icon to be notified when we release new videos thank you
for watching
you
Seek backward 5s ←
Longer seek (10s) Shift ←
Play Space Seek forward 5s →
Longer seek (10s) Shift →
0:00 Auto-scroll A Copy link at current time S

How to turn autotrading on or off with code