iEnvelopes

函数返回轨道线指标处理器。

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          // 中线边界差(百分率)

   );

参量

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)

  {

   if(handle!=INVALID_HANDLE)

      IndicatorRelease(handle);

//--- 删除指标后清空图表

   Comment("");

  }