How to specify the trading class for FuturesOption? #115
Unanswered
RedheadedCoding
asked this question in
Q&A
Replies: 1 comment
-
@RedheadedCoding once you get the First part is to get the data from IBKR, from ib_insync import util, OptionChain, Index
from typing import List
contract = Index("NG", exchange="NYMEX", currency="USD")
ib.qualifyContracts(contract)
# get option chains
ibkr_option_chains: List[OptionChain] = ib.reqSecDefOptParams(contract.symbol, 'CME', 'IND', contract.conId)
# helper method in ib_async to convert List[OptionChain] to a pd.DataFrame
ibkr_exchange_option_chains: pd.DataFrame = util.df(ibkr_option_chains) ![]() Second step is to use a mix of import re
def lower_camel_case_to_snake_case(s):
return re.sub(r"(?<!^)(?=[A-Z])", "_", s).lower()
def option_chain_df(df, underlying_symbol):
"""
Transforms a 'compressed exchange option chain' to a pd.DataFrame
with rows for each strike and expiration
Args:
df (pd.DataFrame): df, the result of util.df(ibkr_option_chains)
underlying_symbol (str): underlying symbol. For example, NG
Returns:
pd.DataFrame: df with basic option chain columns:
"""
df = df.copy()
df = df.explode("expirations")
df = df.explode("strikes")
df = df.rename(columns={"expirations": "expiry", "strikes": "strike"})
df["expiry"] = pd.to_datetime(df["expiry"], format="%Y%m%d", errors="coerce")
df = df.rename(columns=lower_camel_case_to_snake_case)
df.strike = df.strike.astype("float64")
df = df.sort_values(by=["expiry", "strike"])
df = df.reset_index(drop=True)
df.underlying_con_id = df.underlying_con_id.astype(int)
# remove the EC{symbol} contracts
df = df[~df.trading_class.str.startswith("EC")]
# set underlying_symbol
df["underlying_symbol"] = underlying_symbol
return df
option_chain_df = option_chain_df(ibkr_exchange_option_chains, "NG")
option_chain_df
lne_option_chain = option_chain_df.query("trading_class == 'LNE'")
lne_option_chain ![]() I've done a bit of work to pull down future options and match them to the specific underlying futures. This notebook demos that approach with |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
For FutureOption how does one specify a trading class such as for natural gas (NG) options there are LNE and ON plus other trading classes?
Beta Was this translation helpful? Give feedback.
All reactions