aboutsummaryrefslogtreecommitdiffstats
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/markit/__main__.py2
-rw-r--r--python/yieldcurve.py87
2 files changed, 56 insertions, 33 deletions
diff --git a/python/markit/__main__.py b/python/markit/__main__.py
index 765d1537..803e5c68 100644
--- a/python/markit/__main__.py
+++ b/python/markit/__main__.py
@@ -86,6 +86,6 @@ elif args.cds:
insert_tranche(engine, workdate)
elif args.rates:
- for curr in ["USD", "EUR"]:
+ for curr in ["USD", "EUR", "JPY"]:
downloadMarkitIRData(workdate, currency=curr)
logger.info("Downloaded {} rates".format(curr))
diff --git a/python/yieldcurve.py b/python/yieldcurve.py
index 997c7ad0..6edcad2c 100644
--- a/python/yieldcurve.py
+++ b/python/yieldcurve.py
@@ -1,4 +1,5 @@
from blist import sorteddict
+from collections import namedtuple
from contextlib import closing
from itertools import islice
import datetime
@@ -6,11 +7,11 @@ import lz4.block
import os
import pandas as pd
from quantlib.settings import Settings
-from quantlib.time.api import (WeekendsOnly, Date, Period, Days, Schedule, Annual,
+from quantlib.time.api import (WeekendsOnly, Japan,
+ Date, Period, Days, Schedule, Annual,
Semiannual, today, Actual360, Months, Years,
- ModifiedFollowing, Thirty360, Actual365Fixed,
- calendar_from_name)
-from quantlib.currency.api import USDCurrency, EURCurrency
+ ModifiedFollowing, Thirty360, Actual365Fixed)
+from quantlib.currency.api import USDCurrency, EURCurrency, JPYCurrency
from quantlib.indexes.ibor_index import IborIndex
from quantlib.termstructures.yields.api import (
PiecewiseYieldCurve, DepositRateHelper, SwapRateHelper, BootstrapTrait, Interpolator)
@@ -90,6 +91,36 @@ def get_futures_data(date=datetime.date.today()):
quotes = [float(line.split(",")[1]) for line in fh]
return quotes
+def get_curve_params(currency):
+ if currency == "USD":
+ currency = USDCurrency()
+ calendar = WeekendsOnly()
+ fixed_dc = Thirty360()
+ floating_dc = Actual360()
+ mm_dc = Actual360()
+ floating_freq = Period(3, Months)
+ fixed_freq = Semiannual
+ elif currency == "EUR":
+ currency = EURCurrency()
+ calendar = WeekendsOnly()
+ fixed_dc = Thirty360()
+ floating_dc = Actual360()
+ mm_dc = Actual360()
+ floating_freq = Period(6, Months)
+ fixed_freq = Annual
+ elif currency == "JPY":
+ currency = JPYCurrency()
+ calendar = Japan()
+ fixed_dc = Actual365Fixed()
+ floating_dc = Actual360()
+ mm_dc = Actual360()
+ floating_freq = Period(6, Months)
+ fixed_freq = Semiannual
+ CurveParams = namedtuple('CurveParam',
+ 'currency, calendar, fixed_dc, floating_dc, '
+ 'mm_dc, floating_freq, fixed_freq')
+ return CurveParams(currency, calendar, fixed_dc, floating_dc, mm_dc,
+ floating_freq, fixed_freq)
def rate_helpers(currency="USD", MarkitData=None, evaluation_date=None):
"""Utility function to build a list of RateHelpers
@@ -120,22 +151,18 @@ def rate_helpers(currency="USD", MarkitData=None, evaluation_date=None):
evaluation_date),
RuntimeWarning)
settings.evaluation_date = Date.from_datetime(MarkitData['effectiveasof'])
- calendar = WeekendsOnly()
- if currency == "USD":
- isda_ibor = IborIndex("IsdaIbor", Period(3, Months), 2, USDCurrency(),
- calendar, ModifiedFollowing, False, Actual360())
- fix_freq = Semiannual
- elif currency == "EUR":
- isda_ibor = IborIndex("IsdaIbor", Period(6, Months), 2, EURCurrency(),
- calendar, ModifiedFollowing, False, Actual360())
- fix_freq = Annual
+ params = get_curve_params(currency)
+ isda_ibor = IborIndex("IsdaIbor", params.floating_freq, 2,
+ params.currency, params.calendar, ModifiedFollowing,
+ False, params.floating_dc)
# we use SimpleQuotes, rather than just float to make it updateable
- deps = [DepositRateHelper(SimpleQuote(q), Period(t), 2, calendar,
- ModifiedFollowing, False, Actual360())
+ deps = [DepositRateHelper(SimpleQuote(q), Period(t), 2, params.calendar,
+ ModifiedFollowing, False, params.mm_dc)
for t, q in MarkitData['deposits']]
# this matches with bloomberg, but according to Markit, maturity should be unadjusted
- swaps = [SwapRateHelper.from_tenor(SimpleQuote(q), Period(t), calendar, fix_freq, ModifiedFollowing,
- Thirty360(), isda_ibor) for t, q in MarkitData['swaps']]
+ swaps = [SwapRateHelper.from_tenor(SimpleQuote(q), Period(t), params.calendar,
+ params.fixed_freq, ModifiedFollowing,
+ params.fixed_dc, isda_ibor) for t, q in MarkitData['swaps']]
return deps + swaps
@@ -218,25 +245,20 @@ def ql_to_jp(ql_yc):
def build_curves(currency="USD"):
settings = Settings()
- calendar = WeekendsOnly()
- if currency == "USD":
- isda_ibor = IborIndex("IsdaIbor", Period(3, Months), 2, USDCurrency(),
- calendar, ModifiedFollowing, False, Actual360())
- fix_freq = Semiannual
- elif currency == "EUR":
- isda_ibor = IborIndex("IsdaIbor", Period(6, Months), 2, EURCurrency(),
- calendar, ModifiedFollowing, False, Actual360())
- fix_freq = Annual
- rates = pd.read_sql_table('{}_rates'.format(currency.lower()),
+ params = get_curve_params(currency)
+ isda_ibor = IborIndex("IsdaIbor", params.floating_freq, 2,
+ params.currency, params.calendar, ModifiedFollowing,
+ False, params.floating_dc)
+ rates = pd.read_sql_table(f"{currency.lower()}_rates",
serenitas_engine,
index_col='effective_date')
quotes = [SimpleQuote() for c in rates.columns]
gen = zip(quotes, rates.columns)
- deps = [DepositRateHelper(q, Period(t), 2, calendar, ModifiedFollowing,
- False, Actual360()) for q, t in islice(gen, 6)]
- swaps = [SwapRateHelper.from_tenor(q, Period(t), calendar,
- fix_freq, ModifiedFollowing,
- Thirty360(), isda_ibor) for q, t in gen]
+ deps = [DepositRateHelper(q, Period(t), 2, params.calendar, ModifiedFollowing,
+ False, params.mm_dc) for q, t in islice(gen, 6)]
+ swaps = [SwapRateHelper.from_tenor(q, Period(t), params.calendar,
+ params.fixed_freq, ModifiedFollowing,
+ params.fixed_dc, isda_ibor) for q, t in gen]
sql_str = f"INSERT INTO {currency}_curves VALUES(%s, %s) ON CONFLICT DO NOTHING"
conn = serenitas_engine.raw_connection()
for effective_date, curve_data in rates.iterrows():
@@ -262,6 +284,7 @@ if __name__ == "__main__":
Settings.instance().evaluation_date = today()
import matplotlib.pyplot as plt
+ from quantlib.time.api import calendar_from_name
helpers = rate_helpers("USD")
ts = YC(helpers)
cal = calendar_from_name('USA')