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 limit;
int shift;
if(rates_total<Rperiod){
return rates_total;
}
…
}
一、跳过K线数小于period的计算
period为计算某一值需要的最小的K线数
如MA5(五日平均)最少需要已有5根K线
if(rates_total<Rperiod){
return rates_total;
}
二、避免计算过的指标值重复计算
利用rates_total总K线数与prev_calculated 先前计算过的K线数
的差值定义为limit,由limit限制每次价格跳动时
指标数组的赋值操作,可以避免计算过的指标值被
重复计算
limit=rates_total-prev_calculated;
for(shift=limit-1;shift>=0;shift–)
{
//指标数组赋值操作
…
}
三、柱状图的绘制
柱状图的绘制需要两个柱状图或一柱状图和一条线,柱状图会绘制两个数组的差值部分
四、指标的属性指定
1、属性设定
//— plot Label1
#property indicator_label1 “Label1”
#property indicator_type1 DRAW_LINE
#property indicator_color1 clrRed
#property indicator_style1 STYLE_SOLID
#property indicator_width1 1
2、代码设定
SetIndexStyle(index,type,style,width,ncolor);
五、主图指标与附图指标的区别
1、指定为主图指标
#property indicator_chart_window
2、指定为附图指标
#property indicator_separate_window