/// <summary> /// 计算隐含波动率及其他希腊值的类。 /// </summary> /// <param name="option">期权类型</param> /// <param name="optionData">期权数据</param> /// <param name="etfData">etf数据</param> /// <param name="duration">到期时间</param> public Impv(optionFormat option, levelOne optionData, levelOne etfData, double duration) { this.option = option; this.optionData = optionData; this.etfData = etfData; this.duration = duration; }
/// <summary> /// 计算隐含波动率 /// </summary> /// <param name="myData">数据</param> /// <param name="index">时间下标</param> public void ComputeVolDaily(GetDataDaily myData, int index) { levelOne etfData = myData.dataDaily["510050.SH"][index]; foreach (int code in optionListInt) { string codeStr = code.ToString() + ".SH"; optionFormat option = OptionInformation.myOptionList[code]; levelOne optionData = myData.dataDaily[codeStr][index]; double duration = TradeDays.GetTimeSpan(date, option.endDate); Impv vol = new Impv(option, optionData, etfData, duration); double sigma = vol.computeVol(); GetCurve(option.strike, option.optionType, duration, sigma); } }
/// <summary> /// 从数据库中读取期权信息,并判断是否完整。 /// </summary> /// <param name="startDate">开始时间</param> /// <param name="endDate">结束时间</param> /// <returns>是否完整</returns> private bool GetOptionListFromTable(int startDate, int endDate) { using (SqlConnection conn = new SqlConnection(connectString)) { conn.Open();//打开数据库 SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from [" + dataBaseName + "].[dbo].[" + optionCodeTableName + "]"; int theLastCode = 0; int theLastStartDate = 0; try { //从数据库中读取数据流存入reader中 SqlDataReader reader = cmd.ExecuteReader(); //从reader中读取下一行数据,如果没有数据,reader.Read()返回flase while (reader.Read()) { optionFormat newOption = new optionFormat(); int optionCode = reader.GetInt32(reader.GetOrdinal("OptionCode")); theLastCode = (optionCode > theLastCode) ? optionCode : theLastCode; newOption.optionCode = optionCode; newOption.optionName = reader.GetString(reader.GetOrdinal("OptionName")).Trim(); newOption.executeType = reader.GetString(reader.GetOrdinal("ExecuteType")).Trim(); newOption.strike = reader.GetDouble(reader.GetOrdinal("Strike")); newOption.optionType = reader.GetString(reader.GetOrdinal("OptionType")).Trim(); newOption.startDate = reader.GetInt32(reader.GetOrdinal("StartDate")); newOption.endDate = reader.GetInt32(reader.GetOrdinal("EndDate")); newOption.market = reader.GetString(reader.GetOrdinal("Market")).Trim(); theLastStartDate = (theLastStartDate > newOption.startDate) ? theLastStartDate : newOption.startDate; if (myOptionList.ContainsKey(optionCode) == false) { myOptionList.Add(optionCode, newOption); } } } catch (Exception myerror) { System.Console.WriteLine(myerror.Message); } if (theLastStartDate >= endDate && theLastCode % 10000 == myOptionList.Count) { return(true); } } return(false); }
/// <summary> /// 按日期遍历,添加期权信息。写入静态哈希表myOptionList。 /// </summary> /// <param name="startDate">开始日期</param> /// <param name="endDate">结束日期</param> private void GetOptionInformationList(int startDate, int endDate) { //定义交易日期的类。 TradeDays myTradeDays = new TradeDays(startDate, endDate); //从数据库表中获取信息,如果已经是完整信息就不需要连接万德数据库。 if (GetOptionListFromTable(startDate, endDate) == true) { return; } //按日期遍历,添加期权信息。 WindAPI w = new WindAPI(); w.start(); foreach (int today in myTradeDays.myTradeDays) { if (TradeDays.IsOptionExerciseDate(today) || today == myTradeDays.myTradeDays[myTradeDays.myTradeDays.Count - 1]) { WindData optionToday = w.wset("OptionChain", "date=" + today.ToString() + ";us_code=" + Configuration.underlyingAsset + ";option_var=;month=全部;call_put=全部"); object[] optionList = optionToday.data as object[]; int num = optionList.Length / 13; for (int i = 0; i < num; i++) { optionFormat option = new optionFormat(); string codeString = (string)optionList[i * 13 + 4 - 1]; option.optionCode = Convert.ToInt32(codeString.Substring(0, 8)); option.market = codeString.Substring(9, 2); option.optionName = (string)optionList[i * 13 + 5 - 1]; option.executeType = (string)optionList[i * 13 + 6 - 1]; option.strike = (double)optionList[i * 13 + 7 - 1]; option.optionType = (string)optionList[i * 13 + 9 - 1]; option.startDate = TradeDays.DateTimeToInt((DateTime)optionList[i * 13 + 10 - 1]); option.endDate = TradeDays.DateTimeToInt((DateTime)optionList[i * 13 + 11 - 1]); if (myOptionList.ContainsKey(option.optionCode) == false) { myOptionList.Add(option.optionCode, option); } } } } w.stop(); }