- UID
- 2
- 积分
- 2874604
- 威望
- 1387331 布
- 龙e币
- 1487273 刀
- 在线时间
- 13155 小时
- 注册时间
- 2009-12-3
- 最后登录
- 2024-11-24
|
计算历史波动率(calculating historical volatility)
1、头文件声明变量:- double Volatility(string period, string inst, int num);//计算历史波动率(CALCULATING HISTORICAL VOLATILITY)
复制代码 2、公式模块增加计算公式:- //计算历史波动率(calculating historical volatility)
- double test::Volatility(string period, string inst, int num)
- {
- double dPreClose = 0;
- int n = 0;
- double sumatr = 0;
- double xatr = 0;
- vector<double>vAtr;
- if (mapK[period][inst].size() < num) return 0;
- map<string, TKVALUE >::iterator it;
- for (it = mapK[period][inst].begin(); it != mapK[period][inst].end(); ++it)
- {
- if (dPreClose != 0)
- {
- double d = it->second.dHigh - it->second.dLow;
- double dHC = abs(it->second.dHigh - dPreClose);
- double dLC = abs(it->second.dLow - dPreClose);
- double e = max3(d, dHC, dLC);
- vAtr.push_back(e); //获得每一个bar对应的truerange
- }
- dPreClose = it->second.dClose;
- }
- for (int it = 0; it < vAtr.size(); ++it)
- {
- if (it < num) xatr = 0;
- if (it > num)
- {
- xatr = (2 * vAtr[num - 1] + (double(num) - 1) * xatr) / (double(num) + 1);
- }
- }
- return xatr;
- }
复制代码 3、调用方法:- Volatility(sPeriod, sInst, length);//计算length周期的历史波动率
复制代码 |
|