Opening and closing charts via scripts

 

Hi,

i have a script that can identify price action bars to give probable trade signals. what i wanted to do was once i run this script, i want to close all open charts and then open all charts with specified timeframe that were signaled by my scripts.

is there a way to program closing of all open charts and then open some specific one?

Thanks.

Kya

 
You can't close all open charts as the script/EA has to have a chart to run on.
 
Easiest way is for the EA (not a script) to look at one pair (the one on its chart) You can then have it change the time frame. Send the command and return and wait for a deinit/init cycle
// https://www.mql5.com/en/forum/132241
#define MT4_WMCMD_PERIOD_D1       33134
#define MT4_WMCMD_PERIOD_H1       33135
#define MT4_WMCMD_PERIOD_H4       33136
#define MT4_WMCMD_PERIOD_M1       33137
#define MT4_WMCMD_PERIOD_M5       33138
#define MT4_WMCMD_PERIOD_M15      33139
#define MT4_WMCMD_PERIOD_M30      33140
#define MT4_WMCMD_PERIOD_W1       33141

// https://www.mql5.com/en/forum/128554
// Each chart consists of two windows (i.e. two hWnds): the drawing area (WH,)
// and a container (GA.) There is then a standard MDI container which holds all
// the chart windows (GA1.) And that sits inside the main MT4 window (GA2.)

#include <WinUser32.mqh>
#import "user32.dll"
    int GetAncestor(int, int);
    int GetParent(int hWnd);
    int SendMessageA(int hWnd, int Msg, int wParam, int lParam);
#import
#define WM_MDIACTIVATE 0x222    // https://www.mql5.com/en/forum/128744
int Window.activate(int hwnd) {
   int p = GetParent(hwnd);
   SendMessageA(GetParent(p), WM_MDIACTIVATE, p, 0);
}
if (!IsTesting() IsDllsAllowed() && 
    !IsStopped()){                          // https://www.mql5.com/en/forum/126618 WH-DL
    int hwnd = WindowHandle( Symbol(), Period() );
    Window.activate(hwnd);
    int HTerminal = GetAncestor(hwnd),2);
    PostMessageA(HTerminal, WM_COMMAND , MT4_WMCMD_PERIOD_H4, 0);
}
Not compiled, not tested.
It's also possible to How to programmatically maximize a chart window - MQL4 forum
 
whroeder1:
Easiest way is for the EA (not a script) to look at one pair (the one on its chart) You can then have it change the time frame. Send the command and return and wait for a deinit/init cycle
Not compiled, not tested.
It's also possible to How to programmatically maximize a chart window - MQL4 forum

Although old, I must reply with a better answer than this and fix the answer that was given.

I will start with @whroeder1's answer, while it is a good one, there is a much easier way to change the current chart's timeframe:

ChartSetSymbolPeriod(0, Symbol(), period);  //chart_id, symbol name, period desired

This can change Symbol name and period for any chart on your MT4 platform, given the correct Chart ID, symbol desired and period.

See the following link for further explanation:   https://docs.mql4.com/chart_operations/chartsetsymbolperiod,    https://www.mql5.com/en/docs/constants/chartconstants/enum_timeframes


Now, to answer the primary question given by kya, this is the way to close all open charts, not including the chart the Script/EA runs on:

   //Closing all chart which are not the current chart
   long currChart,prevChart=ChartFirst(); 
   int i=0,limit=100; 
   while(i<limit)// We have certainly not more than 100 open charts 
     { 
      currChart=ChartNext(prevChart); // Get the new chart ID by using the previous chart ID 
      if(currChart<0) break;          // Have reached the end of the chart list 
      if(prevChart!=ChartID()) ChartClose(prevChart); // Close the chart which is not current chart
      prevChart=currChart;// let's save the current chart ID for the ChartNext() 
      i++;// Do not forget to increase the counter 
     }

  //Opening new chart
  //This is an example to open 1 new chart, you can widen it to open as many as you wish
  string my_symbol="EURUSD";
  int    my_period=PERIOD_H1;
  long chart_id = ChartOpen( my_symbol, my_period );
  
  //load a saved template onto the new chart
  string my_template="Default.tpl"
  ChartApplyTemplate(chart_id,my_template));
ChartSetSymbolPeriod - Chart Operations - MQL4 Reference
ChartSetSymbolPeriod - Chart Operations - MQL4 Reference
  • docs.mql4.com
Changes the symbol and period of the specified chart. The function is asynchronous, i.e. it sends the command and does not wait for its execution completion. The command is added to chart message queue and executed only after all previous commands have been...
Reason: