要先打开一个窗口。
模板引用
//--- 应用模板示例,位于\MQL5\Files
// 如果手动保存的模板是默认放 MQL5\Profiles\Templates
if(FileIsExist("my_template.tpl")) {
Print("The file my_template.tpl found in \Files'");
//--- 应用模板
if(ChartApplyTemplate(0,"\\Files\\my_template.tpl")) {
Print("The template 'my_template.tpl' applied successfully");
//--- 重绘图表
ChartRedraw();
} else
Print("Failed to apply 'my_template.tpl', error code ",GetLastError());
} else {
Print("File 'my_template.tpl' not found in "
+TerminalInfoString(TERMINAL_PATH)+"\\MQL5\\Files");
}
打开指定的货币对,周期,设置模板
注意:ChartRedraw 通常更改物件属性之后使用该函数。
void OnStart() {
long chartOpenID=ChartOpen("GBPUSD",PERIOD_H4);
Print("打开图标的id"+ chartOpenID);
//--- 应用模板示例,位于\MQL5\Files
// 如果手动保存的模板是默认放 MQL5\Profiles\Templates
if(FileIsExist("my_template.tpl")) {
Print("The file my_template.tpl found in \Files'");
//--- 应用模板
if(ChartApplyTemplate(chartOpenID,"\\Files\\my_template.tpl")) {
Print("The template 'my_template.tpl' applied successfully");
//--- 重绘图表
ChartRedraw();
} else
Print("Failed to apply 'my_template.tpl', error code ",GetLastError());
} else {
Print("File 'my_template.tpl' not found in "
+TerminalInfoString(TERMINAL_PATH)+"\\MQL5\\Files");
}
}
遍历图标窗口id
void OnStart() {
//--- 用于图表 ID的变量
long currChart,prevChart=ChartFirst();
int i=0,limit=100;
Print("ChartFirst =",ChartSymbol(prevChart)," ID =",prevChart);
while(i<limit) { // 不允许超过100个打开的窗口
currChart=ChartNext(prevChart); // 通过使用之前图表ID获得新图表ID
if(currChart<0) {
break; // 到达了图表列表末端
}
Print(i,ChartSymbol(currChart)," ID =",currChart);
prevChart=currChart;// 为ChartNext()保存当前图表ID
i++;// 不要忘记增加计数器
}
}
运行效果:
把指定的图表关闭
long fistId=ChartFirst();
for(int i=0; i<100; i++) {
if(ChartSymbol(fistId)=="GBPUSD") {
ChartClose(fistId);
}
if(fistId<0) {
break; // 到达了图表列表末端
}
long nextId= ChartNext(fistId);
fistId=nextId;
}