MQL4调用ChartOpen触发ERROR 4024不返回图表ID问题求助
问题根因
该4024错误为MT4终端已知的消息队列时序bug:当终端UI线程处于高负载状态(频繁执行图表开/关、模板加载、截图操作时),ChartOpen的创建请求已被投递到UI线程并实际执行,但新图表ID的返回值未及时投递回EA执行上下文,就会返回0并抛出4024内部错误,图表本身已成功创建。
现有临时方案的核心缺陷:
- 依靠图表总数差、枚举顺序取最后一个图表的逻辑不可靠:
ChartFirst()/ChartNext()返回的图表顺序为终端内部维护的列表顺序,与图表打开时间无关;若有其他EA、用户同时操作终端打开图表,会匹配到错误的图表,后续模板应用、截图操作就会触发4024或操作到无关图表。
可靠规避方案
- 打开图表前,先枚举所有已存在的、与目标
symbol+period完全匹配的图表ID,存入临时列表 ChartOpen返回4024时,先等待200ms让终端完成图表初始化,再执行枚举匹配逻辑- 打开操作完成后,再次枚举所有同
symbol+period的图表,找出不在旧列表中的ID,即为刚创建的目标图表,完全规避枚举顺序、其他操作干扰的问题 - 修正数据加载等待逻辑:不再使用错误的
SeriesInfoInteger参数,改为判断目标图表的K线数量大于0,确认数据加载完成后再执行后续操作 - 所有图表操作(应用模板、重绘、关闭)增加短延迟与重试机制:遇到4024错误时等待100ms后重试,最多重试3次
修正后的核心实现代码
// ********************* 修正后的OpenNewChart函数 ********************* long OpenNewChart( const string _symbol, const ENUM_TIMEFRAMES _period, const string _template = "") { // 1. 先收集所有已存在的 同symbol+period 的图表ID long oldMatchedCharts[]; ArrayResize(oldMatchedCharts, 0); long chartTmp = ChartFirst(); while(chartTmp > 0) { if(ChartSymbol(chartTmp) == _symbol && ChartPeriod(chartTmp) == _period) { int len = ArraySize(oldMatchedCharts); ArrayResize(oldMatchedCharts, len + 1); oldMatchedCharts[len] = chartTmp; } chartTmp = ChartNext(chartTmp); } // 2. 原有最大图表数清理逻辑保留 int nCharts = 0; chartTmp = ChartFirst(); while(chartTmp > 0) { chartTmp = ChartNext(chartTmp); nCharts ++; } if(nCharts >= CHARTS_MAX) { Print(" Max Number of Charts reached! Close till 20 charts"); chartTmp = ChartFirst(); for(int i=1; i<20; i++) { chartTmp = ChartNext(chartTmp); } for(int i=0; i<100; i++) { chartTmp = ChartNext(chartTmp); if(chartTmp<0) { break; } if(!ChartClose(chartTmp)) { Print("Chart Close failed! ID: ", chartTmp, " ", GetLastError()); } Sleep(100); } Sleep(100); } // 3. 调用ChartOpen,带重试 ResetLastError(); Sleep(100); long chartID = ChartOpen(_symbol, _period); int err = GetLastError(); // 4. 处理4024错误,匹配新创建的图表 if(chartID == 0 && err == 4024) { Print(" Chart Open returned 4024, waiting for chart initialization..."); Sleep(200); // 等待终端完成图表初始化 // 枚举所有同symbol+period的图表,找出不在旧列表中的ID chartID = -1; chartTmp = ChartFirst(); while(chartTmp > 0) { if(ChartSymbol(chartTmp) == _symbol && ChartPeriod(chartTmp) == _period) { bool isOld = false; for(int i=0; i<ArraySize(oldMatchedCharts); i++) { if(oldMatchedCharts[i] == chartTmp) { isOld = true; break; } } if(!isOld) { chartID = chartTmp; break; } } chartTmp = ChartNext(chartTmp); } } // 其他错误的重试逻辑保留 else if(chartID == 0) { int attempt = 1; while(chartID == 0 && attempt < 4) { Print(attempt, ". Attempt to open a new Chart failed! ", GetLastError()); Sleep(1000); ResetLastError(); chartID = ChartOpen(_symbol, _period); attempt ++; // 重试时遇到4024同样走匹配逻辑 if(chartID == 0 && GetLastError() == 4024) { Sleep(200); chartTmp = ChartFirst(); while(chartTmp > 0) { if(ChartSymbol(chartTmp) == _symbol && ChartPeriod(chartTmp) == _period) { bool isOld = false; for(int i=0; i<ArraySize(oldMatchedCharts); i++) { if(oldMatchedCharts[i] == chartTmp) { isOld = true; break; } } if(!isOld) { chartID = chartTmp; break; } } chartTmp = ChartNext(chartTmp); } if(chartID > 0) break; } } } if(chartID <= 0) { Print(" Chart open failed completely!"); return -1; } // 5. 修正数据加载等待逻辑:确认K线数>0 int wait=100; while(--wait>0) { long bars = ChartGetInteger(chartID, CHART_BARS); if(bars > 0) { Print(" Waited for ", (100-wait)*0.1, " Seconds to load chart data [", _symbol, " (", _period, ")]"); break; } Sleep(100); } // 6. 应用模板加重试 bool res = false; if(_template != "") { int tmplAttempt = 0; while(tmplAttempt < 3) { ResetLastError(); res = ChartApplyTemplate(chartID, _template); if(res) break; int tmplErr = GetLastError(); if(tmplErr == 4024 || tmplErr == 5020) { Sleep(100); tmplAttempt ++; continue; } else break; } if(res) Print("Applied Chart Template ", _template, " successfully"); else Print(" Apply Template to Chart failed, Template: ", _template, " Error: ", GetLastError()); } if(_template == "" || res == false) { if(!ChartSetInteger(chartID,CHART_SHOW_GRID,false)) Print(" Error at ChartSetInteger -> Show Grid (false); ", GetLastError()); if(!ChartSetInteger(chartID,CHART_SHOW_PERIOD_SEP,false)) Print(" Error at ChartSetInteger -> Show Period Sep (false); ", GetLastError()); if(!ChartSetInteger(chartID,CHART_SCALE,3)) Print(" Error at ChartSetInteger -> Chart Scale (3); ", GetLastError()); } Sleep(100); ChartRedraw(chartID); Sleep(200); // 给图表重绘留足够时间,避免截图空白 return chartID; }
内容的提问来源于stack exchange,提问作者Roth99
相关产品推荐
相关产品推荐

