diff options
| -rw-r--r-- | python/analytics/portfolio.py | 17 | ||||
| -rw-r--r-- | python/exchange.py | 6 | ||||
| -rw-r--r-- | python/mark_swaptions.py | 8 | ||||
| -rw-r--r-- | python/parse_gs_exchange.py | 20 |
4 files changed, 31 insertions, 20 deletions
diff --git a/python/analytics/portfolio.py b/python/analytics/portfolio.py index d8bc675f..583f5582 100644 --- a/python/analytics/portfolio.py +++ b/python/analytics/portfolio.py @@ -1,15 +1,21 @@ from .index import Index -from .option import BlackSwaption, BlackSwaptionVolSurface +from .option import BlackSwaption from warnings import warn import pandas as pd import numpy as np + def portf_repr(method): def f(*args): obj = args[0] - thousands = lambda x: "{:,.2f}".format(x) - percent = lambda x: "N/A" if np.isnan(x) else f"{100*x:.2f}%" - header = "Portfolio {}\n\n".format(obj.value_date) + thousands = "{:,.2f}".format + + def percent(x): + if np.isnan(x): + return "N/A" + else: + return f"{100*x:.2f}%" + header = f"Portfolio {obj.value_date}\n\n" kwargs = {'formatters': {'Notional': thousands, 'PV': thousands, 'Delta': percent, @@ -38,8 +44,7 @@ class Portfolio: self._keys.add((swaption.index.index_type, swaption.index.series, swaption.index.tenor)) self._value_date = value_dates.pop() if len(value_dates) >= 1: - warn("not all instruments have the same trade date, picking {}". - format(self._value_date)) + warn(f"not all instruments have the same trade date, picking {self._value_date}") def __iter__(self): for t in self.trades: diff --git a/python/exchange.py b/python/exchange.py index 23943c4f..d341cacb 100644 --- a/python/exchange.py +++ b/python/exchange.py @@ -2,6 +2,7 @@ from exchangelib import Credentials, Configuration, Account, DELEGATE from pathlib import Path import json + def get_account(email_address): with open(Path('.credentials') / (email_address + '.json')) as fh: creds = json.load(fh) @@ -10,8 +11,9 @@ def get_account(email_address): return Account(primary_smtp_address=email_address, config=config, autodiscover=False, access_type=DELEGATE) -def get_msgs(account=None, email_address='ghorel@lmcg.com', count=None, path=['GS', 'Swaptions'], - subject_filter=None): + +def get_msgs(account=None, email_address='ghorel@lmcg.com', count=None, + path=['GS', 'Swaptions'], subject_filter=None): if account is None: account = get_account(email_address) folder = account.inbox diff --git a/python/mark_swaptions.py b/python/mark_swaptions.py index c30355ca..e459184b 100644 --- a/python/mark_swaptions.py +++ b/python/mark_swaptions.py @@ -1,14 +1,14 @@ import argparse -import datetime import logging import os import pandas as pd from pandas.tseries.offsets import BDay -from analytics import Index, Swaption, Portfolio, BlackSwaption +from analytics import Portfolio, BlackSwaption from db import dbconn, DataError -logging.basicConfig(filename=os.path.join(os.getenv("LOG_DIR"), 'swaption_marks.log'), +logging.basicConfig(filename=os.path.join(os.getenv("LOG_DIR"), + 'swaption_marks.log'), level=logging.INFO, format='%(asctime)s %(message)s') @@ -128,7 +128,7 @@ def insert_swaption_portfolio(portf, conn): if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument('workdate', nargs='?', - type = lambda s: pd.datetime.strptime(s, "%Y-%m-%d").date()), + type=lambda s: pd.datetime.strptime(s, "%Y-%m-%d").date()), args = parser.parse_args() if args.workdate is None: workdate = (pd.Timestamp.today()-BDay()).date() diff --git a/python/parse_gs_exchange.py b/python/parse_gs_exchange.py index e80d8b1e..0d6de7c0 100644 --- a/python/parse_gs_exchange.py +++ b/python/parse_gs_exchange.py @@ -1,4 +1,3 @@ -from db import dbconn from exchange import get_msgs from pytz import timezone from parse_emails import write_todb @@ -7,9 +6,11 @@ import logging import pandas as pd import re + class ParseError(Exception): pass + def parse_email(email, fwd_index): m = re.search("(IG|HY)(\d{2}) 5y (?:.*)SWAPTION (?:UPDATE|CLOSES|CLOSE) - Ref\D+(.+)$", email.subject) @@ -18,7 +19,8 @@ def parse_email(email, fwd_index): series = int(series) if indextype == 'HY': refprice, refspread = map(float, - re.match("([\S]+)\s+\(([^)]+)\)", ref).groups()) + re.match(r"([\S]+)\s+\(([^)]+)\)", ref). + groups()) else: refspread = float(ref) else: @@ -51,25 +53,25 @@ def parse_email(email, fwd_index): r.append(vals) continue else: - if indextype=='HY': + if indextype == 'HY': cols = ['Strike', 'Sprd', 'Pay', 'DeltaPay', 'Rec', 'Vol', 'VolChg', 'VolBpd', 'Tail'] else: cols = ['Strike', 'Pay', 'DeltaPay', 'Rec', 'Vol', 'VolChg', 'VolBpd', 'Tail'] - df = pd.DataFrame.from_records(r, columns = cols) + df = pd.DataFrame.from_records(r, columns=cols) df[['PayBid', 'PayOffer']] = df.Pay.str.split('/', expand=True) df[['RecBid', 'RecOffer']] = df.Rec.str.split('/', expand=True) df.drop(['Pay', 'Rec'], axis=1, inplace=True) for col in df: - df[col] = pd.to_numeric(df[col], errors = 'coerce') + df[col] = pd.to_numeric(df[col], errors='coerce') df.set_index('Strike', inplace=True) d = {'quotedate': quotedate, 'expiry': date, 'index': indextype, 'series': series, - 'ref': refspread if indextype =="IG" else refprice} + 'ref': refspread if indextype == "IG" else refprice} if indextype == "IG": d['fwdspread'] = float(fwspread) else: @@ -80,7 +82,8 @@ def parse_email(email, fwd_index): flag = False r = [] continue - return (quotedate, indextype, series, pd.concat(masterdf, names=['expiry'])) + return quotedate, indextype, series, pd.concat(masterdf, names=['expiry']) + def clean_df(all_df): all_df = pd.concat(all_df, names=['quotedate', 'index', 'series'], sort=True) @@ -100,7 +103,8 @@ def clean_df(all_df): del all_df['Sprd'] return all_df -if __name__=="__main__": + +if __name__ == "__main__": fwd_index = [] swaption_quotes = {} for email in get_msgs(count=20): |
