从新手到专家:非农就业数据公布后斐波那契回撤交易策略的实现
目录
引言
错过重大经济数据(如非农就业数据NFP)发布的那一刻,往往会引发错失机会的恐惧(FOMO)。这通常源于交易者急于追赶行情 —— 即便这波行情已经结束,也不例外。在大多数情况下,最初的价格急变持续不到一分钟,价格会沿市场倾向的方向波动许多标准点(pips)。在这种情况下,过晚入场风险极高,因为这波急剧波动已经结束,价格可能迅速反转,造成重大损失。
然而,专业交易者不会追逐这些突发行情。相反,他们要么提前做好准备,以便更早、更安全地入场;要么耐心等待市场回撤,在精心计算的入场价位寻找新机会。他们的优势在于理解斐波那契回撤原理,并能严守纪律,耐心等待合适的条件形成。
在本次讨论中,我们将以算法方式应用斐波那契原理,应对新闻引发突发行情后的交易挑战。为了打好基础,接下来将简要介绍斐波那契回撤,供初次接触该概念的读者参考,之后我们将进入策略的设计与实现阶段。
斐波那契回撤简介
斐波那契数列由意大利数学家莱昂纳多·博纳奇(通常称为斐波那契)于数个世纪前首创,如今已成为现代技术分析中最具影响力的工具之一。在数学中,这个数列很简单:每个数都是前两个数的和(1, 1, 2, 3, 5, 8, 13, …)。然而,它在自然界、建筑乃至人类行为中的表现,却让几代学者着迷不已。
在金融市场中,斐波那契数列通过斐波那契回撤位得到应用,交易者借此识别市场调整期间可能发生反转或延续原有趋势的区域。最常用的回撤比率为23.6%、38.2%、50%、61.8%和78.6%,这些比率均源自斐波那契数列内部的关系。这些回撤位是交易者的心理参考点,交易者预期价格会在这些位置暂时停顿、反弹或反转。
回撤背后的原理是,市场走势从来不会呈直线。在经历一波强劲的上涨或下跌之后,价格通常会先回撤,再延续原有趋势。斐波那契回撤提供了一个衡量此类回撤可能幅度的框架。例如:
- 38.2%的回撤通常意味着强劲趋势中的浅幅回撤。
- 50%的回撤位(尽管50%并非真正的斐波那契数)被广泛用作回撤至原有走势中点的参考位。
- 61.8%的回撤位被称为"黄金比例",被视为最关键的价位,重大反转往往在此发生。
在交易实践中,斐波那契回撤位很少单独使用。当与价格行为、支撑位和阻力位、移动平均线,或事件驱动的市场行为(例如重大新闻引发突发行情后不久出现的回撤)结合使用时,这些回撤位会更有效。
在此策略中,我们重点关注NFP发布后的交易,利用斐波那契回撤过滤初始脉冲带来的噪音,引导交易者找到有明确结构依据、成功概率更高的入场点。交易者不再屈服于错失机会的恐惧(FOMO),而是学会"等待回撤",并在更具持续性的价位入场。MQL5 社区平台上有丰富的斐波那契相关资料,可供进一步阅读。
MetaTrader 5 交易终端中的工具可用性
在 MetaTrader 5 的默认界面中,图表工具箱位于显眼且便于操作的位置 —— 最常用的分析对象已预先加载,可直接拖拽至图表上。绘图工具(例如斐波那契回撤线)具有可拖动的顶点,您可以将其放置在波段转折点上,以直观地显示价格水平。交易者利用这些图形对象,既可以回顾价格过去在各价位的反应,也可以预测未来回撤时可能出现反应的区域。
实用建议:
- 将该工具的两个端点分别拖至清晰的波段高点和波段低点(或反向放置),以有意义的参考点为锚点绘制回撤线。
- 使用对象的顶点控制柄微调位置,根据您偏好的规则,使各价位与K线收盘价或影线对齐。
- 保存常用模板或配置文件,以便在所有图表中都能立即调用您偏好的斐波那契设置和图表布局。
请参见下图1,图中显示了图表上绘制的斐波那契回撤线。

图1. 在 MetaTrader 5 交易终端中绘制斐波那契回撤线
以2025年9月5日NFP公布为案例的概念研究:
对2025年9月5日NFP公布后的价格行为进行人工复盘,其结果印证了一点:斐波那契回撤位能够为数据发布后的入场提供有效指引。附图使用斐波那契回撤线,展示了价格在初始脉冲后如何测试特定价位并在这些价位受阻或获得支撑,从而帮助区分高概率的再次入场区域与市场噪音。
这些观察结果使我们能够以回撤区间和最近的参考价格(急变前基准价和急涨或急跌形成的极值)为依据,制定更有纪律性的下单和风险管理规则。为了更清晰地说明这一点,我分析了三个涉及美元的货币对的图表,因为在美国重要经济数据发布前后,美元货币对往往会出现更强劲、更一致的走势。
本研究参考了截至撰写本文时最近公布的几次NFP。在案例研究中,我们分析了三个以美元为中心的货币对 —— GBPUSD(英镑兑美元)、USDJPY(美元兑日元)和EURUSD(欧元兑美元) —— 然后将这一思路转化为算法。以下图表截图展示了该方法在各个示例中的表现。

图2. EURUSD,M5,NFP公布后的价格行为
如图2(EURUSD,M5)所示,价格明显遵循回撤规律。标记A和B分别表示急变前基准价和NFP公布后急涨或急跌形成的极值。针对数据公布后的第一根M5 K线,从A点(基准价)到B点(价格急变的高点)绘制斐波那契回撤线。随后,价格回撤并测试斐波那契区间,这些斐波那契回撤位明显发挥了作用 —— 在本例中,价格触及50%回撤位,并在该点位出现明显反应。价格回撤并测试61.8%斐波那契回撤位,甚至略微越过了该区间,但并未完全回撤至基准价(100%)。在略微越过该区间后,上涨动能恢复,市场创下新高,超过了最初的急涨或急跌形成的极值,确认了看涨趋势的延续。

图3. GBPUSD,M5,NFP公布后的价格行为
EURUSD与GBPUSD高度相关,GBPUSD在数据公布后也出现了类似但并不完全相同的看涨反应。图3(上图)展示了GBPUSD的走势,并叠加了与图2相同的斐波那契回撤线,说明该货币对的价格如何对消息作出反应,也进一步印证了不同货币对回撤行为的一致性。

