mirror of
https://github.com/OpenBB-finance/OpenBB.git
synced 2026-10-29 05:21:48 +00:00
SDK - Small enhancement to openbb.stocks.options.chains, when source is Tradier. (#3724)
* Improves Getting All Options From Tradier * make expiration flag work * black
This commit is contained in:
@@ -142,21 +142,78 @@ def get_full_option_chain(symbol: str) -> pd.DataFrame:
|
||||
"""
|
||||
|
||||
expirations = option_expirations(symbol)
|
||||
options_dfs: pd.DataFrame = []
|
||||
|
||||
options = pd.DataFrame()
|
||||
for expiry in expirations:
|
||||
options_dfs.append(get_option_chains(symbol, expiry))
|
||||
|
||||
for date in expirations:
|
||||
temp = get_option_chains(symbol, date)
|
||||
calls = temp[temp.option_type == "call"][option_cols].rename(
|
||||
columns=option_col_map
|
||||
)
|
||||
puts = temp[temp.option_type == "put"][option_cols].rename(
|
||||
columns=option_col_map
|
||||
)
|
||||
temp = pd.merge(calls, puts, how="outer", on="strike", suffixes=("_c", "_p"))
|
||||
temp["expiration"] = date
|
||||
options_df = pd.concat(options_dfs)
|
||||
|
||||
options = pd.concat([options, temp], axis=0)
|
||||
options_df.set_index(keys="symbol", inplace=True)
|
||||
|
||||
option_df_index = pd.Series(options_df.index).str.extractall(
|
||||
r"^(?P<Ticker>\D*)(?P<Expiration>\d*)(?P<Type>\D*)(?P<Strike>\d*)"
|
||||
)
|
||||
option_df_index.reset_index(inplace=True)
|
||||
option_df_index = pd.DataFrame(
|
||||
option_df_index, columns=["Ticker", "Expiration", "Strike", "Type"]
|
||||
)
|
||||
option_df_index["Strike"] = options_df["strike"].values
|
||||
option_df_index["Type"] = options_df["option_type"].values
|
||||
option_df_index["Expiration"] = pd.DatetimeIndex(
|
||||
data=option_df_index["Expiration"], yearfirst=True
|
||||
).strftime("%Y-%m-%d")
|
||||
option_df_index["Type"] = pd.DataFrame(option_df_index["Type"]).replace(
|
||||
to_replace=["put", "call"], value=["Put", "Call"]
|
||||
)
|
||||
options_df_columns = list(options_df.columns)
|
||||
option_df_index.set_index(
|
||||
keys=["Ticker", "Expiration", "Strike", "Type"], inplace=True
|
||||
)
|
||||
options_df = pd.DataFrame(
|
||||
data=options_df.values, index=option_df_index.index, columns=options_df_columns
|
||||
)
|
||||
|
||||
options_df.rename(
|
||||
columns={
|
||||
"bid": "Bid",
|
||||
"ask": "Ask",
|
||||
"strike": "Strike",
|
||||
"bidsize": "Bid Size",
|
||||
"asksize": "Ask Size",
|
||||
"volume": "Volume",
|
||||
"open_interest": "OI",
|
||||
"delta": "Delta",
|
||||
"gamma": "Gamma",
|
||||
"theta": "Theta",
|
||||
"vega": "Vega",
|
||||
"ask_iv": "Ask IV",
|
||||
"bid_iv": "Bid IV",
|
||||
"mid_iv": "IV",
|
||||
},
|
||||
inplace=True,
|
||||
)
|
||||
|
||||
options_columns = [
|
||||
"Volume",
|
||||
"OI",
|
||||
"IV",
|
||||
"Delta",
|
||||
"Gamma",
|
||||
"Theta",
|
||||
"Vega",
|
||||
"Bid Size",
|
||||
"Bid",
|
||||
"Ask",
|
||||
"Ask Size",
|
||||
"Bid IV",
|
||||
"Ask IV",
|
||||
]
|
||||
|
||||
options = pd.DataFrame(options_df, columns=options_columns)
|
||||
options = options.reset_index()
|
||||
options.drop(labels=["Ticker"], inplace=True, axis=1)
|
||||
options.rename(columns={"Expiration": "expiration"}, inplace=True)
|
||||
|
||||
return options
|
||||
|
||||
|
||||
Reference in New Issue
Block a user