diff options
Diffstat (limited to 'python/exploration')
| -rw-r--r-- | python/exploration/option_trades.py | 50 |
1 files changed, 15 insertions, 35 deletions
diff --git a/python/exploration/option_trades.py b/python/exploration/option_trades.py index f9685fbb..cbb1bf2f 100644 --- a/python/exploration/option_trades.py +++ b/python/exploration/option_trades.py @@ -44,47 +44,27 @@ def atm_vol_fun(v, moneyness=0.2, index=None): atm_val = v['fwdspread'].iat[0] otm_val = atm_val * (1 + moneyness) else: - if 'HY' in index.name: - index.price = v['ref'].iat[0] - atm_val = ForwardIndex(index, v.index.get_level_values('expiry')[0], ref_is_price=True).forward_spread - otm_val = atm_val * (1 + moneyness) + index.ref = v['ref'].iat[0] + atm_val = ForwardIndex(index, v.index.get_level_values('expiry')[0]).forward_spread + otm_val = atm_val * (1 + moneyness) + if index._quote_is_price: index.spread = atm_val atm_val = index.price index.spread = otm_val otm_val = index.price - else: - index.spread = v['ref'].iat[0] - atm_val = ForwardIndex(index, v.index.get_level_values('expiry')[0], ref_is_price=False).forward_spread - otm_val = atm_val * (1 + moneyness) return pd.Series(f(np.array([atm_val, otm_val])), index = ['atm_vol', 'otm_vol']) -def atm_vol(index, date, series = None, moneyness=0.2): - if series is None: - df = pd.read_sql_query('SELECT quotedate, series, expiry, strike, vol ' \ - 'FROM swaption_quotes ' \ - 'WHERE index = %s and quotedate >= %s', - serenitasdb, - index_col=['quotedate', 'expiry', 'series'], - params=(index.upper(), date), parse_dates=['quotedate']) - index_data = pd.read_sql_query( - 'SELECT quotedate, expiry, series, ref, fwdspread FROM swaption_ref_quotes ' \ - 'WHERE index= %s and quotedate >= %s', - serenitasdb, index_col=['quotedate', 'expiry', 'series'], - params=(index.upper(), date), parse_dates=['quotedate']) - else: - df = pd.read_sql_query('SELECT quotedate, series, expiry, strike, vol from swaption_quotes ' \ - 'WHERE index = %s and series = %s and quotedate >= %s', - serenitasdb, index_col=['quotedate', 'expiry', 'series'], - params = (index.upper(), series, date)) - index_data = pd.read_sql_query( - 'SELECT quotedate, series, expiry, ref, fwdspread from swaption_ref_quotes ' \ - 'WHERE index= %s and series = %s and quotedate >= %s', - serenitasdb, index_col = ['quotedate', 'expiry', 'series'], - params = (index.upper(), series, date)) - - df = df.join(index_data) - df = df.groupby(df.index).filter(lambda x: len(x) >= 2) - +def atm_vol(index, date, series=None, moneyness=0.2): + sql_str = "SELECT * from swaption_ref_quotes JOIN swaption_quotes " \ + "USING (quotedate, index, series, expiry) WHERE index=%s " \ + "and quotedate >=%s" + params = (index.upper(), date) + if series: + sql_str += ' AND series = %s' + params = params + (series,) + df = pd.read_sql_query(sql_str, serenitasdb, + index_col=['quotedate', 'expiry', 'series'], + params=params, parse_dates=['quotedate']) df1 = atm_vol_calc(df, index) return df1 |
