aboutsummaryrefslogtreecommitdiffstats
path: root/python/index_data.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/index_data.py')
-rw-r--r--python/index_data.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/python/index_data.py b/python/index_data.py
index f04d7d4a..62cffeb2 100644
--- a/python/index_data.py
+++ b/python/index_data.py
@@ -1,5 +1,12 @@
from db import dbengine, dbconn
from dates import bond_cal
+import numpy as np
+
+from analytics.utils import roll_date, previous_twentieth
+from pandas.tseries.offsets import BDay
+from pyisda.curve import SpreadCurve, fill_curve
+from multiprocessing import Pool
+from yieldcurve import get_curve
import datetime
import pandas as pd
@@ -119,3 +126,46 @@ def index_returns(df=None, index=None, series=None, tenor=None, from_date=None,
groupby(level=['index', 'series', 'tenor'])['price_return'].
transform(add_accrued))
return df
+
+def get_singlenames_quotes(indexname, date):
+ conn = dbconn('serenitasdb')
+ with conn.cursor() as c:
+ c.execute("SELECT * FROM curve_quotes(%s, %s)", vars=(indexname, date))
+ return [r for r in c]
+
+def build_curve(r, today_date, yc, start_date, step_in_date, value_date, end_dates):
+ spread_curve = 1e-4 * np.array(r['spread_curve'][1:], dtype='float')
+ upfront_curve = 1e-2 * np.array(r['upfront_curve'][1:], dtype='float')
+ recovery_curve = np.array(r['recovery_curve'][1:], dtype='float')
+ try:
+ sc = SpreadCurve(today_date, yc, start_date, step_in_date, value_date,
+ end_dates, spread_curve, upfront_curve, recovery_curve,
+ ticker=r['cds_ticker'])
+ if len(sc) != end_dates.shape[0]:
+ sc = fill_curve(sc, end_dates)
+ except ValueError as e:
+ print(r[0], e)
+ return None
+ return sc
+
+def build_curves(quotes, args):
+ return [build_curve(q, *args) for q in quotes if q is not None]
+
+def build_curves_dist(quotes, args, workers=4):
+ ## about twice as fast as the non distributed version
+ ## non thread safe for some reason so need ProcessPool
+ with Pool(workers) as pool:
+ r = pool.starmap(build_curve, [(q, *args) for q in quotes], 30)
+ return r
+
+def get_singlenames_curves(index_type, series, trade_date):
+ end_dates = roll_date(trade_date, [1, 2, 3, 4, 5, 7, 10], nd_array=True)
+ sn_quotes = get_singlenames_quotes("{}{}".format(index_type.lower(), series),
+ trade_date.date())
+ jp_yc = get_curve(trade_date)
+ start_date = previous_twentieth(trade_date)
+ step_in_date = trade_date + datetime.timedelta(days=1)
+ value_date = pd.Timestamp(trade_date) + 3* BDay()
+ args = (trade_date, jp_yc, start_date, step_in_date, value_date, end_dates)
+ curves = build_curves_dist(sn_quotes, args)
+ return curves, args