MQL4 Add indicator to chart from EA - page 5

 

Forum on trading, automated trading systems and testing trading strategies

MQL4 Add indicator to chart from EA

Alain Verleyen, 2016.02.13 11:39

#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);
  }

Hi every body

above code works in a Expert ,How do I use from above codes in a Indicator ?

please help.

 
Is it possible in MT4 to remove the indicator from the chart using EA ? Some code may be written in OnDenit() function.
 
Abdul Manan:
Is it possible in MT4 to remove the indicator from the chart using EA ? Some code may be written in OnDenit() function.

Removes an indicator with a specified name from the specified chart window. The command is added to chart message queue and executed only after all previous commands have been processed.

bool  ChartIndicatorDelete(
   long           chart_id,              // chart id
   int            sub_window,            // number of the subwindow
   const string   indicator_shortname    // short name of the indicator
   );

Parameters

chart_id

[in]  Chart ID. 0 denotes the current chart.

sub_window

[in]  Number of the chart subwindow. 0 denotes the main chart subwindow.

const indicator_shortname

[in]  The short name of the indicator which is set in the INDICATOR_SHORTNAMEproperty with the IndicatorSetString()function. To get the short name of an indicator use the ChartIndicatorName()function.

Return Value

Returns true if the command has been added to chart queue, otherwise false. To get errordetails use the GetLastError()function.

ChartIndicatorDelete - 图表操作 - MQL4参考
ChartIndicatorDelete - 图表操作 - MQL4参考
  • docs.mql4.com
Removes an indicator with a specified name from the specified chart window. The command is added to chart message queue and executed only after all previous commands have been processed. If the short name of an indicator is not set explicitly, then the name of the file containing the source code of the indicator will be specified during...
 
Alain Verleyen:
Is there way to attach the indicator by auto on the chart from EA when backtest?
 
int OnInit()
  {
//---
// In my code where I launch the indicator
   int hWnd=WindowHandle(Symbol(),0);
   StartCustomIndicator(hWnd,INDICATOR_NAME);

   return(INIT_SUCCEEDED);
  }


This code work, but only in OnInit section. If i try transfer it to section CHARTEVENT_OBJECT_CLICK for launch with key, nothing happend. Can you help me ?
 
Hi, I'm not able to load 2 indicators from 1 EA using the same code.
 
This thread was tremendously helpful to me but didn't get me quite there -- see this thread (https://www.forexfactory.com/showthread.php?p=12764287#) which picks up where the dialog here left off. 
 
Kray:

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")

Hi I got this 

 Cannot call 'user32.dll::RegisterWindowMessageW', 'user32.dll' is not loaded

Can anyone help me?


 
Alain Verleyen:

I don't have this problem (with the code I posted).

Check that hWnd is different from 0.

You can also try to increase the sleep value to 50 or 100

Dear Alain , 

I used your code to add one indicator to all open charts. but it only added to the chart on which I run the EA. 

here is the code  , can you please modify your code to add the same indicator to all open charts ? 

please help 

BTW ( I tried it as EA and also as script , both added the indicator to only one chart ) :)

regards, 



#import "user32.dll"

int RegisterWindowMessageW(string MessageName);

int PostMessageW(int hwnd,int msg,int wparam,uchar &Name[]);

#import


#define INDICATOR_NAME "CCI_FILTER6C"


int NOS  ;

string suffix="";

string symbol[30];

long chartid[30];


  

 

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+

//+------------------------------------------------------------------+ 


int OnStart()

   {   

       NOS=10;

          suffix="c";

       symbol[0]  = "AUDCAD" + suffix ; 

       symbol[1]  = "AUDCHF" + suffix ; 

       symbol[2]  = "AUDJPY" + suffix;  

       symbol[3]  = "AUDNZD" + suffix;  

       symbol[4]  = "AUDUSD" + suffix ; 

       symbol[5]  = "CADJPY" + suffix ; 

       symbol[6]  = "CHFJPY" + suffix;  

       symbol[7]  = "EURAUD" + suffix ;

       symbol[8]  = "EURCAD" + suffix ; 

       symbol[9]  = "EURCHF" + suffix;  

       

for(int is=0;is<NOS;is++)

     {

      int hWnd=WindowHandle(symbol[is],PERIOD_M30);

      StartCustomIndicator(hWnd,INDICATOR_NAME);

     }    

  return(0);   

}

   

   

   void StartCustomIndicator(int hWnd,string IndicatorName,bool AutomaticallyAcceptDefaults=true)

  {

   uchar name2[];

   StringToCharArray(IndicatorName,name2,0,StringLen(IndicatorName));


   int MessageNumber=RegisterWindowMessageW("MetaTrader4_Internal_Message");

   int r=PostMessageW(hWnd,MessageNumber,15,name2);

   Sleep(100);

  }

      

 
Abdul Manan:
Hi, I'm not able to load 2 indicators from 1 EA using the same code.

looking for this as well - has anyone figured out how to add multiple indicators from the same EA?


i want to add 2 https://docs.mql4.com/indicators/ima from the same EA


thanks

Reason: