0
$\begingroup$

I am trying to learn QuantLib for Python but I must say I am banging my head against the documentation (I also bought Ballabio's python cookbook) - maybe it's me, but even finding the most basic concepts seems a struggle.

How can I do something as banal as discounting a set of irregular cashflows? The cashflows could be anything - the returns from a real estate investment, an asset-backed bond, etc.

The code below runs without any errors, but the result is... zero!

What I have done:

My questions:

  • What am I doing wrong?
  • Should calc_date be... what? The date of the first cashflow? I have tried that, I have tried days before, nothing.
  • does FlatForward(calc_date, rate, ql.ActualActual()) define a yield curve from calc_date to infinity? Or do we need to specify a start date and an end date for the curve?
  • is there any way to inspect / debug the SimpleCashFlow instances ( I use Spyder)? cfs[0] returns <QuantLib.QuantLib.SimpleCashFlow; proxy of <Swig Object of type 'ext::shared_ptr< SimpleCashFlow > *' at 0x0000021354440B40> > but how can I check what date and values are really there?

This is the code:

import QuantLib as ql
d1 = ql.Date(15,1,2011)
cfs = [ql.SimpleCashFlow(10, d1),
        ql.SimpleCashFlow(10, d1 + 365),
        ql.SimpleCashFlow(110, d1 + 365*2)]

calc_date = ql.Date(15,1,2011)
risk_free_rate = 0.05
discount_curve = ql.YieldTermStructureHandle(
                     ql.FlatForward(calc_date, risk_free_rate, ql.ActualActual()))

# WHY is this zero??
pv = ql.CashFlows.npv(cfs, discount_curve, True)
$\endgroup$
2
$\begingroup$

By default, QuantLib will set the evaluation data as the present date.

If you inspect your current date with:

ql.Settings.instance().evaluationDate

You will see that you are trying to get the npv of all past cashflows, which would be zero.

Try inserting this line after defining the d1.

ql.Settings.instance().setEvaluationDate(d1)
$\endgroup$
1
  • $\begingroup$ It seems we stepped on each other's toes :) $\endgroup$ – Luigi Ballabio Feb 6 at 22:53
2
$\begingroup$

QuantLib has a concept of evaluation date (for a number of reasons that I'm now glossing over). By default, it's today's date, which implies that all your cashflows are in the past and thus worthless. If you want to calculate the NPV as of the calc date, add:

ql.Settings.instance().evaluationDate = calc_date

before calculating. Your construction of the curve is correct.

(Also, if you have any suggestion on how to document this better, I'm all ears—I would have thought you'd come across this in the examples, but evidently I was wrong...)

To inspect the cashflows, you can use their interface: for instance, cf[0].date() and cf[0].amount() will work.

$\endgroup$
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.