private void LoadData() { string riskFreeRatesFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols3\cdi_20161124_154509.csv"; this.RiskFreeRates = this.GetRiskFreeRates(riskFreeRatesFile); string underlyingMarketDataFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols3\underlying_20161124_154509.csv"; this.UnderlyingOrderBook = this.GetUnderlyingMarketData(underlyingMarketDataFile); string optionsMarketDataFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols3\options_20161124_154509.csv"; this.OptionsMarketData = this.GetOptionsMarketData(optionsMarketDataFile); this.VolSurface = this.GetVolSurface(this.RiskFreeRates, this.UnderlyingOrderBook, this.OptionsMarketData); }
static void Main(string[] args) { Test test = new Test(); test.SimpleImpliedVolatilityTest(); double riskFreeRate = 0.132343187; string ammOptionsFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols\AMMOptions2016-04-04_56771925.csv"; string underlyingsMarketDataFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols\bbas3_underlying_book.csv"; string optionsMarketDataFile = ""; double daysToMaturity = 0; double volGuess = 0.00; string outputFile = ""; VolSurface volSurface = new VolSurface(); if (true) { optionsMarketDataFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols\bbas3_april_book.csv"; daysToMaturity = 11; volGuess = 0.70; outputFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols2\bbas3_april_vols.csv"; //test.CalculateImpliedVolatilityToFile(ammOptionsFile, underlyingsMarketDataFile, optionsMarketDataFile, outputFile, daysToMaturity, riskFreeRate, volGuess); test.CalculateImpliedVolatility(ammOptionsFile, underlyingsMarketDataFile, optionsMarketDataFile, daysToMaturity, riskFreeRate, ref volSurface); } if (true) { optionsMarketDataFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols\bbas3_may_book.csv"; daysToMaturity = 30; volGuess = 0.70; outputFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols2\bbas3_may_vols.csv"; //test.CalculateImpliedVolatilityToFile(ammOptionsFile, underlyingsMarketDataFile, optionsMarketDataFile, outputFile, daysToMaturity, riskFreeRate, volGuess); test.CalculateImpliedVolatility(ammOptionsFile, underlyingsMarketDataFile, optionsMarketDataFile, daysToMaturity, riskFreeRate, ref volSurface); } if (true) { optionsMarketDataFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols\bbas3_june_book.csv"; daysToMaturity = 54; volGuess = 0.70; outputFile = @"C:\Users\Rubens\Google Drive\Mestrado\Projeto_Pesquisa\vols2\bbas3_june_vols.csv"; //test.CalculateImpliedVolatilityToFile(ammOptionsFile, underlyingsMarketDataFile, optionsMarketDataFile, outputFile, daysToMaturity, riskFreeRate, volGuess); test.CalculateImpliedVolatility(ammOptionsFile, underlyingsMarketDataFile, optionsMarketDataFile, daysToMaturity, riskFreeRate, ref volSurface); } return; }
private VolSurface GetVolSurface(SortedDictionary <DateTime, RiskFreeRate> riskFreeRates, OrderBook underlyingOrderBook, List <OptionMarketData> optionsMarketData) { double businessDaysInTheYear = 252; VolSurface volSurface = new VolSurface(); double spot = underlyingOrderBook.GetPriceMid(); foreach (OptionMarketData option in optionsMarketData) { RiskFreeRate riskFreeRate = null; riskFreeRates.TryGetValue(option.OptionInfo.ExpiryDate, out riskFreeRate); double timeToMaturity = riskFreeRate.BusinessDays / businessDaysInTheYear; double bid = (option.OrderBook.Bid != null) ? option.OrderBook.Bid.Value : 0.00; double bidSize = (option.OrderBook.BidSize != null) ? option.OrderBook.BidSize.Value : 0.00;; double ask = (option.OrderBook.Ask != null) ? option.OrderBook.Ask.Value : 0.00;; double askSize = (option.OrderBook.AskSize != null) ? option.OrderBook.AskSize.Value : 0.00;; BlackScholes bs = new BlackScholes(); double bidImpliedVolatility = 0.00; double askImpliedVolatility = 0.00; if (bid > 0.00 && bidSize > 0.00) { bidImpliedVolatility = bs.GetImpliedVolatility(option.OptionInfo.Type, bid, spot, option.OptionInfo.Strike, riskFreeRate.Rate, timeToMaturity); } if (ask > 0.00 && askSize > 0.00) { askImpliedVolatility = bs.GetImpliedVolatility(option.OptionInfo.Type, ask, spot, option.OptionInfo.Strike, riskFreeRate.Rate, timeToMaturity); } if (!(bidImpliedVolatility > 0.0000)) { bidImpliedVolatility = 0.00; } if (!(askImpliedVolatility > 0.0000)) { askImpliedVolatility = 0.00; } VolQuote volQuote = new VolQuote(bidSize, askSize, bidImpliedVolatility, askImpliedVolatility); volSurface.Update(option.OptionInfo.ExpiryDate, option.OptionInfo.Strike, option.OptionInfo.Type, volQuote); } return(volSurface); }
public override VolSurface RequestVolSurface(Product prod, int tenorType, int surfaceType, DateTime marketDate, DateTime fromDate, DateTime toDate) { VolSurface myVolSurface = new VolSurface(prod, SOURCE_TYPE.RWE_DS, tenorType, surfaceType, marketDate); object[,] vol_raw = RWE_API.MarketDataLoader_Wrapper.GetVolGrid("iVol_NG_" + prod.Name + "_P_" + (tenorType == TENOR_TYPE.DAY? "D" : "M"), marketDate, fromDate, toDate); // convert object[,] to SortedDict DateTime delDate; SortedDictionary<DateTime, SortedDictionary<double, double>> vol = new SortedDictionary<DateTime, SortedDictionary<double, double>>(); for (int i = 1; i < vol_raw.GetLength(0); i++) { delDate = (DateTime)vol_raw[i,0]; vol.Add(delDate, new SortedDictionary<double,double>()); for (int j = 1; j < vol_raw.GetLength(1); j++) { vol[delDate].Add((double)vol_raw[0, j], (double)vol_raw[i, j]); } } myVolSurface.SetSurface(vol); return myVolSurface; }
public void CalculateImpliedVolatility(string ammOptionsFile, string underlyingsMarketDataFile, string optionsMarketDataFile, double daysToMaturity, double riskFreeRate, ref VolSurface volSurface) { double workingDaysInTheYear = 252; double timeToMaturity = daysToMaturity / workingDaysInTheYear; Dictionary <string, OptionInfo> allAvailableOptions = this.ListAllAvailableOptions(ammOptionsFile); Dictionary <string, OrderBook> underlyingsMarketData = this.GetMarketData(underlyingsMarketDataFile); Dictionary <string, OrderBook> optionsMarketData = this.GetMarketData(optionsMarketDataFile); foreach (var pair in optionsMarketData) { OrderBook optionOrderBook = pair.Value; OptionInfo option = null; if (allAvailableOptions.TryGetValue(optionOrderBook.Name, out option)) { OrderBook underlyingOrderBook = null; if (underlyingsMarketData.TryGetValue(option.Underlying, out underlyingOrderBook)) { double spot = underlyingOrderBook.GetPriceMid(); double bid = (optionOrderBook.Bid != null) ? optionOrderBook.Bid.Value : 0.00; double bidSize = (optionOrderBook.BidSize != null) ? optionOrderBook.BidSize.Value : 0.00;; double ask = (optionOrderBook.Ask != null) ? optionOrderBook.Ask.Value : 0.00;; double askSize = (optionOrderBook.AskSize != null) ? optionOrderBook.AskSize.Value : 0.00;; BlackScholes bs = new BlackScholes(); double bidImpliedVolatility = 0.00; double askImpliedVolatility = 0.00; if (bid > 0.00 && bidSize > 0.00) { bidImpliedVolatility = bs.GetImpliedVolatility(option.Type, bid, spot, option.Strike, riskFreeRate, timeToMaturity); } if (ask > 0.00 && askSize > 0.00) { askImpliedVolatility = bs.GetImpliedVolatility(option.Type, ask, spot, option.Strike, riskFreeRate, timeToMaturity); } if (!(bidImpliedVolatility > 0.0000)) { bidImpliedVolatility = 0.00; } if (!(askImpliedVolatility > 0.0000)) { askImpliedVolatility = 0.00; } VolQuote volQuote = new VolQuote(bidSize, askSize, bidImpliedVolatility, askImpliedVolatility); volSurface.Update(option.ExpiryDate, option.Strike, option.Type, volQuote); } } } }