2023年3月7日 星期二

Chatgpt: option price prediction program python

 Here's an example Python program that allows the user to input the necessary parameters to predict the price of a call or put option using the Black-Scholes model:




import numpy as np from scipy.stats import norm def black_scholes(S, K, T, r, sigma, option='call'): """ Calculates the Black-Scholes option price and Greeks. S: current stock price K: option strike price T: time to expiration (in years) r: risk-free interest rate sigma: implied volatility option: 'call' or 'put' option (default is 'call') """ d1 = (np.log(S/K) + (r + 0.5 * sigma**2) * T) / (sigma * np.sqrt(T)) d2 = d1 - sigma * np.sqrt(T) if option == 'call': price = S * norm.cdf(d1) - K * np.exp(-r * T) * norm.cdf(d2) delta = norm.cdf(d1) gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) vega = S * norm.pdf(d1) * np.sqrt(T) theta = (-S * norm.pdf(d1) * sigma) / (2 * np.sqrt(T)) - r * K * np.exp(-r * T) * norm.cdf(d2) rho = K * T * np.exp(-r * T) * norm.cdf(d2) else: price = K * np.exp(-r * T) * norm.cdf(-d2) - S * norm.cdf(-d1) delta = -norm.cdf(-d1) gamma = norm.pdf(d1) / (S * sigma * np.sqrt(T)) vega = S * norm.pdf(d1) * np.sqrt(T) theta = (-S * norm.pdf(d1) * sigma) / (2 * np.sqrt(T)) + r * K * np.exp(-r * T) * norm.cdf(-d2) rho = -K * T * np.exp(-r * T) * norm.cdf(-d2) return {'price': price, 'delta': delta, 'gamma': gamma, 'vega': vega, 'theta': theta, 'rho': rho} # User inputs S = float(input("Enter the current stock price: ")) K = float(input("Enter the option strike price: ")) T = float(input("Enter the time to expiration in years: ")) r = float(input("Enter the risk-free interest rate: ")) sigma = float(input("Enter the implied volatility: ")) option = input("Enter 'call' or 'put' option (default is 'call'): ") # Calculate option price and Greeks using Black-Scholes model result = black_scholes(S, K, T, r, sigma, option) # Display results print("Option price: {:.2f}".format(result['price'])) print("Delta: {:.2f}".format(result['delta'])) print("Gamma: {:.2f}".format(result['gamma'])) print("Vega: {:.2f}".format(result['vega'])) print("Theta: {:.2f}".format(result['theta'])) print("Rho: {:.2f}".format(result['rho']))


In this program, we define the black_scholes function that takes in the Black-Scholes inputs and calculates the theoretical price of a call or put option, as well as its Greeks (delta, gamma, vega, theta, rho). We then ask the user to input the necessary

沒有留言:

張貼留言