Re: Duration and Yield of a Portfolio of Bonds

Posted by Luigi Ballabio on
URL: http://quantlib.414.s1.nabble.com/Duration-and-Yield-of-a-Portfolio-of-Bonds-tp6219p6220.html

On Fri, 2011-03-04 at 14:27 +0100, Lluis Pujol Bajador wrote:
> Under Python I need to obtain the overall yield and casflow duration
> of a portfolio of bonds (plain vanilla either Fixed or FRN).
>
> I am already able to obtain the single duration and yield for each
> bond but in order to correctly obtain the real yield (and not a
> weigthed average yield) of the portfolio I should create a Leg object
> with all the cashflows of the portfolio. As far as I know the
> cashflows for a Leg object need to be sorted and thus I don't know how
> to easily proceed .

As for adding them,

all = []
for b in bonds:
    all += bonds.cashflows()

Then, write a function to compare dates and let Python sort them:

def cmp_cf(c1,c2):
    return cmp(c1.date(),c2.date())

all.sort(cmp = cmp_cf)

Luigi

P.S. Warning: I haven't tested the above, so there might be errors.  But
the idea is the correct one.          


--

Greenspun's Tenth Rule of Programming:
Any sufficiently complicated C or Fortran program contains an
ad-hoc, informally-specified bug-ridden slow implementation of
half of Common Lisp.



------------------------------------------------------------------------------
What You Don't Know About Data Connectivity CAN Hurt You
This paper provides an overview of data connectivity, details
its effect on application quality, and explores various alternative
solutions. http://p.sf.net/sfu/progress-d2d
_______________________________________________
QuantLib-users mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/quantlib-users