Add custom indicator to subwindow from EA

 

I know that this question has been asked before, but I can't get it to work. I hope someone can help.


From my custom EA, I want to programmatically add my custom indicator to a subwindow of the chart the EA was added to. I've managed to do this in MQL5 with ChartIndicatorAdd. So, I have my indicator which is added to a separate window (#property indicator_separate_window) and from my EA, I'm calling the following code from https://www.mql5.com/en/forum/115967 :

#import "user32.dll"
   int RegisterWindowMessageA(string MessageName);
   int PostMessageA(int hwnd, int msg, int wparam, string Name);
   void keybd_event(int VirtualKey, int ScanCode, int Flags, int ExtraInfo);
#import

void StartCustomIndicator(int hWnd, string IndicatorName, bool AutomaticallyAcceptDefaults = false)
{
   int MessageNumber = RegisterWindowMessageA("MetaTrader4_Internal_Message");
   PostMessageA(hWnd, MessageNumber, 15, IndicatorName);
   if (AutomaticallyAcceptDefaults) ClearConfigDialog();
}

// In my code where I launch the indicator

int hWnd = WindowHandle(Symbol(), 0);

StartCustomIndicator(hWnd, "MyIndicator");


However, when I execute this code, nothing is happening. Am I doing something wrong? Also, is it possible to provided parameters to the indicator using this manner?

 

Since Feb, 2014 Build 600, all strings are Unicode. RegisterWindowMessageA and PostMessageA will never work.

Whether the adjusted code will work is doubtful.

Create a template with the indicator added and then have EA call ChartApplyTemplate - MQL4 Documentation

The real question is why are you trying to do this? Indicators need to be visible only for Humans. EA's just get their value via iCustom.

 

Thanks for the reply.

To answer your question why I need this: I want to visually analyze my EA and see how my EA buys/sells when the indicator changes. Yes, I could add the indicator manually, but I love things that are automated.

With regards to the unicode strings: will RegisterWindowMessageA and PostMessageA never-ever work, or should they work if I convert the strings to ANSI and pass them to the functions?

 

Other people who are interested. I've managed to get it working:


#import "user32.dll"
  int RegisterWindowMessageW(string MessageName);
  int PostMessageW(int hwnd, int msg, int wparam, char &Name[]);
#import


 string name = "MyIndicator";
 char name2[];
 StringToCharArray(name , name2);
 
 int MessageNumber = RegisterWindowMessageW("MetaTrader4_Internal_Message");
 int r = PostMessageW(hWnd, MessageNumber, 15, name2);
 
goocreations:

Other people who are interested. I've managed to get it working:


#import "user32.dll"
  int RegisterWindowMessageW(string MessageName);
  int PostMessageW(int hwnd, int msg, int wparam, char &Name[]);
#import

void callIndic(){
   string name = "ATR";
   char name2[256];
   StringToCharArray(name , name2);
   
   int hWnd = WindowHandle(Symbol(), Period());
   int MessageNumber = RegisterWindowMessageW("MetaTrader4_Internal_Message");
   int r = PostMessageW(hWnd, MessageNumber, 15, name2);

}


 

Can somebode check this code in the latest 880 build?

For me it is crashing mt4, though after restarting the mt4 ATR indicator is on the chart, so it was attached before the crash.

With using SendMessageW instead of PostMessageW - no crash, but indicator's dialog window appears and waits for user input (calling keybd_event(13, 0, 0, 0) does not work).

 
nickbilak:

Can somebode check this code in the latest 880 build?

For me it is crashing mt4, though after restarting the mt4 ATR indicator is on the chart, so it was attached before the crash.

With using SendMessageW instead of PostMessageW - no crash, but indicator's dialog window appears and waits for user input (calling keybd_event(13, 0, 0, 0) does not work).



Hi,

   Any update, on getting the code to work on current build.

 
EA's have no eyes, there is no need for the EA to attach an indicator. If you want it on the chart, add it.
 

The SendMessageW instead of PostMessageW works but as nickbilak says indicator's dialog window appears and waits for user input.

Anyone have the code to close the dialog window please ???

 
MadMik:

The SendMessageW instead of PostMessageW works but as nickbilak says indicator's dialog window appears and waits for user input.

Anyone have the code to close the dialog window please ???

It's in the original code at https://www.mql5.com/en/forum/115967

 
JC:

It's in the original code at https://www.mql5.com/en/forum/115967


Thanks for the help.

Works now BUT ....

had to put the    keybd_event(13, 0, 0, 0);    before the SendMessageW(hWnd, MessageNumber, 15, name2)

otherwise it did not work ... instead it left dialog open and then opened the pair input box at base of chart.


MM