MQL4 Add indicator to chart from EA - page 6

 

Alain Verleyen:

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

#define INDICATOR_NAME "Indicator_name"
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void StartCustomIndicator(int hWnd,string IndicatorName,bool AutomaticallyAcceptDefaults=false)
  {
   uchar name2[];
   StringToCharArray(IndicatorName,name2,0,StringLen(IndicatorName));

   int MessageNumber=RegisterWindowMessageW("MetaTrader4_Internal_Message");
   int r=PostMessageW(hWnd,MessageNumber,15,name2);
   Sleep(10);
  }
//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int OnInit()
  {
//---
// In my code where I launch the indicator
   int hWnd=WindowHandle(Symbol(),0);
   StartCustomIndicator(hWnd,INDICATOR_NAME);

   return(INIT_SUCCEEDED);
  }


Hello


I am trying to use this code in a SCRIPT, but sometimes my metatrader close soddenly


Do you know what is the problem ? 

 

Chart.mqh is pretty obvious........


//+------------------------------------------------------------------+
//|                                                       Chart.mqh  |
//|                   Copyright 2009-2016, MetaQuotes Software Corp. |
//|                                              http://www.mql5.com |
//+------------------------------------------------------------------+
#include <Object.mqh>
//---
#ifdef __MQL4__
bool ChartIndicatorAdd(long chart_id,int subwin,int handle) { return(false); }
#endif
 
Alain Verleyen #:
You just have to add code to simulate key stroke, like you do manually.

Doesn't work. I need to execute the add indicator code from an indicator. I have tried many solutions, but I get no results. The indicator I want to add gets added, but the indicator configuration window stays on and waits for manual confirmation.
I am doing this in MT4 Build 1353.

To be clear, those codes work from script fine, and the keyboard emulation works from script, too.
No luck from an indicator...
 
Nicolas Baptista #:

Hello everyone,

I would like to thanks Georgiy Liashchenko, and I would like to share my upgraded version of his code that add the indicator directly into the chart with AutomaticallyAcceptDefaults (when it is on "true").

I hope it will help some people that need to insert indicators from the expert.

Note : I works for custom indicators, else change the windows name

Note2 : Don't use indicators with more than 15 characters (before the ".ex4")

Great
 
Raphael Adetunji Olaiyapo #:
Great
#import "user32.dll"
   int  RegisterWindowMessageW(string lpString);
   int  FindWindowW(string lpClassName, string lpWindowName);
   int  GetDlgItem(int hDlg, int nIdDlgItem);
   int  SetActiveWindow(int hWnd);
   bool PostMessageA(int hWnd, int msg, int wParam, uchar &lParam[]);
   bool PostMessageW(int hWnd, int msg, int wParam, int lParam);
#import
#define MAX_PATH                                      260              // e.g. the max. path on drive D is "D:\some-256-chars-path-string<NUL>"
#define MT4_LOAD_CUSTOM_INDICATOR     		     15
#define IDC_CUSTOM_INDICATOR_OK            1                // control id of "Ok" button in "Custom Indicator" dialog

#define BM_CLICK                                     0x00F5 
uchar buffer[MAX_PATH];
void StartCustomIndicator(int hWnd, string indicatorName, bool autoCloseDlg = true) {
   StringToCharArray(indicatorName, buffer); 

   int WM_MT4 = RegisterWindowMessageW("MetaTrader4_Internal_Message");
   PostMessageA(hWnd, WM_MT4, MT4_LOAD_CUSTOM_INDICATOR, buffer); 

   if (autoCloseDlg) {
      string className, title = "Custom Indicator - "+ indicatorName;
      int i = 0;
      while (i < 5) {
         Sleep(10);
         int hWndDlg = FindWindowW(className, title);
         if (hWndDlg != 0) {
            int hWndOk = GetDlgItem(hWndDlg, IDC_CUSTOM_INDICATOR_OK);
            if (hWndOk != 0) {
               SetActiveWindow(hWndDlg);
               PostMessageW(hWndOk, BM_CLICK, 0, 0);
            }

            else Print("Error: \"OK\" button not found");
            break;
         }
         i++;
         if (i >= 5) Print("Error: Dialog \""+ title +"\" not found");
      }
   }
}
int OnInit()
  {
//---
   int hWnd=WindowHandle(Symbol(),0);
   StartCustomIndicator(hWnd,"Stochastic");   // For example STO
//---
   return(INIT_SUCCEEDED);
  }
 

Hi


with all the different codes related to user32.dll and messages, it should be a good idea to share an updated version of the include file with additionnal codes.


Here is the include file I am using:

Files:
WinUser32.mqh  54 kb
 
Hossein Sadeghian Nezhad #:
Thank you, Hossein, it was very helpful. Your code is the only that worked for me =)
 
Nicolas Baptista #:

Hello everyone,

I would like to thanks Georgiy Liashchenko, and I would like to share my upgraded version of his code that add the indicator directly into the chart with AutomaticallyAcceptDefaults (when it is on "true").

I hope it will help some people that need to insert indicators from the expert.

Note : I works for custom indicators, else change the windows name

Note2 : Don't use indicators with more than 15 characters (before the ".ex4")

Hello everyone,

This solution is really interesting and works well, but there is a small problem. If the same EA is active on more than one chart, on the first the loading of the indicator is automatic, on the others it requires manual confirmation on OK. Is there any solution?

 
Just wanted to say thanks to Hossein Sadeghian Nezhad.. I used your code and it worked great.. 
Hossein Sadeghian Nezhad
Hossein Sadeghian Nezhad
  • 2017.06.15
  • www.mql5.com
Trader's profile
 

Hi all,

I have also used the code from @Hossein Sadeghian Nezhad and it works pretty good - thanks for that!

However, I want to add custom indicators on the main chart with specific input variables. When using the code above, you can't change the the input values, it will only show the indicator with "standard" settings.

How can I show the custom indicator in the chart with other input values?

Thanks in advance for feedback!

Reason: