Present Value
The present value script includes functions for computing the present value of a series of cash flows (including a single cash flow) given a term structure curve.
import QuantLib as ql
'''
cash_flows = [('2026-10-10', 100.0)]
'''
def present_value(cash_flows, discount_curve, today=None):
# 1. Set the evaluation date
#split = today.split('-')
#today = ql.Date(int(split[2]), int(split[1]), int(split[0]))
#ql.Settings.instance().evaluationDate = today
cash_flow_dates = [ql.Date(int(x.split('-')[2]),int(x.split('-')[1]), int(x.split('-')[0]))
for x,_ in cash_flows
]
# 3. Create a list of cash flows
'''cash_flow_dates = [
ql.Date(26, 9, 2026), # 3 months out
ql.Date(26, 6, 2027), # 1 year out
ql.Date(26, 6, 2028) # 2 years out
]'''
cash_flow_amounts = [x for _,x in cash_flows]
# Instantiate SimpleCashFlow objects
cash_flows = [
ql.SimpleCashFlow(amount, date)
for amount, date in zip(cash_flow_amounts, cash_flow_dates)
]
# 4. Calculate Net Present Value (Discounting)
# The boolean 'False' indicates we do not include past/expired cash flows
npv = ql.CashFlows.npv(cash_flows, discount_curve, False)
return npv