-1

I am continuing on with a bigger project and for this part I am trying to use wallstreet to pull the implied volatility of a certain call or put for options trading. I am able to get it to work individually for calls and puts, but not when I combine them to make it one action.

input variables like aapl 180 call 5 days till expiration -> use an if elif statement for the functions for calls and puts to get the respective IV -> return that IV as a variable for further math in a different section

here is an example of the latest that I have. Any help is appreciated I am newer to coding so if this is obvious apologies.

And for reference my problem is that when I run the call and put functions separately they give two different numbers, but like this regardless of a P or C input, i get the value for calls.

Thanks.

from wallstreet import Call, Put
import datetime
from datetime import date

ticker = "tsla"
K = 900
today = date.today()
expiry = today + datetime.timedelta(days=5)
y = expiry.year
d = expiry.day
m = expiry.month
CorP = "P"

if CorP == "c" or "C":
    call = Call(ticker, d=d, m=m, y=y, strike=K)
    sigma = call.implied_volatility()
elif CorP == "p" or "P":
    put = Put(ticker, d=d, m=m, y=y, strike=K)
    sigma = put.implied_volatility()

print(sigma)
NMK09
  • 1
  • 1

2 Answers2

0

I believe there's a logic error in your code,

replace the line

if CorP == "c" or "C":

with

if CorP == "c" or CroP == "C":

and same with elif part

replace

elif CorP == "p" or "P":

with

elif CorP == "p" or CorP == "P":
Rashid F.
  • 12
  • 3
0

You should use the lower() function to simplify your if statement

if CorP.lower() == "c": # Converts capital "C" to lower "c" and runs if statement
    call = Call(ticker, d=d, m=m, y=y, strike=K)
    sigma = call.implied_volatility()
elif CorP.lower() == "p": # Converts capital "P" to lower "p" and runs if statement
    put = Put(ticker, d=d, m=m, y=y, strike=K)
    sigma = put.implied_volatility()
Aaseer
  • 117
  • 10