#property description "Indicator calculates absolute values of the difference between"
#property description "Open and Close or High and Low prices displaying them in a separate subwindow"
#property description "as a histogram."
//---指标设置
#property indicator_separate_window
#property indicator_buffers 1
#property indicator_plots 1
//---- 绘制
#property indicator_type1 DRAW_HISTOGRAM
#property indicator_style1 STYLE_SOLID
#property indicator_width1 3
//--- 导入参数
input bool InpAsSeries=true; // 指标缓冲区的标引导向
input bool InpPrices=true; // 计算价格 (true - 开盘价,收盘价; false - 最高价,最低价)
//--- 指标缓冲区
double ExtBuffer[];
//+------------------------------------------------------------------+
//| 计算指标的值 |
//+------------------------------------------------------------------+
void CandleSizeOnBuffer(const int rates_total,const int prev_calculated,
const double &first[],const double &second[],double &buffer[])
{
//--- 计算柱形的开始变量
int start=prev_calculated;
//--- 如果前一个订单号已经计算了指标值,那么使用最近的柱
if(prev_calculated>0)
start--;
//--- 定义数组的标引导向
bool as_series_first=ArrayGetAsSeries(first);
bool as_series_second=ArrayGetAsSeries(second);
bool as_series_buffer=ArrayGetAsSeries(buffer);
//--- 如果有必要,直接替代标引导向
if(as_series_first)
ArraySetAsSeries(first,false);
if(as_series_second)
ArraySetAsSeries(second,false);
if(as_series_buffer)
ArraySetAsSeries(buffer,false);
//--- 计算指标值
for(int i=start;i<rates_total;i++)
buffer[i]=MathAbs(first[i]-second[i]);
}
//+------------------------------------------------------------------+
//| 自定义指标初始函数 |
//+------------------------------------------------------------------+
int OnInit()
{
//--- 绑定指标缓冲区
SetIndexBuffer(0,ExtBuffer);
//--- 在指标缓冲区设置标引元素
ArraySetAsSeries(ExtBuffer,InpAsSeries);
//--- 检查指标计算的价格
if(InpPrices)
{
//--- 开盘价和收盘价
PlotIndexSetString(0,PLOT_LABEL,"BodySize");
//--- 设置指标颜色
PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrOrange);
}
else
{
//--- 最高价和最低价
PlotIndexSetString(0,PLOT_LABEL,"ShadowSize");
//--- 设置指标颜色
PlotIndexSetInteger(0,PLOT_LINE_COLOR,clrDodgerBlue);
}
//---
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[])
{
//--- 根据标识值计算指标
if(InpPrices)
CandleSizeOnBuffer(rates_total,prev_calculated,open,close,ExtBuffer);
else
CandleSizeOnBuffer(rates_total,prev_calculated,high,low,ExtBuffer);
//--- 为下次调用返回prev_calculated值
return(rates_total);
}
|