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
                    }
                }
            }
        }
예제 #2
0
        public bool TryGetOptionsChain(string ticker, Exchange?exchange, out OptionsChain optionChain, out string errorMessage)
        {
            DateTime from = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
            DateTime to   = new DateTime(DateTime.Now.Year, DateTime.Now.Month, GetLastDay(DateTime.Now.Month)).AddYears(3);
            bool     ok   = NasdaqApiCaller.GetOptionChain(ticker, from, to, out string jsonResponse, out errorMessage);

            optionChain = new OptionsChain();
            NasdaqResponse nasdaqResponse = JsonConvert.DeserializeObject <NasdaqResponse>(jsonResponse);

            foreach (var optionRow in nasdaqResponse.Data.OptionChainList.Rows)
            {
                if (optionRow.Call != null)
                {
                    var    call = optionRow.Call;
                    Option o    = new Option(OptionClass.Call, call.Symbol)
                    {
                        LastPrice      = call.Last.ToNullableDecimal(),
                        Strike         = call.Strike.ToDouble(-1),
                        ExpirationDate = call.ExpiryDate.ToDateTime(),
                    };

                    o.LastPrice    = call.Last.ToNullableDecimal();
                    o.Change       = call.Change.ToNullableDouble();
                    o.Bid          = call.Bid.ToNullableDouble();
                    o.Ask          = call.Ask.ToNullableDouble();
                    o.Volume       = call.Volume.ToNullableInt();
                    o.OpenInterest = call.Openinterest.ToNullableInt();

                    optionChain.Add(o);
                }

                if (optionRow.Put != null)
                {
                    var    put = optionRow.Put;
                    Option o   = new Option(OptionClass.Put, put.Symbol)
                    {
                        LastPrice      = put.Last.ToNullableDecimal(),
                        Strike         = put.Strike.ToDouble(-1),
                        ExpirationDate = put.ExpiryDate.ToDateTime(),
                    };
                    o.LastPrice    = put.Last.ToNullableDecimal();
                    o.Change       = put.Change.ToNullableDouble();
                    o.Bid          = put.Bid.ToNullableDouble();
                    o.Ask          = put.Ask.ToNullableDouble();
                    o.Volume       = put.Volume.ToNullableInt();
                    o.OpenInterest = put.Openinterest.ToNullableInt();

                    optionChain.Add(o);
                }
            }

            errorMessage = "ok";
            return(true);
        }
        public bool TryGetOptionsChainWithTheoricalValue(string ticker, Exchange?exchange, double lastPrice, PriceList historicalPrices, out OptionsChain optionsChain, out string errorMessage)
        {
            if (optionChainDataSource.TryGetOptionsChain(ticker, exchange, out optionsChain, out errorMessage) == false)
            {
                return(false);
            }

            //TODO: get this data 1 per day and store it in a cache
            if (TryGetRiskFreeRates(out RiskFreeRates riskFreeRate, out errorMessage) == false)
            {
                return(false);
            }

            OptionsCalculator.CalculateTheoricalValue(historicalPrices, optionsChain, riskFreeRate, lastPrice);

            return(true);
        }
        public bool TryGetOptionsChainWithTheoricalValue(string ticker, Exchange?exchange, double lastPrice, out OptionsChain optionsChain, out string errorMessage)
        {
            DateTime?from = DateTime.Now.AddYears(-1).AddDays(-1);
            DateTime?to   = DateTime.Now;

            if (TryGetHistoricalPrices(ticker, exchange, from, to, PriceInterval.Daily, out PriceList prices, out errorMessage) == false)
            {
                optionsChain = null;
                return(false);
            }
            return(TryGetOptionsChainWithTheoricalValue(ticker, exchange, lastPrice, prices, out optionsChain, out errorMessage));
        }
 public bool TryGetOptionsChain(string ticker, Exchange?exchange, out OptionsChain optionChain, out string errorMessage)
 {
     return(optionChainDataSource.TryGetOptionsChain(ticker, exchange, out optionChain, out errorMessage));
 }
예제 #6
0
 public bool TryGetOptionsChainWithTheoricalValue(string ticker, Exchange?exchange, double lastPrice, out OptionsChain optionsChain, out string errorMessage)
 {
     throw new NotImplementedException();
 }
예제 #7
0
 public bool TryGetOptionsChain(string ticker, Exchange?exchange, out OptionsChain optionChain, out string errorMessage)
 {
     throw new NotImplementedException();
 }
        /// <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);
        }