在MetaTrader5中创建交互应用来展现RSS订阅(基础篇)
📘

在MetaTrader5中创建交互应用来展现RSS订阅(基础篇)

第 1/3 篇

用 MT5 交互应用拉取 RSS 订阅源

想在 MT5 客户端里直接看外部资讯流,不必切到浏览器。借助 MQL5 的 WinAPI 调用,EA 可以发起 HTTP 请求并把 RSS 返回的 XML 解析后画到图表上的 OBJ_EDIT 或自定义面板里。 这类应用本质是一个常驻 EA:在 OnTick 或定时器里周期性拉取订阅地址,把标题、时间、链接写进文本框。2015 年 9 月发布的示例里,基础框架在普通 VPS 上约每 30 秒刷新一次,CPU 占用可忽略。 外汇与贵金属市场受突发新闻影响大,这类 RSS 面板只作信息聚合,不构成任何方向判断;行情瞬动,杠杆品种高风险,接入外部源也需防解析失败导致的卡顿。

◍ RSS阅读器工程的模块切分

这套 MT5 下的 RSS 阅读器不是单文件堆出来的,而是按职责拆成了界面、解析、事件三层。先看骨架:用户看到的是独立控件界面,背后由 XML 解析器把 RSS 喂给 EA,EA 再调度 CRssReader 类做具体读写。 代码层面被切得很细——LoadDocument() 负责把远程或本地文档载进内存树,ItemNodesTotal() 数清条目节点,FreeDocumentTree() 释放避免泄漏;提取层有 getChannelTitle() 和 getTitle() 直取频道与单条标题。 文本清洗交给 FormatString()、removeTags()、removeSpecialCharacters() 和 tagPosition() 四个方法串行处理,保证 ListView 里不出现乱码标签。事件侧 OnChangeListView()、OnObjectEdit()、OnClickButton1/2() 把控件交互接回逻辑层,整个工程在 2.11 节 EA 主体里拼装完成。 外汇与贵金属行情资讯通过此类工具拉取时,需注意外部 RSS 源中断或延迟可能引发误判,属高风险辅助手段,仅作盘前信息过滤用。

「从 MQL4 脚本到 MT5 互动 RSS 应用」

早前在 MQL4 环境里,已有脚本借助简易库在终端内直接解析 HTML 并呈现 RSS 订阅源,属于单次读取、静态展示的雏形。 MetaTrader 5 与 MQL5 登场后,语言层面的对象化和标准类库让「互动式」RSS 应用有了落地可能——不再是跑完就结束的脚本,而是可响应、可刷新的组件。 本文这一节只交代起点:用 MQL5 标准类库,加社区会员写的辅助工具,就能把 RSS 内容做成终端内的可读模块。想验证可行性,开 MT5 建个空 EA,把标准库里 HTTP 请求相关类先调出来即可。

RSS文档的骨架与必填节点

要在 MT5 里写资讯抓取或舆情监控类 EA,先得吃透 RSS 这个 XML 子类的结构。它最外层是 <rss> 容器,其下直接挂 <channel> 作为频道根节点;channel 里既描述站点本身,也承载若干条 <item> 内容节点。 所有合法 RSS 文档都必须具备四个 channel 级子标签:title(站点名)、link(站点 URL)、description(简介)、以及至少 1 个 item。缺任一标签,解析器会直接判非 RSS。 每个 item 内部也强制含 title、link、description、date(发布时间)四标签。下面是一份真实结构的完整样例,注意它含 3 个 item、pubDate 时间格式为 RFC822(如 Wed, 07 Feb 2007 14:20:24 GMT)。 读这段代码时重点看层级缩进:rss→channel→(title/link/description/pubDate/item*),item 再嵌套自身四标签。MT5 的 XML 解析函数就是按这个路径做 SelectNodes 的。

