第二十六章 技术指标

所有函数,如iMA,iAC,iMACD,iIchimoku等,在客户端的全局缓存中创建相应技术指标的副本。如果已存在具有此类参数的指标的副本,则不会创建新副本,并且对现有副本的引用计数器会增加。

这些函数返回指标的相应副本的句柄。此外,使用此句柄,您可以接收由相应指标计算的数据。可以使用CopyBuffer()函数将相应的缓冲区数据(技术指标包含其内部缓冲区中的计算数据,可以在1到5之间变化,具体取决于指标)复制到mql5程序中。

您无法在创建指标数据后立即参考指标数据,因为指标值的计算需要一些时间,因此最好在OnInit()中创建指标处理。函数iCustom()创建相应的自定义指标,并在成功创建时返回其句柄。自定义指标最多可包含512个指示缓冲区,其内容也可通过CopyBuffer()函数使用获取的句柄获得。

使用IndicatorCreate() 函数创建任意技术指标是一种通用的办法,该函数以输入参数的形式接收如下数据:
• 交易品种名称;
• 时间框架;
• 创建的指标类型;
• 指标的输入参数的数量;
• MqlParam类型数组包含所有必要的输入参量。

使用指标句柄传递到IndicatorRelease()函数,可以删除不使用的指标以便释放计算机内存。

注意。在一个MQL5程序中反复调用具有相同参数的指标函数不会导致参考计数器的多次增加; 计数器只会增加一次。但是,建议在函数OnInit() 或 类构造函数 中获取指标句柄,并在其他函数中进一步使用这些句柄。当MQL5程序被取消初始化时,引用计数器减少。

所有指标函数都有至少2个参数 —— 交易品种 和 时间周期。交易品种 NULL 值代表当前交易品种,周期0值代表当前时间表。

函数 返回指标处理器
iAC 加速震荡指标
iAD 累积/分配
iADX 平均定向指数
iADXWilder 韦尔达平均定向指数
iAlligator 鳄鱼指标
iAMA 适合的移动平均数
iAO 动量震荡指标
iATR 平均真实区域
iBearsPower 熊市
iBands 布林带
iBullsPower 牛市
iCCI 商品通道指数
iChaikin 蔡金摆动指标
iCustom 自定义指标
iDEMA 双指数移动平均线
iDeMarker 指标
iEnvelopes 轨道线指标
iForce 强力指数
iFractals 分形学
iFrAMA 分形学适合移动平均数
iGator 鳄鱼振荡器
iIchimoku 一目均衡图
iBWMFI 威廉姆斯的市场便利指标
iMomentum 动量指标
iMFI 货币流量指标
iMA 移动平均数
iOsMA 移动平均振荡指标(MACD柱状图)
iMACD 移动平均聚散指标
iOBV 平衡交易量
iSAR 抛物转向系统
iRSI 相对强弱指标
iRVI 相对活力指标
iStdDev 标准偏差
iStochastic 随机摆动指标
iTEMA 三倍指数移动平均数
iTriX 三倍指数移动平均数振荡指标
iWPR 威廉指数
iVIDyA 动态平均数便利指标
iVolumes 成交量

# 26.1 iAC

此该函数在客户端的全局缓存中创建 加速振荡(Accelerator Oscillator)指标 并返回其句柄。 它只有一个缓冲区。

int  iAC( 
   string           symbol,     // 交易品种名称 
   ENUM_TIMEFRAMES  period      // 周期 
   );
