我构建了自己的自定义指标,使用前 X 根柱线计算图表的近似斜率和加速度。
当我将指标附加到图表时,它按预期工作,但是当我尝试使用 iCustom(( 检索 EA 中的最新值时,它只返回2147483647。
我已经尝试对iCustom()
shift 参数使用不同的值,但没有成功。
double SlopeBuffer[];
double AccelerationBuffer[];
extern int delta;
int OnInit()
{
//--- indicator buffers mapping
SetIndexBuffer(0,SlopeBuffer);
SetIndexBuffer(1,AccelerationBuffer);
//---
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Custom indicator iteration function |
//+------------------------------------------------------------------+
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 i, counted_bars;
counted_bars = IndicatorCounted();
i = Bars - counted_bars - delta; // Here I offset the initial bar by the delta(period)
// so that the first bar has delta previous bars on the chart.
while(i>=0){
double Ex = 0; //intermediate calculation variables...
double Ey = 0;
double Exy = 0;
double Exx = 0;
for(int n=0;n<delta;n++){ // This for loop iterates over the previous delta bars
Ex += n; // to calculate various sigma variables used to find the
Ey += Close[i+delta-n-1]; // slope.
Exy += n*Close[i+delta-n-1];
Exx += n*n;
}
double slope = 100*(delta*Exy - Ex*Ey)/(delta*Exx - Ex*Ex); // final slope calculation.
SlopeBuffer[i] = slope; // add to the buffer.
AccelerationBuffer[i] = (slope - SlopeBuffer[i+1]); // calculate acceleration
i--; // and adding to buffer.
}
//--- return value of prev_calculated for next call
return(rates_total);
}
我想出了问题。
这条线是罪魁祸首。
i = Bars - counted_bars - delta;
一旦指标"赶上"图表上所有已完成的柱线,i
将等于负数的1 - delta
。结果是 while 循环永远不会运行。