Example #1
0
        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);
        }
Example #2
0
        private SortedDictionary <DateTime, RiskFreeRate> GetRiskFreeRates(string filePath)
        {
            SortedDictionary <DateTime, RiskFreeRate> riskFreeRates = new SortedDictionary <DateTime, RiskFreeRate>();
            string line;

            System.IO.StreamReader file = new System.IO.StreamReader(filePath);
            while ((line = file.ReadLine()) != null)
            {
                string[] tokens = line.Split(';');

                DateTime maturity     = DateTime.ParseExact(tokens[0], "yyyyMMdd", null);
                double   days         = Double.Parse(tokens[1], CultureInfo.InvariantCulture);
                double   businessDays = Double.Parse(tokens[2], CultureInfo.InvariantCulture);
                double   rate         = Double.Parse(tokens[3], CultureInfo.InvariantCulture);

                RiskFreeRate riskFreeRate = new RiskFreeRate(maturity, days, businessDays, rate);
                riskFreeRates.Add(maturity, riskFreeRate);
            }

            file.Close();

            return(riskFreeRates);
        }
Example #3
0
        private SortedDictionary<DateTime, RiskFreeRate> GetRiskFreeRates(string filePath)
        {
            SortedDictionary<DateTime, RiskFreeRate> riskFreeRates = new SortedDictionary<DateTime, RiskFreeRate>();
            string line;

            System.IO.StreamReader file = new System.IO.StreamReader(filePath);
            while ((line = file.ReadLine()) != null)
            {
                string[] tokens = line.Split(';');

                DateTime maturity = DateTime.ParseExact(tokens[0], "yyyyMMdd", null);
                double days = Double.Parse(tokens[1], CultureInfo.InvariantCulture);
                double businessDays = Double.Parse(tokens[2], CultureInfo.InvariantCulture);
                double rate = Double.Parse(tokens[3], CultureInfo.InvariantCulture);

                RiskFreeRate riskFreeRate = new RiskFreeRate(maturity, days, businessDays, rate);
                riskFreeRates.Add(maturity, riskFreeRate);
            }

            file.Close();

            return riskFreeRates;
        }