i need to calculate some properties for items in a class ive created called options.
I want to calculate the properties a few differnet ones overall but i dont want to use a for loop as i will to calculate for a specific one at any one time. The calculation changes based on whether the option.type is either a call or a put. Can anyone help with this problem? Code below:
import subprocess
subprocess.run(["python", "-m", "venv", "trading_env"])
subprocess.run(["trading_env\Scripts\activate.bat"])
subprocess.run(["pip", "install", "numpy", "pandas", "matplotlib", "seaborn", "scipy"])
print("Python environment is complete wth all the required packages installed.")
#import time data
from datetime import datetime
t = datetime.now()
print(t) # Example output: 2024-06-22 14:31:59.331225
# creating a class for options (Object)
class Option:
def __init__(self, type, strike, expiry):
self.type = type
self.strike = strike
self.expiry = expiry
# Creating an option
Option1 = Option('Call', 100, datetime(2024, 11, 15, 0, 0))
print(Option1.type)
print(Option1.strike)
print(Option1.expiry)
#Inputs for BS Model
K = Option1.strike #K = Strike Price
S = 100 #S = Current Stock Price
T = (Option1.expiry - t).days / 365.25 #T = Time to Expiration
r = 0.05 #5% #r = Risk free interest rate
sigma = 0.2 #20% #sigma = Volatility
D = 0 #D = Dividend Yield
print(f"{K:.3f}, {S:.3f}, {T:.3f}, {r:.3f}, {sigma:.3f}, {D:.3f}")
#importing the normal cumulative distribution function
from scipy.stats import norm
import numpy as np
# calculating the option price using Black scholes
for i in range(len(Option)):
if Option[i].type == 'Call':
Option[i].price = (S*norm.cdf(d1) - K*norm.cdf(d2)*np.exp(-r*T))
print(f"The Black Scholes Stock option price is: {Option_[i].price}")
else:
Option[i].price = (K*norm.cdf(-d2)*np.exp(-r*T) - S*norm.cdf(-d1))
print(f"The Black Scholes Stock option price is: {Option_[i].price}")
# calculating the options delta
if Option[i].type == 'Call':
Option[i].delta = np.exp(-D-(T))*norm.cdf(d1)
else:
Option[i].delta = -np.exp(-D-(T))*norm.cdf(-d1)
print(Option[i].delta)
from this code when i run it, the option class and parameters are created and then the first option(option1 is defined) when calculating the option price however how do i referenece just the specific one I want without having to manually type out option1 or option2 everytime?
user25674747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.