aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/calibrate_swaption.py64
1 files changed, 37 insertions, 27 deletions
diff --git a/python/calibrate_swaption.py b/python/calibrate_swaption.py
index 41210a26..bd3a17d7 100644
--- a/python/calibrate_swaption.py
+++ b/python/calibrate_swaption.py
@@ -1,23 +1,20 @@
import pandas as pd
+import argparse
from analytics import Index, Swaption
-import pdb
from db import dbconn
from joblib import Parallel, delayed
from pickle import loads, dumps
-serenitasdb = dbconn('serenitasdb')
-
-data = pd.read_sql("SELECT * from swaption_ref_quotes JOIN swaption_quotes " \
- "USING (quotedate, index, series, expiry) WHERE index=%s and series=%s " \
- "ORDER BY quotedate",
- "postgresql://serenitas_user@debian/serenitasdb",
- params = ('IG', 27), parse_dates = ['quotedate', 'expiry'])
-ig27 = Index.from_name("ig", 27, "5yr")
-sigma = {}
-sql_str = "INSERT INTO swaption_calib VALUES({}) ON CONFLICT DO NOTHING".format(",".join(["%s"] * 9))
+serenitasdb = dbconn('serenitasdb')
+def get_data(index, series):
+ return pd.read_sql("SELECT * from swaption_ref_quotes JOIN swaption_quotes " \
+ "USING (quotedate, index, series, expiry) WHERE index=%s and series=%s " \
+ "ORDER BY quotedate",
+ "postgresql://serenitas_user@debian/serenitasdb",
+ params = (index, series), parse_dates = ['quotedate', 'expiry'])
-def calib(d, option, expiry):
+def calib(d, option, expiry, index, series):
option.strike = d['strike']
option.ref = d['ref']
r = []
@@ -27,22 +24,35 @@ def calib(d, option, expiry):
option.option_type = 'payer' if option_type == 'pay' else 'receiver'
try:
setattr(option, pv_type, mid)
- except ValueError:
+ except ValueError as e:
r.append(None)
- print(d['ref'], d['strike'], mid, option.intrinsic_value)
+ print(e)
else:
r.append(option.sigma)
- return [d['quotedate'], "IG", 27, expiry, d['strike']] + r
+ return [d['quotedate'], index, series, expiry, d['strike']] + r
+
+def calibrate(index_type, series):
+ sql_str = ("INSERT INTO swaption_calib VALUES({}) ON CONFLICT DO NOTHING".
+ format(",".join(["%s"] * 9)))
+ data = get_data(index_type, series)
+ index = Index.from_name(index_type, series, "5yr")
+
+ for k, v in data.groupby([data['quotedate'].dt.date, 'expiry']):
+ trade_date, expiry = k
+ index.trade_date = trade_date
+ option = Swaption(index, expiry.date(), 100,
+ strike_is_price = index_type == "HY")
+ r = Parallel(n_jobs=4)(
+ delayed(calib)(d, option, expiry.date(), index_type, series) for d in
+ v[['ref', 'quotedate', 'strike', 'pay_bid', 'pay_offer', 'rec_bid', 'rec_offer']].
+ to_dict(orient = 'records'))
+ with serenitasdb.cursor() as c:
+ c.executemany(sql_str, r)
+ serenitasdb.commit()
-for k, v in data.groupby([data['quotedate'].dt.date, 'expiry']):
- trade_date, expiry = k
- print(trade_date, expiry.date())
- ig27.trade_date = trade_date
- option = Swaption(ig27, expiry.date(), 70)
- r = Parallel(n_jobs=4)(delayed(calib)(d, option, expiry.date()) for d in
- v[['ref', 'quotedate', 'strike', 'pay_bid', 'pay_offer',
- 'rec_bid', 'rec_offer']].
- to_dict(orient = 'records'))
- with serenitasdb.cursor() as c:
- c.executemany(sql_str, r)
- serenitasdb.commit()
+if __name__ == "__main__":
+ parser = argparse.ArgumentParser()
+ parser.add_argument('--index', required=True, type = lambda s: s.upper())
+ parser.add_argument('--series', required=True, type=int)
+ args = parser.parse_args()
+ calibrate(args.index, args.series)