public bool TryGetRiskFreeRates(out RiskFreeRates riskFreeRate, out string message) { USTreasuryApiCaller.GetDailyTreasuryYieldCurveRateData(DateTime.Now.Year, DateTime.Now.Month, out string xml, out message); XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); XmlElement root = doc.DocumentElement; XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable); nsmgr.AddNamespace("xx", "http://www.w3.org/2005/Atom"); nsmgr.AddNamespace("d", "http://schemas.microsoft.com/ado/2007/08/dataservices"); nsmgr.AddNamespace("m", "http://schemas.microsoft.com/ado/2007/08/dataservices/metadata"); //https://stackoverflow.com/questions/3786443/xpath-to-get-the-element-with-the-highest-id var properties = root.SelectSingleNode("/xx:feed/xx:entry/xx:content/m:properties[not(/xx:feed/xx:entry/xx:content/m:properties/d:Id > d:Id)]", nsmgr); var enus = new CultureInfo("en-US"); riskFreeRate = new RiskFreeRates(); riskFreeRate.Date = DateTime.ParseExact(properties.SelectSingleNode("d:NEW_DATE", nsmgr).InnerText, "s", enus); riskFreeRate.OneMonth = double.Parse(properties.SelectSingleNode("d:BC_1MONTH", nsmgr).InnerText, enus) / 100; riskFreeRate.TwoMonths = double.Parse(properties.SelectSingleNode("d:BC_3MONTH", nsmgr).InnerText, enus) / 100; riskFreeRate.ThreeMonths = double.Parse(properties.SelectSingleNode("d:BC_3MONTH", nsmgr).InnerText, enus) / 100; riskFreeRate.SixMonths = double.Parse(properties.SelectSingleNode("d:BC_6MONTH", nsmgr).InnerText, enus) / 100; riskFreeRate.OneYear = double.Parse(properties.SelectSingleNode("d:BC_1YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.TwoYears = double.Parse(properties.SelectSingleNode("d:BC_2YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.ThreeYears = double.Parse(properties.SelectSingleNode("d:BC_3YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.FiveYears = double.Parse(properties.SelectSingleNode("d:BC_5YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.SevenYears = double.Parse(properties.SelectSingleNode("d:BC_7YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.TenYears = double.Parse(properties.SelectSingleNode("d:BC_10YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.TwentyYears = double.Parse(properties.SelectSingleNode("d:BC_20YEAR", nsmgr).InnerText, enus) / 100; riskFreeRate.ThirtyYears = double.Parse(properties.SelectSingleNode("d:BC_30YEAR", nsmgr).InnerText, enus) / 100; return(true); }
public bool TryGetRiskFreeRates(out RiskFreeRates riskFreeRate, out string message) { return(riskFreeRatesDataSource.TryGetRiskFreeRates(out riskFreeRate, out message)); }
public static void CalculateTheoricalValue(PriceList prices, OptionsChain optionChain, RiskFreeRates riskFreeRates, double lastPrice, double volatility) { optionChain.HistoricalVolatility = volatility; optionChain.Prices = prices; optionChain.RiskFreeRates = riskFreeRates; foreach (var options in optionChain) { foreach (Option option in options.Value) { if (option.IsCall) { option.TheoricalValue = CalculateCall(lastPrice, volatility, option, riskFreeRates.TwoYears); } else if (option.IsPut) { option.TheoricalValue = CalculatePut(lastPrice, volatility, option, riskFreeRates.TwoYears); } else { //Exotic Option //https://www.investopedia.com/terms/e/exoticoption.asp } } } }
/// <summary> /// It uses the Black-Scholes formula to calculate the theorical value of an european option. /// /// See: /// Book "Options, futures and other derivatives" from John Hull, 8th edition /// Chapter 14 "The Black-Scholes-Merton model" (page 299) /// </summary> /// <param name="stock">Underlying asset</param> /// <param name="prices">Daily prices of the underlying asset for the last year</param> /// <param name="optionChain">Current options chain existing in the market</param> /// <param name="riskFreeRate"> /// When the Black-Scholes formula is used in practice the interest rate r (risk free rate) /// is set equal to the zero-coupon risk-free interest rate for a maturity T /// </param> public static void CalculateTheoricalValue(PriceList prices, OptionsChain optionChain, RiskFreeRates riskFreeRates, double lastPrice) { double volatility = CalculateVolatility(prices); CalculateTheoricalValue(prices, optionChain, riskFreeRates, lastPrice, volatility); }