鼎元C++量化程式码指标与函数模块系列之【布林带(bollingerbands,BOLL)计算方法及调用方法】
一、先在C++头文件声明公式变量:[code]double bollingerbands(string period, string inst, int num,int ref);//BOLL计算公式;
[/code]二、在test.cpp源文件中增加BOLL公式模块:[code]
//布林带(bollingerbands)开始
double test::bollingerbands(string period, string inst, int num, int ref)
{
double ma = average(sPeriod, sInst,num);//中间均线ma
int key = 0;
double sumdev = 0;
double madev = 0;
if (mapK[period][inst].size() < num) return 0;
map<string, TKVALUE>::reverse_iterator it;
for (it = mapK[period][inst].rbegin(); it != mapK[period][inst].rend(); ++it)
{
sumdev += pow(it->second.dClose - ma,2); //diff平方加总
key++;
if (key >= num)break;
}
madev = sqrt(sumdev / num); //得到方差后再取标准差
return ma + ref*madev;
}
[/code]三、调用方法[code]
RsqBar(sPeriod, sInst);
bollingerbands(sPeriod, sInst, length,2); //指标BOLL的上沿;
bollingerbands(sPeriod, sInst, length,-2); //指标BOLL的下沿;
[/code]
页:
[1]