Reputation: 47
for the input such as name AARTIIND
and ltp 1014
I want output as url
corresponding to lower bound of the ltp,
in this case
https: // kite.zerodha.com/chart/ext/tvc/NFO-OPT/AARTIIND21JUN1020CE/13192450
I am trying to write function to do this, I am stuck here, unable to think what should be the searching condition which will return correct url
def find_url(name,ltp,df):
return(df.iloc[df.index == name, ???)
name strike url
AARTIIND 1000 https: // kite.zerodha.com/chart/ext/tvc/NFO-OPT/AARTIIND21JUN1000CE/13190658
AARTIIND 1020 https: // kite.zerodha.com/chart/ext/tvc/NFO-OPT/AARTIIND21JUN1020CE/13192450
AARTIIND 1040 https: // kite.zerodha.com/chart/ext/tvc/NFO-OPT/AARTIIND21JUN1040CE/13201410
AARTIIND 1060 https: // kite.zerodha.com/chart/ext/tvc/NFO-OPT/AARTIIND21JUN1060CE/13202434
how can I get the required result?
Upvotes: 1
Views: 126
Reputation: 120391
To find the closest value, subtract ltp
to strike
and compute the absolute value. The lowest value is the closest.
Try this:
def find_url(name, ltp, df):
df1 = df.loc[df["name"] == name]
return df1.loc[df1["strike"].sub(ltp).abs().idxmin(), "url"]
url = find_url("AARTIIND", 1014, df)
>>> url
'https: // kite.zerodha.com/chart/ext/tvc/NFO-OPT/AARTIIND21JUN1020CE/13192450'
Edit:
>>> pd.DataFrame({'df["strike"]': df["strike"],
".sub(ltp)": df["strike"].sub(ltp),
".abs()": df["strike"].sub(ltp).abs()})
df["strike"] .sub(ltp) .abs()
0 1000 -14 14
1 1020 6 6 # idxmin() return 1 (6 == the lowest value)
2 1040 26 26
3 1060 46 46
Upvotes: 1