OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

calculating properties of items in a class

  • Thread starter Thread starter user25674747
  • Start date Start date
U

user25674747

Guest
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:

Code:
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.")

Code:
#import time data
from datetime import datetime
t = datetime.now()
print(t) # Example output: 2024-06-22 14:31:59.331225

Code:
# creating a class for options (Object) 
class Option:
    def __init__(self, type, strike, expiry):
        self.type = type
        self.strike = strike
        self.expiry = expiry

Code:
# Creating an option
Option1 = Option('Call', 100, datetime(2024, 11, 15, 0, 0))
print(Option1.type)
print(Option1.strike)
print(Option1.expiry)

Code:
#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}")

Code:
#importing the normal cumulative distribution function
from scipy.stats import norm 
import numpy as np

Code:
# 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?
<p>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:</p>
<pre><code>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.")
</code></pre>
<pre><code>#import time data
from datetime import datetime
t = datetime.now()
print(t) # Example output: 2024-06-22 14:31:59.331225
</code></pre>
<pre><code># creating a class for options (Object)
class Option:
def __init__(self, type, strike, expiry):
self.type = type
self.strike = strike
self.expiry = expiry
</code></pre>
<pre><code># Creating an option
Option1 = Option('Call', 100, datetime(2024, 11, 15, 0, 0))
print(Option1.type)
print(Option1.strike)
print(Option1.expiry)
</code></pre>
<pre><code>#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}")
</code></pre>
<pre><code>#importing the normal cumulative distribution function
from scipy.stats import norm
import numpy as np
</code></pre>
<pre><code># calculating the option price using Black scholes
for i in range(len(Option)):
if Option.type == 'Call':
Option.price = (S*norm.cdf(d1) - K*norm.cdf(d2)*np.exp(-r*T))
print(f"The Black Scholes Stock option price is: {Option_.price}")
else:
Option.price = (K*norm.cdf(-d2)*np.exp(-r*T) - S*norm.cdf(-d1))
print(f"The Black Scholes Stock option price is: {Option_.price}")
# calculating the options delta
if Option.type == 'Call':
Option.delta = np.exp(-D-(T))*norm.cdf(d1)
else:
Option.delta = -np.exp(-D-(T))*norm.cdf(-d1)
print(Option.delta)
</code></pre>
<p>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?</p>
 

Latest posts

M
Replies
0
Views
1
Mohit Pant
M
Top