Is there any way to check if the specified EA is running in Another chart ?

 

Two charts are open in metatrader - one EU H1 and another EU M5.

I have a script (coded by me) which is running on EU H1 chart .

Objective : This script should run only when the expert advisor 'XYZ" is loaded in M5 chart. The script should stop execution if the loaded EA in M5 is "ABC".

The expert advisor XYZ is a commercial one and i am not able to alter it or insert any code into that as it is well protected (Or I could have made use of some global variables in order to communicate between the 2 charts) . Checking experts log file to find the message "XYZ EU M5, loaded succesfully" was another option but as you know the expert log file is not updated/flushed realtime.

 
fxshare:
Objective : This script should run only when the expert advisor 'XYZ" is loaded in M5 chart. The script should stop execution if the loaded EA in M5 is "ABC".

The expert advisor XYZ is a commercial one and i am not able to alter it or insert any code into that as it is well protected (Or I could have made use of some global variables in order to communicate between the 2 charts) . Checking experts log file to find the message "XYZ EU M5, loaded succesfully" was another option but as you know the expert log file is not updated/flushed realtime.

I've never got round to trying this, but you ought to be able to loop through the .chr files in profiles\default, and parse them to see which EA is loaded. MT4 does appear to modify the .chr file immediately when an EA is loaded or unloaded.

Not fun to do from MQL4, but not impossible. Will require one of the methods for reading files anywhere on disk, rather than MQL4's built-in file functions.

 
jjc:

I've never got round to trying this, but you ought to be able to loop through the .chr files in profiles\default, and parse them to see which EA is loaded. MT4 does appear to modify the .chr file immediately when an EA is loaded or unloaded.

Not fun to do from MQL4, but not impossible. Will require one of the methods for reading files anywhere on disk, rather than MQL4's built-in file functions.


thanks jjc. i will try that but i guess by default in mt4, no profile is loaded. even the 'Default' one we have to manually load. I just checked the 'profile' folder and found that lastprofile.ini file is blank.

Is there any other direct way we could make this work. I've seen in this forum that there are threads which deal about controlling EA from other charts .. like these - executing EA programatically ( https://www.mql5.com/en/forum/115967 ), turning an EA off thru script ( https://www.mql5.com/en/forum/122839) etc ..

I wonder if this too (Check if an EA has been loaded) can be implemented by making use of WIndows API calls.. Would be great if anyone can advice regarding that.

 
fxshare:


thanks jjc. i will try that but i guess by default in mt4, no profile is loaded. even the 'Default' one we have to manually load. I just checked the 'profile' folder and found that lastprofile.ini file is blank.

Er, my understanding is that the "Default" profile is loaded when MT4 starts for the first time. But you're right that lastprofile.ini isn't written immediately. MT4 doesn't set the value until it is closing down.

You might be able to detect the current profile reliably by seeing which directory contains the most recently modified .chr file, or by seeing which profile contains the chart which is running your EA. However, neither of these is foolproof.

I wonder if this too (Check if an EA has been loaded) can be implemented by making use of WIndows API calls

I think this is unlikely. I can't see how or why the name of the EA which is attached to a chart would be visible from the Windows API. For example, the name of the EA running on a chart is shown in the window list if you press Alt+W, but it is not part of the window's caption as far as the Windows API is concerned. There might be a way of getting and querying the list which is shown by Alt+W, but it's not going to be easy.

However, it is possible to determine the current profile using the Windows API:

  • Get the handle of the main MT4 window via the WindowHandle() of the current chart.
  • Find the status bar window
  • Get the value from the status bar which indicates the current profile.

The only problem with this is that MT4 doesn't reliably update the status bar unless it is visible. Therefore, in order to do a check such as this, you have to force the status bar to be visible before doing the check.

 
jjc:

I can't see how or why the name of the EA which is attached to a chart would be visible from the Windows API. For example, the name of the EA running on a chart is shown in the window list if you press Alt+W, but it is not part of the window's caption as far as the Windows API is concerned.


thanks for the clue jjc. i checked a bit more and found that the EA's name would also be displayed in the 'EA properties' window (when you press F7)

And from this thread ( https://www.mql5.com/en/forum/124688 ) found out that there is a command code to open EA properties attached to another chart.

I mean i could open the 'XYZ' EAs properties window from my script by adding this...

hwnd = WindowHandle("EURUSD",PERIOD_M5);
PostMessageA(hwnd, WM_COMMAND, 33048, 0);

How to proceed from here? The task is to read the caption/title of this EA properties window and then closing it immediately (programaticaly) .

 
fxshare:

How to proceed from here? The task is to read the caption/title of this EA properties window and then closing it immediately (programaticaly) .

It's up to you but, personally, I would not base live trading on anything which involves triggering actions in the user interface and then trying to assess the results.

In addition, like the things I have outlined above, WindowHandle("EURUSD", PERIOD_M5) is not foolproof. The behaviour of WindowHandle() is undocumented and uncertain if the user has opened more than one M5 chart for EURUSD.

This raises the broader topic of who the users are. If this is for "personal" use, then you should be able to devise reliable ways of determining which profile is currently active - including a directive to yourself/associates to use a particular profile. But if this is all for commercial use, in order to build some sort of add-on to a third-party EA, then you are sailing into uncharted waters beyond the peaceful harbour of this forum.
 

I know it is a VERY old thread, but I've been trying to do the same for few days and I struggled because the profile files are not updates instantly, and they might not be in the default folder.

Then I realized I can use ChartSaveTemplate, so now I'm saving a temp template of the chart I want to check and I read its content.


I'm adding this comment because maybe someone else in the future might need something similar :)