Python Option delta calculation

 from py_vollib.black_scholes.implied_volatility import implied_volatility

from py_vollib.black_scholes.greeks.analytical import delta

# for buy trade itm means 55400,55300....call
# for sell trade itm means 55400, 55500...put
def getstrikes_put(ltp, step , strikestep):
result = {}
result[int(ltp)] = None

for i in range(step):
result[int(ltp + strikestep * (i + 1))] = None
return result

def getstrikes_call(ltp, step , strikestep):
result = {}
result[int(ltp)] = None
for i in range(step):
result[int(ltp - strikestep * (i + 1))] = None

return result

def fetchcorrectstrike(strikelist):
target_value = 0.6
closest_key = None
min_difference = float('inf')

for key, value in strikelist.items():
if value > target_value and value - target_value < min_difference:
min_difference = value - target_value
closest_key = key

return closest_key
def convert_date_to_short_format(date_string):
# Parse the date string into a datetime object
date_obj = datetime.strptime(date_string, "%Y-%m-%d")
# Format the datetime object into the desired short format
short_format = date_obj.strftime("%y%b").upper() # Convert to uppercase
return short_format
def convert_julian_date(julian_date):
input_format = "%y%m%d"
parsed_date = datetime.strptime(str(julian_date), input_format)
desired_time = "15:30:00"
formatted_date_with_time = parsed_date.replace(hour=15, minute=30, second=0)

return formatted_date_with_time


def get_delta(strikeltp,underlyingprice,strike,timeexpiery,riskfreeinterest,flag):
# flag me call 'c' ya put 'p'
from py_vollib.black_scholes.greeks.analytical import delta
iv= implied_volatility(price=strikeltp,S=underlyingprice,K=strike,t=timeexpiery,r=riskfreeinterest,flag=flag)
value = delta(flag,underlyingprice,strike,timeexpiery,riskfreeinterest,iv)
print("delta",value)
return value

def option_delta_calculation(symbol,expiery,strike,optiontype,underlyingprice,MODE):
optionsymbol = f"NSE:{symbol}{expiery}{strike}{optiontype}"
if symbol == "SENSEX":
optionsymbol = f"BSE:{symbol}{expiery}{strike}{optiontype}"

optionltp= FyresIntegration.get_ltp(optionsymbol)
print("expiery: ",expiery)
if MODE=="WEEKLY":
distanceexp=convert_julian_date(expiery)
if MODE=="MONTHLY":
distanceexp=expiery
print("distanceexp: ",distanceexp)
t= (distanceexp-datetime.now())/timedelta(days=1)/365
print("t: ",t)
if optiontype=="CE":
fg="c"
else :
fg = "p"
print("optionltp: ",optionltp)
print("underlyingprice: ", underlyingprice)
print("strike: ", strike)
value=get_delta(strikeltp=optionltp, underlyingprice=underlyingprice, strike=strike, timeexpiery=t,flag=fg ,riskfreeinterest=0.1)
return value







Function calling


strikelist = getstrikes_call(ltp=round_to_nearest(number=currentBar_close, nearest=params['strikestep']),
step=params['StrikeNumber'],
strikestep=params['strikestep'])
for strike in strikelist:
delta = float(
option_delta_calculation(symbol=symbol, expiery=params['TradeExpiery'], strike=strike,
optiontype="CE",
underlyingprice=currentBar_close, MODE=params["USEEXPIERY"]))
strikelist[strike] = delta
final = get_max_delta_strike(strikelist)






Comments

Popular posts from this blog

MQL5 : Add time to current time in mins

MQL5: Closed order detail