diff options
Diffstat (limited to 'python/yieldcurve.py')
| -rw-r--r-- | python/yieldcurve.py | 65 |
1 files changed, 41 insertions, 24 deletions
diff --git a/python/yieldcurve.py b/python/yieldcurve.py index 2e6b7d61..97b811e9 100644 --- a/python/yieldcurve.py +++ b/python/yieldcurve.py @@ -4,10 +4,16 @@ import requests, zipfile from io import BytesIO import xml.etree.ElementTree as ET import datetime -from quantlib.time.api import Calendar, Period, Days, Schedule, today, Actual360, calendar_from_name -from quantlib.time import imm -from quantlib.util.converter import qldate_to_pydate, pydate_to_qldate -from quantlib.market.market import libor_market, next_imm_date +from quantlib.settings import Settings +from quantlib.time.api import (WeekendsOnly, Date, Period, Days, Schedule, Annual, + Semiannual, today, Actual360, Months, ModifiedFollowing, + Thirty360, Actual365Fixed, calendar_from_name) +from quantlib.currency.api import USDCurrency, EURCurrency +from quantlib.indexes.ibor_index import IborIndex +from quantlib.termstructures.yields.api import ( + PiecewiseYieldCurve, DepositRateHelper, SwapRateHelper) +from quantlib.time.date import pydate_from_qldate + import numpy as np import matplotlib.pyplot as plt from quantlib.quotes import SimpleQuote @@ -43,30 +49,41 @@ def get_futures_data(date = datetime.date.today()): quotes = [float(line.split(",")[1]) for line in fh] return quotes -def YC(date = datetime.date.today(), currency="USD", MarkitData=None, futures = None): +def rate_helpers(currency="USD", MarkitData=None): + settings = Settings() if not MarkitData: - MarkitData = getMarkitIRData(date, currency) - # if not futures: - # futures = get_futures_data(date) + MarkitData = getMarkitIRData(pydate_from_qldate(settings.evaluation_date-1), currency) + if MarkitData['effectiveasof'] != pydate_from_qldate(settings.evaluation_date): + raise RuntimeError("Yield curve effective date: {0} doesn't " \ + "match the evaluation date: {1}".format( + MarkitData['effectiveasof'], + qldate_to_pydate(settings.evaluation_date))) + calendar = WeekendsOnly() if currency == "USD": - m = libor_market('USD(NY)', calendar='WO') + isda_ibor = IborIndex("IsdaIbor", Period(3, Months), 2, USDCurrency(), calendar, + ModifiedFollowing, False, Actual360()) + fix_freq = Semiannual elif currency == "EUR": - m = libor_market('EUR:1Y', calendar='WO') - m.fixed_leg_convention = 'ModifiedFollowing' - # m.settle_date is not available until we set_quotes, so we compute it again - # need a better way to do this - # quotes = [('ED', i+1, v) for i, v in enumerate(futures)] - # if next_imm_date(date, 9) == settle_date + Period('2Yr'): - # quotes.pop(7) - quotes = [('DEP', t, SimpleQuote(v)) for t, v in MarkitData['deposits'].items()] - quotes += [('SWAP', t, SimpleQuote(v)) for t, v in MarkitData['swaps'].items()] - m.set_quotes(date, quotes) - ts = m.bootstrap_term_structure() - return ts + isda_ibor = IborIndex("IsdaIbor", Period(6, Months), 2, EURCurrency(), calendar, + ModifiedFollowing, False, Actual360()) + fix_freq = Annual + deps = [DepositRateHelper(q, Period(t), 2, calendar, ModifiedFollowing, False, Actual360()) + for t, q in MarkitData['deposits'].items()] + swaps = [SwapRateHelper.from_tenor(q, Period(t), calendar, fix_freq, ModifiedFollowing, + Thirty360(), isda_ibor) for t, q in MarkitData['swaps'].items()] + return deps + swaps + +def YC(currency="USD", MarkitData=None): + settings = Settings() + helpers = rate_helpers(currency, MarkitData) + curve = PiecewiseYieldCurve("discount", "loglinear", settings.evaluation_date, + helpers, Actual365Fixed()) + return curve if __name__=="__main__": - date = datetime.date(2014, 4, 29) - ts = YC(date) + #evaluation_date = Date(29, 4, 2014) + Settings.instance().evaluation_date = today() + ts = YC() cal = calendar_from_name('USA') p1 = Period('1Mo') p2 = Period('2Mo') @@ -74,7 +91,7 @@ if __name__=="__main__": p6 = Period('6Mo') p12 = Period('12Mo') sched = Schedule(ts.reference_date, ts.reference_date+Period('5Yr'), Period('3Mo'), cal) - days = [qldate_to_pydate(d) for d in sched] + days = [pydate_from_qldate(d) for d in sched] f3 = [ts.forward_rate(d, d+p3, Actual360(), 0).rate for d in sched] f6 = [ts.forward_rate(d, d+p6, Actual360(), 0).rate for d in sched] f2 = [ts.forward_rate(d, d+p2, Actual360(), 0).rate for d in sched] |
