private double[][] computeRollingFractile(int start, int end, int period) { double[][] disArr = new double[etfDailyData.Count()][]; //获取前复权的价格 double[] etfPrice = new double[etfDailyData.Count()]; for (int i = 0; i < etfDailyData.Count(); i++) { etfPrice[i] = etfDailyData[i].close * etfDailyData[i].adjustFactor / etfDailyData.Last().adjustFactor; } //获取ETF每日年化波动率 double[] etfVol = new double[etfDailyData.Count()]; etfVol = Volatility.HVYearly(etfPrice, step); this.etfVol = etfVol; //统计每日波动率分位数 List <double> volList = new List <double>(); for (int i = start; i < etfPrice.Count(); i++) { //按周期向前推算历史波动率 volList = etfVol.ToList().GetRange(start - period + 1, period).OrderBy(x => x).ToList(); if (i >= start) { int L = volList.Count() - 1; disArr[i] = new double[11]; disArr[i][0] = volList[0]; disArr[i][1] = volList[(int)Math.Ceiling(L * 0.1)]; disArr[i][2] = volList[(int)Math.Ceiling(L * 0.2)]; disArr[i][3] = volList[(int)Math.Ceiling(L * 0.3)]; disArr[i][4] = volList[(int)Math.Ceiling(L * 0.4)]; disArr[i][5] = volList[(int)Math.Ceiling(L * 0.5)]; disArr[i][6] = volList[(int)Math.Ceiling(L * 0.6)]; disArr[i][7] = volList[(int)Math.Ceiling(L * 0.7)]; disArr[i][8] = volList[(int)Math.Ceiling(L * 0.8)]; disArr[i][9] = volList[(int)Math.Ceiling(L * 0.9)]; disArr[i][10] = volList[L]; } } return(disArr); }
/// <summary> /// 计算历史波动率的分位数 /// </summary> /// <returns></returns> private double[][] computeFractile(int start, int end) { double[][] disArr = new double[etfDailyData.Count()][]; //获取前复权的价格 double[] etfPrice = new double[etfDailyData.Count()]; for (int i = 0; i < etfDailyData.Count(); i++) { etfPrice[i] = etfDailyData[i].close * etfDailyData[i].adjustFactor / etfDailyData.Last().adjustFactor; } //获取ETF每日年化波动率 double[] etfVol = new double[etfDailyData.Count()]; etfVol = Volatility.HVYearly(etfPrice, step); this.etfVol = etfVol; //统计每日波动率分位数 List <double> volList = new List <double>(); for (int i = 1; i < etfPrice.Count(); i++) { //按循序依次向数组中插入波动率 if (volList.Count() == 0) { volList.Add(etfVol[i]); } else { if (etfVol[i] < volList[0]) { volList.Insert(0, etfVol[i]); } else if (etfVol[i] > volList.Last()) { volList.Insert(volList.Count(), etfVol[i]); } else { for (int j = 1; j < volList.Count() - 1; j++) { if (etfVol[i] > volList[j - 1] && etfVol[i] <= volList[j]) { volList.Insert(j, etfVol[i]); continue; } } } } if (i >= start) { int L = volList.Count() - 1; disArr[i] = new double[11]; disArr[i][0] = volList[0]; disArr[i][1] = volList[(int)Math.Ceiling(L * 0.1)]; disArr[i][2] = volList[(int)Math.Ceiling(L * 0.2)]; disArr[i][3] = volList[(int)Math.Ceiling(L * 0.3)]; disArr[i][4] = volList[(int)Math.Ceiling(L * 0.4)]; disArr[i][5] = volList[(int)Math.Ceiling(L * 0.5)]; disArr[i][6] = volList[(int)Math.Ceiling(L * 0.6)]; disArr[i][7] = volList[(int)Math.Ceiling(L * 0.7)]; disArr[i][8] = volList[(int)Math.Ceiling(L * 0.8)]; disArr[i][9] = volList[(int)Math.Ceiling(L * 0.9)]; disArr[i][10] = volList[L]; } } return(disArr); }
private void computeVol(string code, int period, ref List <HistoricalVol> volList) { var list = getHistoricalDailyData(code); var mdata = (from x in list where x.close > 0 select x).ToList(); var timelist = mdata.Select(x => x.time).ToArray(); var closelist = mdata.Select(x => x.close).ToArray(); var vol = Volatility.HVYearly(closelist, period); int start = volList.Count() - vol.Count(); string[] monthList = code.Split('.'); string month = monthList[0].Substring(monthList[0].Length - 2, 2); for (int i = start; i < volList.Count(); i++) { if (month == "01" && period == period1) { volList[i].M01_1 = vol[i - start]; } else if (month == "01" && period == period2) { volList[i].M01_2 = vol[i - start]; } else if (month == "03" && period == period1) { volList[i].M03_1 = vol[i - start]; } else if (month == "03" && period == period2) { volList[i].M03_2 = vol[i - start]; } else if (month == "05" && period == period1) { volList[i].M05_1 = vol[i - start]; } else if (month == "05" && period == period2) { volList[i].M05_2 = vol[i - start]; } else if (month == "07" && period == period1) { volList[i].M07_1 = vol[i - start]; } else if (month == "07" && period == period2) { volList[i].M07_2 = vol[i - start]; } else if (month == "08" && period == period1) { volList[i].M08_1 = vol[i - start]; } else if (month == "08" && period == period2) { volList[i].M08_2 = vol[i - start]; } else if (month == "09" && period == period1) { volList[i].M09_1 = vol[i - start]; } else if (month == "09" && period == period2) { volList[i].M09_2 = vol[i - start]; } else if (month == "11" && period == period1) { volList[i].M11_1 = vol[i - start]; } else if (month == "11" && period == period2) { volList[i].M11_2 = vol[i - start]; } else if (month == "12" && period == period1) { volList[i].M12_1 = vol[i - start]; } else if (month == "12" && period == period2) { volList[i].M12_2 = vol[i - start]; } } }