Python strike selection based on premium
def finc_closest_Ce(price_dict, target_premium):
closest_ce_symbol = None
closest_ce_premium = float('-inf')
for price in price_dict:
ce_premium = price_dict[price]["CEPREMIUM"]
if ce_premium < target_premium and ce_premium > closest_ce_premium:
closest_ce_premium = ce_premium
closest_ce_symbol = price_dict[price]["CESymbol"]
return closest_ce_symbol
def generatepricedictce_buy(price, step, distance,BaseSymbol,formatted_date):
start_price = price
end_price = price + (step * distance)
price_list = [start_price + i * distance for i in range((end_price - start_price) // distance + 1)]
print("price_list: ",price_list)
price_dict = {price: {"CESymbol": f"{BaseSymbol}{formatted_date}{price}CE", "CEPREMIUM": "PRE"} for price in price_list}
for price in price_dict:
ce_symbol = price_dict[price]["CESymbol"]
params = {'Symbol': BaseSymbol, 'CESymbol': ce_symbol,"Strike":price}
price_dict[price]["CEPREMIUM"] = AngelIntegration.get_ltp(segment="NFO", symbol=params['CESymbol'],
token=get_token(params['CESymbol']))
return price_dict
def genertaepricedictpe_sell(price, step, distance,BaseSymbol,formatted_date):
start_price = price
end_price = price + (step * distance)
price_list = [start_price + i * distance for i in range((end_price - start_price) // distance + 1)]
price_dict = {price: {"PESymbol": f"{BaseSymbol}{formatted_date}{price}PE", "PEPREMIUM": "PRE"} for price in
price_list}
for price in price_dict:
pe_symbol = price_dict[price]["PESymbol"]
params = {'Symbol': BaseSymbol, 'PESymbol': pe_symbol,"Strike":price}
price_dict[price]["PEPREMIUM"] = AngelIntegration.get_ltp(segment="NFO", symbol=params['PESymbol'],
token=get_token(params['PESymbol']))
return price_dict
def genertaepricedictpe_buy(price, step, distance,BaseSymbol,formatted_date):
start_price = price - (step * distance)
end_price = price
price_list = [start_price + i * distance for i in range((end_price - start_price) // distance + 1)]
price_dict = {price: {"PESymbol": f"{BaseSymbol}{formatted_date}{price}PE", "PEPREMIUM": "PRE"} for price in
price_list}
for price in price_dict:
pe_symbol = price_dict[price]["PESymbol"]
params = {'Symbol': BaseSymbol, 'PESymbol': pe_symbol,"Strike":price}
price_dict[price]["PEPREMIUM"] = AngelIntegration.get_ltp(segment="NFO", symbol=params['PESymbol'],
token=get_token(params['PESymbol']))
return price_dict
def genertaepricedictpe_sell(price, step, distance,BaseSymbol,formatted_date):
start_price = price
end_price = price + (step * distance)
price_list = [start_price + i * distance for i in range((end_price - start_price) // distance + 1)]
price_dict = {price: {"PESymbol": f"{BaseSymbol}{formatted_date}{price}PE", "PEPREMIUM": "PRE"} for price in
price_list}
for price in price_dict:
pe_symbol = price_dict[price]["PESymbol"]
params = {'Symbol': BaseSymbol, 'PESymbol': pe_symbol,"Strike":price}
price_dict[price]["PEPREMIUM"] = AngelIntegration.get_ltp(segment="NFO", symbol=params['PESymbol'],
token=get_token(params['PESymbol']))
return price_dict
def genertaepricedictpe_buy(price, step, distance,BaseSymbol,formatted_date):
start_price = price - (step * distance)
end_price = price
price_list = [start_price + i * distance for i in range((end_price - start_price) // distance + 1)]
price_dict = {price: {"PESymbol": f"{BaseSymbol}{formatted_date}{price}PE", "PEPREMIUM": "PRE"} for price in
price_list}
for price in price_dict:
pe_symbol = price_dict[price]["PESymbol"]
params = {'Symbol': BaseSymbol, 'PESymbol': pe_symbol,"Strike":price}
price_dict[price]["PEPREMIUM"] = AngelIntegration.get_ltp(segment="NFO", symbol=params['PESymbol'],
token=get_token(params['PESymbol']))
return price_dict
Function Calling
SpotLtp=AngelIntegration.get_ltp(segment="NFO", symbol=params['Symbol'],
token=get_token(params['Symbol']))
print(SpotLtp)
params['BuyrangeValue'] =SpotLtp*params['BuyPercentage']*0.01
params['BuyrangeValue']= SpotLtp+params['BuyrangeValue']
params['SellrangeVale']=SpotLtp*params['SellPercentage']*0.01
params['SellrangeVale']=SpotLtp-params['SellrangeVale']
print("BuyrangeValue: ",params['BuyrangeValue'])
print("SellrangeVale: ", params['SellrangeVale'])
OrderLog = f"{timestamp} BuyrangeValue : {params['BuyrangeValue']} @ SellrangeVale: {params['SellrangeVale']}"
print(OrderLog)
write_to_order_logs(OrderLog)
date_obj = datetime.strptime(params["TradeExpiery"], "%d-%b-%y")
formatted_date = date_obj.strftime("%d%b%y").upper()
targetbuystrike= round_to_nearest(number=params['BuyrangeValue'], nearest=params['StepSize'])
targetsellstrike = round_to_nearest(number=params['SellrangeVale'], nearest=params['StepSize'])
CallStrikeListBuy=generatepricedictce_buy(price=targetbuystrike, step=params['StepNumberBUYCE'],
distance=params['StepSize'],BaseSymbol=params['BaseSymbol'],
formatted_date=formatted_date)
CallStrikeListSell=generatepricedictce_sell(price=targetbuystrike, step=params['StepNumberSELLCE'],
distance=params['StepSize'],BaseSymbol=params['BaseSymbol'],
formatted_date=formatted_date)
PutStrikeListBuy=genertaepricedictpe_buy(price=targetsellstrike, step=params['StepNumberBUYPE'],
distance=params['StepSize'],BaseSymbol=params['BaseSymbol'],formatted_date=formatted_date)
PutStrikeListSell=genertaepricedictpe_sell(price=targetsellstrike, step=params['StepNumberSELLPE'],
distance=params['StepSize'],BaseSymbol=params['BaseSymbol'],formatted_date=formatted_date)
params['Initial_Buy_Ce']= finc_closest_Ce(CallStrikeListBuy, target_premium=params['BuyPremium_CE'])
params['Initial_Buy_Pe']= finc_closest_Pe( PutStrikeListBuy , target_premium=params['BuyPremium_PE'])
params['Initial_Sell_Ce']= finc_closest_Ce(CallStrikeListSell, target_premium=params['SellPremium_CE'])
params['Initial_Sell_Pe']= finc_closest_Pe(PutStrikeListSell, target_premium=params['SellPremium_PE'])
Comments
Post a Comment