MQL5 / C++
<rss version="class="num">2.0">
  <channel>
    <title>Xul.fr: Tutorials and Applications of the Web class="num">2.0</title>
    <link>http:class=class="str">"cmt">//www.xul.fr/</link>
    <description>Ajax, JavaScript, XUL, RSS, PHP and all technologies of the Web class="num">2.0. Building a CMS, tutorial and application.</description>
    <pubDate>Wed, class="num">07 Feb class="num">2007 class="num">14:class="num">20:class="num">24 GMT</pubDate>
    <item>
      <title>News on interfaces of the Web in class="num">2010</title>
      <link>http:class=class="str">"cmt">//www.xul.fr/en/class="num">2010.php</link>
      <description>Steve Jobs explains why iPad does not support Adobe Flash:&lt;em&gt;At Adobe they are lazy. They have the potential to make interesting things, but they refuse to do so. Apple does not support Flash because it is too buggy. Each time a Mac crashes, most often it is because of Flash. Nobody will use Flash. The world is moving to &lt;a href="http:class=class="str">"cmt">//www.xul.fr/en/html5/" target="_parent"&gt;HTML class="num">5&lt;/a&gt;</description>
      <pubDate>Sat, class="num">11 Dec class="num">10 class="num">09:class="num">41:class="num">06 +class="num">0100</pubDate>
    </item>
    <item>
      <title>Textured Border in CSS</title>
      <link>http:class=class="str">"cmt">//www.xul.fr/en/css/textured-border.php</link>
      <description> The border attribute of the style sheets can vary in class="type">class="kw">color and width, but it was not expected to give it a texture. However, only a CSS rule is required to add this graphic effect... The principle is to assign a texture to the whole &lt;em&gt;fieldset&lt;/em&gt; and insert into it another &lt;em&gt;fieldset&lt;/em&gt; (for rounded edges) or a &lt;em&gt;div&lt;/em&gt;, whose background is the same as that of the page</description>
      <pubDate>Wed, class="num">29 Jul class="num">09 class="num">15:class="num">56:class="num">54 class="num">0200</pubDate>
    </item>
    <item>
      <title>Create an RSS feed from SQL, example with Wordpress</title>
      <link>http:class=class="str">"cmt">//www.xul.fr/feed/rss-sql-wordpress.html</link>
      <description>Articles contain at least the following items: And possibly, author&class="macro">#x27;s name, or an image. This produces the following table: The returned value is true if the database is found, class="kw">false otherwise. It remains to retrieve the data from the array</description>
    </item>
  </channel>
</rss>

◍ RSS 条目里的浏览器与标准碎片

这段原始数据是一组 RSS item 节点,记录了 2009 年若干 Web 技术条目的发布时间与描述,和外汇交易无直接关系,但结构本身值得盯盘工具开发者留意。 其中 Firefox 3.5 条目(pubDate: Wed, 24 Jun 09 15:18:47 +0200)提到原生识别 JSON 格式,可避开不安全的 eval(),也省掉更慢的第三方库。对写 MT5 外接数据桥的人来说,原生 JSON 解析思路同样能减少 EA 里的字符串处理开销。 另有 HTML 5 争议条目指出规范制定高度依赖单人决策,DOM 教程条目(pubDate: Wed, 06 May 2009 18:30:11 GMT)则说明文档对象模型用于访问 XML/HTML 每个元素。若你用 MT5 抓网页公告,理解 DOM 结构才能稳定提取 pubDate 与 description 字段。

「收尾的标签闭合」

这段 MQL5 代码片段只做一件事:把之前打开的 <channel> 与 <rss> 两个根节点依次闭合。 在生成 RSS 类输出文件时,若只写了头部 opening tag 却漏掉这里的收尾,MT5 的 FileWrite 流程虽不报错,但产出的 xml 在第三方阅读器里会解析失败。 开 MT5 跑一段自定义脚本写 RSS,最后务必确认这两个闭合标签已落地,否则文件就是半截不可用状态。

MQL5 / C++
</channel>
</rss>

常见问题

可以写一个交互应用拉取RSS订阅源,把新闻列表嵌在软件面板里,开盘时顺手扫一眼就行。
按数据获取、文档解析、条目渲染、交互控制切分模块,各自独立好维护,别全堆一个文件。
小布可以接入你的订阅源做AIGC摘要,把影响行情的条目标红推给你,省去手动翻。
channel下的title、link、description和item是骨架必填项,缺了多数解析器会直接失败。
按标准剥掉非法碎片只留纯文本或安全标签,结尾标签记得闭合,否则界面会错版。