2023年3月7日 星期二

Chatgpt: give me a program about how to get latest option price and prediction using quantlib

 Here's an example Python program that uses QuantLib to calculate the Black-Scholes option price and predicts the option price based on the latest market data:



import QuantLib as ql import yfinance as yf # User inputs symbol = input("Enter stock symbol: ") expiry = input("Enter option expiry date (YYYY-MM-DD): ") option_type = input("Enter option type (call/put): ") strike_price = float(input("Enter option strike price: ")) risk_free_rate = float(input("Enter risk-free interest rate: ")) # Get option chain for the stock symbol option_chain = yf.Ticker(symbol).option_chain(expiry) # Get the latest market data underlying_price = option_chain.calls['lastPrice'][0] implied_volatility = option_chain.calls['impliedVolatility'][0] # Set up QuantLib objects calendar = ql.UnitedStates() day_count = ql.Actual365Fixed() calculation_date = ql.Date.todaysDate() ql.Settings.instance().evaluationDate = calculation_date option_expiry_date = ql.Date(expiry.split('-')[2], int(expiry.split('-')[1]), int(expiry.split('-')[0])) option_type_q = ql.Option.Call if option_type == 'call' else ql.Option.Put option = ql.EuropeanOption(ql.PlainVanillaPayoff(option_type_q, strike_price), ql.EuropeanExercise(option_expiry_date)) # Set up Black-Scholes process underlying_q = ql.QuoteHandle(ql.SimpleQuote(underlying_price)) risk_free_rate_q = ql.YieldTermStructureHandle(ql.FlatForward(calculation_date, risk_free_rate, day_count)) dividend_yield_q = ql.YieldTermStructureHandle(ql.FlatForward(calculation_date, 0.0, day_count)) volatility_q = ql.BlackVolTermStructureHandle(ql.BlackConstantVol(calculation_date, calendar, implied_volatility, day_count)) process = ql.BlackScholesProcess(underlying_q, dividend_yield_q, risk_free_rate_q, volatility_q) # Calculate Black-Scholes option price option.setPricingEngine(ql.AnalyticEuropeanEngine(process)) bs_price = option.NPV() # Display Black-Scholes option price print("The Black-Scholes option price is {:.2f}".format(bs_price)) # Predict option price based on latest market data delta_t = day_count.yearFraction(calculation_date, option_expiry_date) forward_price = underlying_price * np.exp(risk_free_rate * delta_t) predicted_price = bs_price + (implied_volatility / np.sqrt(delta_t)) * ((forward_price / underlying_price) - 1) print("The predicted option price based on the latest market data is {:.2f}".format(predicted_price))



In this program, we use the QuantLib library to calculate the Black-Scholes option price using the latest market data retrieved from Yahoo Finance. We start by asking the user to input the stock symbol, option expiry date, option type, strike price, and risk-free interest rate. We then retrieve the option chain for the stock symbol and extract the latest market data (underlying price and implied volatility).

We set up the necessary QuantLib objects, including the calculation date, option expiry date, option type, and Black-Scholes process. We then calculate the Black-Scholes option price using the setPricingEngine method.

Finally, we predict the option price based on the latest market data by calculating the forward price and using it to adjust the Black-Scholes price accordingly. We display the Black-Scholes option price and the predicted option price to the user. Note that you'll need to install the `

沒有留言:

張貼留言