How to Open Existing chart with mouse click

 

Hi, I'm hoping someone can me please?

I have been writing in mql4 a dashboard for my trading strategy, I have about 20 pairs open as well, and the dashboard provides a central place to report various information. I have coded a button (I'm using rectangle label) and passing the chart ID by a click mouse event on the button. (There is a one character suffix and so the chart ID is determined by StringSubstr(sparam,1); The code works, in that it will move the respective chart to the specified position but the current chart is still loaded, BUT I want to do is for this function to automatically switch to this new chart. I have searched but can't find any help on how to do this simple operation. I would be really grateful if someone could advise how to programmatically move to an existing chart.

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
   if (id==CHARTEVENT_OBJECT_CLICK)
   {
      ObjectSetInteger(0,sparam,OBJPROP_SELECTED, false );
      ObjectSetInteger(0,sparam,OBJPROP_STATE, false );

      long chart_number = (long)StringSubstr(sparam,1);

      ChartNavigate(chart_number,CHART_END,0);
   }
}
 

Have you tried:

ChartSetInteger(chart_number,CHART_BRING_TO_TOP,0,true);

(untried)

Edit: tested it, doesn't work. You'll probably have to go user32.dll as Roberto suggests below. Something along the lines of:

#include <WinUser32.mqh>
int handle = (int)ChartGetInteger(chartID, CHART_WINDOW_HANDLE);
int parent = GetParent(handle);
SendMessageW(GetParent(parent), WM_MDIMAXIMIZE, parent, 0);
 
elginsword:

Hi, I'm hoping someone can me please?

I have been writing in mql4 a dashboard for my trading strategy, I have about 20 pairs open as well, and the dashboard provides a central place to report various information. I have coded a button (I'm using rectangle label) and passing the chart ID by a click mouse event on the button. (There is a one character suffix and so the chart ID is determined by StringSubstr(sparam,1); The code works, in that it will move the respective chart to the specified position but the current chart is still loaded, BUT I want to do is for this function to automatically switch to this new chart. I have searched but can't find any help on how to do this simple operation. I would be really grateful if someone could advise how to programmatically move to an existing chart.


Maybe you should use #include WinUser32.mqh, and find the command you want on WinUser32 Get / Set Active Window.
 

Thanks Guys, that really helped, and now I've got this working as follows for anyone else looking for a solution to open an existing chart.

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
   if (id==CHARTEVENT_OBJECT_CLICK)
   {
     
      //the chart ID gets passed to the function because the clicked button has the chart ID prefaced by one character
      long chart_number = (long)StringSubstr(sparam,1);      
      long chartwindow = -1;
        
      if (!ChartGetInteger(chart_number,CHART_WINDOW_HANDLE,0,chartwindow))
      {
         Print ("Couldn't Get Chart Window");
      }
      int parent = GetParent(chartwindow);
      SendMessageW(GetParent(parent), WM_MDIMAXIMIZE, parent, 0);
   }
}
 

Great, glad you got it working. 

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
   if (id==CHARTEVENT_OBJECT_CLICK)
   {
    
      //the chart ID gets passed to the function because the clicked button has the chart ID prefaced by one character
      long chart_number = (long)StringSubstr(sparam,1);      
      long chartwindow = -1;
        
      if (!ChartGetInteger(chart_number,CHART_WINDOW_HANDLE,0,chartwindow))
      {
         Print ("Couldn't Get Chart Window");
      }
      int parent = GetParent(chartwindow);
      SendMessageW(GetParent(parent), WM_MDIMAXIMIZE, parent, 0);
   }
}

Just for your own interest, ChartGetInteger() returns a long (not a bool) so you are printing a message if the window handle is 0 (false). You don't store the returned value anywhere so I imagine your code will work the same with this section removed entirely.

Out of interest, do you have this at the top of your code? I don't think you do

#property strict

 If not, add it in and re-compile. You'll almost certainly have some warnings and errors to work through.

 
honest_knave:

Great, glad you got it working. 

void OnChartEvent(const int id, const long &lparam, const double &dparam, const string &sparam) {
   if (id==CHARTEVENT_OBJECT_CLICK)
   {
    
      //the chart ID gets passed to the function because the clicked button has the chart ID prefaced by one character
      long chart_number = (long)StringSubstr(sparam,1);      
      long chartwindow = -1;
        
      if (!ChartGetInteger(chart_number,CHART_WINDOW_HANDLE,0,chartwindow))
      {
         Print ("Couldn't Get Chart Window");
      }
      int parent = GetParent(chartwindow);
      SendMessageW(GetParent(parent), WM_MDIMAXIMIZE, parent, 0);
   }
}

Just for your own interest, ChartGetInteger() returns a long (not a bool) so you are printing a message if the window handle is 0 (false). You don't store the returned value anywhere so I imagine your code will work the same with this section removed entirely.

Out of interest, do you have this at the top of your code? I don't think you do

#property strict

 If not, add it in and re-compile. You'll almost certainly have some warnings and errors to work through.

Thanks Honest_Knave, I understood your point which I agree with. The reason I coded it this way was from the MQL4 docs that gives the following, so I just adapted it. Hope that's okay, it seems to be working fine. I did have the strict property already included, but thanks for pointing that out. So pleased to get this sorted it had me stuck for a while. Cheers

//+------------------------------------------------------------------+
//| The function gets the chart handle                               |
//+------------------------------------------------------------------+
int ChartWindowsHandle(const long chart_ID=0)
  {
//--- prepare the variable to get the property value
   long result=-1;
//--- reset the error value
   ResetLastError();
//--- receive the property value
   if(!ChartGetInteger(chart_ID,CHART_WINDOW_HANDLE,0,result))
     {
      //--- display the error message in Experts journal
      Print(__FUNCTION__+", Error Code = ",GetLastError());
     }
//--- return the value of the chart property
   return((int)result);
  }
 
elginsword:

Thanks Honest_Knave, I understood your point which I agree with. The reason I coded it this way was from the MQL4 docs that gives the following, so I just adapted it. Hope that's okay, it seems to be working fine. 

You know what, ignore what I said! It was the second variant of the function and is actually correct. That also explains why I thought you didn't have #property strict

My bad! 

Reason: