Ok. I have managed to install Quantlab and connect it to my python dev. How do I call an instrument or a list of instruments (i.e. a curve)?
Can anyone give a short example.
Ok. I have managed to install Quantlab and connect it to my python dev. How do I call an instrument or a list of instruments (i.e. a curve)?
Can anyone give a short example.
Hi,
sure, I have a simple demo example that shows how to:
Will try to attach the python example code below.
#!/usr/bin/env python
# coding: utf-8
# In[30]:
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import ql
#print(sys.version_info)
cn = "SEMTG="
s = list()
m = list()
d = ql.move_bus_days(ql.today(), -30)
qs = "Mid"
t = ql.curve(cn, d,qs, True, True).n_instruments()
r = range(0, t-1)
for n in r:
s.append(ql.curve(cn, d, qs, True, True).instruments()[n].quote())
m.append(ql.curve(cn, d, qs, True, True).instruments()[n].maturity())
fix, ax = plt.subplots()
ax.plot(m,s)
ax.set(xlabel='maturity', ylabel='Yield%',title='ML experiments')
ax.grid()
plt.show()
# In[24]:
names = list()
dur = list()
mat = list()
yld = list()
for n in range(0,t-1):
names.append(ql.curve(cn, d,qs, True, True).instruments()[n].name())
dur.append(round(ql.curve(cn, d,qs, True, True).instruments()[n].mac_dur(),1))
yld.append(ql.curve(cn,d,qs, True, True).instruments()[n].quote())
mat.append(ql.curve(cn, d,qs, True, True).instruments()[n].maturity())
print(np.array([names, yld, dur, mat]).transpose())
# In[10]:
print(ql.curve(cn, d).get_cashflows(20))
Adding to the above example.
A five parameter interest rate model referred to as Nelson-Siegel-Laguerre can be fitted to the yields using a L2 optimizer (Levenberg Marquardt).
(Note that this example needs the parameters and imports of the previous examples to work.)
r2 = ql.range_vector(1,30)
s2 = list()
d2 = ql.move_bus_days(ql.today(), -2)
for myt in r2:
s2.append(ql.fit_l2(ql.curve(cn,d,qs, True, True), ql.ns_laguerre(), ql.std_weights.WS_EQUAL)
.effective_rate_spot(myt)*100)
fix, ax = plt.subplots()
ax.plot(r2,s2)
ax.set(xlabel='time (years)', ylabel='Yield%',title='Quantlab to the people!')
ax.grid()
plt.show()
Result should look something like this when run in a Jupyter notebook:
That’s a nice example Robert! But it seems a bit excessive to recalibrate the curve again and again when plotting the graph. Might I suggest something like this instead:
fitted = ql.fit_l2(ql.curve(cn,d,qs, True, True), ql.ns_laguerre(), ql.std_weights.WS_EQUAL)
for myt in r2:
s2.append(fitted.effective_rate_spot(myt)*100)
Well spotted and of course you are right. I had an ambition to plot the history of the fit but then opted for one date only. Then one fit should be enough.