diff options
Diffstat (limited to 'python/position.py')
| -rw-r--r-- | python/position.py | 28 |
1 files changed, 15 insertions, 13 deletions
diff --git a/python/position.py b/python/position.py index 51280765..057feddb 100644 --- a/python/position.py +++ b/python/position.py @@ -1,7 +1,7 @@ from quantlib.time.date import Friday, nth_weekday, pydate_from_qldate from serenitas.analytics.bbg_helpers import bbg_retry, retrieve_data from serenitas.analytics.dates import prev_business_day -from serenitas.utils.db2 import InfDateLoaderPandas +from serenitas.utils.db2 import InfDateLoaderPandas, NaNtoNone from itertools import product import datetime import numpy as np @@ -458,24 +458,26 @@ def update_hyg_option_quotes(conn, session, start_date: datetime.date = None): d = retrieve_data( session, securities, - fields=[ - "PX_LAST", - ], + fields=["PX_MID", "IVOL_MID"], start_date=start_date, ) + + def make_tuple(e, exp, s, t): + return e[0], exp, s, t, e[1], NaNtoNone(e[2]), e[1], NaNtoNone(e[2]) + for exp, s, t in product(expiries, strikes, ("P", "C")): - data = d[bbg_ticker(exp, t, s)] - if "PX_LAST" in data: - df = data["PX_LAST"] - else: + df = d[bbg_ticker(exp, t, s)] + if df.empty: continue - + if "IVOL_MID" not in df: + df["IVOL_MID"] = np.NaN with conn.cursor() as c: c.executemany( - "INSERT INTO hyg_option_quotes(date, expiry, strike, option_type, price) " - "VALUES (%s, %s, %s, %s, %s) " - "ON CONFLICT DO NOTHING", - [(d, exp, s, t, val) for d, val in df.items()], + "INSERT INTO hyg_option_quotes(date, expiry, strike, option_type, price, ivol) " + "VALUES (%s, %s, %s, %s, %s, %s) " + "ON CONFLICT (date, expiry, strike, option_type) " + "DO UPDATE SET price=%s, ivol=%s", + [make_tuple(e, exp, s, t) for e in df.itertuples(index=True)], ) |