1
2
3
4

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                     Demo_iAC.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iAC technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   1 
//--- iAC标图 
#property indicator_label1  "iAC" 
#property indicator_type1   DRAW_COLOR_HISTOGRAM 
#property indicator_color1  clrGreen, clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iAC,               // 使用iAC 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iAC;          // 函数类型  
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double iACBuffer[]; 
double iACColors[]; 
//--- 存储iAC 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在加速震荡指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iACBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,iACColors,INDICATOR_COLOR_INDEX); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iAC) 
      handle=iAC(name,period); 
   else 
      handle=IndicatorCreate(name,period,IND_AC); 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iAC indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示加速震荡指标计算的交易品种/时间帧 
   short_name=StringFormat("iAC(%s/%s)",name,EnumToString(period)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iAC 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iAC指标数量值更改 
//---或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iACBuffer 数组大于交易品种/周期iAC 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 用来自加速震荡指标的值填充iACBuffer 和 iACColors 数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffer(iACBuffer,iACColors,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住动量震荡指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iAC 指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffer(double &values[],        // 加速震荡指标值指标缓冲区 
                          double &color_indexes[], // 颜色缓冲区(用于存储颜色标引) 
                          int ind_handle,          // iAC 指标的处理程序 
                          int amount               // 复制值的数量 
                          ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分 iACBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAC indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 现在复制颜色标引 
   if(CopyBuffer(ind_handle,1,0,amount,color_indexes)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy color values from the iAC indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.2 iAD

该函数返回 累积/分布(Accumulation/Distribution) 指标的句柄。 它只有一个缓冲区。

int  iAD( 
   string               symbol,             // 交易品种名称 
   ENUM_TIMEFRAMES      period,             // 周期 
   ENUM_APPLIED_VOLUME  applied_volume      // 用于计算的交易量类型 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举值之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                     Demo_iAD.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iAD technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 标图 iAD 
#property indicator_label1  "iAD" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iAD,               // 使用iAD 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iAD;          // 函数类型 
input ENUM_APPLIED_VOLUME  volumes;                // 使用的交易量 
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         iADBuffer[]; 
//--- 存储 iAD 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在离散指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iADBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iAD) 
      handle=iAD(name,period,volumes); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=volumes; 
      handle=IndicatorCreate(name,period,IND_AD,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iAD indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示离散指标计算的交易品种/时间帧 
   short_name=StringFormat("iAD(%s/%s)",name,EnumToString(period)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iAD 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iAD指标数量值更改 
//---或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iADBuffer 数组大于交易品种/周期iAD 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 用来自离散指标的值填充iADBuffer数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iADBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住离散指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iAD 指标的指标缓冲区                                           | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],   // 离散指标线的指标缓冲区 
                          int ind_handle,    // iAD指标的处理程序 
                          int amount         // 复制值的数量 
                          ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iADBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAD indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  }   
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.3 iADX

该函数返回 平均方向运动(Average Directional Movement Index)指数 指标的句柄。

int  iADX( 
   string           symbol,         // 交易品种名称 
   ENUM_TIMEFRAMES  period,         // 周期 
   int              adx_period      // 平均周期 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

adx_period

[in] 周期计算指数。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下: 0 —— MAIN_LINE, 1 —— PLUSDI_LINE, 2 —— MINUSDI_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iADX.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iADX technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 3 
#property indicator_plots   3 
//--- 标图 ADX 
#property indicator_label1  "ADX" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 标图 DI_plus 
#property indicator_label2  "DI_plus" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrYellowGreen 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- 标图 DI_minus 
#property indicator_label3  "DI_minus" 
#property indicator_type3   DRAW_LINE 
#property indicator_color3  clrWheat 
#property indicator_style3  STYLE_SOLID 
#property indicator_width3  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iADX,              // 使用iADX 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iADX;         // 函数类型 
input int                  adx_period=14;          // 计算周期 
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         ADXBuffer[]; 
double         DI_plusBuffer[]; 
double         DI_minusBuffer[]; 
//--- 存储iADX 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在平均方向移动指数指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,ADXBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,DI_plusBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,DI_minusBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iADX) 
      handle=iADX(name,period,adx_period); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=adx_period; 
      handle=IndicatorCreate(name,period,IND_ADX,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iADX indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示平均方向移动指数指标计算的交易品种/时间帧 
   short_name=StringFormat("iADX(%s/%s period=%d)",name,EnumToString(period),adx_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化     
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iADX 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iADX 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iADXBuffer 数组大于交易品种/周期 iADX 指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以平均方向移动指数指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(ADXBuffer,DI_plusBuffer,DI_minusBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住平均方向移动指数指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iADX指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &adx_values[],      // ADX 线的指标缓冲区 
                           double &DIplus_values[],   // DI+指标缓冲区 
                           double &DIminus_values[],  // DI- 指标缓冲区 
                           int ind_handle,            // iADXWilder 指标的处理程序 
                           int amount                 // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分 iADXBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,adx_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分 DI_plusBuffer 数组 
   if(CopyBuffer(ind_handle,1,0,amount,DIplus_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以2标引指标缓冲区的值填充部分 DI_plusBuffer 数组 
   if(CopyBuffer(ind_handle,2,0,amount,DIminus_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iADX indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.4 iADXWilder

该函数返回 韦尔达平均定向移动指数(Welles Wilder)的句柄。

int  iADXWilder( 
   string           symbol,         // 交易品种名称 
   ENUM_TIMEFRAMES  period,         // 周期 
   int              adx_period      // 平均周期 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

adx_period

[in] 周期计算指数。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下: 0 - MAIN_LINE, 1 - PLUSDI_LINE, 2 - MINUSDI_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                                   iADXWilder.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iADXWilder technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 3 
#property indicator_plots   3 
//--- 标图 ADX 
#property indicator_label1  "ADX" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 标图 DI_plus 
#property indicator_label2  "DI_plus" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrYellowGreen 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- 标图 DI_minus 
#property indicator_label3  "DI_minus" 
#property indicator_type3   DRAW_LINE 
#property indicator_color3  clrWheat 
#property indicator_style3  STYLE_SOLID 
#property indicator_width3  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iADXWilder,        // 使用iADXWilder 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iADXWilder;   // 函数类型  
input int                  adx_period=14;          // 计算周期 
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         ADXBuffer[]; 
double         DI_plusBuffer[]; 
double         DI_minusBuffer[]; 
//--- 存储iADXWilder 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在平均方向移动指数Welles Wilder指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,ADXBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,DI_plusBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,DI_minusBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iADXWilder) 
      handle=iADXWilder(name,period,adx_period); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=adx_period; 
      handle=IndicatorCreate(name,period,IND_ADXW,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iADXWilder indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示平均方向移动指数Welles Wilder指标计算的交易品种/时间帧 
   short_name=StringFormat("iADXWilder(%s/%s period=%d)",name,EnumToString(period),adx_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化     
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iADXWilder 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iADXWilder 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iADXBuffer 数组大于交易品种/周期 iADXWilder 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以平均方向移动指数Welles Wilder指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(ADXBuffer,DI_plusBuffer,DI_minusBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住平均方向移动指数指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iADXWilder 指标的指标缓冲区                                     | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &adx_values[],      // ADX 线的指标缓冲区 
                           double &DIplus_values[],   // DI+指标缓冲区 
                           double &DIminus_values[],  // DI- 指标缓冲区 
                           int ind_handle,            // iADXWilder 指标的处理程序 
                           int amount                 // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分 iADXBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,adx_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iADXWilder indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分 DI_plusBuffer 数组 
   if(CopyBuffer(ind_handle,1,0,amount,DIplus_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iADXWilder indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以2标引指标缓冲区的值填充部分 DI_plusBuffer 数组 
   if(CopyBuffer(ind_handle,2,0,amount,DIminus_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iADXWilder indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.5 iAlligator

该函数返回 鳄鱼(Alligator)指标的句柄。

int  iAlligator( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 jaw_period,        // 咽喉计算周期 
   int                 jaw_shift,         // 咽喉平移 
   int                 teeth_period,      // 牙齿计算周期 
   int                 teeth_shift,       // 牙齿平移 
   int                 lips_period,       // 唇部计算周期 
   int                 lips_shift,        // 唇部平移 
   ENUM_MA_METHOD      ma_method,         // 平滑类型 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7
8
9
10
11
12

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

jaw_period

[in] 蓝线的平均周期(鳄鱼颌骨)

jaw_shift

[in] 关于价格图表的蓝线平移转换。

teeth_period

[in] 红线的平均周期(鳄鱼牙)。

teeth_shift

[in] 关于价格图表的红线平移转换。

lips_period

[in] 绿线的平均周期 (鳄鱼唇部)。

lips_shift

[in] 关于价格图表的绿线平移转换。

ma_method

[in] 求平均值的方式,取值范围是 ENUM_MA_METHOD 枚举值之一。

applied_price

[in] 使用价格。取值范围是 ENUM_APPLIED_PRICE 枚举值之一 或 另外的指标句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下: 0 - GATORJAW_LINE, 1 - GATORTEETH_LINE, 2 - GATORLIPS_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                              Demo_iAlligator.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iAlligator technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Alligator." 
  
#property indicator_chart_window 
#property indicator_buffers 3 
#property indicator_plots   3 
//--- 标图咽喉 
#property indicator_label1  "Jaws" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 标图牙齿 
#property indicator_label2  "Teeth" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- 标图唇部 
#property indicator_label3  "Lips" 
#property indicator_type3   DRAW_LINE 
#property indicator_color3  clrLime 
#property indicator_style3  STYLE_SOLID 
#property indicator_width3  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iAlligator,        // 使用iAlligator 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iAlligator;   // 函数类型  
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
input int                  jaw_period=13;          // 咽喉线周期 
input int                  jaw_shift=8;            // 咽喉线移动 
input int                  teeth_period=8;         // 牙齿线周期 
input int                  teeth_shift=5;          // 牙齿线移动 
input int                  lips_period=5;          // 唇部线周期 
input int                  lips_shift=3;           // 唇部线移动 
input ENUM_MA_METHOD       MA_method=MODE_SMMA;    // 鳄鱼平均线的方法 
input ENUM_APPLIED_PRICE   applied_price=PRICE_MEDIAN;// 用于计算鳄鱼的价格类型 
//--- 指标缓冲区 
double         JawsBuffer[]; 
double         TeethBuffer[]; 
double         LipsBuffer[]; 
//--- 存储 iAlligator 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在鳄鱼指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,JawsBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,TeethBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,LipsBuffer,INDICATOR_DATA); 
//--- 设置每条线的移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,jaw_shift); 
   PlotIndexSetInteger(1,PLOT_SHIFT,teeth_shift); 
   PlotIndexSetInteger(2,PLOT_SHIFT,lips_shift); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iAlligator) 
      handle=iAlligator(name,period,jaw_period,jaw_shift,teeth_period, 
                        teeth_shift,lips_period,lips_shift,MA_method,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[8]; 
     //--- 鳄鱼线的周期和移动 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=jaw_period; 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=jaw_shift; 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=teeth_period; 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=teeth_shift; 
      pars[4].type=TYPE_INT; 
      pars[4].integer_value=lips_period; 
      pars[5].type=TYPE_INT; 
      pars[5].integer_value=lips_shift; 
//--- 平滑类型 
      pars[6].type=TYPE_INT; 
      pars[6].integer_value=MA_method; 
//--- 价格类型 
      pars[7].type=TYPE_INT; 
      pars[7].integer_value=applied_price; 
//--- 创建处理程序 
      handle=IndicatorCreate(name,period,IND_ALLIGATOR,8,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iAlligator indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示鳄鱼指标计算的交易品种/时间帧 
   short_name=StringFormat("iAlligator(%s/%s, %d,%d,%d,%d,%d,%d)",name,EnumToString(period), 
                           jaw_period,jaw_shift,teeth_period,teeth_shift,lips_period,lips_shift); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化     
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iAlligator 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iAlligator指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果JawsBuffer 数组大于交易品种/周期 iAlligator 指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以鳄鱼指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(JawsBuffer,jaw_shift,TeethBuffer,teeth_shift,LipsBuffer,lips_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住鳄鱼指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  }   
//+------------------------------------------------------------------+ 
//| 填充 iAlligator指标的指标缓冲区                                     | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &jaws_buffer[],  // 咽喉线的指标缓冲区 
                           int j_shift,            // 咽喉线移动 
                           double &teeth_buffer[], // 牙齿线的指标缓冲区  
                           int t_shift,            // 牙齿线移动 
                           double &lips_buffer[],  // 唇部线的指标缓冲区 
                           int l_shift,            // 唇部线移动 
                           int ind_handle,         // iAlligator 指标的处理程序 
                           int amount              // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分JawsBuffer 数组 
   if(CopyBuffer(ind_handle,0,-j_shift,amount,jaws_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分TeethBuffer 数组 
   if(CopyBuffer(ind_handle,1,-t_shift,amount,teeth_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以2标引指标缓冲区的值填充部分 LipsBuffer 数组 
   if(CopyBuffer(ind_handle,2,-l_shift,amount,lips_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAlligator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  }   
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245

# 26.6 iAMA

该函数返回 自适应移动平均线(Adaptive Moving Average) 指标的句柄。 它只有一个缓冲区。

int  iAMA( 
   string              symbol,             // 交易品种名称 
   ENUM_TIMEFRAMES     period,             // 周期 
   int                 ama_period,         //  AMA平均周期 
   int                 fast_ma_period,     // 快速 MA 周期 
   int                 slow_ma_period,     // 慢速 MA 周期 
   int                 ama_shift,          // 指标平移 
   ENUM_APPLIED_PRICE  applied_price       // 价格或者处理器类型 
   );
1
2
3
4
5
6
7
8
9

参数

symbol

[in] 证券交易品种名称,数据用来计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值可以是 ENUM_TIMEFRAMES 值中的一个,0代表当前时间表。

ama_period

[in] 计算周期,计算效率系数。

fast_ma_period

[in] 在快速市场为平滑系数计算快速周期。

slow_ma_period

[in] 在缺乏趋势时为平滑系数计算的慢周期。

ama_shift

[in] 转换关于价格图表的指标。

applied_price

[in] 使用价格。取值范围是 ENUM_APPLIED_PRICE 枚举的价格常量 或者 另外指标句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iAMA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iAMA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard AMA." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 标图 iAMA 
#property indicator_label1  "iAMA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iAMA,              // 使用iAMA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iAMA;          // 函数类型  
input string               symbol=" ";              // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;   // 时间帧 
input int                  ama_period=15;           // 计算周期 
input int                  fast_ma_period=2;        // 快速MA周期 
input int                  slow_ma_period=30;       // 慢速MA周期 
input int                  ama_shift=0;             // 水平移动 
input ENUM_APPLIED_PRICE   applied_price;           // 价格类型 
//--- 指标缓冲区 
double         iAMABuffer[]; 
//--- 存储 iAMA 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在适当移动平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 指标缓冲区映射 
   SetIndexBuffer(0,iAMABuffer,INDICATOR_DATA); 
//--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ama_shift);    
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iAMA) 
      handle=iAMA(name,period,ama_period,fast_ma_period,slow_ma_period,ama_shift,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[5]; 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ama_period; 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=fast_ma_period; 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=slow_ma_period; 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=ama_shift; 
      //--- 价格类型 
      pars[4].type=TYPE_INT; 
      pars[4].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_AMA,5,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iAMA indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示适当移动平均指标计算的交易品种/时间帧 
   short_name=StringFormat("iAMA(%s/%s,%d,%d,%d,d)",name,EnumToString(period),ama_period,fast_ma_period,slow_ma_period,ama_shift); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化     
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iAMA 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iAMA 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iAMABuffer 数组大于交易品种/周期iAMA指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以适应移动平均指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iAMABuffer,ama_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住适当移动平均指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iAMA指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &ama_buffer[],  //AMA线的指标缓冲区 
                         int a_shift,           // AMA线移动 
                         int ind_handle,        // iAMA 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分 iAMABuffer 数组 
   if(CopyBuffer(ind_handle,0,-a_shift,amount,ama_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAMA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.7 iAO

该函数返回动量震荡(Awesome Oscillator)指标的句柄。它只有一个缓冲区。

int  iAO( 
   string           symbol,     // 交易品种名称 
   ENUM_TIMEFRAMES  period      // 周期 
   );
1
2
3
4

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                     Demo_iAO.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iAO technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   1 
//--- iAO 标图 
#property indicator_label1  "iAO" 
#property indicator_type1   DRAW_COLOR_HISTOGRAM 
#property indicator_color1  clrGreen,clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iAO,               // 使用iAO 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iAO;          // 函数类型  
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         iAOBuffer[]; 
double         iAOColors[]; 
//--- 存储 iAO指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在动量震荡指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iAOBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,iAOColors,INDICATOR_COLOR_INDEX); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iAO) 
      handle=iAO(name,period); 
   else 
      handle=IndicatorCreate(name,period,IND_AO); 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iAO indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示动量震荡指标计算的交易品种/时间帧 
   short_name=StringFormat("iAO(%s/%s)",name,EnumToString(period)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iAO 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iAO 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iAOBuffer数组大于交易品种/周期iAO指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 用来自动量震荡指标的值填充iAOBuffer 和 iAOColors数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffer(iAOBuffer,iAOColors,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住动量震荡指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iAO 指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffer(double &values[],        //动量震荡指标值的指标缓冲区 
                          double &color_indexes[], // 颜色缓冲区(用于存储颜色标引) 
                          int ind_handle,          // iAO 指标的处理程序 
                          int amount               // 复制值的数量 
                          ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//---以0标引指标缓冲区的值填充部分iAOBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iAO indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 现在复制颜色标引 
   if(CopyBuffer(ind_handle,1,0,amount,color_indexes)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy color values from the iAO indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.8 iATR

该函数返回平均真实区域(Average True Range)指标的句柄。它只有一个缓冲区。

int  iATR( 
   string           symbol,        // 交易品种名称 
   ENUM_TIMEFRAMES  period,        // 周期 
   int              ma_period      // 平均周期  
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算平均周期值。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iATR.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iATR technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 标图 iATR 
#property indicator_label1  "iATR" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iATR,// 使用 iATR 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input int                  atr_period=14;          // 计算周期 
input Creation             type=Call_iATR;         // 函数类型  
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         iATRBuffer[]; 
//--- 存储iAC 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在平均真实区域指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iATRBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iATR) 
      handle=iATR(name,period,atr_period); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=atr_period; 
      handle=IndicatorCreate(name,period,IND_ATR,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iATR indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示平均真实范围指标计算的交易品种/时间帧 
   short_name=StringFormat("iATR(%s/%s, period=%d)",name,EnumToString(period),atr_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iATR指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iATR 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iATRBuffer 数组大于交易品种/周期iATR指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以平均真实波动范围指标的值填充 iATRBuffer 数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iATRBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住动量震荡指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iATR 指标的指标缓冲区                                           | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // ATR值的指标缓冲区 
                         int ind_handle,    // iATR指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iATRBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iATR indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.9 iBearsPower

该函数返回 熊市(Bears Power )指标句柄。它只有一个缓冲区。

int  iBearsPower( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算平均周期值。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                             Demo_iBearsPower.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iBearsPower technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iBearsPower 标图 
#property indicator_label1  "iBearsPower" 
#property indicator_type1   DRAW_HISTOGRAM 
#property indicator_color1  clrSilver 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iBearsPower,       // 使用iBearsPower 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iBearsPower;  // 函数类型  
input int                  ma_period=13;           // 平均移动周期 
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         iBearsPowerBuffer[]; 
//--- 存储 iBearsPower 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在熊市指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iBearsPowerBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iBearsPower) 
      handle=iBearsPower(name,period,ma_period); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      //--- ma周期       
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      handle=IndicatorCreate(name,period,IND_BEARS,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iBearsPower indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示熊市指标计算的交易品种/时间帧 
   short_name=StringFormat("iBearsPower(%s/%s, period=%d)",name,EnumToString(period),ma_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iBearsPower指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iBearsPower 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iBearsPowerBuffer数组大于交易品种/周期 iBearsPower 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以熊市指标的值填充iBearsPowerBuffer 数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iBearsPowerBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住熊市指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iBearsPower 指标的指标缓冲区                                   | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 熊市值的指标缓冲区 
                         int ind_handle,    // iBearsPower 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iBearsPowerBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBearsPower indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.10 iBands

函数返回 布林带(Bollinger Bands®) 指标的句柄。

int  iBands( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 bands_period,      // 平均线计算周期 
   int                 bands_shift,       // 指标平移 
   double              deviation,         // 标准差数 
   ENUM_APPLIED_PRICE  applied_price      // 价格或处理器类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

bands_period

[in] 指标主线的平均周期。

bands_shift

[in] 相关价格图表的指标转换。

deviation

[in] 偏离值。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下: 0 - BASE_LINE, 1 - UPPER_BAND, 2 - LOWER_BAND。

例如:

//+------------------------------------------------------------------+ 
//|                                                  Demo_iBands.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iBands technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_chart_window 
#property indicator_buffers 3 
#property indicator_plots   3 
//--- 上标图 
#property indicator_label1  "Upper" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrMediumSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 下标图 
#property indicator_label2  "Lower" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrMediumSeaGreen 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- 中间标图 
#property indicator_label3  "Middle" 
#property indicator_type3   DRAW_LINE 
#property indicator_color3  clrMediumSeaGreen 
#property indicator_style3  STYLE_SOLID 
#property indicator_width3  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iBands,            // 使用iBands 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iBands;          // 函数的类型  
input int                  bands_period=20;           // 平均移动周期 
input int                  bands_shift=0;             // 移动 
input double               deviation=2.0;             //标准偏差数  
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         UpperBuffer[]; 
double         LowerBuffer[]; 
double         MiddleBuffer[]; 
//--- 存储 iBands 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在布林带指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA); 
   SetIndexBuffer(2,MiddleBuffer,INDICATOR_DATA); 
//--- 设置每条线的移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,bands_shift); 
   PlotIndexSetInteger(1,PLOT_SHIFT,bands_shift);       
   PlotIndexSetInteger(2,PLOT_SHIFT,bands_shift);       
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iBands) 
      handle=iBands(name,period,bands_period,bands_shift,deviation,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[4]; 
      //--- ma周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=bands_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=bands_shift; 
      //--- 标准偏差数 
      pars[2].type=TYPE_DOUBLE; 
      pars[2].double_value=deviation; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_BANDS,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iBands indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示布林带指标计算的交易品种/时间帧 
   short_name=StringFormat("iBands(%s/%s, %d,%d,%G,%s)",name,EnumToString(period), 
                           bands_period,bands_shift,deviation,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iBands 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iBands 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果指标缓冲区的大小大于交易品种/周期iBands 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以布林带指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(MiddleBuffer,UpperBuffer,LowerBuffer,bands_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住布林带指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iBands指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &base_values[],     // 布林带中间线的指标缓冲区 
                           double &upper_values[],    // 上边界的指标缓冲区 
                           double &lower_values[],    // 下边界的指标缓冲区 
                           int shift,                 // 移动 
                           int ind_handle,            // iBands 指标的处理程序 
                           int amount                 // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分MiddleBuffer数组 
   if(CopyBuffer(ind_handle,0,-shift,amount,base_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分UpperBuffer 数组 
   if(CopyBuffer(ind_handle,1,-shift,amount,upper_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以2标引指标缓冲区的值填充部分 LowerBuffer 数组 
   if(CopyBuffer(ind_handle,2,-shift,amount,lower_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBands indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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
224
225
226
227
228
229

# 26.11 iBullsPower

函数返回 牛市(Bulls Power )指标的句柄。它只有一个缓冲区。

int  iBullsPower( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算平均周期值。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                             Demo_iBullsPower.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iBullsPower technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iBullsPower标图 
#property indicator_label1  "iBullsPower" 
#property indicator_type1   DRAW_HISTOGRAM 
#property indicator_color1  clrSilver 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iBullsPower,       // 使用iBullsPower 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iBullsPower;  // 函数类型  
input int                  ma_period=13;           // 平均移动周期 
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
//--- 指标缓冲区 
double         iBullsPowerBuffer[]; 
//--- 存储 iBullsPower指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在牛市指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iBullsPowerBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iBullsPower) 
      handle=iBullsPower(name,period,ma_period); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      //--- ma周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      handle=IndicatorCreate(name,period,IND_BULLS,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iBullsPower indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示牛市指标计算的交易品种/时间帧 
   short_name=StringFormat("iBullsPower(%s/%s, period=%d)",name,EnumToString(period),ma_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iBullsPower 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iBullsPower 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iBullsPowerBuffer 数组大于交易品种/周期 iBullsPower 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以牛市指标的值填充iBullsPowerBuffer 数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iBullsPowerBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住牛市指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iBullsPower 指标的指标缓冲区                                   | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 牛市值的指标缓冲区 
                         int ind_handle,    // iBullsPower 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iBullsPowerBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBullsPower indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  } 
//+------------------------------------------------------------------+
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

# 26.12 iCCI

函数返回 商品通道指数(Commodity Channel Index)指标的句柄。它只有一个缓冲区。

int  iCCI( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   ENUM_APPLIED_PRICE  applied_price      // 价格或处理器类型 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算平均周期值。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iCCI.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iCCI technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iCCI 标图 
#property indicator_label1  "iCCI" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 指标窗口的横向水平 
#property indicator_level1  -100.0 
#property indicator_level2  100.0 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iCCI,              // 使用iCCI 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iCCI;               // 函数类型 
input int                  ma_period=14;                 // 平均移动周期 
input ENUM_APPLIED_PRICE   applied_price=PRICE_TYPICAL;  // 价格类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iCCIBuffer[]; 
//--- 存储 iCCI 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在商品通道指数指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iCCIBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iCCI) 
      handle=iCCI(name,period,ma_period,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[2]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 价格类型 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_CCI,2,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iCCI indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示布林带® 指标计算的交易品种/时间帧 
   short_name=StringFormat("iCCI(%s/%s, %d, %s)",name,EnumToString(period), 
                           ma_period,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iCCI 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iCCI指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iCCIBuffer 数组大于交易品种/周期 iCCI 指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以商品通道指数指标的值填充iCCIBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iCCIBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住商品通道指数指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iCCI指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 商品通道指数值的指标缓冲区 
                         int ind_handle,    // iCCI 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iCCIBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iCCI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.13 iChaikin

函数返回蔡金摆动指标的处理句柄。它只有一个缓冲区。

int  iChaikin( 
   string               symbol,             // 交易品种名称 
   ENUM_TIMEFRAMES      period,             // 周期 
   int                  fast_ma_period,     // 快速周期 
   int                  slow_ma_period,     // 慢速周期 
   ENUM_MA_METHOD       ma_method,          // 平滑类型 
   ENUM_APPLIED_VOLUME  applied_volume      // 交易量类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

fast_ma_period

[in] 计算快速平均周期。

slow_ma_period

[in] 计算慢速平均周期。

ma_method

[in] 平滑类型。取值范围是 ENUM_MA_METHOD枚举的平均常量之一。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举常量之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                Demo_iChaikin.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iChaikin technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iChaikin标图 
#property indicator_label1  "iChaikin" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iChaikin,          // 使用iChaikin 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iChaikin;           // 函数类型  
input int                  fast_ma_period=3;             // 快速ma周期 
input int                  slow_ma_period=10;            // 慢速ma周期 
input ENUM_MA_METHOD       ma_method=MODE_EMA;           // 平滑类型 
input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;   // 交易量类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iChaikinBuffer[]; 
//--- 存储 iChaikin 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在蔡金摆动指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iChaikinBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iChaikin) 
      handle=iChaikin(name,period,fast_ma_period,slow_ma_period,ma_method,applied_volume); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[4]; 
      //--- 快速ma周期  
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=fast_ma_period; 
      //--- 慢速ma周期 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=slow_ma_period; 
      //--- 平滑类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=ma_method; 
      //--- 交易量类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_volume; 
      handle=IndicatorCreate(name,period,IND_CHAIKIN,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iChaikin indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示蔡金摆动指标计算的交易品种/时间帧 
   short_name=StringFormat("iChaikin(%s/%s, %d, %d, %s, %s)",name,EnumToString(period), 
                           fast_ma_period,slow_ma_period, 
                           EnumToString(ma_method),EnumToString(applied_volume)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iChaikin 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iChaikin 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iChaikinBuffer 数组大于交易品种/周期iChaikin 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以蔡金摆动指标的值填充iChaikinBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iChaikinBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住蔡金摆动指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iChaikin 指标的指标缓冲区                                      | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 蔡金摆动指标值的指标缓冲区 
                         int ind_handle,    // iChaikin 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//---以0标引指标缓冲区的值填充部分iChaikinBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iChaikin indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }  
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

# 26.14 iCustom

函数返回指定 自定义指标 的句柄。

int  iCustom( 
   string           symbol,     // 交易品种名称 
   ENUM_TIMEFRAMES  period,     // 周期 
   string           name        // 文件夹/自定义指标_名称 
   ...                          // 指标输入参量列表 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

name

[in] 自定义指标的名称,其路径相对于指标的根目录(MQL5 / Indicators /)。如果指标位于子目录中,例如,在MQL5 / Indicators / Examples中,其名称必须指定为:“Examples \ indicator_name”(必须使用 双斜杠 而不是 单斜杠 作为分隔符)。

...

[in] 自定义指标的 输入参数 列表,用 逗号 分开,类型和参数命令必须匹配,如果没有指定参数,则使用默认值。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

自定义指标 必须是已编译的(扩展名为EX5) 并且 位于客户端 或 其子目录的MQL5 / Indicators目录中。

如果通过常量字符串设置相应的参数,则需要通过调用iCustom()函数自动定义需要测试的指标。对于所有其它情况(使用IndicatorCreate()函数 或 在设置指标名称的参数中使用非常量字符串),属性 #property tester_indicator是必需的:

#property tester_indicator "indicator_name.ex5"

如果在指标中使用了第一个调用表单,则在自定义指示器启动时,您还可以在其“参数”选项卡中指示要计算的数据。如果未明确选择“应用于”参数,则默认计算基于“收盘价”的值。

基于“收盘价”

当从MQL5程序中调用自定义指标时,”应用于”(Applied_Price)参数 或者 另外的指标句柄应该最后传递,在所有自定义指标的输入变量之后。

相关参考

程序属性, 时间序列和指标访问, IndicatorCreate(), IndicatorRelease()

示例:

#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//---- 标签图1 
#property indicator_label1  "Label1" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 输入参量 
input int MA_Period=21; 
input int MA_Shift=0; 
input ENUM_MA_METHOD MA_Method=MODE_SMA; 
//--- 指标缓冲区 
double         Label1Buffer[]; 
//--- 自定义移动平均数处理程序。mq5自定义指标 
int MA_handle; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 指标缓冲区绘图 
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA); 
   ResetLastError(); 
   MA_handle=iCustom(NULL,0,"Examples\\Custom Moving Average", 
                     MA_Period, 
                     MA_Shift, 
                     MA_Method, 
                     PRICE_CLOSE // 使用收盘价 
                     ); 
   Print("MA_handle = ",MA_handle,"  error = ",GetLastError()); 
//--- 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标重复函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 复制指标自定义移动平均值到指标缓冲区 
   int copy=CopyBuffer(MA_handle,0,0,rates_total,Label1Buffer); 
   Print("copy = ",copy,"    rates_total = ",rates_total); 
//--- 如果尝试失败-这里报道 
   if(copy<=0) 
      Print("An attempt to get the values if Custom Moving Average has failed"); 
//--- 为下次调用返回prev_calculated值 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+
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

# 26.15 iDEMA

该函数返回 双指数移动平均线(Double Exponential Moving Average ) 指标的句柄。它只有一个缓冲区。

int  iDEMA( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   int                 ma_shift,          // 平移 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 计算平均周期(计算K线柱的数量)。

ma_shift

[in] 关于价格图表转变的指标。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                   Demo_iDEMA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iDEMA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iDEMA 标图 
#property indicator_label1  "iDEMA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iDEMA,             // 使用iDEMA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iDEMA;           // 函数类型  
input int                  ma_period=14;              // 平均移动周期 
input int                  ma_shift=0;                // 移动 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iDEMABuffer[]; 
//--- 存储iDEMA 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在双指数移动平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iDEMABuffer,INDICATOR_DATA); 
   //--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iDEMA) 
      handle=iDEMA(name,period,ma_period,ma_shift,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[3]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_shift; 
      //--- 价格类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_DEMA,3,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iDEMA indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示双指数移动平均指标计算的交易品种/时间帧 
   short_name=StringFormat("iDEMA(%s/%s, %d, %d, %s)",name,EnumToString(period), 
                           ma_period,ma_shift,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iDEMA 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iDEMA指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iDEMABuffer 数组大于交易品种/周期 iDEMA 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以双指数移动平均指标的值填充iDEMABuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iDEMABuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住双指数移动平均指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iDEMA指标的指标缓冲区                                           | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 双指数移动平均值的指标缓冲区 
                         int shift,         // 移动 
                         int ind_handle,    // iDEMA指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iDEMABuffer 数组 
   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iDEMA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.16 iDeMarker

函数返回DeMarker指标的句柄。它只有一个缓冲区。

int  iDeMarker( 
   string           symbol,        // 交易品种名称 
   ENUM_TIMEFRAMES  period,        // 周期 
   int              ma_period      // 平均周期 
   );
1
2
3
4
5

参数 symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 计算平均周期(计算K线柱的数量)。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                               Demo_iDeMarker.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iDeMarker technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iDeMarker 标图 
#property indicator_label1  "iDeMarker" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 指标窗口的横向水平 
#property indicator_level1  0.3 
#property indicator_level2  0.7 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iDeMarker,         // 使用iDeMarker 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iDeMarker;       // 函数类型  
input int                  ma_period=14;              // 平均移动周期 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iDeMarkerBuffer[]; 
//--- 存储iDeMarker指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在DeMarker 指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iDeMarkerBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iDeMarker) 
      handle=iDeMarker(name,period,ma_period); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      handle=IndicatorCreate(name,period,IND_DEMARKER,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iDeMarker indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示DeMarker 指标计算的交易品种/时间帧 
   short_name=StringFormat("iDeMarker(%s/%s, period=%d)",name,EnumToString(period),ma_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iDeMarker指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iDeMarker 指标数量值更改 
//---或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iDeMarkerBuffer数组大于交易品种/周期iDeMarker 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以DeMarker 指标的值填充iDeMarkerBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iDeMarkerBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住DeMarker 指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iDeMarker 指标的指标缓冲区                                      | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // DeMarker值的指标缓冲区 
                         int ind_handle,    // iDeMarker 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iDeMarkerBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iDeMarker indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.17 iEnvelopes

函数返回 轨道线(Envelopes) 指标的句柄。

int  iEnvelopes( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均线计算周期 
   int                 ma_shift,          // 指标平移 
   ENUM_MA_METHOD      ma_method,         // 平滑类型 
   ENUM_APPLIED_PRICE  applied_price,     // 价格或者处理器类型 
   double              deviation          // 中线边界差(百分率) 
   );
1
2
3
4
5
6
7
8
9

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 主线平均周期

ma_shift

[in] 相关价格图表的指标转换。

ma_method

[in] 平滑类型。取值范围是 ENUM_MA_METHOD 枚举值之一。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

deviation

[in] 偏离值(百分比)。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注释

缓冲区代码: 0 - UPPER_LINE,1 - LOWER_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                              Demo_iEnvelopes.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iEnvelopes technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_plots   2 
//--- 上标图 
#property indicator_label1  "Upper" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 下标图 
#property indicator_label2  "Lower" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iEnvelopes,        // 使用iEnvelopes 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iEnvelopes;      // 函数类型 
input int                  ma_period=14;              // 平均移动周期 
input int                  ma_shift=0;                // 移动  
input ENUM_MA_METHOD       ma_method=MODE_SMA;        // 平滑类型 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input double               deviation=0.1;             // 移动平均的边界偏差 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         UpperBuffer[]; 
double         LowerBuffer[]; 
//--- 存储iEnvelopes指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在轨道线指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,UpperBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,LowerBuffer,INDICATOR_DATA); 
//--- 设置每条线的移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); 
   PlotIndexSetInteger(1,PLOT_SHIFT,ma_shift);    
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iEnvelopes) 
      handle=iEnvelopes(name,period,ma_period,ma_shift,ma_method,applied_price,deviation); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[5]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_shift; 
      //--- 平滑类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=ma_method; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      //--- 价格类型 
      pars[4].type=TYPE_DOUBLE; 
      pars[4].double_value=deviation; 
      handle=IndicatorCreate(name,period,IND_ENVELOPES,5,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iEnvelopes indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示轨道线指标计算的交易品种/时间帧 
   short_name=StringFormat("iEnvelopes(%s/%s, %d, %d, %s,%s, %G)",name,EnumToString(period), 
   ma_period,ma_shift,EnumToString(ma_method),EnumToString(applied_price),deviation); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iEnvelopes指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iEnvelopes指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果UpperBuffer 数组大于交易品种/周期iEnvelopes 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 用来自轨道线指标的值填充UpperBuffer和LowerBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(UpperBuffer,LowerBuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住轨道线指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iEnvelopes指标的指标缓冲区                                      | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &upper_values[],    // 上边界的指标缓冲区 
                           double &lower_values[],    // 下边界的指标缓冲区 
                           int shift,                 // 移动 
                           int ind_handle,            // 轨道线指标的处理程序 
                           int amount                 // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分UpperBuffer 数组 
   if(CopyBuffer(ind_handle,0,-shift,amount,upper_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iEnvelopes indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 以1标引指标缓冲区的值填充部分LowerBuffer数组 
   if(CopyBuffer(ind_handle,1,-shift,amount,lower_values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iEnvelopes indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.18 iForce

函数返回 强力指数(Force Index ) 指标的句柄。它只有一个缓冲区。

int  iForce( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   ENUM_MA_METHOD      ma_method,         // 平滑类型 
   ENUM_APPLIED_VOLUME applied_volume     // 计算的交易量类型 
   );
1
2
3
4
5
6
7

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算的平均周期。

ma_method

[in] 平滑类型。取值范围是 ENUM_MA_METHOD 枚举值之一。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举值之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                  Demo_iForce.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iForce technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 绘制iForce 
#property indicator_label1  "iForce" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iForce,            // 使用iForce 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iForce;             // 函数类型 
input int                  ma_period=13;                 // 平均移动周期 
input ENUM_MA_METHOD       ma_method=MODE_SMA;           // 平滑类型 
input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;   // 交易量类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iForceBuffer[]; 
//--- 存储iForce指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在强力指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iForceBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iForce) 
      handle=iForce(name,period,ma_period,ma_method,applied_volume); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[3]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 平滑类型 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_method; 
      //--- 交易量类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=applied_volume; 
      //--- 价格类型 
      handle=IndicatorCreate(name,period,IND_FORCE,3,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iForce indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示强力指标计算的交易品种/时间帧 
   short_name=StringFormat("iForce(%s/%s, %d, %s, %s)",name,EnumToString(period), 
                           ma_period,EnumToString(ma_method),EnumToString(applied_volume)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iForce指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iForce指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iForceBuffer 数组大于交易品种/周期iForce 指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以强力指标的值填充iForceBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iForceBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住强力指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iForce指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 强力指数值的指标缓冲区 
                         int ind_handle,    // iForce指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iForceBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iForce indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.19 iFractals

函数返回 分形(Fractals) 指标的句柄。

int  iFractals( 
   string           symbol,     // 交易品种名称 
   ENUM_TIMEFRAMES  period      // 周期 
   );
1
2
3
4

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下:0 - UPPER_LINE, 1 - LOWER_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                               Demo_iFractals.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iFractals technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
#property indicator_chart_window 
#property indicator_buffers 2 
#property indicator_plots   2 
//--- FractalUp 标图 
#property indicator_label1  "FractalUp" 
#property indicator_type1   DRAW_ARROW 
#property indicator_color1  clrBlue 
//--- FractalDown 标图 
#property indicator_label2  "FractalDown" 
#property indicator_type2   DRAW_ARROW 
#property indicator_color2  clrRed 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iFractals,         // 使用iFractals 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iFractals;          // 函数类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         FractalUpBuffer[]; 
double         FractalDownBuffer[]; 
//--- 存储iFractals指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在分形学指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,FractalUpBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,FractalDownBuffer,INDICATOR_DATA); 
//--- 使用来自Wingdings字符集的交易品种为PLOT_ARROW属性设置代码 
   PlotIndexSetInteger(0,PLOT_ARROW,217); // 箭头向上 
   PlotIndexSetInteger(1,PLOT_ARROW,218); // 箭头向下 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iFractals) 
      handle=iFractals(name,period); 
   else 
      handle=IndicatorCreate(name,period,IND_FRACTALS); 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iFractals indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示分形学指标计算的交易品种/时间帧 
   short_name=StringFormat("iFractals(%s/%s)",name,EnumToString(period)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iFractals指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iFractals指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果FractalUpBuffer数组大于交易品种/周期iFractals指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 用来自分形学指标的值填充FractalUpBuffer和FractalDownBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(FractalUpBuffer,FractalDownBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住分形学指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iFractals指标的指标缓冲区                                       | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &up_arrows[],        // 向上箭头的指标缓冲区 
                           double &down_arrows[],      // 向下箭头的指标缓冲区 
                           int ind_handle,             // iFractals指标的处理程序 
                           int amount                  // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分FractalUpBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,up_arrows)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iFractals indicator to the FractalUpBuffer array, error code %d", 
                  GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 以1标引指标缓冲区的值填充部分FractalDownBuffer数组 
   if(CopyBuffer(ind_handle,1,0,amount,down_arrows)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iFractals indicator to the FractalDownBuffer array, error code %d", 
                  GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.20 iFrAMA

函数返回 分形自适应均线(Fractal Adaptive Moving Average)指标的句柄。它只有一个缓冲区。

int  iFrAMA( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   int                 ma_shift,          // 图表平移 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算周期(计算K线柱的数量)。

ma_shift

[in] 在价格图表中的指标转换。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                  Demo_iFrAMA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iFrAMA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 绘制iFrAMA 
#property indicator_label1  "iFrAMA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iFrAMA,            // 使用iFrAMA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iFrAMA;             // 函数类型 
input int                  ma_period=14;                 // 平均移动周期 
input int                  ma_shift=0;                   // 移动 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE;    // 价格类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iFrAMABuffer[]; 
//--- 存储iFrAMA 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在分形学适合移动平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iFrAMABuffer,INDICATOR_DATA); 
//--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iFrAMA) 
      handle=iFrAMA(name,period,ma_period,ma_shift,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[3]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_shift; 
      //--- 价格类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=applied_price; 
      //--- 价格类型 
      handle=IndicatorCreate(name,period,IND_FRAMA,3,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iFrAMA indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示iFrAMA indicator指标计算的交易品种/时间帧 
   short_name=StringFormat("iFrAMA(%s/%s, %d, %d, %s)",name,EnumToString(period), 
                           ma_period,ma_shift,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iFrAMA指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iFrAMA指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iFrAMABuffer数组大于交易品种/周期iFrAMA 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以分形学适合移动平均指标的值填充iFrAMABuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iFrAMABuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住分形学适合移动平均指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iFrAMA指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 分形学适合移动平均值的指标缓冲区 
                         int shift,         // 移动 
                         int ind_handle,    // iFrAMA 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iFrAMABuffer数组 
   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iFrAMA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.21 iGator

函数返回 鳄鱼振荡器(Gator) 指标的句柄。振荡器表示鳄鱼指标蓝线和红线(上升柱状图)的区别以及红线和绿线的区别(下降柱状图)。

int  iGator( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 jaw_period,        // 咽喉计算周期 
   int                 jaw_shift,         // 咽喉平移 
   int                 teeth_period,      // 牙齿计算周期 
   int                 teeth_shift,       // 牙齿平移 
   int                 lips_period,       // 唇部计算周期 
   int                 lips_shift,        // 唇部平移 
   ENUM_MA_METHOD      ma_method,         // 平滑类型 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7
8
9
10
11
12

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

jaw_period

[in] 蓝线平均周期(鳄鱼颌骨)。

jaw_shift

[in] 与价格图表有关的蓝线变化。不能直接连接到指标柱状图的视觉变化。

teeth_period

[in] 红线的平均周期(鳄鱼牙)。

teeth_shift

[in] 关于价格图表的红线变化。不能直接连接到指标柱状图的视觉变化。

lips_period

[in] 绿线的平均周期 (鳄鱼唇)。

lips_shift

[in] 关于价格图表的绿线变化。不能直接连接到指标柱状图的视觉变化。

ma_method

[in] 平滑类型。取值范围是 ENUM_MA_METHOD 枚举值之一。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注释

缓冲区代码:
0 ---- UPPER_HISTOGRAM,
1 ----上升柱状图的颜色缓冲区,
2 ---- LOWER_HISTOGRAM,
3 ---- 下降柱状图的颜色缓冲区。

例如:

//+------------------------------------------------------------------+ 
//|                                                  Demo_iGator.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iGator technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All other parameters are as in a standard Gator Oscillator." 
  
#property indicator_separate_window 
#property indicator_buffers 4 
#property indicator_plots   2 
//--- 绘制GatorUp 
#property indicator_label1  "GatorUp" 
#property indicator_type1   DRAW_COLOR_HISTOGRAM 
#property indicator_color1  clrGreen, clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 绘制GatorDown 
#property indicator_label2  "GatorDown" 
#property indicator_type2   DRAW_COLOR_HISTOGRAM 
#property indicator_color2  clrGreen, clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iGator,            // 使用iGator 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iGator;       // 函数类型  
input string               symbol=" ";             // 交易品种 
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;  // 时间帧 
input int                  jaw_period=13;          // 咽喉线周期 
input int                  jaw_shift=8;            // 咽喉线移动 
input int                  teeth_period=8;         // 牙齿线周期 
input int                  teeth_shift=5;          // 牙齿线移动 
input int                  lips_period=5;          // 唇部线周期 
input int                  lips_shift=3;           // 唇部线移动 
input ENUM_MA_METHOD       MA_method=MODE_SMMA;    // 鳄鱼平均线的方法 
input ENUM_APPLIED_PRICE   applied_price=PRICE_MEDIAN;// 用于计算鳄鱼的价格类型 
//--- 指标缓冲区 
double         GatorUpBuffer[]; 
double         GatorUpColors[]; 
double         GatorDownBuffer[]; 
double         GatorDownColors[]; 
//--- 存储iGator 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 移动上下直方图的值 
int shift; 
//--- 我们将在加多摆动指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,GatorUpBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,GatorUpColors,INDICATOR_COLOR_INDEX); 
   SetIndexBuffer(2,GatorDownBuffer,INDICATOR_DATA); 
   SetIndexBuffer(3,GatorDownColors,INDICATOR_COLOR_INDEX); 
/* 
  参数中指定的所有移动指的是绘制加多摆动指标基础上的鳄鱼指标! 
  这就是我们不能移动加多摆动指标本身的原因,但是他们移动鳄鱼线, 
  其值用于计算加多摆动指标! 
*/ 
//--- 我们计算上下直方图的移动,这等于咽喉线和牙齿线之间的区别 
   shift=MathMin(jaw_shift,teeth_shift); 
   PlotIndexSetInteger(0,PLOT_SHIFT,shift); 
//--- 尽管指标包含两个直方图,也使用相同的移动 - 就是实施iGator 指标。 
   PlotIndexSetInteger(1,PLOT_SHIFT,shift); 
  
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iGator) 
      handle=iGator(name,period,jaw_period,jaw_shift,teeth_period,teeth_shift, 
                    lips_period,lips_shift,MA_method,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[8]; 
      //--- 鳄鱼线的周期和移动 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=jaw_period; 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=jaw_shift; 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=teeth_period; 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=teeth_shift; 
      pars[4].type=TYPE_INT; 
      pars[4].integer_value=lips_period; 
      pars[5].type=TYPE_INT; 
      pars[5].integer_value=lips_shift; 
      //--- 平滑类型 
      pars[6].type=TYPE_INT; 
      pars[6].integer_value=MA_method; 
      //--- 价格类型 
      pars[7].type=TYPE_INT; 
      pars[7].integer_value=applied_price; 
      //--- 创建处理程序 
      handle=IndicatorCreate(name,period,IND_GATOR,8,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iGator indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示加多摆动指标计算的交易品种/时间帧 
   short_name=StringFormat("iGator(%s/%s, %d, %d ,%d, %d, %d, %d)",name,EnumToString(period), 
                           jaw_period,jaw_shift,teeth_period,teeth_shift,lips_period,lips_shift); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iGator指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iGator指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果GatorUpBuffer数组大于交易品种/周期iGator指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以加多摆动指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(GatorUpBuffer,GatorUpColors,GatorDownBuffer,GatorDownColors, 
      shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住加多摆动指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iGator 指标的指标缓冲区                                        | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &ups_buffer[],         // 向上直方图的指标缓冲区 
                           double &up_color_buffer[],    // 向上直方图价格指数的指标缓冲区 
                           double &downs_buffer[],       // 向下直方图的指标缓冲区 
                           double &downs_color_buffer[], // 向下直方图价格指数的指标缓冲区 
                           int u_shift,                  // 上下直方图移动 
                           int ind_handle,               // iGator 指标的处理程序 
                           int amount                    // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分GatorUpBuffer 数组 
   if(CopyBuffer(ind_handle,0,-u_shift,amount,ups_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分GatorUpColors 数组 
   if(CopyBuffer(ind_handle,1,-u_shift,amount,up_color_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以2标引指标缓冲区的值填充部分GatorDownBuffer 数组 
   if(CopyBuffer(ind_handle,2,-u_shift,amount,downs_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以3标引指标缓冲区的值填充部分GatorDownColors 数组 
   if(CopyBuffer(ind_handle,3,-u_shift,amount,downs_color_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iGator indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259

# 26.22 iIchimoku

函数返回 一目均衡图(Ichimoku Kinko Hyo) 指标的句柄。

int  iIchimoku( 
   string           symbol,            // 交易品种类型 
   ENUM_TIMEFRAMES  period,            // 周期 
   int              tenkan_sen,        // Tenkan-sen转换线周期 
   int              kijun_sen,         // Kijun-sen基准线周期 
   int              senkou_span_b      // Senkou Span B周期 
   );
1
2
3
4
5
6
7

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

tenkan_sen

[in] 转换线平均周期。

kijun_sen

[in] 基准线平均周期。

senkou_span_b

[in] Senkou Span B 平均周期。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注释

缓冲区代码:0 - TENKANSEN_LINE, 1 - KIJUNSEN_LINE, 2 - SENKOUSPANA_LINE, 3 - SENKOUSPANB_LINE, 4 - CHIKOUSPAN_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                               Demo_iIchimoku.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iIchimoku technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All other parameters just like in the standard Ichimoku Kinko Hyo." 
  
#property indicator_chart_window 
#property indicator_buffers 5 
#property indicator_plots   4 
//--- Tenkan_sen 标图 
#property indicator_label1  "Tenkan_sen" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- Kijun_sen 标图 
#property indicator_label2  "Kijun_sen" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrBlue 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- Senkou_Span 标图 
#property indicator_label3  "Senkou Span A;Senkou Span B" // two fields will be shown in Data Window 
#property indicator_type3   DRAW_FILLING 
#property indicator_color3  clrSandyBrown, clrThistle 
#property indicator_style3  STYLE_SOLID 
#property indicator_width3  1 
//--- Chinkou_Span 标图 
#property indicator_label4  "Chinkou_Span" 
#property indicator_type4   DRAW_LINE 
#property indicator_color4  clrLime 
#property indicator_style4  STYLE_SOLID 
#property indicator_width4  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iIchimoku,         // 使用iIchimoku 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iIchimoku;       // 函数类型 
input int                  tenkan_sen=9;              // Tenkan-sen周期 
input int                  kijun_sen=26;              // Kijun-sen周期 
input int                  senkou_span_b=52;          // Senkou Span B周期 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         Tenkan_sen_Buffer[]; 
double         Kijun_sen_Buffer[]; 
double         Senkou_Span_A_Buffer[]; 
double         Senkou_Span_B_Buffer[]; 
double         Chinkou_Span_Buffer[]; 
//--- 存储iIchimoku指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在一目均衡图指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,Tenkan_sen_Buffer,INDICATOR_DATA); 
   SetIndexBuffer(1,Kijun_sen_Buffer,INDICATOR_DATA); 
   SetIndexBuffer(2,Senkou_Span_A_Buffer,INDICATOR_DATA); 
   SetIndexBuffer(3,Senkou_Span_B_Buffer,INDICATOR_DATA); 
   SetIndexBuffer(4,Chinkou_Span_Buffer,INDICATOR_DATA); 
//--- 设置未来方向kijun_sen 柱形的Senkou 跨通道的移动 
   PlotIndexSetInteger(2,PLOT_SHIFT,kijun_sen); 
//--- 设置无需Chinkou跨线的移动,因为数据跨度 
//--- 已经存储在iIchimoku移动 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iIchimoku) 
      handle=iIchimoku(name,period,tenkan_sen,kijun_sen,senkou_span_b); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[3]; 
      //--- 鳄鱼线的周期和移动 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=tenkan_sen; 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=kijun_sen; 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=senkou_span_b; 
      //--- 创建处理程序 
      handle=IndicatorCreate(name,period,IND_ICHIMOKU,3,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iIchimoku indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示一目均衡图指标计算的交易品种/时间帧 
   short_name=StringFormat("iIchimoku(%s/%s, %d, %d ,%d)",name,EnumToString(period), 
                           tenkan_sen,kijun_sen,senkou_span_b); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化     
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iIchimoku指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iIchimoku指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果Tenkan_sen_Buffer数组大于交易品种/周期iIchimoku指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以一目均衡图指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(Tenkan_sen_Buffer,Kijun_sen_Buffer,Senkou_Span_A_Buffer,Senkou_Span_B_Buffer,Chinkou_Span_Buffer, 
      kijun_sen,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住一目均衡图指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iIchimoku 指标的指标缓冲区                                     | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &tenkan_sen_buffer[],     // Tenkan-sen线的指标缓冲区 
                           double &kijun_sen_buffer[],      // Kijun_sen线的指标缓冲区 
                           double &senkou_span_A_buffer[],  // Senkou Span A 线的指标缓冲区 
                           double &senkou_span_B_buffer[],  // Senkou Span B线的指标缓冲区 
                           double &chinkou_span_buffer[],   // Chinkou Span线的指标缓冲区 
                           int senkou_span_shift,           // 未来方向Senkou 的跨线移动 
                           int ind_handle,                  // iIchimoku 指标的处理程序 
                           int amount                       // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分Tenkan_sen_Buffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,tenkan_sen_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("1.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分Kijun_sen_Buffer 数组 
   if(CopyBuffer(ind_handle,1,0,amount,kijun_sen_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("2.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以2标引指标缓冲区的值填充部分Chinkou_Span_Buffer 数组 
//--- 如果senkou_span_shift>0,线根据senkou_span_shift柱形的未来方向移动。 
   if(CopyBuffer(ind_handle,2,-senkou_span_shift,amount,senkou_span_A_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("3.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以3标引指标缓冲区的值填充部分Senkou_Span_A_Buffer 数组 
//--- 如果senkou_span_shift>0,线根据senkou_span_shift柱形的未来方向移动。 
   if(CopyBuffer(ind_handle,3,-senkou_span_shift,amount,senkou_span_B_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("4.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以0标引指标缓冲区的值填充部分Senkou_Span_B_Buffer 数组 
//--- 当复制Chinkou Span时,我们无需考虑移动,因为Chinkou数据跨度 
//--- 已经存储在iIchimoku移动   
   if(CopyBuffer(ind_handle,4,0,amount,chinkou_span_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("5.Failed to copy data from the iIchimoku indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260

# 26.23 iBWMFI

函数返回 市场促进指数(Market Facilitation Index) 指标的句柄。它只有一个缓冲区。

int  iBWMFI( 
   string               symbol,             // 交易品种类型 
   ENUM_TIMEFRAMES      period,             // 周期 
   ENUM_APPLIED_VOLUME  applied_volume      // 计算的交易量类型 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举的常量之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                  Demo_iBWMFI.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iBWMFI technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   1 
//--- iBWMFI 标图 
#property indicator_label1  "iBWMFI" 
#property indicator_type1   DRAW_COLOR_HISTOGRAM 
#property indicator_color1  clrLime,clrSaddleBrown,clrBlue,clrPink 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iBWMFI,            // 使用iBWMFI 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iBWMFI;          // 函数类型 
input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;// 交易量类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iBWMFIBuffer[]; 
double         iBWMFIColors[]; 
//--- 存储 iBWMFI 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在市场便利指数比尔 威廉姆斯指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iBWMFIBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,iBWMFIColors,INDICATOR_COLOR_INDEX); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iBWMFI) 
      handle=iBWMFI(name,period,applied_volume); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[1]; 
      //--- 交易量类型 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=applied_volume; 
      handle=IndicatorCreate(name,period,IND_BWMFI,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iBWMFI indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示市场便利指数比尔 威廉姆斯指标计算的交易品种/时间帧 
   short_name=StringFormat("iBWMFI(%s/%s, %s)",name,EnumToString(period), 
                           EnumToString(applied_volume)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iBWMFI 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iBWMFI指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iBWMFIBuffer 数组大于交易品种/周期 iBWMFI 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以市场便利指数比尔 威廉姆斯指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(iBWMFIBuffer,iBWMFIColors,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住市场便利指数比尔 威廉姆斯指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iBWMFI 指标的指标缓冲区                                         | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &values[],    // 直方图值的指标缓冲区 
                           double &colors[],    // 直方图颜色的指标缓冲区 
                           int ind_handle,      // iBWMFI 指标的处理程序 
                           int amount           // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iBWMFIBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBWMFI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//---以1标引指标缓冲区的值填充部分 iBWMFIColors 数组 
   if(CopyBuffer(ind_handle,1,0,amount,colors)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iBWMFI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.24 iMomentum

函数返回 动量(Momentum) 指标句柄。它只有一个缓冲区。

int  iMomentum( 
   string               symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES      period,            // 周期 
   int                  mom_period,        // 平均周期 
   ENUM_APPLIED_PRICE   applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

mom_period

[in] 计算价格变化的平均周期(计算K线柱的数量)。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                               Demo_iMomentum.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iMomentum technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Momentum." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 标图iMomentum 
#property indicator_label1  "iMomentum" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrDodgerBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iMomentum,         // 使用iMomentum 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iMomentum;       // 函数类型  
input int                  mom_period=14;             // 动量周期 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iMomentumBuffer[]; 
//--- 存储 iMomentum 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在动量指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iMomentumBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iMomentum) 
      handle=iMomentum(name,period,mom_period,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[2]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=mom_period; 
      //--- 价格类型 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_MOMENTUM,2,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iMomentum indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示动量指标计算的交易品种/时间帧 
   short_name=StringFormat("iMomentum(%s/%s, %d, %s)",name,EnumToString(period), 
                           mom_period, EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iMomentum指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iMomentum指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iMomentumBuffer数组大于交易品种/周期iMomentum指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以动量指标的值填充iMomentumBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iMomentumBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住动量指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iMomentum指标的指标缓冲区                                      | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 动量值的指标缓冲区 
                         int ind_handle,    // iMomentum 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iMomentumBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iMomentum indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }  
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

# 26.25 iMFI

函数返回资金流向(Money Flow Index )指标的句柄。

int  iMFI( 
   string               symbol,             // 交易品种名称 
   ENUM_TIMEFRAMES      period,             // 周期 
   int                  ma_period,          // 平均周期 
   ENUM_APPLIED_VOLUME  applied_volume      // 计算的交易量类型 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 计算的平均周期(计算的K线柱数量)。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举的常量之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iMFI.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iMFI technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Money Flow Index." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iMFI标图 
#property indicator_label1  "iMFI" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrDodgerBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 指标窗口的横向水平 
#property indicator_level1  20 
#property indicator_level2  80 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iMFI,              // 使用iMFI 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iMFI;               // 函数类型 
input int                  ma_period=14;                 // 周期 
input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;   // 交易量类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iMFIBuffer[]; 
//--- 存储iMFI指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在资金流向指数指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iMFIBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种   
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iMFI) 
      handle=iMFI(name,period,ma_period,applied_volume); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[2]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 交易量类型 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=applied_volume; 
      handle=IndicatorCreate(name,period,IND_MFI,2,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iMFI indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示资金流向指数指标计算的交易品种/时间帧 
   short_name=StringFormat("iMFI(%s/%s, %d, %s)",name,EnumToString(period), 
                           ma_period, EnumToString(applied_volume)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iMFI 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iMFI 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iMFIBuffer 数组大于交易品种/周期 iMFI 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以资金流向指数指标的值填充iMFIBuffer数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iMFIBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住资金流向指数指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iMFI 指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],  // 资金流向指数值的指标缓冲区 
                         int ind_handle,    // iMFI 指标的处理程序 
                         int amount         // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分 iMFIBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iMFI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }   
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

# 26.26 iMA

函数返回 移动平均线(Moving Average) 的指标句柄。只有一个缓冲区。

int  iMA( 
   string               symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES      period,            // 周期 
   int                  ma_period,         // 平均周期 
   int                  ma_shift,          // 平移 
   ENUM_MA_METHOD       ma_method,         // 平滑类型 
   ENUM_APPLIED_PRICE   applied_price      // 价格或者处理程序类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 计算移动平均数的平均周期。

ma_shift

[in] 与价格图表有关的指标变化。

ma_method

[in] 平滑类型。取值范围是 ENUM_MA_METHOD 枚举值之一。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                     Demo_iMA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iMA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All other parameters like in the standard Moving Average." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iMA 标图 
#property indicator_label1  "iMA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iMA,               // 使用iMA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iMA;                // 函数类型 
input int                  ma_period=10;                 // ma周期 
input int                  ma_shift=0;                   // 移动 
input ENUM_MA_METHOD       ma_method=MODE_SMA;           // 平滑类型 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE;    // 价格类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iMABuffer[]; 
//--- 存储iMA指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在移动平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iMABuffer,INDICATOR_DATA); 
//--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift);    
//--- 定义绘制指标的交易品种   
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iMA) 
      handle=iMA(name,period,ma_period,ma_shift,ma_method,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构 
      MqlParam pars[4]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_shift; 
      //--- 平滑类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=ma_method; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_MA,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iMA indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示移动平均指标计算的交易品种/时间帧 
   short_name=StringFormat("iMA(%s/%s, %d, %d, %s, %s)",name,EnumToString(period), 
                           ma_period, ma_shift,EnumToString(ma_method),EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iMA指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iMA 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iMABuffer数组大于交易品种/周期iMA指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以适合移动平均指标的值填充iMABuffer 数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iMABuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住移动平均指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充MA指标的指标缓冲区                                              | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &values[],   // 移动平均值的指标缓冲区 
                         int shift,          // 移动 
                         int ind_handle,     // iMA指标的处理程序 
                         int amount          // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iMABuffer数组 
   if(CopyBuffer(ind_handle,0,-shift,amount,values)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iMA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }     
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

# 26.27 iOsMA

该函数返回“振荡器移动平均线(Moving Average of Oscillator)”指标的句柄。 OsMA振荡器显示MACD值与其信号线之间的差异。 它只有一个缓冲区。

int  iOsMA( 
   string              symbol,              // 交易品种名称 
   ENUM_TIMEFRAMES     period,              // 周期 
   int                 fast_ema_period,     // 快速移动平均数周期 
   int                 slow_ema_period,     // 慢速移动平均数周期 
   int                 signal_period,       // 不同点的平均周期 
   ENUM_APPLIED_PRICE  applied_price        // 价格或者处理器的类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

fast_ema_period

[in] 快速移动平均数计算周期。

slow_ema_period

[in] 慢速移动平均数计算周期。

signal_period

[in] 信号线计算平均周期。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

在一些系统中该振荡器也被认为是MACD柱状图。

例如:

//+------------------------------------------------------------------+ 
//|                                                   Demo_iOsMA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iOsMA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Moving Average of Oscillator." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iOsMA 标图 
#property indicator_label1  "iOsMA" 
#property indicator_type1   DRAW_HISTOGRAM 
#property indicator_color1  clrSilver 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iOsMA,             // 使用iOsMA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iOsMA;           // 函数类型  
input int                  fast_ema_period=12;        // 快速ma周期 
input int                  slow_ema_period=26;        // 慢速ma周期 
input int                  signal_period=9;           // 平均差异周期 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型  
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iOsMABuffer[]; 
//--- 存储 iAMA 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在移动平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iOsMABuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iOsMA) 
      handle=iOsMA(name,period,fast_ema_period,slow_ema_period,signal_period,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[4]; 
      //--- 快速ma周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=fast_ema_period; 
      //--- 慢速ma周期 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=slow_ema_period; 
      //--- 快速慢速移动平均差异的周期 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=signal_period; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_OSMA,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create a handle of iOsMA for the pair %s/%s, error code is %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示移动平均震荡指标计算的交易品种/时间帧 
   short_name=StringFormat("iOsMA(%s/%s,%d,%d,%d,%s)",name,EnumToString(period), 
                           fast_ema_period,slow_ema_period,signal_period,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化     
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iOsMA 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iOsMA 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iOsMABuffer 数组大于交易品种/周期iOsMA指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iOsMA 指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iOsMABuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住移动平均震荡指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iOsMA 指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &ama_buffer[],  // OsMA 值的指标缓冲区 
                         int ind_handle,        // iOsMA 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iOsMABuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,ama_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iOsMA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }  
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

# 26.28 iMACD

该函数返回 指数平滑均线离散(Moving Averages Convergence / Divergence)指标的句柄。 在OsMA称为MACD直方图的系统中,该指标显示为两条曲线。在客户端中,指数平滑均线离散 看起来像柱状图。

int  iMACD( 
   string              symbol,              // 交易品种名称 
   ENUM_TIMEFRAMES     period,              // 周期 
   int                 fast_ema_period,     // 快速移动平均数周期 
   int                 slow_ema_period,     // 慢速移动平均数周期 
   int                 signal_period,       // 不同点的平均周期 
   ENUM_APPLIED_PRICE  applied_price        // 价格或者处理器的类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

fast_ema_period

[in] 快速移动平均数计算周期。

slow_ema_period

[in] 慢速移动平均数计算周期。

signal_period

[in] 信号线计算周期。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下: 0 - MAIN_LINE, 1 - SIGNAL_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                                   Demo_iMACD.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iMACD technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All other parameters like in the standard MACD." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   2 
//--- MACD 标图 
#property indicator_label1  "MACD" 
#property indicator_type1   DRAW_HISTOGRAM 
#property indicator_color1  clrSilver 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 信号标图 
#property indicator_label2  "Signal" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_DOT 
#property indicator_width2  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iMACD,             // 使用iMACD 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iMACD;           // 函数类型 
input int                  fast_ema_period=12;        // 快速ma周期 
input int                  slow_ema_period=26;        // 慢速ma周期 
input int                  signal_period=9;           // 平均差异周期 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型  
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         MACDBuffer[]; 
double         SignalBuffer[]; 
//--- 存储iMACD 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在移动平均聚/散指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,MACDBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iMACD) 
      handle=iMACD(name,period,fast_ema_period,slow_ema_period,signal_period,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[4]; 
      //--- 快速ma周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=fast_ema_period; 
      //--- 慢速ma周期 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=slow_ema_period; 
      //--- 快速慢速移动平均差异的周期 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=signal_period; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_MACD,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iMACD indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示移动平均聚/散指标计算的交易品种/时间帧 
   short_name=StringFormat("iMACD(%s/%s,%d,%d,%d,%s)",name,EnumToString(period), 
                           fast_ema_period,slow_ema_period,signal_period,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从 iMACD 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iMACD 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 MACDBuffer数组大于交易品种/周期iMACD指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iMACD指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(MACDBuffer,SignalBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住移动平均聚/散指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iMACD 指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &macd_buffer[],    // MACD值的指标缓冲区 
                           double &signal_buffer[],  // MACD 信号线的指标缓冲区 
                           int ind_handle,           // iMACD 指标的处理程序 
                           int amount                // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iMACDBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,macd_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
  
//--- 以1标引指标缓冲区的值填充部分SignalBuffer数组 
   if(CopyBuffer(ind_handle,1,0,amount,signal_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iMACD indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.29 iOBV

函数返回 平衡交易量(On Balance Volume) 指标的句柄。它只有一个缓冲区。

int  iOBV( 
   string                symbol,             // 交易品种名称 
   ENUM_TIMEFRAMES       period,             // 周期 
   ENUM_APPLIED_VOLUME   applied_volume      // 计算的交易量类型 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举的常量之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iOBV.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iOBV technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//---  iOBV 
#property indicator_label1  "iOBV" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iOBV ,             // 使用iOBV 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iOBV;               // 函数类型  
input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;   // 交易量类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iOBVBuffer[]; 
//--- 存储iOBV指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在平衡交易量指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iOBVBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iOBV) 
      handle=iOBV(name,period,applied_volume); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[1]; 
      //--- 交易量类型 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=applied_volume; 
      handle=IndicatorCreate(name,period,IND_OBV,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iOBV indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示平衡交易量指标计算的交易品种/时间帧 
   short_name=StringFormat("iOBV(%s/%s, %s)",name,EnumToString(period), 
                           EnumToString(applied_volume)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iOBV 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iOBV 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iOBVBuffer 数组大于交易品种/周期iOBV 指标的数量值,那么我们不会复制任何内容  
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iOBV 指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iOBVBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住平衡交易量指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iOBV指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &obv_buffer[],  // OBV 值的指标缓冲区 
                         int ind_handle,        // iOBV 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iOBVBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,obv_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iOBV indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }   
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

# 26.30 iSAR

函数返回 抛物转向系统(Parabolic Stop and Reverse system)指标的句柄。它只有一个缓冲区。

int  iSAR( 
   string           symbol,      // 交易品种名称 
   ENUM_TIMEFRAMES  period,      // 周期 
   double           step,        // 逐步增加 
   double           maximum      // 最大止损水平 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

step

[in] 停止水平增量,通常是0.02。

maximum

[in] 最大停止水平,通常是0.2。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iSAR.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iSAR technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Parabolic Stop and Reverse." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 绘制iSAR 
#property indicator_label1  "iSAR" 
#property indicator_type1   DRAW_ARROW 
#property indicator_color1  clrBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iSAR,              // 使用iSAR 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iSAR;               // 函数类型  
input double               step=0.02;                    // 步骤 - 停止拖拽的加速因素 
input double               maximum=0.2;                  // 步骤的最大值 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iSARBuffer[]; 
//--- 存储iSAR 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在抛物线转向指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iSARBuffer,INDICATOR_DATA); 
//--- 从Wingdings字符集为PLOT_ARROW属性设置图表展示的交易品种代码 
   PlotIndexSetInteger(0,PLOT_ARROW,159); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iSAR) 
      handle=iSAR(name,period,step,maximum); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[2]; 
      //--- 步骤值 
      pars[0].type=TYPE_DOUBLE; 
      pars[0].double_value=step; 
      //--- 可以用于计算的步骤值的限制 
      pars[1].type=TYPE_DOUBLE; 
      pars[1].double_value=maximum; 
      handle=IndicatorCreate(name,period,IND_SAR,2,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iSAR indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示抛物线转向指标计算的交易品种/时间帧 
   short_name=StringFormat("iSAR(%s/%s, %G, %G)",name,EnumToString(period), 
                           step,maximum); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iSAR指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iSAR 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iSARBuffer数组大于交易品种/周期iSAR 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iSAR 指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iSARBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住抛物线转向指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iSAR 指标的指标缓冲区                                           | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &sar_buffer[],  // 抛物线转向值的指标缓冲区 
                         int ind_handle,        // iSAR 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iSARBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,sar_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iSAR indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }    
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

# 26.31 iRSI

函数返回 相对强度指数 指标的句柄。它只有一个缓冲区。

int  iRSI( 
   string              symbol,            // 交易品种类型 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] RSI计算平均周期。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iRSI.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iRSI technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Relative Strength Index." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- 绘制iRSI 
#property indicator_label1  "iRSI" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrDodgerBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 在指标窗口显示值的限制 
#property indicator_maximum 100 
#property indicator_minimum 0 
//--- 指标窗口的横向水平 
#property indicator_level1  70.0 
#property indicator_level2  30.0 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iRSI,              // 使用iRSI 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iRSI;               // 函数类型  
input int                  ma_period=14;                 // 平均移动周期 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE;    // 价格类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iRSIBuffer[]; 
//--- 存储iRSI指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在相对强度指数指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iRSIBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iRSI) 
      handle=iRSI(name,period,ma_period,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[2]; 
      //--- 平均移动周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 可以用于计算的步骤值的限制 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_RSI,2,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iRSI indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示相对强度指数指标计算的交易品种/时间帧 
   short_name=StringFormat("iRSI(%s/%s, %d, %d)",name,EnumToString(period), 
                           ma_period,applied_price); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iRSI 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果 iRSI 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iRSIBuffer 数组大于交易品种/周期iRSI指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iRSI指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iRSIBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住相对强度指数指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iRSI 指标的指标缓冲区                                           | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &rsi_buffer[],  // 相对强度指数值的指标缓冲区 
                         int ind_handle,        // iRSI 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//---以0标引指标缓冲区的值填充部分iRSIBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,rsi_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iRSI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }     
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

# 26.32 iRVI

函数返回 相对活力指数(Relative Vigor Index )指标的句柄处理器。

int  iRVI( 
   string           symbol,        // 交易品种名称 
   ENUM_TIMEFRAMES  period,        // 周期 
   int              ma_period      // 平均周期 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] RVI 计算平均周期。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码如下: 0 - MAIN_LINE,1 - SIGNAL_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iRVI.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iRVI technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Relative Vigor Index." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   2 
//--- RVI 标图 
#property indicator_label1  "RVI" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 信号标图 
#property indicator_label2  "Signal" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iRVI,              // 使用iRVI 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iRVI;               // 函数类型  
input int                  ma_period=10;                 // 计算周期 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         RVIBuffer[]; 
double         SignalBuffer[]; 
//--- 存储iRVI指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在相对活力指数指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,RVIBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iRVI) 
      handle=iRVI(name,period,ma_period); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[1]; 
      //--- 计算周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      handle=IndicatorCreate(name,period,IND_RVI,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iRVI indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示相对活力指数指标计算的交易品种/时间帧 
   short_name=StringFormat("iRVI(%s/%s, %d, %d)",name,EnumToString(period),ma_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iRVI 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iRVI指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果RVIBuffer数组大于交易品种/周期iRVI 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iRVI 指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(RVIBuffer,SignalBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住相对活力指数指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iRVI 指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &rvi_buffer[],     // 相对活力指数值的指标缓冲区 
                         double &signal_buffer[],  // 信号线的指标缓冲区 
                         int ind_handle,           // iRVI指标的处理程序 
                         int amount                // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iRVIBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,rvi_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iRVI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 以1标引指标缓冲区的值填充部分SignalBuffer数组 
   if(CopyBuffer(ind_handle,1,0,amount,signal_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iRVI indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.33 iStdDev

函数返回 标准偏差(Standard Deviation) 指标的句柄。它只有一个缓冲区。

int  iStdDev( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   int                 ma_shift,          // 平移 
   ENUM_MA_METHOD      ma_method,         // 平滑类型 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 指标计算平均周期。

ma_shift

[in] 与价格图表有关的指标变化。

ma_method

[in] 平均类型。可以使 ENUM_MA_METHOD 中任意值。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                 Demo_iStdDev.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iStdDev technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the normal Standard Deviation." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iStdDev 标图 
#property indicator_label1  "iStdDev" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrMediumSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iStdDev,           // 使用iStdDev 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iStdDev;         // 函数类型  
input int                  ma_period=20;              // 平均周期 
input int                  ma_shift=0;                // 移动 
input ENUM_MA_METHOD       ma_method=MODE_SMA;        // 平滑类型 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iStdDevBuffer[]; 
//--- 存储 iStdDev 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在标准偏差指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iStdDevBuffer,INDICATOR_DATA); 
//--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iStdDev) 
      handle=iStdDev(name,period,ma_period,ma_shift,ma_method,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[4]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_shift; 
      //--- 平滑类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=ma_method; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_STDDEV,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iStdDev indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示标准偏差指标计算的交易品种/时间帧 
   short_name=StringFormat("iStdDev(%s/%s, %d, %d, %s, %s)",name,EnumToString(period), 
                           ma_period,ma_shift,EnumToString(ma_method),EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iStdDev指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<0=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iStdDev指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果 iStdDevBuffer 数组大于交易品种/周期iStdDev 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以标准偏差指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iStdDevBuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住标准偏差指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iStdDev指标的指标缓冲区                                         | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &std_buffer[],  // 标准偏差线的指标缓冲区 
                         int std_shift,         // 标准偏差线的移动 
                         int ind_handle,        // iStdDev 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iStdDevBuffer数组 
   if(CopyBuffer(ind_handle,0,-std_shift,amount,std_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iStdDev indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.34 iStochastic

函数返回 随机摆动(Stochastic Oscillator )指标的句柄。

int  iStochastic( 
   string           symbol,          // 交易品种名称 
   ENUM_TIMEFRAMES  period,          // 周期 
   int              Kperiod,         // K线周期 (用于计算的柱数) 
   int              Dperiod,         // D线周期 (开始平滑周期) 
   int              slowing,         // 最终平滑 
   ENUM_MA_METHOD   ma_method,       // 平滑类型 
   ENUM_STO_PRICE   price_field      // 随机计算法 
   );
1
2
3
4
5
6
7
8
9

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

Kperiod

[in] %K 线计算平均周期(计算柱)。

Dperiod

[in] %D 线计算平均周期(计算柱)。

slowing

[in] 减速值。

ma_method

[in] 平滑类型。取值范围是 ENUM_MA_METHOD 枚举值之一。

price_field

[in] 计算价格选择参数。取值范围是 ENUM_STO_PRICE 枚举值之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

注意

缓冲区代码: 0 - MAIN_LINE, 1 - SIGNAL_LINE。

例如:

//+------------------------------------------------------------------+ 
//|                                             Demo_iStochastic.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iStochastic technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Stochastic Oscillator." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   2 
//--- 随机标图 
#property indicator_label1  "Stochastic" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrLightSeaGreen 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 信号标图 
#property indicator_label2  "Signal" 
#property indicator_type2   DRAW_LINE 
#property indicator_color2  clrRed 
#property indicator_style2  STYLE_SOLID 
#property indicator_width2  1 
//--- 设置指标值的限制 
#property indicator_minimum 0 
#property indicator_maximum 100 
//--- 指标窗口的横向水平 
#property indicator_level1  -100.0 
#property indicator_level2  100.0 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iStochastic,       // 使用iStochastic 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iStochastic;     // 函数类型 
input int                  Kperiod=5;                 // K 线周期 (用于计算的柱数) 
input int                  Dperiod=3;                 // D 线周期(主要平滑周期) 
input int                  slowing=3;                 // 最后平滑周期       
input ENUM_MA_METHOD       ma_method=MODE_SMA;        // 平滑类型  
input ENUM_STO_PRICE       price_field=STO_LOWHIGH;   // 随机计算方法 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         StochasticBuffer[]; 
double         SignalBuffer[]; 
//--- 存储iStochastic指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在随机震荡指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,StochasticBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,SignalBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iStochastic) 
      handle=iStochastic(name,period,Kperiod,Dperiod,slowing,ma_method,price_field); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[5]; 
      //--- 用于计算的K线周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=Kperiod; 
      //--- 用于主要平滑的D线周期 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=Dperiod; 
      //--- 最后平滑的K线周期 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=slowing; 
      //--- 平滑类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=ma_method; 
      //--- 随机计算方法 
      pars[4].type=TYPE_INT; 
      pars[4].integer_value=price_field; 
      handle=IndicatorCreate(name,period,IND_STOCHASTIC,5,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iStochastic indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示随机震荡指标计算的交易品种/时间帧 
   short_name=StringFormat("iStochastic(%s/%s, %d, %d, %d, %s, %s)",name,EnumToString(period), 
                           Kperiod,Dperiod,slowing,EnumToString(ma_method),EnumToString(price_field)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iStochastic指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iStochastic指标数量值更改 
//---或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果StochasticBuffer数组大于交易品种/周期iStochastic 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iStochastic 指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(StochasticBuffer,SignalBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住随机震荡指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iStochastic指标的指标缓冲区                                     | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &main_buffer[],    // 随机震荡值的指标缓冲区 
                           double &signal_buffer[],  // 信号线的指标缓冲区 
                           int ind_handle,           // iStochastic 指标的处理程序 
                           int amount                // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分StochasticBuffer数组 
   if(CopyBuffer(ind_handle,MAIN_LINE,0,amount,main_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 以1标引指标缓冲区的值填充部分SignalBuffer数组 
   if(CopyBuffer(ind_handle,SIGNAL_LINE,0,amount,signal_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iStochastic indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.35 iTEMA

函数返回 三倍指数均线(Triple Exponential Moving Average) 指标的句柄。它只有一个缓冲区。

int  iTEMA( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   int                 ma_shift,          // 指标平移 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 计算平均周期(计算K线柱数)。

ma_shift

[in] 与价格图表相关的指标变化。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                   Demo_iTEMA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iTEMA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All the other parameters are similar to the standard Triple Exponential Moving Average." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iTEMA 标图 
#property indicator_label1  "iTEMA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iTEMA,             // 使用iTEMA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iTEMA;           // 函数类型  
input int                  ma_period=14;              // 平均周期 
input int                  ma_shift=0;                // 移动 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iTEMABuffer[]; 
//--- 存储iTEMA 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在三倍指数移动平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iTEMABuffer,INDICATOR_DATA); 
//--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iTEMA) 
      handle=iTEMA(name,period,ma_period,ma_shift,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[3]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 移动 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ma_shift; 
      //--- 价格类型 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_TEMA,3,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iTEMA indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示三倍指数移动平均指标计算的交易品种/时间帧 
   short_name=StringFormat("iTEMA(%s/%s, %d, %d, %s)",name,EnumToString(period), 
                           ma_period,ma_shift,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iTEMA 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iTEMA 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iTEMABuffer数组大于交易品种/周期iTEMA 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以三倍指数移动平均指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iTEMABuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住三倍指数移动平均指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iTEMA 指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &tema_buffer[], // 三倍指数移动平均值的指标缓冲区 
                         int t_shift,           // 线的移动 
                         int ind_handle,        // iTEMA 指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iTEMABuffer数组 
   if(CopyBuffer(ind_handle,0,-t_shift,amount,tema_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iTEMA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.36 iTriX

函数返回 三倍指数均线振荡(Triple Exponential Moving Averages Oscillator)指标的句柄。它只有一个缓冲区。

int  iTriX( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 ma_period,         // 平均周期 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

ma_period

[in] 计算平均周期(计算K线柱数)。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                   Demo_iTriX.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iTriX technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iTriX标图 
#property indicator_label1  "iTriX" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iTriX,             // 使用iTriX 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iTriX;           // 函数类型  
input int                  ma_period=14;              // 周期 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iTriXBuffer[]; 
//--- 存储iTriX 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在三倍指数移动平均震荡指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iTriXBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iTriX) 
      handle=iTriX(name,period,ma_period,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[2]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=ma_period; 
      //--- 价格类型 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=applied_price;       
      handle=IndicatorCreate(name,period,IND_TRIX,2,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iTriX indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示三倍指数移动平均震荡指标计算的交易品种/时间帧 
   short_name=StringFormat("iTriX(%s/%s, %d, %s)",name,EnumToString(period), 
                          ma_period,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iTriX指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iTriX指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iTriXBuffer 数组大于交易品种/周期 iTriX 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以三倍指数移动平均震荡指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iTriXBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住三倍指数移动平均震荡指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iTriX 指标的指标缓冲区                                          | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &trix_buffer[], // 三倍指数移动平均震荡值的指标缓冲区 
                         int ind_handle,        // iTriX指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iTriXBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,trix_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iTriX indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }  
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

# 26.37 iWPR

函数返回 威廉指数(Larry Williams' Percent Range ) 指标。它只有一个缓冲区。

int  iWPR( 
   string           symbol,          // 交易品种名称 
   ENUM_TIMEFRAMES  period,          // 周期 
   int              calc_period      // 平均周期 
   );
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

calc_period

[in] 指标计算周期(计算K线柱)。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                    Demo_iWPR.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iWPR technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iWPR 标图 
#property indicator_label1  "iWPR" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrCyan 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//--- 设置指标值的限制 
#property indicator_minimum -100 
#property indicator_maximum 0 
//--- 指标窗口的横向水平 
#property indicator_level1  -20.0 
#property indicator_level2  -80.0 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iWPR,              // 使用iWPR 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iWPR;            // 函数类型 
input int                  calc_period=14;            // 周期 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iWPRBuffer[]; 
//--- 存储iWPR 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在Larry Williams百分比范围指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iWPRBuffer,INDICATOR_DATA); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iWPR) 
      handle=iWPR(name,period,calc_period); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[1]; 
      //--- 周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=calc_period; 
      handle=IndicatorCreate(name,period,IND_WPR,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iWPR indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示Larry Williams百分比范围指标计算的交易品种/时间帧 
   short_name=StringFormat("iWPR(%s/%s, %d)",name,EnumToString(period),calc_period); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化 
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                 | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iWPR指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iWPR 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iWPRBuffer 数组大于交易品种/周期iWPR 指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以威廉姆斯百分比范围指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iWPRBuffer,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住Larry Williams百分比范围指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iWPR指标的指标缓冲区                                            | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &wpr_buffer[],  // 威廉姆斯百分比范围值的指标缓冲区 
                         int ind_handle,        // iWPR指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//---以0标引指标缓冲区的值填充部分iWPRBuffer数组 
   if(CopyBuffer(ind_handle,0,0,amount,wpr_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iWPR indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.38 iVIDyA

函数返回 变量指数动态平均(Variable Index Dynamic Average)指标的句柄。它只有一个缓冲区。

int  iVIDyA( 
   string              symbol,            // 交易品种名称 
   ENUM_TIMEFRAMES     period,            // 周期 
   int                 cmo_period,        // Chande 动量指标周期 
   int                 ema_period,        // EMA 平滑周期 
   int                 ma_shift,          // 价格图表平移 
   ENUM_APPLIED_PRICE  applied_price      // 价格或者处理器类型 
   );
1
2
3
4
5
6
7
8

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

period

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

cmo_period

[in] 钱德动量摆动指标计算周期(计算K线柱数量)。

ema_period

[in] 平滑系数计算EMA 周期(计算K线柱数量)。

ma_shift

[in] 与价格图表相关的指标变化。

applied_price

[in] 应用价格。取值范围是 ENUM_APPLIED_PRICE 价格常量 或者 另外指标的句柄。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                  Demo_iVIDyA.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iVIDyA technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
#property description "All other parameters like in the standard Variable Index Dynamic Average." 
  
#property indicator_chart_window 
#property indicator_buffers 1 
#property indicator_plots   1 
//--- iVIDyA 标图 
#property indicator_label1  "iVIDyA" 
#property indicator_type1   DRAW_LINE 
#property indicator_color1  clrBlue 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iVIDyA,            // 使用iVIDyA 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iVIDyA;          // 函数类型 
input int                  cmo_period=15;             // Chande 动量周期 
input int                  ema_period=12;             // 平滑因素周期 
input int                  ma_shift=0;                // 移动 
input ENUM_APPLIED_PRICE   applied_price=PRICE_CLOSE; // 价格类型 
input string               symbol=" ";                // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;     // 时间帧 
//--- 指标缓冲区 
double         iVIDyABuffer[]; 
//--- 存储iVIDyA 指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在变量指数动态平均指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iVIDyABuffer,INDICATOR_DATA); 
//--- 设置移动 
   PlotIndexSetInteger(0,PLOT_SHIFT,ma_shift); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iVIDyA) 
      handle=iVIDyA(name,period,cmo_period,ema_period,ma_shift,applied_price); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[4]; 
      //--- Chande 动量周期 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=cmo_period; 
      //--- 平滑因素周期 
      pars[1].type=TYPE_INT; 
      pars[1].integer_value=ema_period; 
      //--- 移动 
      pars[2].type=TYPE_INT; 
      pars[2].integer_value=ma_shift; 
      //--- 价格类型 
      pars[3].type=TYPE_INT; 
      pars[3].integer_value=applied_price; 
      handle=IndicatorCreate(name,period,IND_VIDYA,4,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iVIDyA indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示变量指数动态平均指标计算的交易品种/时间帧 
   short_name=StringFormat("iVIDyA(%s/%s, %d, %d, %d, %s)",name,EnumToString(period), 
                           cmo_period,ema_period,ma_shift,EnumToString(applied_price)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iVIDyA 指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iVIDyA 指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iVIDyABuffer数组大于交易品种/周期iVIDyA指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以变量指数动态平均指标的值填充数组 
//--- 如果FillArrayFromBuffer 返回 false,它表示信息还未准备,退出操作 
   if(!FillArrayFromBuffer(iVIDyABuffer,ma_shift,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住变量指数动态平均指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充iVIDyA 指标的指标缓冲区                                         | 
//+------------------------------------------------------------------+ 
bool FillArrayFromBuffer(double &vidya_buffer[],// 变量指数动态平均值的指标缓冲区 
                         int v_shift,           // 线的移动  
                         int ind_handle,        // iVIDyA指标的处理程序 
                         int amount             // 复制值的数量 
                         ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iVIDyABuffer 数组 
   if(CopyBuffer(ind_handle,0,-v_shift,amount,vidya_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iVIDyA indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }
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

# 26.39 iVolumes

函数返回 交易量 指标的句柄。它只有一个缓冲区。

int  iVolumes( 
   string               symbol,             // 交易品种名称 
   ENUM_TIMEFRAMES      period,             // 周期 
   ENUM_APPLIED_VOLUME  applied_volume      // 计算的交易量类型 
   )
1
2
3
4
5

参数

symbol

[in] 金融产品的交易品种名称,其报价数据应用于计算指标。 NULL 值代表当前交易品种。

[in] 周期值的取值范围是 ENUM_TIMEFRAMES 枚举值之一,0代表当前时间表。

applied_volume

[in] 使用的交易量。取值范围是 ENUM_APPLIED_VOLUME 枚举的常量之一。

返回值

返回指定技术指标的句柄,如果失败则返回INVALID_HANDLE。使用指标句柄传递到的IndicatorRelease()函数,表示不再使用该指标并且释放计算机内存。

例如:

//+------------------------------------------------------------------+ 
//|                                                Demo_iVolumes.mq5 | 
//|                        Copyright 2011, MetaQuotes Software Corp. | 
//|                                             https://www.mql5.com | 
//+------------------------------------------------------------------+ 
#property copyright "Copyright 2011, MetaQuotes Software Corp." 
#property link      "https://www.mql5.com" 
#property version   "1.00" 
#property description "The indicator demonstrates how to obtain data" 
#property description "of indicator buffers for the iVolumes technical indicator." 
#property description "A symbol and timeframe used for calculation of the indicator," 
#property description "are set by the symbol and period parameters." 
#property description "The method of creation of the handle is set through the 'type' parameter (function type)." 
  
#property indicator_separate_window 
#property indicator_buffers 2 
#property indicator_plots   1 
//--- iVolumes标图 
#property indicator_label1  "iVolumes" 
#property indicator_type1   DRAW_COLOR_HISTOGRAM 
#property indicator_color1  clrGreen, clrRed 
#property indicator_style1  STYLE_SOLID 
#property indicator_width1  1 
//+------------------------------------------------------------------+ 
//| 枚举处理创建方法                                                   | 
//+------------------------------------------------------------------+ 
enum Creation 
  { 
   Call_iVolumes,          // 使用iVolumes 
   Call_IndicatorCreate    // 使用IndicatorCreate 
  }; 
//--- 输入参数 
input Creation             type=Call_iVolumes;           // 函数类型 
input ENUM_APPLIED_VOLUME  applied_volume=VOLUME_TICK;   // 交易量类型 
input string               symbol=" ";                   // 交易品种  
input ENUM_TIMEFRAMES      period=PERIOD_CURRENT;        // 时间帧 
//--- 指标缓冲区 
double         iVolumesBuffer[]; 
double         iVolumesColors[]; 
//--- 存储iVolumes指标处理程序的变量 
int    handle; 
//--- 存储变量 
string name=symbol; 
//--- 图表上的指标名称 
string short_name; 
//--- 我们将在交易量指标中保持值的数量 
int    bars_calculated=0; 
//+------------------------------------------------------------------+ 
//| 自定义指标初始化函数                                                | 
//+------------------------------------------------------------------+ 
int OnInit() 
  { 
//--- 分配指标缓冲区数组 
   SetIndexBuffer(0,iVolumesBuffer,INDICATOR_DATA); 
   SetIndexBuffer(1,iVolumesColors,INDICATOR_COLOR_INDEX); 
//--- 定义绘制指标的交易品种 
   name=symbol; 
//--- 删除向左和向右的空格 
   StringTrimRight(name); 
   StringTrimLeft(name); 
//--- 如果它返回 'name' 字符串的零长度 
   if(StringLen(name)==0) 
     { 
      //--- 获得指标附属的图表交易品种 
      name=_Symbol; 
     } 
//--- 创建指标处理程序 
   if(type==Call_iVolumes) 
      handle=iVolumes(name,period,applied_volume); 
   else 
     { 
      //--- 以指标参数填充结构    
      MqlParam pars[1]; 
      //--- 价格类型 
      pars[0].type=TYPE_INT; 
      pars[0].integer_value=applied_volume; 
      handle=IndicatorCreate(name,period,IND_VOLUMES,1,pars); 
     } 
//--- 如果没有创建处理程序 
   if(handle==INVALID_HANDLE) 
     { 
      //--- 叙述失败和输出错误代码 
      PrintFormat("Failed to create handle of the iVolumes indicator for the symbol %s/%s, error code %d", 
                  name, 
                  EnumToString(period), 
                  GetLastError()); 
      //--- 指标提前停止 
      return(INIT_FAILED); 
     } 
//--- 显示交易量指标计算的交易品种/时间帧 
   short_name=StringFormat("iVolumes(%s/%s, %s)",name,EnumToString(period),EnumToString(applied_volume)); 
   IndicatorSetString(INDICATOR_SHORTNAME,short_name); 
//--- 指标正常初始化   
   return(INIT_SUCCEEDED); 
  } 
//+------------------------------------------------------------------+ 
//| 自定义指标迭代函数                                                  | 
//+------------------------------------------------------------------+ 
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[]) 
  { 
//--- 从iVolumes指标复制的值数 
   int values_to_copy; 
//--- 确定指标计算的数量值 
   int calculated=BarsCalculated(handle); 
   if(calculated<=0) 
     { 
      PrintFormat("BarsCalculated() returned %d, error code %d",calculated,GetLastError()); 
      return(0); 
     } 
//--- 如果它是指标计算的最初起点或如果iVolumes指标数量值更改 
//--- 或如果需要计算两个或多个柱形的指标(这意味着价格历史中有些内容会发生变化) 
   if(prev_calculated==0 || calculated!=bars_calculated || rates_total>prev_calculated+1) 
     { 
      //--- 如果iVolumesBuffer数组大于交易品种/周期iVolumes指标的数量值,那么我们不会复制任何内容 
      //--- 否则,我们复制小于指标缓冲区的大小 
      if(calculated>rates_total) values_to_copy=rates_total; 
      else                       values_to_copy=calculated; 
     } 
   else 
     { 
      //--- 它意味着这不是初次指标计算,因为 OnCalculate())最近调用 
      //--- 为了计算,添加不超过一柱 
      values_to_copy=(rates_total-prev_calculated)+1; 
     } 
//--- 以iVolumes指标的值填充数组 
//--- 如果FillArraysFromBuffer返回false,它表示信息还未准备,退出操作 
   if(!FillArraysFromBuffers(iVolumesBuffer,iVolumesColors,handle,values_to_copy)) return(0); 
//--- 形成信息 
   string comm=StringFormat("%s ==>  Updated value in the indicator %s: %d", 
                            TimeToString(TimeCurrent(),TIME_DATE|TIME_SECONDS), 
                            short_name, 
                            values_to_copy); 
//--- 在图表上展示服务信息 
   Comment(comm); 
//--- 记住交易量指标的数量值 
   bars_calculated=calculated; 
//--- 返回prev_calculated值以便下次调用 
   return(rates_total); 
  } 
//+------------------------------------------------------------------+ 
//| 填充 iVolumes 指标的指标缓冲区                                      | 
//+------------------------------------------------------------------+ 
bool FillArraysFromBuffers(double &volume_buffer[],   // 交易量值的指标缓冲区 
                           double &color_buffer[],    // 颜色指标缓冲区 
                           int ind_handle,            // iVolumes 指标的处理程序 
                           int amount                 // 复制值的数量 
                           ) 
  { 
//--- 重置错误代码 
   ResetLastError(); 
//--- 以0标引指标缓冲区的值填充部分iVolumesBuffer 数组 
   if(CopyBuffer(ind_handle,0,0,amount,volume_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iVolumes indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 以1标引指标缓冲区的值填充部分iVolumesColors 数组 
   if(CopyBuffer(ind_handle,1,0,amount,color_buffer)<0) 
     { 
      //--- 如果复制失败,显示错误代码 
      PrintFormat("Failed to copy data from the iVolumes indicator, error code %d",GetLastError()); 
      //--- 退出零结果 - 它表示被认为是不计算的指标 
      return(false); 
     } 
//--- 一切顺利 
   return(true); 
  } 
//+------------------------------------------------------------------+ 
//| 指标去初始化函数                                                   | 
//+------------------------------------------------------------------+ 
void OnDeinit(const int reason) 
  { 
//--- 删除指标后清空图表 
   Comment(""); 
  }  
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