图4. USDJPY,M5,NFP公布后的价格行为
图4(USDJPY)显示,NFP公布后,市场出现了明显的看跌反应。在最初向下的价格急变(A点→B点)之后,价格回撤至约50%的斐波那契回撤位,随后恢复跌势并创下新低。综合来看,这三张图表(EURUSD、GBPUSD和USDJPY)表明,即使不同货币对的走势方向不同,在消息引发突发行情后,价格回撤至斐波那契区域的概率仍呈现一致性。
启示与总体思路(编码前)
结合这三个例子(EURUSD、GBPUSD和USDJPY),我们可以制定一个简洁的总体方案,将这一理念转化为算法:
定义- P_base —— 急变前基准价(价格快速变动的起始价)。
- P_spike —— 急涨或急跌形成的极值(价格急涨的最高价,价格急跌的最低价)。
- 价格急变方向 —— 如果 P_spike > P_base,则为看涨(向上急变);如果 P_spike < P_base,则为看跌(向下急变)。
1. 事件发生后的第一根参考K线收盘后,在 P_base 和 P_spike 之间绘制斐波那契回撤位。2. 等待价格有序回撤至目标区域(例如 38.2%–61.8%),并出现您选定的确认信号(K线收盘确认、价格测试价位后被拒绝,并留下影线、Tick成交量恢复正常等)。3. 对于价格快速上涨(价格先急剧上涨,随后回撤):
- 入场:当价格回撤至选定的斐波那契回撤区域且出现确认信号时做多。
- 止盈(TP):主要止盈位设在 P_spike(急涨或急跌形成的极值)。次级或扩展止盈(TP)目标可采用斐波那契扩展或 ATR 倍数来确定。
- 止损(SL):将止损位设在 P_base(价格急变的起点,即快速变动的起始价)处或略低于该价位。留出少量缓冲(例如,点差 + X 标准点),以避免市场微小噪声触发止损。
- 入场:当价格回撤至选定的斐波那契回撤区域且出现确认信号时做空。
- 止盈(TP):主要止盈位设在 P_spike(急跌的最低价)。
- 止损(SL):将止损位设在P_base(快速下跌走势的起点)处或略高于该点,并留出少量缓冲空间。
- 将 P_spike 用作止盈(TP),可使交易目标与最近可确认的极值保持一致 —— 我们试图捕捉对价格急涨或急跌所形成极值的再次测试(即"重返急变极值")。
- 将 P_base 用作止损(SL),就是将快速行情的起点作为交易逻辑的失效点:如果价格回撤并越过起点,价格急变后重新入场的逻辑便不再成立。
实际调整与风险控制
我们将在止损(SL)价位外留出一个较小的固定缓冲(例如几个标准点或点差 × 1.5),并根据账户风险百分比 × 止损价与入场价之间的距离计算交易手数,以将绝对风险控制在较低水平(目标为 0.5%)。如果 P_base 距离过远,我们将提供一个距离更近的替代止损(SL)价位(下一个斐波那契回撤位或 ATR(5m) × 系数),供用户通过参数选择。该智能交易系统(EA)将支持在 P_spike 处部分平仓、对剩余仓位实施移动止损、在 M 分钟后自动撤销失效的限价单,并提供相关货币对确认功能的可选开关。

