[{"data":1,"prerenderedAt":738},["ShallowReactive",2],{"\u002Fzh\u002Fguides\u002Fnews-filter-mt5-expert-advisor-built-in-calendar-or-api":3,"\u002Fzh\u002Fguides\u002Fnews-filter-mt5-expert-advisor-built-in-calendar-or-api-surround":724,"\u002Fzh\u002Fguides\u002Fnews-filter-mt5-expert-advisor-built-in-calendar-or-api-langs":734},{"id":4,"title":5,"authors":6,"badge":12,"body":14,"date":712,"description":713,"draft":714,"extension":715,"image":716,"meta":718,"navigation":239,"path":719,"seo":720,"stem":721,"updated":722,"__hash__":723},"posts\u002Fzh\u002F3.guides\u002F42.news-filter-mt5-expert-advisor-built-in-calendar-or-api.md","MT5 新闻过滤器：用内置日历还是 API？",[7],{"name":8,"to":9,"avatar":10},"ShowMyTrades Team","https:\u002F\u002Fshowmytrades.com",{"src":11},"\u002Ficon-512.png",{"label":13},"自动化",{"type":15,"value":16,"toc":703},"minimark",[17,26,29,34,159,163,174,360,367,382,385,418,422,425,433,460,507,522,526,594,597,601,609,613,619,634,647,653,657,683,686,699],[18,19,20,21,25],"p",{},"为 MetaTrader 5 编写 EA（智能交易系统），您多了一个 MT4 用户没有的选择：终端自带",[22,23,24],"strong",{},"财经日历","，MQL5 用几个函数就能读取。不用下载，不用允许 URL，也不用解析任何东西。",[18,27,28],{},"那为什么还要用外部 API？因为内置日历有短板，其中三点对新闻过滤器很关键。本指南先给出原生代码，再给出 API 代码，最后说明怎么选。",[30,31,33],"h2",{"id":32},"️-两种方式对比","⚖️ 两种方式对比",[35,36,37,56],"table",{},[38,39,40],"thead",{},[41,42,43,46,49],"tr",{},[44,45],"th",{},[44,47,48],{},"MQL5 内置日历",[44,50,51],{},[52,53,55],"a",{"href":54},"\u002Fdocs\u002Fapi\u002Feconomic-calendar","ShowMyTrades 日历 API",[57,58,59,74,89,100,111,122,137,148],"tbody",{},[41,60,61,65,68],{},[62,63,64],"td",{},"设置",[62,66,67],{},"无",[62,69,70,71],{},"在“工具 → 选项 → 智能交易系统”（Tools → Options → Expert Advisors）中允许 ",[72,73,9],"code",{},[41,75,76,79,82],{},[62,77,78],{},"数据来源",[62,80,81],{},"MetaQuotes，经由经纪商的服务器提供",[62,83,84,85,88],{},"ShowMyTrades 合并多个公开日历来源，主要数据的发布日期会与发布机构和央行核对（",[72,86,87],{},"confidence: official","）",[41,90,91,94,97],{},[62,92,93],{},"适用范围",[62,95,96],{},"开启了日历的经纪商（大多数都开了，部分白标服务器没有）",[62,98,99],{},"任何经纪商，任何能上网的终端",[41,101,102,105,108],{},[62,103,104],{},"也能用于 MT4",[62,106,107],{},"否",[62,109,110],{},"是，同一个文件",[41,112,113,116,119],{},[62,114,115],{},"可在策略测试器中使用",[62,117,118],{},"是，带历史数据",[62,120,121],{},"否（测试器不能联网）",[41,123,124,127,130],{},[62,125,126],{},"时区",[62,128,129],{},"交易服务器时间，与 UTC 的时差得自己推算",[62,131,132,133,136],{},"UTC（",[72,134,135],{},"ts"," 为 Unix 秒），每次响应还附带服务器当前的 UTC 时间，EA 因此完全不依赖经纪商或 VPS 的时钟",[41,138,139,142,145],{},[62,140,141],{},"数值",[62,143,144],{},"预期值、前值、公布值，含修正",[62,146,147],{},"预期值、前值、公布值",[41,149,150,153,156],{},[62,151,152],{},"费用",[62,154,155],{},"免费",[62,157,158],{},"免费，无需密钥",[30,160,162],{"id":161},"方式-a内置日历","🧱 方式 A：内置日历",[18,164,165,166,169,170,173],{},"下面的函数判断：图表品种的两种货币里，有没有哪一种的",[22,167,168],{},"高影响","数据发布落在暂停窗口内。时间用的是交易服务器时间，这正是 ",[72,171,172],{},"CalendarValueHistory()"," 要求的。",[175,176,182],"pre",{"className":177,"code":178,"filename":179,"language":180,"meta":181,"style":181},"language-cpp shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","\u002F\u002F--- MQL5 内置日历：当图表货币的高影响数据发布\n\u002F\u002F--- 处于当前时间的 [-minutesBefore, +minutesAfter] 范围内时返回 true\nbool NativeNewsTime(const int minutesBefore, const int minutesAfter, string &reason)\n{\n   reason = \"\";\n   datetime now  = TimeTradeServer();\n   datetime from = now - minutesAfter * 60;\n   datetime to   = now + minutesBefore * 60;\n\n   string currencies[2];\n   currencies[0] = SymbolInfoString(_Symbol, SYMBOL_CURRENCY_BASE);\n   currencies[1] = SymbolInfoString(_Symbol, SYMBOL_CURRENCY_PROFIT);\n\n   for(int c = 0; c \u003C 2; c++)\n   {\n      if(c == 1 && currencies[1] == currencies[0]) break;\n      MqlCalendarValue values[];\n      if(CalendarValueHistory(values, from, to, NULL, currencies[c]) \u003C= 0) continue;\n      for(int i = 0; i \u003C ArraySize(values); i++)\n      {\n         MqlCalendarEvent ev;\n         if(!CalendarEventById(values[i].event_id, ev)) continue;\n         if(ev.importance != CALENDAR_IMPORTANCE_HIGH) continue;\n         reason = currencies[c] + \": \" + ev.name;\n         return true;\n      }\n   }\n   return false;\n}\n","NativeNewsFilter.mqh","cpp","",[72,183,184,192,198,204,210,216,222,228,234,241,247,253,259,264,270,276,282,288,294,300,306,312,318,324,330,336,342,348,354],{"__ignoreMap":181},[185,186,189],"span",{"class":187,"line":188},"line",1,[185,190,191],{},"\u002F\u002F--- MQL5 内置日历：当图表货币的高影响数据发布\n",[185,193,195],{"class":187,"line":194},2,[185,196,197],{},"\u002F\u002F--- 处于当前时间的 [-minutesBefore, +minutesAfter] 范围内时返回 true\n",[185,199,201],{"class":187,"line":200},3,[185,202,203],{},"bool NativeNewsTime(const int minutesBefore, const int minutesAfter, string &reason)\n",[185,205,207],{"class":187,"line":206},4,[185,208,209],{},"{\n",[185,211,213],{"class":187,"line":212},5,[185,214,215],{},"   reason = \"\";\n",[185,217,219],{"class":187,"line":218},6,[185,220,221],{},"   datetime now  = TimeTradeServer();\n",[185,223,225],{"class":187,"line":224},7,[185,226,227],{},"   datetime from = now - minutesAfter * 60;\n",[185,229,231],{"class":187,"line":230},8,[185,232,233],{},"   datetime to   = now + minutesBefore * 60;\n",[185,235,237],{"class":187,"line":236},9,[185,238,240],{"emptyLinePlaceholder":239},true,"\n",[185,242,244],{"class":187,"line":243},10,[185,245,246],{},"   string currencies[2];\n",[185,248,250],{"class":187,"line":249},11,[185,251,252],{},"   currencies[0] = SymbolInfoString(_Symbol, SYMBOL_CURRENCY_BASE);\n",[185,254,256],{"class":187,"line":255},12,[185,257,258],{},"   currencies[1] = SymbolInfoString(_Symbol, SYMBOL_CURRENCY_PROFIT);\n",[185,260,262],{"class":187,"line":261},13,[185,263,240],{"emptyLinePlaceholder":239},[185,265,267],{"class":187,"line":266},14,[185,268,269],{},"   for(int c = 0; c \u003C 2; c++)\n",[185,271,273],{"class":187,"line":272},15,[185,274,275],{},"   {\n",[185,277,279],{"class":187,"line":278},16,[185,280,281],{},"      if(c == 1 && currencies[1] == currencies[0]) break;\n",[185,283,285],{"class":187,"line":284},17,[185,286,287],{},"      MqlCalendarValue values[];\n",[185,289,291],{"class":187,"line":290},18,[185,292,293],{},"      if(CalendarValueHistory(values, from, to, NULL, currencies[c]) \u003C= 0) continue;\n",[185,295,297],{"class":187,"line":296},19,[185,298,299],{},"      for(int i = 0; i \u003C ArraySize(values); i++)\n",[185,301,303],{"class":187,"line":302},20,[185,304,305],{},"      {\n",[185,307,309],{"class":187,"line":308},21,[185,310,311],{},"         MqlCalendarEvent ev;\n",[185,313,315],{"class":187,"line":314},22,[185,316,317],{},"         if(!CalendarEventById(values[i].event_id, ev)) continue;\n",[185,319,321],{"class":187,"line":320},23,[185,322,323],{},"         if(ev.importance != CALENDAR_IMPORTANCE_HIGH) continue;\n",[185,325,327],{"class":187,"line":326},24,[185,328,329],{},"         reason = currencies[c] + \": \" + ev.name;\n",[185,331,333],{"class":187,"line":332},25,[185,334,335],{},"         return true;\n",[185,337,339],{"class":187,"line":338},26,[185,340,341],{},"      }\n",[185,343,345],{"class":187,"line":344},27,[185,346,347],{},"   }\n",[185,349,351],{"class":187,"line":350},28,[185,352,353],{},"   return false;\n",[185,355,357],{"class":187,"line":356},29,[185,358,359],{},"}\n",[18,361,362,363,366],{},"在 ",[72,364,365],{},"OnTick()"," 里调用它，和调用其他过滤器没有区别：",[175,368,370],{"className":177,"code":369,"language":180,"meta":181,"style":181},"string news;\nif(NativeNewsTime(30, 30, news)) { Comment(\"新闻过滤器：\", news); return; }\n",[72,371,372,377],{"__ignoreMap":181},[185,373,374],{"class":187,"line":188},[185,375,376],{},"string news;\n",[185,378,379],{"class":187,"line":194},[185,380,381],{},"if(NativeNewsTime(30, 30, news)) { Comment(\"新闻过滤器：\", news); return; }\n",[18,383,384],{},"用它之前，有三点要清楚：",[386,387,388,406,412],"ol",{},[389,390,391,394,395,397,398,401,402,405],"li",{},[22,392,393],{},"它依赖经纪商","。日历经由交易服务器送到终端。如果在您的经纪商那里，",[72,396,172],{}," 返回 ",[72,399,400],{},"-1","、错误码 ",[72,403,404],{},"5402","，说明该服务器没有开启日历，代码再怎么改也没用。",[389,407,408,411],{},[22,409,410],{},"它用的是服务器时间，不是 UTC","。在实时图表上没问题，一旦要和别的数据对照就很麻烦。",[389,413,414,417],{},[22,415,416],{},"它只支持 MT5","。如果您的 EA 也出 MT4 版本，就得再写一套。",[30,419,421],{"id":420},"方式-b日历-api","🌐 方式 B：日历 API",[18,423,424],{},"另一个选择是免费的 HTTP 端点：按您选择的货币和影响级别，返回未来几天的新闻，格式为 CSV 或 JSON：",[175,426,431],{"className":427,"code":429,"language":430,"meta":181},[428],"language-text","https:\u002F\u002Fshowmytrades.com\u002Fapi\u002Fpublic\u002Fcalendar\u002Fv1?currency=EUR,USD&impact=high&days=2&format=csv\n","text",[72,432,429],{"__ignoreMap":181},[18,434,435,436,439,440,444,445,448,449,452,453,456,457,459],{},"读取它的包含文件 ",[72,437,438],{},"SMT_NewsFilter.mqh","，完整代码见 ",[52,441,443],{"href":442},"\u002Fguides\u002Fadd-a-news-filter-to-your-mt4-expert-advisor","MT4 指南","和 ",[52,446,447],{"href":54},"API 参考","。",[22,450,451],{},"它在 MQL5 中无需修改即可编译","：保存到 ",[72,454,455],{},"MQL5\\Include","，在“工具 → 选项 → 智能交易系统”中允许 ",[72,458,9],{},"，然后这样调用：",[175,461,463],{"className":177,"code":462,"language":180,"meta":181,"style":181},"#include \u003CSMT_NewsFilter.mqh>\n\nvoid OnTick()\n{\n   string news;\n   if(SMT_IsNewsTime(news)) { Comment(\"新闻过滤生效中：\", news); return; }\n   Comment(\"\");\n   \u002F\u002F ……您的入场逻辑……\n}\n",[72,464,465,470,474,479,483,488,493,498,503],{"__ignoreMap":181},[185,466,467],{"class":187,"line":188},[185,468,469],{},"#include \u003CSMT_NewsFilter.mqh>\n",[185,471,472],{"class":187,"line":194},[185,473,240],{"emptyLinePlaceholder":239},[185,475,476],{"class":187,"line":200},[185,477,478],{},"void OnTick()\n",[185,480,481],{"class":187,"line":206},[185,482,209],{},[185,484,485],{"class":187,"line":212},[185,486,487],{},"   string news;\n",[185,489,490],{"class":187,"line":218},[185,491,492],{},"   if(SMT_IsNewsTime(news)) { Comment(\"新闻过滤生效中：\", news); return; }\n",[185,494,495],{"class":187,"line":224},[185,496,497],{},"   Comment(\"\");\n",[185,499,500],{"class":187,"line":230},[185,501,502],{},"   \u002F\u002F ……您的入场逻辑……\n",[185,504,505],{"class":187,"line":236},[185,506,359],{},[18,508,509,510,513,514,517,518,521],{},"它每 15 分钟下载一次，用 ",[72,511,512],{},"TimeGMT()"," 按 UTC 比较时间，输入参数（",[72,515,516],{},"SMT_Currencies","、",[72,519,520],{},"SMT_Impact","、事前和事后的分钟数）都放在 EA 的设置对话框里。",[30,523,525],{"id":524},"选哪一个","🎯 选哪一个",[35,527,528,538],{},[38,529,530],{},[41,531,532,535],{},[44,533,534],{},"您的情况",[44,536,537],{},"选择",[57,539,540,550,561,572,586],{},[41,541,542,545],{},[62,543,544],{},"只用 MT5，经纪商提供日历，还要回测过滤器",[62,546,547],{},[22,548,549],{},"内置",[41,551,552,555],{},[62,553,554],{},"同一个 EA 要同时跑 MT4 和 MT5",[62,556,557,560],{},[22,558,559],{},"API","：一个文件，行为一致",[41,562,563,568],{},[62,564,565,566,88],{},"经纪商没有日历（",[72,567,404],{},[62,569,570],{},[22,571,559],{},[41,573,574,577],{},[62,575,576],{},"您还在 cTrader 上跑 cBot",[62,578,579,581,582],{},[22,580,559],{},"：",[52,583,585],{"href":584},"\u002Fguides\u002Fadd-a-news-filter-to-a-ctrader-cbot","cTrader 上用的是同一份数据",[41,587,588,591],{},[62,589,590],{},"想把新闻和交易一起记下来，方便日后复盘",[62,592,593],{},"都行；API 给的是 UTC 时间戳，和您其他日志对得上",[18,595,596],{},"两者也可以同时用：内置日历有响应就用它，没响应就用 API。不过实际上，一个就够了。",[30,598,600],{"id":599},"回测新闻过滤器","🧪 回测新闻过滤器",[18,602,603,604,608],{},"两者之中只有内置日历能在策略测试器里用，因为测试器不能联网。即便如此，也要想清楚新闻过滤器的回测到底证明了什么：测试用的是",[605,606,607],"em",{},"今天","掌握的日历，时间是修正过的，还包含事后才补上的事件。而实盘中的过滤器，只知道当时已经排定的内容。",[30,610,612],{"id":611},"常见问题","❓ 常见问题",[18,614,615,618],{},[22,616,617],{},"MT5 内置日历免费吗？","\n免费。它是终端自带的功能，经由您经纪商的服务器提供，您这边不需要开启任何设置。",[18,620,621,627,628,581,631,633],{},[22,622,623,624,626],{},"为什么在我的经纪商那里，",[72,625,172],{}," 什么都不返回？","\n有些服务器（往往是白标服务器）不转发日历。查一下 ",[72,629,630],{},"GetLastError()",[72,632,404],{}," 表示日历不可用。这种情况就用 API。",[18,635,636,639,640,643,644,448],{},[22,637,638],{},"能按另一个品种的货币来过滤吗？","\n可以。直接传入想要的货币代码，不必从 ",[72,641,642],{},"_Symbol"," 读取。API 版本以字符串参数的形式接收：",[72,645,646],{},"EUR,USD,GBP",[18,648,649,652],{},[22,650,651],{},"API 需要账户吗？","\n不需要。不用密钥，不用登录。限制是每个 IP 地址每分钟 600 次请求；每 15 分钟请求一次的话，开再多图表也远远用不完。",[30,654,656],{"id":655},"相关内容","🔗 相关内容",[658,659,660,666,672,677],"ul",{},[389,661,662,665],{},[52,663,664],{"href":442},"如何为您的 MT4 EA 添加新闻过滤器","：完整的包含文件",[389,667,668,671],{},[52,669,670],{"href":54},"财经日历 API 参考","：参数、格式、限制",[389,673,674],{},[52,675,676],{"href":584},"cTrader cBot 的新闻过滤器",[389,678,679],{},[52,680,682],{"href":681},"\u002Fglossary\u002Fwhat-is-an-expert-advisor","什么是 EA（智能交易系统）",[684,685],"hr",{},[18,687,688,689,693,694,698],{},"不管选哪种日历，效果都会体现在业绩记录上：在行情突变时开的单更少，",[52,690,692],{"href":691},"\u002Fglossary\u002Fwhat-is-drawdown","回撤","也更平稳。",[52,695,697],{"href":696},"\u002Fsignup","免费追踪您的 MT5 账户","，在图表上亲眼看到变化。",[700,701,702],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":181,"searchDepth":194,"depth":194,"links":704},[705,706,707,708,709,710,711],{"id":32,"depth":194,"text":33},{"id":161,"depth":194,"text":162},{"id":420,"depth":194,"text":421},{"id":524,"depth":194,"text":525},{"id":599,"depth":194,"text":600},{"id":611,"depth":194,"text":612},{"id":655,"depth":194,"text":656},"2026-10-17T00:00:00.000Z","MetaTrader 5 自带财经日历，EA 无需下载就能读取。什么时候够用、什么时候不够，以及怎样改用免费的日历 API，两种方式都附 MQL5 代码。",false,"md",{"src":717},"\u002Fimages\u002Fblog\u002Fnews-filter-mt5-expert-advisor-built-in-calendar-or-api-hero.svg",{},"\u002Fzh\u002Fguides\u002Fnews-filter-mt5-expert-advisor-built-in-calendar-or-api",{"title":5,"description":713},"zh\u002F3.guides\u002F42.news-filter-mt5-expert-advisor-built-in-calendar-or-api",null,"FKS-2ytVZBeJ4GtO_phlsbsNiCbylGrsU9tiWtfJ_gs",[725,729],{"title":664,"path":726,"stem":727,"description":728,"children":-1},"\u002Fzh\u002Fguides\u002Fadd-a-news-filter-to-your-mt4-expert-advisor","zh\u002F3.guides\u002F41.add-a-news-filter-to-your-mt4-expert-advisor","MetaTrader 4 的 EA 没有财经日历可用。本指南教您十分钟补上：一个免费的财经日历 API、一个包含文件，每次入场前调用一个函数即可。附完整 MQL4 代码，无需 API 密钥。",{"title":730,"path":731,"stem":732,"description":733,"children":-1},"如何为 cTrader cBot 添加新闻过滤器（含云端）","\u002Fzh\u002Fguides\u002Fadd-a-news-filter-to-a-ctrader-cbot","zh\u002F3.guides\u002F43.add-a-news-filter-to-a-ctrader-cbot","cTrader 没有给 cBot 用的财经日历 API，cTrader Cloud 还会拦截 HTTP 请求。这里给出一个桌面端和云端都能用的新闻过滤器：通过 WebSocket 获取免费财经日历，附完整 C# 代码。",[735,737],{"path":736},"\u002Fguides\u002Fnews-filter-mt5-expert-advisor-built-in-calendar-or-api",{"path":719},1793898039378]