我构建了自己的自定义指示器,它使用前面的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);
}
发布于 2019-08-24 00:31:59
我解决了这个问题。
这条线是罪魁祸首。
i = Bars - counted_bars - delta;
一旦指示符“赶上”了图表上所有已完成的条形图,i
就等于1 - delta
,这将是负值。其结果是while循环永远不会运行。
https://stackoverflow.com/questions/57631610
复制相似问题