pyql
pyql copied to clipboard
make_zero_coupon.py not working out of the box!
to make it work had to perform the following changes which I don't know if they are fully valid:
diff --git a/examples/make_zero_coupon.py b/examples/make_zero_coupon.py
index 6004ffb..2d72510 100644
--- a/examples/make_zero_coupon.py
+++ b/examples/make_zero_coupon.py
@@ -15,14 +15,13 @@ from __future__ import print_function
from quantlib.settings import Settings
from quantlib.termstructures.yields.rate_helpers import DepositRateHelper, SwapRateHelper
-from quantlib.termstructures.yields.piecewise_yield_curve import \
- term_structure_factory
+from quantlib.termstructures.yields.piecewise_yield_curve import PiecewiseYieldCurve
from quantlib.time.api import Date, TARGET, Period, Months, Years, Days
from quantlib.time.api import (ModifiedFollowing, Unadjusted, Actual360,
Thirty360, ActualActual)
from quantlib.time.api import September, ISDA, today
-from quantlib.currency import USDCurrency
+from quantlib.currency.api import USDCurrency
from quantlib.quotes import SimpleQuote
from quantlib.indexes.libor import Libor
@@ -80,7 +79,7 @@ def get_term_structure(df_libor, dtObs):
for m, period, label in depositData:
tenor = Period(m, Months)
rate = df_libor.get_value(dtObs, label)
- helper = DepositRateHelper(float(rate/100.0), tenor,
+ helper = DepositRateHelper(SimpleQuote(rate/100.0), tenor,
settlement_days,
calendar, ModifiedFollowing,
end_of_month,
@@ -92,27 +91,31 @@ def get_term_structure(df_libor, dtObs):
liborIndex = Libor('USD Libor', Period(6, Months),
settlement_days,
- USDCurrency(), calendar,
- ModifiedFollowing,
- endOfMonth, Actual360())
+ USDCurrency(),
+ calendar,
+ Actual360(),
+ #ModifiedFollowing,
+ #endOfMonth,
+ )
spread = SimpleQuote(0)
fwdStart = Period(0, Days)
for m, period, label in swapData:
rate = df_libor.get_value(dtObs, label)
- helper = SwapRateHelper(SimpleQuote(rate/100.0),
+ helper = SwapRateHelper.from_tenor(SimpleQuote(rate/100.0),
Period(m, Years),
- calendar, Annual,
- Unadjusted, Thirty360(),
- liborIndex, spread, fwdStart)
+ calendar, Annual,
+ Unadjusted, Thirty360(),
+ liborIndex, spread, fwdStart)
+ #helper = SwapRateHelper.from_index(SimpleQuote(rate/100.0),liborIndex)
rate_helpers.append(helper)
ts_day_counter = ActualActual(ISDA)
tolerance = 1.0e-15
- ts = term_structure_factory('discount', 'loglinear',
+ ts = PiecewiseYieldCurve('discount', 'loglinear',
settlement_date, rate_helpers,
ts_day_counter, tolerance)
@@ -120,7 +123,7 @@ def get_term_structure(df_libor, dtObs):
def zero_curve(ts, dtObs):
calendar = TARGET()
- days = range(10, 365*20, 30)
+ days = range(10, 365*17, 30)
dtMat = [calendar.advance(dateToDate(dtObs), d, Days) for d in days]
df = np.array([ts.discount(dt) for dt in dtMat])
dtMat = [QLDateTodate(dt) for dt in dtMat]
@@ -131,7 +134,7 @@ def zero_curve(ts, dtObs):
if __name__ == '__main__':
- df_libor = pandas.load('data/df_libor.pkl')
+ df_libor = pandas.read_pickle('data/df_libor.pkl')
dtObs = df_libor.index
fig = plt.figure()
@@ -150,7 +153,8 @@ if __name__ == '__main__':
ax.set_xlim(dtMin, dtMax)
ax.set_ylim(0.0, 0.1)
- dtI = dtObs[range(0, len(dtObs)-1, 100)]
+ #dtI = dtObs[range(0, len(dtObs)-1, 100)]
+ dtI = dtObs[range(0, len(dtObs)-1, 99)]
for dt in dtI:
ts = get_term_structure(df_libor, dt)
(dtMat, zc) = zero_curve(ts, dt)
@hmarrao thanks! If you could submit the patch in a pull request, I'll be happy to merge it! The examples are not always kept in sync with the code. Thanks for the cleanup.
I'm testing all available examples and notebooks. I will "pull request" all fixes that I can manage to perform!
It now works in the pull request "stovol", but ....
$ ipython examples/scripts/make_zero_coupon.py works fine, but
$ python examples/scripts/make_zero_coupon.py raises an error about quantlib.currency.api module not found! Not sure why, this api is used all over the place.