Can I code in MQL to turn on and off an already running EA? - page 2

 
valleyfir:

I use Ctrl+E to enable and disable the Expert Advisors, so the code is:

keybd_event(17,0,0,0); // 17=Ctrl
keybd_event(69,0,0,0); // 69=E
keybd_event(69,0,KEYEVENTF_KEYUP,0);
keybd_event(17,0,KEYEVENTF_KEYUP,0);


Largely for the record, sending a WM_COMMAND message to the top-level MetaTrader window with wParam of 33020 has the same effect as pressing Ctrl+E. Therefore, an EA could use the SendMessage() API call, using the GetParent() call to turn the handle of its own window - WindowHandle(Symbol(), 0) - into the handle of the top-level MetaTrader window:


SendMessage((GetParent(GetParent(GetParent(WindowHandle(Symbol(), 0))))), 273, 33020, 0);

This has the same disadvantages as simulating Ctrl+E (i.e. can only toggle EAs, rather than turning them off), plus an even greater likelihood of breaking in future versions of MetaTrader. 


The only real reason for mentioning it is that it's less problematic to use from an external program. For example, the following C code is for a Windows console application which toggles the on-off state of EAs in all running instances of MetaTrader. In other words, if you're sure that all instances currently have EAs turned on, then running an app like this will turn EAs off in every active copy of MetaTrader:


#include <windows.h>

BOOL CALLBACK WindowEnumProc(HWND hwnd, LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[])
{
EnumWindows(WindowEnumProc, 0);
}

BOOL CALLBACK WindowEnumProc(HWND hwnd, LPARAM lParam)
{
char cClassBuffer[100];
if (GetClassNameA(hwnd, cClassBuffer, 99))
{
if (strstr(cClassBuffer, "MetaQuotes") == cClassBuffer)
{
SendMessageA(hwnd, WM_COMMAND, 33020, 0);
}
}

return true;
}


 
fbj:

jjc - thanks for the response. yessss - [...]

I will endeavor to [sometime this century!] post my working code - I will consider that is possible within MQL4 + #import "..."

If nothing else, bound to pick up some knowledge... I like to hammer and chisel away. The fun of what may be around the corner is worth the sweat and blisters!

fbj - see 'Subclassing a Metatrader chart window'. Started a new topic on the subject because it may be of wider interest.

 
jjc:


Largely for the record, sending a WM_COMMAND message to the top-level MetaTrader window with wParam of 33020 has the same effect as pressing Ctrl+E. Therefore, an EA could use the SendMessage() API call, using the GetParent() call to turn the handle of its own window - WindowHandle(Symbol(), 0) - into the handle of the top-level MetaTrader window:


SendMessage((GetParent(GetParent(GetParent(WindowHandle(Symbol(), 0))))), 273, 33020, 0);

This has the same disadvantages as simulating Ctrl+E (i.e. can only toggle EAs, rather than turning them off), plus an even greater likelihood of breaking in future versions of MetaTrader.


The only real reason for mentioning it is that it's less problematic to use from an external program. For example, the following C code is for a Windows console application which toggles the on-off state of EAs in all running instances of MetaTrader. In other words, if you're sure that all instances currently have EAs turned on, then running an app like this will turn EAs off in every active copy of MetaTrader:


#include <windows.h>

BOOL CALLBACK WindowEnumProc(HWND hwnd, LPARAM lParam);

int _tmain(int argc, _TCHAR* argv[])
{
EnumWindows(WindowEnumProc, 0);
}

BOOL CALLBACK WindowEnumProc(HWND hwnd, LPARAM lParam)
{
char cClassBuffer[100];
if (GetClassNameA(hwnd, cClassBuffer, 99))
{
if (strstr(cClassBuffer, "MetaQuotes") == cClassBuffer)
{
SendMessageA(hwnd, WM_COMMAND, 33020, 0);
}
}

return true;
}


PLS jjc

Ctrl + E for which equal 33,020
pls how to calculate it or how to get this value

thank you

 
pls any one can help me ?
 
for up
 
any one where is jjc ......
 
what u need
 
qjol:
what u need

thank you gjol

Largely for the record, sending a WM_COMMAND message to the top-level MetaTrader window with wParam of 33020 has the same effect as pressing Ctrl+E. Therefore, an EA could use the SendMessage() API call, using the GetParent() call to turn the handle of its own window - WindowHandle(Symbol(), 0) - into the handle of the top-level MetaTrader window:


SendMessage((GetParent(GetParent(GetParent(WindowHandle(Symbol(), 0))))), 273, 33020, 0);

Ctrl + E for which equal 33,020
pls how to calculate it or how to get this value


thank you

 
how to calculate it
 

u mean the no. 33020 ? if the answer is yes, this is not a calculation

microsoft spyxx giving u this no.

u can download it from here


Reason: