自定义指标

这是一组用来创建自定义指标的函数,这些函数不能用于编辑EA交易和脚本。

函数

功能

SetIndexBuffer

将指定指标缓冲区和一维双精度动态数组绑定一起

IndicatorSetDouble

设置双精度型指标属性值

IndicatorSetInteger

设置整型型指标属性值

IndicatorSetString

设置字符串指标属性值

PlotIndexSetDouble

设置双精度型指标行属性值

PlotIndexSetInteger

设置整型指标行属性值

PlotIndexSetString

设置字符串指标行属性值

PlotIndexGetInteger

返回整数型指标行属性值

指标属性可以使用编译程序指令或使用函数来设定。若要更好的理解这点,建议您学习指标类型示例

自定义指标中所有必要的运算都在预定义的OnCalculate()函数中,如果短时间使用OnCalculate()函数调用,如

int OnCalculate (const int rates_total, const int prev_calculated, const int begin, const double& price[])

而后rates_total参量包括price[]数组中的全部元素数量值,以输入参量传递到计算指标值中。

参量 prev_calculated 是先前调用的OnCalculate() 计算结果,允许为计算指标值组织一种节约算法,例如,如果当前值是rates_total = 1000, prev_calculated = 999, 足够为每个指标缓冲区计算值。

如果是关于输入数组价格的信息是不能利用的,它会为每个指标缓冲区计算1000个必要值。第一次调用OnCalculate() value prev_calculated = 0,如果price[]数组在某种程度上改变,case prev_calculated 会等于0。

begin参量表示价格数组原始值的数量,不包括计算数据。例如,如果Accelerator Oscillator 值(开头37个值没有计算)以输入参量使用,那么begin = 37。例如,列举一个简单指标:

#property indicator_chart_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
//--- 指标缓冲区
double         Label1Buffer[];
//+------------------------------------------------------------------+
//| 自定义指标初始化函数                                                |
//+------------------------------------------------------------------+
void OnInit()
  {
//--- 指标缓冲区绘图
   SetIndexBuffer(0,Label1Buffer,INDICATOR_DATA);
//---
  }
//+------------------------------------------------------------------+
//| 自定义指标重复函数                                                  |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const int begin,
                const double &price[])
 
  {
//---
   Print("begin = ",begin,"  prev_calculated = ",prev_calculated,"  rates_total = ",rates_total);
//--- 为下一次调用返回prev_calculated值
   return(rates_total);
  }

从"导航器" 界面离开到加速震荡指标窗口,我们认为该计算一定以先前指标的值为基础。

Calculating an indicator on values of the previously attached indicator

结果是,第一次调用prev_calculated的OnCalculate()值等于0,之后调用等于rates_total 值(直到增加价格图表的柱的数量)。

The begin parameter shows the number of initial bars, on ahich values are omitted

开始参量的值直接等于原始字节数量,加速指标的值不能根据指标逻辑值来计算。如果浏览自定义指标加速器.mq5的源代码,可以看到 OnInit() 函数线:

//--- 从被画的指数中设置第一柱
   PlotIndexSetInteger(0,PLOT_DRAW_BEGIN,37);

使用函数PlotIndexSetInteger(0, PLOT_DRAW_BEGIN, empty_first_values), 可以建立自定义指标的0指标数组中非存在第一值的编号,这样,我们不需要接收计算(empty_first_values). 因此,可以机械使用:

  1. 建立指标的原始值数字,不能用了计算另外自定义指标;
  2. 当调用另一自定义指标时,获得关于第一忽略值的数量信息,不需要进入计算逻辑值。