import datetime from quantlib.time.api import Date, Period, Months, Schedule, UnitedStates from analytics.utils import third_wednesday from analytics.option import BlackSwaption, BlackSwaptionVolSurface from analytics.index import Index from analytics.index_data import get_index_quotes from quantlib.time.api import pydate_from_qldate def get_trades(df, d): """ returns a Portfolio of options""" expiry = pydate_from_qldate(third_wednesday(d + Period(1, Months))) d = pydate_from_qldate(d) series, spread = df.loc[d] strike = round((spread * 1.2) / 2.5) * 2.5 index = Index.from_name("IG", series, "5yr", value_date=d) index.spread = spread index.reset_pv() opt = BlackSwaption(index, expiry, strike, direction="Short") opt.notional = 100_000_000 try: vol_surface = BlackSwaptionVolSurface("IG", series, value_date=d) except ValueError as e: print(e) return [] surface_id = vol_surface.list(option_type="payer")[-1] opt.sigma = float(vol_surface.vol(expiry, 1.2, surface_id)) opt.reset_pv() return [opt] sched = Schedule.from_rule(Date(1, 7, 2014), Date(1, 4, 2018), Period(1, Months), UnitedStates()) df = get_index_quotes("IG", tenor=["5yr"], from_date=datetime.date(2014, 7, 1)) df = (df.reset_index(['index', 'version', 'tenor'], drop=True). reset_index(['series']). groupby(['date']).nth(-1))[['series', 'closespread']] l = [] for d in sched: l.extend(get_trades(df, d))