This is the code i have, what would be the way to see the cashflows of this specific bond i created
1 Answer
As Dimitri said, you can use the cashflows inspectors:
for cf in bond.cashflows():
print(cf.date().ISO(), cf.amount())
2019-07-30 1.4999999999999902
2020-01-30 1.4999999999999902
2020-07-30 1.4999999999999902
(...)
2029-01-30 1.4999999999999902
2029-07-30 1.4999999999999902
2030-01-30 1.4999999999999902
2030-01-30 100.0
This way your cashflows will have a date and an amount, and you will see both the interest and redemptions.
If you need a bit more detail, you can use the ql.as_coupon
, ql.as_fixed_rate_coupon
or ql.as_floating_rate_coupon
methods to extract additional fields:
import pandas as pd
fields = [
'accrualDays', 'accrualEndDate', 'accrualPeriod', 'accrualStartDate',
'amount', 'date', 'dayCounter', 'interestRate', 'nominal', 'rate'
]
data = []
for cf in list(map(ql.as_fixed_rate_coupon, bond.cashflows()))[:-1]:
data.append({fld: eval(f"cf.{fld}()") for fld in fields})
pd.DataFrame(data)
Here I made a list of the map to avoid the last coupon because the redemption cashflows does not have these additional fields and will through an error.
(EDIT after comment)
If you wanted the discount factor and discouted cashflow, you could do something like this:
yc = ql.FlatForward(2, ql.TARGET(), 0.05, ql.Actual360())
data = []
for cf in list(map(ql.as_fixed_rate_coupon, bond.cashflows()))[:-1]:
row = {fld: eval(f"cf.{fld}()") for fld in fields}
row['df'] = yc.discount(row['date'])
row['pv'] = row['df'] * row['amount']
data.append(row)
pd.DataFrame(data).head()
-
$\begingroup$ Sorry for asking this question later. @David Duarte, which field names would extract the 'discount factors' and the 'discounted cashflows' in this instance? $\endgroup$– cccCommented Aug 5, 2022 at 22:27
-
$\begingroup$ Those fields are not in the bond object but you could just get the discount factor for each payment date from the yield curve and multiply that by the cashflow amount to get the pv $\endgroup$ Commented Aug 6, 2022 at 7:16
-
$\begingroup$ thanks for clarifying that. Do you perhaps have an example that can work for the snippet Python code above? $\endgroup$– cccCommented Aug 6, 2022 at 11:57
-