效果示意图 · AI 根据源码绘制
Trend_Channel_RSI_Label.mq5223 行
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#property copyright "XAUUSD Indicator Workshop"
#property version "1.0"
#property strict
#property indicator_chart_window
#property indicator_buffers 5
#property indicator_plots 4
// ============ 输入参数 ============
input int FastMA_Period = 20; // 快速MA周期(贴近价格)
input int SlowMA_Period = 50; // 慢速MA周期(平滑趋势线)
input ENUM_MA_METHOD MA_Method = MODE_EMA; // MA类型:MODE_SMA, MODE_EMA, MODE_SMMA, MODE_LWMA
input int Channel_Period = 20; // 通道周期(计算标准差)
input double Channel_StdDev = 2.0; // 通道宽度(标准差倍数,默认2.0)
input int RSI_Period = 14; // RSI周期
input double RSI_Overbought = 70; // RSI超买阈值(标注点位)
input double RSI_Oversold = 30; // RSI超卖阈值(标注点位)
input bool Show_RSI_Labels = true; // 是否显示RSI数值标签
input color Channel_Fill_Color = clrLightBlue; // 通道填充颜色
input int Channel_Transparency = 85; // 通道透明度(0-100,越大越透明)
// ============ 指标缓冲区 ============
double FastMA_Buffer[]; // 快速MA(青色)
double SlowMA_Buffer[]; // 慢速MA(浅蓝)
double UpperChannel_Buffer[]; // 上通道线
double LowerChannel_Buffer[]; // 下通道线
double RSI_Buffer[]; // RSI值(不显示,用于计算)
// ============ 全局句柄 ============
int FastMA_Handle = INVALID_HANDLE;
int SlowMA_Handle = INVALID_HANDLE;
int RSI_Handle = INVALID_HANDLE;
// ============ OnInit 初始化 ============
int OnInit()
{
// 创建快速MA句柄
FastMA_Handle = iMA(_Symbol, PERIOD_CURRENT, FastMA_Period, 0, MA_Method, PRICE_CLOSE);
if (FastMA_Handle == INVALID_HANDLE) {
Alert("快速MA创建失败");
return INIT_FAILED;
}
// 创建慢速MA句柄
SlowMA_Handle = iMA(_Symbol, PERIOD_CURRENT, SlowMA_Period, 0, MA_Method, PRICE_CLOSE);
if (SlowMA_Handle == INVALID_HANDLE) {
Alert("慢速MA创建失败");
return INIT_FAILED;
}
// 创建RSI句柄
RSI_Handle = iRSI(_Symbol, PERIOD_CURRENT, RSI_Period, PRICE_CLOSE);
if (RSI_Handle == INVALID_HANDLE) {
Alert("RSI创建失败");
return INIT_FAILED;
}
// 设置缓冲区数组为时间序列(0=最新)
ArraySetAsSeries(FastMA_Buffer, true);
ArraySetAsSeries(SlowMA_Buffer, true);
ArraySetAsSeries(UpperChannel_Buffer, true);
ArraySetAsSeries(LowerChannel_Buffer, true);
ArraySetAsSeries(RSI_Buffer, true);
// 绑定缓冲区
SetIndexBuffer(0, FastMA_Buffer, INDICATOR_DATA);
SetIndexBuffer(1, SlowMA_Buffer, INDICATOR_DATA);
SetIndexBuffer(2, UpperChannel_Buffer, INDICATOR_DATA);
SetIndexBuffer(3, LowerChannel_Buffer, INDICATOR_DATA);
SetIndexBuffer(4, RSI_Buffer, INDICATOR_CALCULATIONS);
// 快速MA - 青色粗线
PlotIndexSetString(0, PLOT_LABEL, "FastMA");
PlotIndexSetInteger(0, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(0, PLOT_LINE_COLOR, clrCyan);
PlotIndexSetInteger(0, PLOT_LINE_WIDTH, 2);
PlotIndexSetInteger(0, PLOT_LINE_STYLE, STYLE_SOLID);
// 慢速MA - 浅蓝色粗线
PlotIndexSetString(1, PLOT_LABEL, "SlowMA");
PlotIndexSetInteger(1, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(1, PLOT_LINE_COLOR, clrLightBlue);
PlotIndexSetInteger(1, PLOT_LINE_WIDTH, 2);
PlotIndexSetInteger(1, PLOT_LINE_STYLE, STYLE_SOLID);
// 上通道线 - 浅蓝色虚线
PlotIndexSetString(2, PLOT_LABEL, "Upper Channel");
PlotIndexSetInteger(2, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(2, PLOT_LINE_COLOR, clrLightBlue);
PlotIndexSetInteger(2, PLOT_LINE_WIDTH, 1);
PlotIndexSetInteger(2, PLOT_LINE_STYLE, STYLE_DOT);
// 下通道线 - 浅蓝色虚线
PlotIndexSetString(3, PLOT_LABEL, "Lower Channel");
PlotIndexSetInteger(3, PLOT_DRAW_TYPE, DRAW_LINE);
PlotIndexSetInteger(3, PLOT_LINE_COLOR, clrLightBlue);
PlotIndexSetInteger(3, PLOT_LINE_WIDTH, 1);
PlotIndexSetInteger(3, PLOT_LINE_STYLE, STYLE_DOT);
IndicatorSetString(INDICATOR_SHORTNAME, "Trend_Channel_RSI");
IndicatorSetInteger(INDICATOR_DIGITS, _Digits);
return INIT_SUCCEEDED;
}
// ============ 计算标准差 ============
double CalculateStdDev(const double &values[], int size)
{
if (size <= 1) return 0;
double sum = 0;
for (int i = 0; i < size; i++) {
sum += values[i];
}
double average = sum / size;
double variance = 0;
for (int i = 0; i < size; i++) {
double diff = values[i] - average;
variance += diff * diff;
}
variance /= size;
return MathSqrt(variance);
}
// ============ OnCalculate 主计算 ============
int OnCalculate(const int rates_total,
const int prev_calculated,
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 (rates_total < MathMax(FastMA_Period, SlowMA_Period) + Channel_Period) {
return 0; // 数据不足
}
// 设置价格数组为时间序列
ArraySetAsSeries(close, true);
ArraySetAsSeries(time, true);
int limit = rates_total - prev_calculated;
if (limit == 0) limit = 1; // 只计算最新K线
if (prev_calculated == 0) limit = rates_total - 1; // 首次计算全部
// 复制快速MA
if (CopyBuffer(FastMA_Handle, 0, 0, rates_total, FastMA_Buffer) <= 0) {
return 0;
}
ArraySetAsSeries(FastMA_Buffer, true);
// 复制慢速MA
if (CopyBuffer(SlowMA_Handle, 0, 0, rates_total, SlowMA_Buffer) <= 0) {
return 0;
}
ArraySetAsSeries(SlowMA_Buffer, true);
// 复制RSI
if (CopyBuffer(RSI_Handle, 0, 0, rates_total, RSI_Buffer) <= 0) {
return 0;
}
ArraySetAsSeries(RSI_Buffer, true);
// 计算通道(基于收盘价的标准差)
double priceArray[];
ArrayResize(priceArray, Channel_Period);
for (int i = limit; i >= 0; i--) {
if (i + Channel_Period > rates_total) continue;
// 提取最近 Channel_Period 个收盘价
for (int j = 0; j < Channel_Period; j++) {
priceArray[j] = close[i + j];
}
// 计算标准差
double stdDev = CalculateStdDev(priceArray, Channel_Period);
// 计算通道上下轨(以快速MA为中线)
UpperChannel_Buffer[i] = FastMA_Buffer[i] + stdDev * Channel_StdDev;
LowerChannel_Buffer[i] = FastMA_Buffer[i] - stdDev * Channel_StdDev;
}
// ========== 在超买超卖点位标注RSI值 ==========
if (Show_RSI_Labels && prev_calculated != rates_total) {
for (int i = 0; i < MathMin(10, rates_total); i++) { // 只检查最近10根K线
if (RSI_Buffer[i] >= RSI_Overbought || RSI_Buffer[i] <= RSI_Oversold) {
string objName = "RSI_Label_" + TimeToString(time[i]);
// 删除旧对象
if (ObjectFind(0, objName) >= 0) {
ObjectDelete(0, objName);
}
// 创建文本标签
if (ObjectCreate(0, objName, OBJ_TEXT, 0, time[i], high[i] + 10 * _Point)) {
ObjectSetString(0, objName, OBJPROP_TEXT, "RSI_" + DoubleToString(RSI_Buffer[i], 1));
ObjectSetInteger(0, objName, OBJPROP_COLOR, (RSI_Buffer[i] >= RSI_Overbought) ? clrRed : clrLime);
ObjectSetInteger(0, objName, OBJPROP_FONTSIZE, 8);
ObjectSetInteger(0, objName, OBJPROP_ANCHOR, ANCHOR_LOWER);
}
}
}
}
return rates_total;
}
// ============ OnDeinit 清理 ============
void OnDeinit(const int reason)
{
// 释放指标句柄
if (FastMA_Handle != INVALID_HANDLE) IndicatorRelease(FastMA_Handle);
if (SlowMA_Handle != INVALID_HANDLE) IndicatorRelease(SlowMA_Handle);
if (RSI_Handle != INVALID_HANDLE) IndicatorRelease(RSI_Handle);
// 删除所有RSI标签
ObjectsDeleteAll(0, "RSI_Label_", 0, OBJ_TEXT);
}真实 MT5 运行截图服务器把编译好的 EX5 挂到黄金 H1 图表上截的,上面的封面就是照着它画的
作品说明
从图上识别到:两条移动平均线(青色快线贴近价格,浅蓝慢线较平滑)、浅蓝色半透明填充通道(类似布林带的标准差通道)、RSI数值标注(图中标记"-2 RSI_88_5",表示在特定点位显示RSI值)。这是一个趋势通道+RSI标注组合指标,用于趋势跟踪和超买超卖确认。
使用说明
安装方法
- 将代码保存为
Trend_Channel_RSI_Label.mq5到MetaTrader 5\MQL5\Indicators文件夹 - 在 MT5 MetaEditor 中按 Ctrl+F9 编译
- 编译成功后,在图表上右键 → 插入指标 → 自定义 → 选择本指标
参数调整
- FastMA_Period / SlowMA_Period:快慢MA周期,默认 20/50,可根据交易周期调整(日内短线用10/30,波段用50/200)
- Channel_StdDev:通道宽度倍数,默认 2.0(布林带标准值),震荡市场可用 1.5,趋势市场用 2.5
- RSI_Overbought / RSI_Oversold:超买超卖阈值,默认 70/30,可改为 80/20(更保守)
- Show_RSI_Labels:是否显示RSI标签,默认开启,关闭可减少图表干扰
- Channel_Transparency:通道透明度(需手动在图表属性里调整填充效果)
信号解读
- 青色线上穿浅蓝线:趋势转为多头,关注做多机会
- 价格触及下通道线 + RSI<30:强超卖信号,考虑买入
- 价格触及上通道线 + RSI>70:强超买信号,考虑卖出
- RSI标签显示:在关键点位自动标注RSI值(红色=超买,绿色=超卖)
注意事项
- 通道填充颜色需在MT5图表属性中手动设置(右键指标→属性→颜色→填充),代码仅绘制通道线
- RSI标签仅在最近10根K线上显示,避免图表过于混乱
- 强趋势中通道可能被频繁突破,此时更应关注MA方向而非通道边界
- 建议配合成交量和支撑阻力位综合判断入场时机
评论
后可以留言,比如反馈用着怎么样、想加什么功能。
还没有人评论,来抢个沙发。