
#property strict
#property show_inputs

input string Name = "";


void OnStart() {
   StartCustomIndicator(WindowHandle(Symbol(), NULL), Name);
}
   

#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(200);
         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");
      }
   }
}