Python Type Hint Finance Use Case: Implied Volatility and Greeks of Index ‘At The Money’ Options: Part 2

Jonathan Legrand
LSEG Developer Community
3 min readNov 8, 2023

--

Read the full article, and part 2 right now, on the Developer Portal.

In part 1: we showed how one can (i) automatically find the Option (of choice) closest to At The Money (ATM) and (ii) calculate its Implied Volatility & Greeks.

In part 2: We will implement a functionality allowing us to apply all we did in Part 1 to expired options. You'll see, it's not as simple as it seems. We will then put it all in one function using Type Hints. This, in itself, will also be rather new and exciting!

Importing libraries such as `refinitiv.data` to collect financial data.

Now we have to use the aforementioned logic to construct expired Option RICs.

Above we looked at the specific usecase with EUREX, let’s generalise it:

We are now going to look into using PEP 3107 (and PEP 484) (and some decorators). In line with PEP, I will also now use PEP8 naming conventions.

See the notebook in GitHub fot he entire Class.

Let’s have an example case with HSI.

Let’s have an example case with SPX.

The most granular Historical Options’ price data kept are daily time-series. This daily data is captured by the above `OptionRIC().Construct_RIC()` function. Some Options’ historical price data is most “wholesome” (in this case, “has the least amount of `NaN`s” — Not a Number) under the field name `TRDPRC_1`, some under `SETTLE`. While our preference — ceteris paribus (all else equal) — is `TRDPRC_1`, more “wholesome” data-sets are still preferable, so the “fullest prices” in `OptionRIC().Construct_RIC()` picks the series with fewest `NaN`s.

All data from the RDP endpoints are in GMT. We will keep the index for GMT, so we don’t loose this information, but we will also add a Local Timezone, as this is what interests us. To do so, let’s use MultyIndex.

See the notebook in GitHub fot he entire Class.

session: rd.session.Session
# session = rd.open_session(
# name="desktop.workspace4", # name="desktop.workspace4", "platform.rdph"
# config_name="C:/Example.DataLibrary.Python-main/Configuration/refinitiv-data.config.json")
try:
session = rd.open_session(
name="desktop.workspace4", # name="desktop.workspace4", "platform.rdph"
config_name="C:/Example.DataLibrary.Python-main/Configuration/refinitiv-data.config.json")
except:
try:
app_key: str; rd_login:str; rd_password: str
# # You could specify your session with:
session = rd.session.platform.Definition(
app_key=app_key,
grant=rd.session.platform.GrantPassword(
username=rd_login, password=rd_password),
).get_session()
session.open()
except:
session = rd.open_session()
print("We're on Desktop Session")
rd.session.set_default(session) # Letting the RD API know that this is the session we're on
# session.open()

print("Index Implied Volatility aNd Greeks Instrument Pricing Analytics Calculation (IndxImpVolatNGreeksIPACalc) (IIVNGIPAC) expiered option test:")
IIVNGIPAC_exp_optn_test1 = IndxImpVolatNGreeksIPACalc(
time_of_calc_datetime=datetime.strptime("2023-04-01", '%Y-%m-%d'),
after=15,
index_underlying=".STOXX50E",
call_put='Call')
IIVNGIPAC_exp_optn_test2: IndxImpVolatNGreeksIPACalc = IIVNGIPAC_exp_optn_test1.search_index_opt_ATM(
debug=False,
call_or_put='Put',
search_fields=["ExchangeCode", "UnderlyingQuoteName"],
include_weekly_opts=False,
top_nu_srch_results=10)
IIVNGIPAC_exp_optn_test3: IndxImpVolatNGreeksIPACalc = IIVNGIPAC_exp_optn_test2.IPA_calc(
debug=False)

request_fields = [
"DeltaPercent", "GammaPercent", "RhoPercent",
"ThetaPercent", "VegaPercent"]
for i_str in ["ErrorMessage", "MarketValueInDealCcy", "RiskFreeRatePercent",
"UnderlyingPrice", "Volatility"][::-1]:
request_fields.insert(0, i_str)


IIVNGIPAC_exp_optn_test3.graph(mrkt_exhng_open_time='9:00', mrkt_exhng_close_time='17:00').overlay().fig
IIVNGIPAC_exp_optn_test3.graph(mrkt_exhng_open_time='9:00', mrkt_exhng_close_time='17:00').stack3().fig

--

--