图5. NFP发布后斐波那契重新入场策略示意图
图5(上图)是对该策略的概念性可视化展示。对于看涨交易形态,将止损设在 A 点(价格急变前基准价)略下方,止盈设在 B 点(价格急变极值)上方;对于看跌交易形态,则将止损设在 A 点上方,止盈设在 B 点下方(典型示例见图4中的 USDJPY)。在止损价位外留出少量缓冲(例如几个标准点或点差 × 1.5),以减少市场噪声引发的止损出场。
边界情况
如果价格完全回撤至 100%(即回到 P_base),随后继续越过该价位,则应考虑放弃原有的交易逻辑 —— 回撤已经完成,行情可能正在反转。如果突发行情的方向不明确,或波段跨度小于最低 K线根数/标准点数要求,则放弃交易(噪声过多)。
实现策略
为了简化开发流程,我在 MetaEditor 5 中实现了一个专用的类头文件,用于管理 NFP 的公布时间戳,并在美国东部时间(ET)、协调世界时(UTC)和经纪商的服务器时间之间正确转换这些时间戳。该头文件集中处理夏令时、ET 偏移量计算及时间转换,使其他模块无需关注时区细节。
利用该头文件,我开发了一个 EA,它能够:
- 检测NFP发布后首根收盘的 M5 K线,
- 绘制一条以该K线为锚点的斐波那契回撤线,
- 在38.2%、50.0%和61.8%的斐波那契回撤位设置挂单。
- 对经纪商的交易限制进行严格检查(最小止损距离、价格规范化、到期时间),
- 并且管理图表对象和挂单的生命周期(在成交或到期时进行清理)。
在接下来的两个步骤中,我们将针对代码提供详细且便于开发者理解的说明:首先是 NFPTimeManager 头文件(设计决策、API、夏令时和时区逻辑),然后是 EA (数据流、订单管理和安全检查)。这些说明将展示该系统在 MQL5 中的设计、实现和测试过程。
步骤 1:NFPTimeManager 头文件
1.1. 文件头和元数据
该文件以标准的文件头注释块和 MQL5 #property 属性开头。文件头注释块记录了作者和版本信息,以便其他开发者(或未来的自己)能够立即了解该文件的用途和来源。这些属性使 MQL5 构建系统能够在 MetaEditor 5 中显示元数据。
//+------------------------------------------------------------------+ //| NFPTimeManager.mqh | //| Copyright 2025, Clemence Benjamin | //| https://www.mql5.com/en/users/billionaire2024/seller| //+------------------------------------------------------------------+ #property copyright "Clemence Benjamin" #property version "1.0"
1.2. 类声明和私有时区字段
我们将所有与 NFP 相关的实用功能封装在 CNFPTimeManager 类中。两个私有整型字段分别用于记录可选的交易者时区和经纪商/服务器时区;将这些信息封装在类内部,既能确保转换函数结果的确定性,又能避免时区逻辑散布在整个代码库中。
class CNFPTimeManager { private: int m_traderTimeZone; // Trader time zone offset from UTC (hours) - optional int m_serverTimeZone; // Server time zone offset from UTC (hours)
1.3. GetETOffset — 计算考虑夏令时的美国东部时间偏移量
NFP在美国东部时间(ET)的固定时刻 —— 08:30发布。GetETOffset 用于判断给定日期采用的是美国东部标准时间(UTC−5)还是美国东部夏令时(UTC−4)。它采用美国夏令时规则 (3月的第二个星期日至11月的第一个星期日),并返回整数形式的UTC偏移量。这可确保日历计算在不同季节都保持正确。
// returns ET offset (-4 for EDT, -5 for EST) for a given datetime (trader time context) int GetETOffset(datetime time) { MqlDateTime dt; TimeToStruct(time, dt); int year = dt.year; // DST: 2nd Sunday of March to 1st Sunday of November (US rules) datetime dst_start = GetNthDayOfWeek(year, 3, 0, 2); // 2nd Sunday of March datetime dst_end = GetNthDayOfWeek(year, 11, 0, 1); // 1st Sunday of November // Note: We interpret the DST boundaries as local US/Eastern dates at 02:00 local time, // but for our purpose using whole-day thresholds is acceptable (NFP at 8:30 ET). if(time >= dst_start && time < dst_end) return -4; // EDT return -5; // EST }
1.4. GetNthDayOfWeek — 稳健的月历计算
像"3月的第二个星期日"这样的日历运算,出错的可能性出人意料地高。GetNthDayOfWeek 会先构建当月1日的日期,确定当天是星期几,然后计算到该月第 N 次出现的指定星期几的偏移量。该函数返回指定日期对应的精确 datetime 值,夏令时(DST)和NFP时间戳的计算都会复用此函数。
// correctly compute the N-th day_of_week (0=Sunday..6=Saturday) in given month/year datetime GetNthDayOfWeek(int year, int month, int day_of_week, int n) { MqlDateTime dt; // build date for the 1st of month dt.year = year; dt.mon = month; dt.day = 1; dt.hour = 0; dt.min = 0; dt.sec = 0; datetime first = StructToTime(dt); MqlDateTime dt2; TimeToStruct(first, dt2); int dow_first = dt2.day_of_week; // 0..6 int delta = (day_of_week - dow_first + 7) % 7; int days_to_nth = delta + (n - 1) * 7; return first + days_to_nth * 86400; }
1.5. 构造函数和时区设置器
一个简短的构造函数将时区字段初始化为零。如果不希望使用自动检测,或需要出于测试或适配远程服务器的目的覆盖检测结果,调用代码可通过两个公有设置器显式设置交易者时区或服务器时区。
public: CNFPTimeManager() { m_traderTimeZone = 0; m_serverTimeZone = 0; } // set trader timezone if you need to convert between trader/local and server void SetTraderTimeZone(int timezone_hours) { m_traderTimeZone = timezone_hours; } void SetServerTimeZone(int timezone_hours) { m_serverTimeZone = timezone_hours; }
1.6. DetectServerTimeZone 和 GetServerTime — 服务器时间感知辅助函数
DetectServerTimeZone 是一个便捷函数,它将 TimeCurrent()(服务器时间)与 TimeLocal()(本地计算机时间)进行比较,并以整数小时为单位存储时间偏移量。GetServerTime 只是对 TimeCurrent() 进行了封装,以便其他模块能够通过单一来源获取服务器的当前时间戳。
// attempt to auto-detect server timezone (difference between server and local) int DetectServerTimeZone() { datetime local = TimeLocal(); datetime server = TimeCurrent(); int offset_seconds = (int)(server - local); int offset_hours = offset_seconds / 3600; m_serverTimeZone = offset_hours; return offset_hours; } datetime GetServerTime() { return TimeCurrent(); }
1.7. GetNFPTimestampTrader —— 以美国东部时间(ET)表示的NFP公布时刻
该方法返回一个 datetime 值,表示给定年份和月份的第一个星期五美国东部时间(ET)08:30 的 NFP 发布时刻。该函数以"交易者时间"(美国东部时间的当地钟表时间)为基准,其返回值将另行转换为服务器时间。这种关注点的分离使该函数保持简单且可预测。
// NFP candidate time in trader (ET) zone: first Friday of month at 08:30 ET datetime GetNFPTimestampTrader(int year, int month) { // first Friday (5) of the month datetime first_friday = GetNthDayOfWeek(year, month, 5, 1); MqlDateTime dt; TimeToStruct(first_friday, dt); dt.hour = 8; dt.min = 30; dt.sec = 0; return StructToTime(dt); // this is in server timescale semantics but represents the ET timestamp }
1.8. TraderETtoServer — 将美国东部时间(ET)时间戳转换为服务器时间
这是关键的转换函数:它接收一个美国东部时间(ET)的当地钟表时间戳(例如 2025-09-05 08:30 ET),判断当时美国东部时间是否采用夏令时,将该时间戳转换为协调世界时(UTC),再转换到所配置的服务器时区。结果是以服务器时间表示的绝对时刻,EA 应以此为基准进行调度和K线索引计算。
// Convert a trader ET timestamp to server absolute time using ET offset and server tz datetime TraderETtoServer(datetime nfp_trader_time) { int et_offset = GetETOffset(nfp_trader_time); // UTC offset for ET at that date (-4/-5) // nfp_trader_time is interpreted in ET (i.e., wall-clock ET). To get UTC we add -ET offset: // UTC = ET - ET_offset_hours // Server local = UTC + server_tz int server_tz = m_serverTimeZone; datetime nfp_server_time = nfp_trader_time - et_offset * 3600 + server_tz * 3600; return nfp_server_time; }
1.9. GetNextNFPServerTime 和 GetLastNFPServerTime — 向前/向后搜索
这些辅助函数向未来搜索下一次、或向过去搜索最近一次 NFP 发布的服务器时间戳,最多扫描 13 个月。它们将候选的美国东部时间(ET)转换为服务器时间,并与 TimeCurrent() 进行比较。这些函数能正确处理跨年时的边界情况,因为它们会正确地递增或递减月份和年份。
// Return the next NFP server datetime strictly greater than now (search up to 13 months) datetime GetNextNFPServerTime() { datetime server_time = GetServerTime(); MqlDateTime sd; TimeToStruct(server_time, sd); int year = sd.year, month = sd.mon; for(int i=0;i<13;i++) { datetime nfp_trader = GetNFPTimestampTrader(year, month); datetime nfp_server = TraderETtoServer(nfp_trader); if(nfp_server > server_time) return(nfp_server); month++; if(month>12){ month=1; year++; } } return(0); } // Return most recent NFP server datetime <= now (search backwards) datetime GetLastNFPServerTime() { datetime server_time = GetServerTime(); MqlDateTime sd; TimeToStruct(server_time, sd); int year = sd.year, month = sd.mon; for(int i=0;i<13;i++) { datetime nfp_trader = GetNFPTimestampTrader(year, month); datetime nfp_server = TraderETtoServer(nfp_trader); if(nfp_server <= server_time) return(nfp_server); month--; if(month<1){ month=12; year--; } } return(0); }
1.10. IsNFPEventActive — 快速检查活动时间窗口
一个简单的布尔辅助函数用于检测最近一次NFP的公布时间是否在可配置的时间窗口内(默认为前后各一小时)。当您希望代码在NFP时间窗口内采取不同的行为(例如暂停交易或记录事件)时,此函数就很有用。
// Detect if an NFP event is currently within ±window_seconds of server time (default ±3600s) bool IsNFPEventActive(int window_seconds = 3600) { datetime server_time = GetServerTime(); datetime last_nfp = GetLastNFPServerTime(); if(last_nfp == 0) return(false); if(MathAbs((long)(server_time - last_nfp)) <= window_seconds) return(true); return(false); }
1.11. FirstM5OpenAfterNFP — 将事件时间映射到M5开盘时间
交易逻辑在NFP公布后的第一根M5 K线收盘时作出响应。此辅助函数返回事件发生时所在M5 K线的开盘时间(该K线的收盘时间是事件时间戳之后的首个收盘时间)。我们采用简单的向下取整运算(timestamp / 300 * 300),因此计算速度快且结果明确。
// Utility: compute the M5 open time for the bar that contains the NFP or the first bar after it. // Returns the M5 open time for the bar whose close is the first > nfp_server_time. datetime FirstM5OpenAfterNFP(datetime nfp_server_time) { if(nfp_server_time <= 0) return(0); // PeriodSeconds for M5 = 300 int period = PERIOD_M5; int psec = 60 * 5; // floor open time: long open_floor = (long)(nfp_server_time / psec) * psec; // if the event falls exactly at an open boundary, then the bar with open=open_floor is the one that includes event // its close = open_floor + psec -> that is the first close after event return (datetime)open_floor; }
1.12. GetM5BarIndexByOpenTime — 将开盘时间转换为K线索引
最后,此辅助函数封装了iBarShift,用于根据给定的M5开盘时间返回M5时间序列中的K线索引(从当前K线开始,以0为起始索引)。如果未找到完全匹配项,则返回 -1。当EA需要调用CopyRates或按索引访问历史K线时,此函数非常实用。
// Convenience: compute the M5 bar index (iBarShift) for an M5 open time (exact match) int GetM5BarIndexByOpenTime(string symbol, datetime m5_open_time) { if(m5_open_time <= 0) return(-1); // prefer exact match - returns index where rates[].time == m5_open_time int idx = iBarShift(symbol, PERIOD_M5, m5_open_time, true); return idx; // -1 if not found } };
第2步:NFP公布后的斐波那契交易EA
2.1. 文件头注释块、包含文件和输入参数
此顶层代码块声明了EA的元数据,并导入了交易辅助函数和NFPTimeManager头文件。所有输入参数都是提供给交易者的可调设置:颜色、命名、EA是否自动下单、交易手数、止损/止盈(SL/TP)的缓冲值、严格程度,以及斐波那契回撤线的存续时间。在模拟账户中测试时,先使用简单的参数设置,之后再按交易品种进行调整。
//+------------------------------------------------------------------+ //| Post-NFP Fibonacci Trader EA | //| Post-NFP fib + pending orders with robust SL/TP buffer & object | //| lifecycle: destroy object when any order fills OR after expiry | //+------------------------------------------------------------------+ #property copyright "Clemence Benjamin" #property version "1.0" #property strict #include <Trade\Trade.mqh> #include <NFPTimeManager.mqh> // --- visual / fib inputs input color InpFiboColor = clrDodgerBlue; input string InpPrefix = "FIBO_NFP_"; input bool InvertFiboOrientation = true; input bool InpRayRight = true; // Trading inputs input double InpLots = 0.01; input int InpSlippage = 10; input ulong InpMagic = 123456; input int InpOrderExpirationMinutes = 0; input bool InpAutoPlaceOrders = true; input bool InpRequireStrictSLAt100 = false; // strict mode input double InpSLBufferPoints = 20.0; // SL moved away from 100% by this many points input double InpTPBufferPoints = 20.0; // TP moved away from 0% by this many points input int InpFibExpiryHours = 3; // how many hours before fib auto-deletes
2.2. 全局状态 —— 运行时数据的存储位置
这些全局变量保存EA在各次调用之间需要保留的运行状态:斐波那契回撤线的锚点、原始K线是否为看涨K线、所跟踪的挂单订单号、当前斐波那契回撤线对象的名称及其创建时间(用于到期处理)、固定的斐波那契回撤位,以及M5相关的状态记录。它们由开发者声明,并在运行时进行更新。
// --- internal CTrade trade; CNFPTimeManager nfpManager; double g_price0 = 0.0; double g_price100 = 0.0; bool g_fibBullish = false; ulong g_pendingTickets[]; // tracked pending tickets string g_currentFibName = ""; // current fib object name datetime g_fibCreateTime = 0; // server time when fib created static const double S_levels[] = {38.2, 50.0, 61.8}; datetime lastM5Open = 0; datetime lastProcessedNFP = 0;
2.3. OnInit() — 初始设置及与NFP头文件的关联
将EA加载到图表时,OnInit会利用该头文件检测服务器时区(以确保美国东部时间(ET)与服务器时间之间的转换正确),配置CTrade的默认设置(magic数字和滑点),清空此前订单号数组占用的内存,并记录当前M5 K线的开盘时间。这使EA能够检测NFP公布后下一根M5 K线的收盘事件。
int OnInit() { // initialize NFP manager tz int detected = nfpManager.DetectServerTimeZone(); PrintFormat("EA: Detected server timezone UTC%+d", detected); trade.SetExpertMagicNumber(InpMagic); trade.SetDeviationInPoints(InpSlippage); ArrayFree(g_pendingTickets); // initialize last M5 open time MqlRates r[]; if(CopyRates(_Symbol, PERIOD_M5, 0, 1, r) == 1) lastM5Open = r[0].time; else lastM5Open = 0; Print("EA: Initialized v1.50 — monitoring NFP, will create fib on first M5 close after NFP."); return(INIT_SUCCEEDED); }
2.4. OnDeinit() — 关闭与清理
OnDeinit确保EA被移除时不会遗留任何挂单或图表对象。它会取消所有跟踪的挂单,删除当前的斐波那契回撤线(如有),并释放订单号数组。这是保障测试和实盘使用安全的一项重要清理工作。
void OnDeinit(const int reason) { // cleanup: cancel tracked pending orders and delete fib CancelExistingPendingOrders(); DeleteCurrentFibIfExists(); ArrayFree(g_pendingTickets); }
2.5. OnTradeTransaction() — 检测成交并立即响应
该回调函数用于监听交易系统事件。当新增一笔成交时,EA会检查已成交订单的订单号是否与其跟踪的某个挂单订单号匹配。若匹配,则删除斐波那契回撤线,取消其余挂单,并清空订单号列表。如果订单被外部操作删除,它也会将相应的订单号从跟踪列表中移除。这确保图表上的斐波那契回撤线和所跟踪的挂单与实际交易保持同步。
void OnTradeTransaction(const MqlTradeTransaction &trans, const MqlTradeRequest &request, const MqlTradeResult &result) { // We care about deals added (fills) and order deletions possibly made externally if(trans.type == TRADE_TRANSACTION_DEAL_ADD) { ulong orderTicket = trans.order; if(orderTicket == 0) return; for(int i = ArraySize(g_pendingTickets)-1; i >= 0; i--) { if(g_pendingTickets[i] == orderTicket) { PrintFormat("EA: Detected fill for tracked order %I64u -> destroying fib and cancelling remaining pending orders.", orderTicket); ArrayRemove(g_pendingTickets, i); CancelExistingPendingOrders(); DeleteCurrentFibIfExists(); ArrayFree(g_pendingTickets); return; } } } else if(trans.type == TRADE_TRANSACTION_ORDER_DELETE) { ulong orderTicket = trans.order; if(orderTicket == 0) return; for(int i = ArraySize(g_pendingTickets)-1; i>=0; i--) { if(g_pendingTickets[i] == orderTicket) { ArrayRemove(g_pendingTickets, i); PrintFormat("EA: Tracked order %I64u was deleted externally — removed from internal list.", orderTicket); break; } } } }
2.6. OnTick() — 心跳逻辑:到期检查与M5收盘触发
OnTick会在每次市场报价更新时运行。它执行两项核心检查:(A) 如果当前斐波那契回撤线存在且其有效期(例如 3 小时)已满,则将其删除并取消挂单;(B) 检测新的 M5 K线是否开盘(即前一根 M5 K线已收盘)。在该 M5 收盘事件发生时,它会向头文件查询最近一次 NFP 公布的服务器时间,核实刚收盘的 M5 K线是否为 NFP 公布后的第一根 M5 K线;若是,则调用斐波那契回撤线绘制与下单例程。这一设计确保斐波那契回撤线锚定于 NFP 公布后一根已收盘的 M5 K线。
void OnTick() { // 1) Check for fib expiry by time if(StringLen(g_currentFibName) > 0 && g_fibCreateTime > 0) { datetime now = TimeCurrent(); if(now >= (datetime)(g_fibCreateTime + InpFibExpiryHours * 3600)) { PrintFormat("EA: Fib '%s' expired after %d hours -> deleting object and cancelling pending orders.", g_currentFibName, InpFibExpiryHours); CancelExistingPendingOrders(); DeleteCurrentFibIfExists(); } } // 2) Detect M5 new bar open (previous M5 bar closed) MqlRates m5rt[]; if(CopyRates(_Symbol, PERIOD_M5, 0, 1, m5rt) != 1) return; datetime currentM5Open = m5rt[0].time; if(currentM5Open == lastM5Open) return; // previous M5 open is the bar that just closed datetime prevOpen = lastM5Open; lastM5Open = currentM5Open; // find last NFP server time (the release to react to) datetime lastNFP = nfpManager.GetLastNFPServerTime(); if(lastNFP == 0) return; // if already processed this NFP skip if(lastProcessedNFP == lastNFP) return; // compute bar open floor (M5) that contains the event int psec = 60*5; long floorOpen = (long)(lastNFP / psec) * psec; datetime firstM5OpenAfterNFP = (datetime)floorOpen; // we want prevOpen == firstM5OpenAfterNFP (i.e. the bar that closed is that target) if(prevOpen != firstM5OpenAfterNFP) return; // find M5 index of that open time int m5Index = iBarShift(_Symbol, PERIOD_M5, prevOpen, true); if(m5Index < 0) { PrintFormat("EA: Could not find M5 index for open time %s", TimeToString(prevOpen, TIME_DATE|TIME_MINUTES)); return; } // update fib using that M5 bar and create pending orders if(!UpdateFibFromM5Bar(m5Index)) { Print("EA: UpdateFibFromM5Bar failed."); return; } if(InpAutoPlaceOrders) PlaceOrRefreshPendingOrders(); // mark processed lastProcessedNFP = lastNFP; PrintFormat("EA: Processed NFP at %s (server time). Used M5 open %s as anchor.", TimeToString(lastNFP, TIME_DATE|TIME_MINUTES), TimeToString(prevOpen, TIME_DATE|TIME_MINUTES)); }
2.7. UpdateFibFromM5Bar() — 读取 M5 K线、计算锚点并绘制斐波那契回撤线
该函数使用 CopyRates 读取选定的 M5 K线,通过比较收盘价与开盘价判断该K线是看涨还是看跌,按您的映射规则计算 0% 和 100% 锚点,并可根据显示偏好将两者对调,然后保存锚点以供交易使用,删除带有指定前缀的旧对象,最后创建一个名称唯一且恰好覆盖该K线的 OBJ_FIBO 对象。它会记录斐波那契回撤线的创建时间,供到期逻辑使用。
bool UpdateFibFromM5Bar(int m5Index) { if(m5Index < 0) return(false); MqlRates r[]; int copied = CopyRates(_Symbol, PERIOD_M5, m5Index, 1, r); if(copied != 1) { PrintFormat("EA: CopyRates for M5 index %d failed (copied=%d).", m5Index, copied); return(false); } double bar_high = r[0].high; double bar_low = r[0].low; double bar_open = r[0].open; double bar_close= r[0].close; datetime bar_time = r[0].time; bool bullish = (bar_close > bar_open); g_fibBullish = bullish; // user's mapping: double price0 = bullish ? bar_high : bar_low; // 0% anchor double price100= bullish ? bar_low : bar_high; // 100% anchor if(InvertFiboOrientation) { double t = price0; price0 = price100; price100 = t; } // store anchors for trading g_price0 = price0; g_price100 = price100; // delete any existing prefix objects to avoid clutter int tot = ObjectsTotal(0); for(int i = tot-1; i >= 0; i--) { string nm = ObjectName(0, i); if(StringFind(nm, InpPrefix) == 0) ObjectDelete(0, nm); } // create a unique name for this fib g_currentFibName = InpPrefix + _Symbol + "_" + IntegerToString((int)bar_time); bool created = ObjectCreate(0, g_currentFibName, OBJ_FIBO, 0, bar_time, price0, (datetime)(bar_time + PeriodSeconds(PERIOD_M5)), price100); if(!created) { PrintFormat("EA: Failed to create Fibo '%s' (err=%d)", g_currentFibName, GetLastError()); g_currentFibName = ""; return(false); } ObjectSetInteger(0, g_currentFibName, OBJPROP_COLOR, InpFiboColor); ObjectSetInteger(0, g_currentFibName, OBJPROP_RAY_RIGHT, InpRayRight ? 1 : 0); ObjectSetInteger(0, g_currentFibName, OBJPROP_SELECTABLE, true); ObjectSetInteger(0, g_currentFibName, OBJPROP_HIDDEN, false); string desc = bullish ? "Fibo anchored to bullish M5 bar" : "Fibo anchored to bearish M5 bar"; if(InvertFiboOrientation) desc += " (inverted)"; if(InpRayRight) desc += " (ray-right)"; ObjectSetString(0, g_currentFibName, OBJPROP_TEXT, desc); g_fibCreateTime = TimeCurrent(); PrintFormat("EA: Drew Fibo '%s' on M5 index %d (open=%s) high=%.5f low=%.5f", g_currentFibName, m5Index, TimeToString(bar_time, TIME_DATE|TIME_MINUTES), bar_high, bar_low); return(true); }
2.8. DeleteCurrentFibIfExists() — 删除斐波那契回撤线的统一入口
这是一个小型辅助函数,用于删除当前的斐波那契回撤线对象(如果存在),并清除 EA 中与该对象相关的状态。集中处理删除操作可以减少重复,并避免因名称未被清除而导致的隐蔽错误。
void DeleteCurrentFibIfExists() { if(StringLen(g_currentFibName) == 0) return; if(ObjectFind(0, g_currentFibName) >= 0) { ObjectDelete(0, g_currentFibName); PrintFormat("EA: Deleted fib object '%s'.", g_currentFibName); } g_currentFibName = ""; g_fibCreateTime = 0; }
2.9. CancelExistingPendingOrders() — 追踪与清理策略
EA在 g_pendingTickets 中跟踪其创建的订单的订单号。该例程遍历此列表,并针对每个订单号调用 trade.OrderDelete。使用已跟踪的订单号比按交易品种或注释删除更安全,因为这种方式仅针对 EA 本次运行期间创建的订单(除非后续添加持久化功能)。
void CancelExistingPendingOrders() { if(ArraySize(g_pendingTickets) == 0) return; for(int i = ArraySize(g_pendingTickets)-1; i >= 0; i--) { ulong ticket = g_pendingTickets[i]; if(ticket == 0) { ArrayRemove(g_pendingTickets, i); continue; } bool del = trade.OrderDelete(ticket); if(!del) PrintFormat("EA: Failed to delete pending ticket %I64u (err=%d)", ticket, GetLastError()); else PrintFormat("EA: Deleted pending ticket %I64u", ticket); ArrayRemove(g_pendingTickets, i); } }
2.10. PlaceOrRefreshPendingOrders() — 计算入场价、止损(SL)和止盈(TP)并下单(交易核心)
这是交易逻辑的核心。对于每个斐波那契回撤位(38.2、50、61.8),它会执行以下操作:
- 通过 g_price0 和 g_price100 之间的线性插值计算入场价。
- 分别以 100% 和 0% 价位为基准计算目标止损(SL)和止盈(TP)价位,再根据原始 M5 K线是看涨还是看跌,将止损和止盈分别向远离入场价的方向移动 InpSLBufferPoints / InpTPBufferPoints 指定的距离。(这确保在看涨情况下,止损位于 100% 价位下方,止盈位于 0% 价位上方;看跌情况下则相反。)
- 按交易品种的小数位数规范化价格。
- 查询经纪商的限制参数(SYMBOL_TRADE_STOPS_LEVEL、SYMBOL_POINT),并确保止损、止盈及挂单价位满足最小距离要求。如有必要,它会调整止损(SL)和止盈(TP)(或在启用严格模式时跳过该价位)。
- 逐个价位判断应使用 BUY_LIMIT(入场价低于市价)还是 SELL_LIMIT(入场价高于市价),通过 CTrade 下达挂单(如已配置,则指定到期时间和时间类型),记录成功或失败的结果,并保存订单号以便后续清理。
这种方法能最大限度地减少经纪商拒单,并确保各价位之间的关系在逻辑上正确。
void PlaceOrRefreshPendingOrders() { // clear previously tracked tickets first CancelExistingPendingOrders(); if(g_price0 == 0.0 || g_price100 == 0.0) { Print("EA: anchors not set - skipping placement"); return; } trade.SetExpertMagicNumber(InpMagic); trade.SetDeviationInPoints(InpSlippage); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double bid = SymbolInfoDouble(_Symbol, SYMBOL_BID); double point = SymbolInfoDouble(_Symbol, SYMBOL_POINT); long stops_level = 0; if(!SymbolInfoInteger(_Symbol, SYMBOL_TRADE_STOPS_LEVEL, stops_level)) stops_level = 0; double min_stop_distance = (double)stops_level * point; if(min_stop_distance < point) min_stop_distance = point; double min_pending_distance = min_stop_distance; // compute buffer values in price units (points * point) double sl_buffer_price = InpSLBufferPoints * point; double tp_buffer_price = InpTPBufferPoints * point; ENUM_ORDER_TYPE_TIME place_time_type = (InpOrderExpirationMinutes > 0) ? ORDER_TIME_SPECIFIED : ORDER_TIME_GTC; datetime place_expiration = (InpOrderExpirationMinutes > 0) ? (TimeCurrent() + InpOrderExpirationMinutes * 60) : 0; int digits = (int)SymbolInfoInteger(_Symbol, SYMBOL_DIGITS); int levelsCount = ArraySize(S_levels); for(int i=0;i<levelsCount;i++) { double pct = S_levels[i] / 100.0; double entry = g_price0 + pct * (g_price100 - g_price0); // base desired SL and TP anchored at 100% and 0% double sl_desired = g_price100; double tp_desired = g_price0; // Apply buffers depending on the original fib bullish/bearish orientation if(g_fibBullish) { sl_desired = (g_price100 - sl_buffer_price); tp_desired = (g_price0 + tp_buffer_price); } else { sl_desired = (g_price100 + sl_buffer_price); tp_desired = (g_price0 - tp_buffer_price); } // normalize values entry = NormalizeDouble(entry, digits); sl_desired = NormalizeDouble(sl_desired, digits); tp_desired = NormalizeDouble(tp_desired, digits); // decide pending type by comparing entry price with market and min_pending_distance bool wantBuy = false, wantSell = false; if(entry <= (ask - min_pending_distance)) wantBuy = true; else if(entry >= (bid + min_pending_distance)) wantSell = true; else { PrintFormat("EA: Skipping level %.2f: entry(%.5f) too close to market (Ask=%.5f Bid=%.5f) min_pending_dist=%.5f", S_levels[i], entry, ask, bid, min_pending_distance); continue; } ENUM_ORDER_TYPE otype = (wantBuy ? ORDER_TYPE_BUY_LIMIT : ORDER_TYPE_SELL_LIMIT); bool adjusted = false; // Validate/adjust stops unless strict mode requested if(otype == ORDER_TYPE_BUY_LIMIT) { // For BUY_LIMIT we need sl < entry < tp if(!(sl_desired < entry - min_stop_distance)) { if(InpRequireStrictSLAt100) { PrintFormat("EA: Strict mode: skipping level %.2f because SL would be adjusted (desired SL %.5f not < entry-%.5f).", S_levels[i], sl_desired, (entry - min_stop_distance)); continue; } double old = sl_desired; sl_desired = NormalizeDouble(entry - min_stop_distance, digits); adjusted = true; PrintFormat("EA: Adjusted SL BUY level %.2f old=%.5f -> new=%.5f", S_levels[i], old, sl_desired); } if(!(tp_desired > entry + min_stop_distance)) { if(InpRequireStrictSLAt100) { PrintFormat("EA: Strict mode: skipping level %.2f because TP would be adjusted (desired TP %.5f not > entry+%.5f).", S_levels[i], tp_desired, (entry + min_stop_distance)); continue; } double old = tp_desired; tp_desired = NormalizeDouble(entry + min_stop_distance, digits); adjusted = true; PrintFormat("EA: Adjusted TP BUY level %.2f old=%.5f -> new=%.5f", S_levels[i], old, tp_desired); } } else // SELL_LIMIT { // For SELL_LIMIT we need tp < entry < sl if(!(sl_desired > entry + min_stop_distance)) { if(InpRequireStrictSLAt100) { PrintFormat("EA: Strict mode: skipping level %.2f because SL would be adjusted (desired SL %.5f not > entry+%.5f).", S_levels[i], sl_desired, (entry + min_stop_distance)); continue; } double old = sl_desired; sl_desired = NormalizeDouble(entry + min_stop_distance, digits); adjusted = true; PrintFormat("EA: Adjusted SL SELL level %.2f old=%.5f -> new=%.5f", S_levels[i], old, sl_desired); } if(!(tp_desired < entry - min_stop_distance)) { if(InpRequireStrictSLAt100) { PrintFormat("EA: Strict mode: skipping level %.2f because TP would be adjusted (desired TP %.5f not < entry-%.5f).", S_levels[i], tp_desired, (entry - min_stop_distance)); continue; } double old = tp_desired; tp_desired = NormalizeDouble(entry - min_stop_distance, digits); adjusted = true; PrintFormat("EA: Adjusted TP SELL level %.2f old=%.5f -> new=%.5f", S_levels[i], old, tp_desired); } } // final relation sanity bool valid = true; if(otype == ORDER_TYPE_BUY_LIMIT) valid = (sl_desired < entry && tp_desired > entry); else valid = (sl_desired > entry && tp_desired < entry); if(!valid) { PrintFormat("EA: Skipping level %.2f due to invalid final SL/TP relation entry=%.5f SL=%.5f TP=%.5f", S_levels[i], entry, sl_desired, tp_desired); continue; } // build order comment string comment = InpPrefix + DoubleToString(S_levels[i], 2); // Place pending with proper signature (includes time type and expiration) bool placed = false; if(otype == ORDER_TYPE_BUY_LIMIT) placed = trade.BuyLimit(InpLots, entry, _Symbol, sl_desired, tp_desired, place_time_type, place_expiration, comment); else placed = trade.SellLimit(InpLots, entry, _Symbol, sl_desired, tp_desired, place_time_type, place_expiration, comment); if(!placed) { int err = GetLastError(); PrintFormat("EA: Failed to place %s at %.5f (lvl %.2f) err=%d ret=%d", (otype==ORDER_TYPE_BUY_LIMIT ? "BUY_LIMIT":"SELL_LIMIT"), entry, S_levels[i], err, (int)trade.ResultRetcode()); } else { ulong ticket = trade.ResultOrder(); PrintFormat("EA: Placed %s ticket=%I64u lvl=%.2f entry=%.5f SL=%.5f TP=%.5f adjusted=%s", (otype==ORDER_TYPE_BUY_LIMIT ? "BUY_LIMIT":"SELL_LIMIT"), ticket, S_levels[i], entry, sl_desired, tp_desired, (adjusted ? "yes":"no")); // track ticket for cleanup and fill detection ArrayResize(g_pendingTickets, ArraySize(g_pendingTickets) + 1); g_pendingTickets[ArraySize(g_pendingTickets)-1] = ticket; } } // levels loop }
2.11. EA如何使用NFPTimeManager头文件 —— 简单的调用点
该头文件负责繁重的日期和时间处理工作:EA 调用 DetectServerTimeZone() 来设置时间转换,这一步在 OnInit() 中完成;随后在每根 M5 K线收盘时调用 GetLastNFPServerTime(),获取最近一次NFP发布时刻对应的服务器时间。EA根据该时间戳计算NFP发布后首根M5 K线的开盘时间,并在该K线收盘时作出反应。这使得EA的核心逻辑保持简洁,并确保时区处理正确。
// example header calls in OnInit/OnTick int detected = nfpManager.DetectServerTimeZone(); datetime lastNFP = nfpManager.GetLastNFPServerTime();
结果
借助NFPTimeManager类,该EA即使在历史数据上也能可靠地识别NFP的发布,从而使我们能够在成功编译后,在策略测试器中运行并验证该策略。以下动图展示了策略测试器中的测试过程。

图6. 2024年6月7日,NFP公布
如图6所示,在2024年6月7日NFP公布期间,EA成功识别了数据公布日,并将NFP发布时间准确对应到本地服务器时间,这一点已在策略测试器中得到验证。斐波那契回撤线按预期绘制并发挥了作用。然而,由于回撤幅度过小、未能触及入场价位,因此均未触发挂单。
我们还发现了另一个问题:挂单的止损和止盈价位设置不正确,与预期不符。这个问题既可以通过修改代码来纠正,也可以通过调整输入参数来纠正。
尽管存在这些局限性,但最重要的成果是,该 EA 能够识别数据发布事件、准确进行时区换算,并自动绘制斐波那契回撤线。这些成果奠定了坚实的基础,在今后的迭代中可以进一步完善。下图展示了2024年11月1日数据发布时的情况。

图7. 2024年11月1日,NFP公布
结论
可以通过算法应用斐波那契回撤线,以结构化、基于规则的方式交易NFP发布行情。这种方法并不追逐最初的价格急变,而是在数据发布几分钟后,即市场进入自然回撤阶段时进行交易。从理论上讲,即使交易者错过了最初的行情波动,仍可将最初五分钟价格急变幅度的30%至60%转化为利润。这可以通过参考斐波那契比率来实现:将价格急变的全部幅度视为100%,据此确定精确的回撤位,用于安排交易入场。
其中,38.2%的斐波那契回撤位往往最有可能带来相当于初始脉冲幅度38.2%的收益。不过,需要注意的是,市场并不总是遵循这种走势,且没有任何交易机会是百分之百有把握的。我们的测试在多种场景下均取得了令人鼓舞的结果,不过应将该方法视为一种有章可循的概率性方法,而非必然成功的保证。
在本研究中,我们并未对该EA进行全面的性能分析,而是侧重于通过策略测试器中的实际模拟来演示这一概念。该原型仍有改进和优化的空间,例如完善订单管理、开展统计验证,或使其适用于其他货币对及非新闻驱动的交易场景。
在构建该策略的过程中所汲取的经验同样宝贵:从零开始,运用模块化设计来保持代码的整洁和可复用性,并着眼于未来的高级扩展。这是可以进一步发展的基础,随着后续开发,它可以演变为一款更强大、更灵活的交易工具。
欢迎您在下方评论区分享看法并提供反馈。此外,请参阅随附的源文件和关键经验总结表,从中获取实用启示。
关键经验
| 关键经验 | 描述: |
|---|---|
| 将逻辑模块化 | 将功能拆分为小型、职责单一的单元(类或函数)。示例:使用专门处理时间和夏令时逻辑的 NFPTimeManager,可使EA的核心代码保持可读性和可复用性。 |
| 显式处理时区和夏令时 | 使用确定性规则,将以美国东部时间(ET)表示的事件时间转换为服务器时间。此处出错会导致历史测试和实盘运行中的锚点错位,因此应集中管理这部分逻辑并进行测试。 |
| 以已收盘的K线为基准 | 计算时应始终以已收盘的K线为依据(例如,NFP公布后首根收盘的M5 K线)。这样可以避免K线内重新计算时出现的不稳定性,并确保在策略测试器中获得可重复的结果。 |
| 遵守经纪商的限制 | 在运行时查询 SYMBOL_TRADE_STOPS_LEVEL、SYMBOL_POINT 和 SYMBOL_DIGITS,并检查止损(SL)、止盈(TP)及挂单的价格距离是否符合要求,以避免订单被拒绝。 |
| 规范化价格并使用正确的精度 | 始终按交易品种的小数位数规范化入场价、止损价(SL)和止盈价(TP)。微小的舍入误差很容易导致经纪商拒绝订单。 |
| 设计安全的回退机制 | 提供严格和自适应两种模式:严格模式下,跳过需要调整止损或止盈设置的斐波那契回撤位;自适应模式下,则自动调整止损或止盈设置以符合经纪商规则。对这两种决策都应记录日志。 |
| 使用可靠的下单API | 调用 CTrade 辅助方法时使用正确的函数签名(包括到期时间和时间类型),以避免后续修改并降低错误处理的复杂性。 |
| 跟踪和管理自身的订单号 | 在内部维护一份已创建订单的订单号列表,以便EA能够可靠地仅取消或监控自己创建的订单,避免删除无关的用户订单。 |
| 对象生命周期与界面整洁性 | 为图表对象赋予简短且唯一的名称(例如:fib_SYMBOL_YYMMDDhhmm),将详细信息存储在OBJPROP_TEXT中,并在订单成交或到期时删除这些对象。 |
| 使用OnTradeTransaction处理成交 | 通过交易事务事件检测订单成交和外部删除操作,以便立即响应(清理、记录日志),并避免轮询造成的延迟。 |
| 记录详细日志以便追溯 | 记录每个关键操作(创建、调整、跳过、下单、失败、删除)。清晰的日志对于调试、回测以及监管和审计工作至关重要。 |
| 先在策略测试器和模拟账户中测试 | 先在策略测试器中针对历史NFP发布日期验证逻辑(确定性测试),然后在模拟账户上运行。使用确定性锚点(已收盘K线),以确保测试结果可重复。 |
附件
| 文件名 | 版本 | 描述 |
|---|---|---|
| NFPTimeManager.mqh | 1.0 | 一个支持夏令时(DST)的时间工具类,用于集中处理所有NFP的时间戳计算,以及美国东部时间(ET)、协调世界时(UTC)和经纪商服务器时间之间的转换。主要功能包括检测服务器时区、计算NFP发布的当地钟表时间(美国东部时间每月第一个星期五08:30)、在美国东部时间与服务器时间之间转换、获取最近一次或下一次NFP发布的服务器时间戳,以及将NFP发布时间映射到M5 K线开盘时间和K线索引的辅助功能。该类旨在支持复用和单元测试,使EA无需关心时区处理。 |
| Post-NFP Fibonacci Trader EA.mq5 | 1.0 | 一款功能完整的EA,利用NFPTimeManager检测NFP发布后首根M5 K线的收盘,以该K线为锚点绘制斐波那契回撤线,并在38.2%、50%和61.8%的斐波那契回撤位下达挂单。功能包括止损(SL)/止盈(TP)缓冲、严格模式与自适应模式下的止损处理、经纪商交易限制校验(SYMBOL_TRADE_STOPS_LEVEL、SYMBOL_POINT、digits)、订单号跟踪、自动清理(成交时或达到可配置的到期期限后),以及用于追溯的详细日志记录。可通过输入参数进行配置;已在策略测试器和模拟环境中进行测试。 |
本文由MetaQuotes Ltd译自英文
原文地址: https://www.mql5.com/en/articles/19496
注意: MetaQuotes Ltd.将保留所有关于这些材料的权利。全部或部分复制或者转载这些材料将被禁止。
本文由网站的一位用户撰写,反映了他们的个人观点。MetaQuotes Ltd 不对所提供信息的准确性负责,也不对因使用所述解决方案、策略或建议而产生的任何后果负责。