[MT4][mql4][programming] Sample code to control all (plural) charts

You may have created MT4 EA or Indicator and want to operate other than the chart to which it is added at the same time.
In such a case, I will introduce sample code for operating all other charts at the same time.

Also, for the sample code that will be the format for implementing mql4 and the outline, first see the format file and outline of Experts Advisor.

Contents

Sample code for manipulating all (multiple) charts

I will introduce the sample code when operating other charts with one EA or Indicator.

This time, I will introduce the sample code that displays the Symbol (currency pair) name of all charts except the chart to which the program is added.

If you replace this symbol name acquisition process with another process you request, that process will be applied to all charts.
(I will show you what you can do with other charts later.)

sample code

long	id				= ChartNext(0); //ChartID for first chart
long	currentChartid	= ChartID(); //ChartID for current chart
string	symbolStr		= "";

//loop for all chart
for(;;)
{
	if(id != currentChartid)
	{
		symbolStr = ChartSymbol(id);
		Print("ChartSymbol:", symbolStr);
	}
	
	id = ChartNext(id);
	
	if(id < 0) { break; }
}

Explanation of sample code

A description of the sample code.

Get the id of the first chart in the chart displayed in MT4

In the first line, the first chart id of the chart displayed in MT4 is obtained using ChartNext().
You can get the first chart id by specifying 0 as an argument.

Get the current chart id where the program is added

In the second line, the id of the chart to which this sample program is added is obtained using ChartID().
This id is obtained to exclude the current chart from the operation target of processing.
If you want to operate the current chart to which this sample program is added too, the id acquisition process here is not necessary.

Circular loop processing of all charts

From the 6th line to the 17th line is the processing of the loop that goes around all the charts.

Exclude the current chart from the operation target

The processing on the 8th line excludes the chart to which the program is added so that only the “chart other than the chart to which this sample program is added” gets the Symbol name.

if(id != currentChartid)

If you want to operate all charts including charts to which this program is added, delete the if part here.

Operation processing of other charts

The 10th and 11th lines are the operation processing of other charts.

This time, as a sample, the process of acquiring and displaying the Symbol name of another chart is used, so the process of acquiring the Symbol name is described.

symbolStr = ChartSymbol(id);
Print("ChartSymbol:", symbolStr);

If you describe the chart operation process you want to implement here, it will be applied to all charts.
What is done for other charts will be described later.

Get the next chart id

In the 14th line, we get the following chart id.
In this way, the implementation is such that processing is applied to all chart ids by advancing the chart ids in order from the first chart id.

id = ChartNext(id);

Finished processing for all charts

Line 16 checks to see if all charts have been processed.
ChartNext () is designed to return -1 when all the patrols are completed, so the loop is broken at that timing to end the process.

if(id < 0) { break; }

This is the end of the outline explanation of the sample code.

What you can do to control each chart is introduced below.

What you can do with other chart operations

Introducing the processing that can be executed for other charts.

Please use it as a reference when creating the process you want to realize based on the above sample code.
※Please refer to the link for detailed function specifications.

Functiondescription
ChartApplyTemplate()Applies a specific template from a specified file to the chart.
ChartSaveTemplate()Saves current chart settings in a template with a specified name. 
ChartWindowFind()The function returns the number of a subwindow where an indicator is drawn. 
ChartTimePriceToXY()Converts the coordinates of a chart from the time/price representation to the X and Y coordinates.
ChartXYToTimePrice()Converts the X and Y coordinates on a chart to the time and price values.
ChartOpen()Opens a new chart with the specified symbol and period.
ChartClose()Closes the specified chart.
ChartSymbol()Returns the symbol name for the specified chart.
ChartPeriod()Returns the timeframe period of specified chart.
ChartRedraw()This function calls a forced redrawing of a specified chart.
ChartSetDouble()Sets a value for a corresponding property of the specified chart.
ChartSetInteger()Sets a value for a corresponding property of the specified chart.
ChartSetString()Sets a value for a corresponding property of the specified chart.
ChartGetDouble()Returns the value of a corresponding property of the specified chart. 
ChartGetInteger()Returns the value of a corresponding property of the specified chart.
ChartGetString()Returns the value of a corresponding property of the specified chart. 
ChartNavigate()Performs shift of the specified chart by the specified number of bars relative to the specified position in the chart.
ChartIndicatorDelete()Removes an indicator with a specified name from the specified chart window.
ChartIndicatorName()Returns the short name of the indicator by the number in the indicators list on the specified chart window.
ChartIndicatorsTotal()Returns the number of all indicators applied to the specified chart window.
ChartSetSymbolPeriod()Changes the symbol and period of the specified chart.
ChartScreenShot()Saves current chart screen shot as a GIF, PNG or BMP file depending on specified extension.
Chart manipulation function