MT5双周期RSI收盘确认指标:避免高周期数据泄漏的完整MQL5源码
高级交易教程 | 完整源码与原创示意图
为什么必须对齐高周期时间
M15图表使用H1 RSI时,最容易犯的错误是把尚未完成的H1数值当作已经确认的历史信息。H1在收盘前会随价格变化,直接读取可能导致历史图表和当时真实可观察信号不一致。本例逐根获取M15时间戳,再定位所在H1周期之前已经结束的H1 K线。
精确的信号规则
本例使用本周期RSI(14)上穿50且前一根已完成H1的RSI(14)>55作为多头确认;反向下穿50并且高周期RSI<45作为空头确认。程序比较索引i和i+1的两根已完成K线,不使用索引0实时形成中的K线。MQL5的iRSI返回句柄,CopyBuffer才用于读取指标数值。
可复核的数字案例
假设10:15的M15 K线完成时,本周期RSI从48.2升至52.4。10:00开始的H1此时还没有收盘,因此只能读取09:00—10:00完成的H1。如果其RSI是58.6,多头条件成立;如果是53.1,即便本周期已经上穿50,也不画箭头。这是用于讲解的假设数据。

为什么iBarShift不能省略
iBarShift负责把每根低周期K线时间映射到包含它的高周期K线;索引加1之后,才能读取此前已完成的H1数据。这同时解决历史回放中的未来数据泄漏。若CopyBuffer尚未准备好,或高周期历史不足,代码直接跳过,不用猜测值填补。
参数敏感性与误信号
默认55/45门槛比50更严格。调为60/40通常会减少信号并使确认推迟;改为52/48则更容易在震荡阶段触发交叉。RSI周期越短越敏感但噪声更多。箭头只表示条件成立,不代表策略能够按箭头价格成交,点差、滑点与手续费必须单独检验。
安装步骤和检查重点
将代码放入MQL5/Indicators,用MetaEditor编译后加载到图表。检查至少十个标记点,记录本周期RSI穿越值、M15时间和对应的前一根H1收盘时间,同时检验周末缺口和实时新K线生成。代码未在真实终端编译,正式发布前需要审核;并不保证盈利。
完整MQL5指标源码
#property indicator_chart_window
#property indicator_buffers 2
#property indicator_plots 2
#property indicator_type1 DRAW_ARROW
#property indicator_color1 clrLimeGreen
#property indicator_type2 DRAW_ARROW
#property indicator_color2 clrTomato
input int LocalPeriod=14;
input ENUM_TIMEFRAMES HigherTF=PERIOD_H1;
input int HigherPeriod=14;
input double LongGate=55.0;
input double ShortGate=45.0;
double Buy[],Sell[];
int hLocal=INVALID_HANDLE,hHigher=INVALID_HANDLE;
int OnInit()
{
SetIndexBuffer(0,Buy,INDICATOR_DATA);
SetIndexBuffer(1,Sell,INDICATOR_DATA);
ArraySetAsSeries(Buy,true); ArraySetAsSeries(Sell,true);
PlotIndexSetInteger(0,PLOT_ARROW,233);
PlotIndexSetInteger(1,PLOT_ARROW,234);
PlotIndexSetDouble(0,PLOT_EMPTY_VALUE,EMPTY_VALUE);
PlotIndexSetDouble(1,PLOT_EMPTY_VALUE,EMPTY_VALUE);
hLocal=iRSI(_Symbol,_Period,LocalPeriod,PRICE_CLOSE);
hHigher=iRSI(_Symbol,HigherTF,HigherPeriod,PRICE_CLOSE);
return(hLocal==INVALID_HANDLE||hHigher==INVALID_HANDLE)?INIT_FAILED:INIT_SUCCEEDED;
}
void OnDeinit(const int reason)
{
if(hLocal!=INVALID_HANDLE) IndicatorRelease(hLocal);
if(hHigher!=INVALID_HANDLE) IndicatorRelease(hHigher);
}
int OnCalculate(const int total,const int prev,const datetime &time[],
const double &open[],const double &high[],const double &low[],
const double &close[],const long &tick_volume[],
const long &volume[],const int &spread[])
{
if(total<LocalPeriod+4) return 0;
ArraySetAsSeries(time,true); ArraySetAsSeries(high,true);
ArraySetAsSeries(low,true);
double local[],higher[];
ArraySetAsSeries(local,true); ArraySetAsSeries(higher,true);
int count=Bars(_Symbol,HigherTF);
if(count<HigherPeriod+3) return prev;
if(CopyBuffer(hLocal,0,0,total,local)!=total) return prev;
if(CopyBuffer(hHigher,0,0,count,higher)!=count) return prev;
Buy[0]=EMPTY_VALUE;Sell[0]=EMPTY_VALUE;
int last=(prev==0)?total-2:MathMin(total-2,total-prev+2);
for(int i=last;i>=1;i--)
{
Buy[i]=EMPTY_VALUE;Sell[i]=EMPTY_VALUE;
int j=iBarShift(_Symbol,HigherTF,time[i],false);
if(j<0||j+1>=count) continue;
double context=higher[j+1]; // previous completed HTF candle
if(local[i]==EMPTY_VALUE||local[i+1]==EMPTY_VALUE) continue;
if(local[i+1]<=50&&local[i]>50&&context>LongGate)
Buy[i]=low[i]-80*_Point;
if(local[i+1]>=50&&local[i]<50&&context<ShortGate)
Sell[i]=high[i]+80*_Point;
}
return total;
}
如果希望根据自己的交易研究调整过滤器或提醒,可查看交易软件定制开发服务并联系我们。