Re: FixedCouponBond pricing wrong?

Posted by Feng Ning on
URL: http://quantlib.414.s1.nabble.com/FixedCouponBond-pricing-wrong-tp3741p3746.html

Thanks to Luigi for his reply. But the problem persist.
1) I am not clear what it means:
Date today(30, October, 2004);

I assume it means we should set the evaluation date, so I use:
   
Settings::instance().setEvaluationDate(Date(31,October,2004));
It turns out: calculated clean price=95.92, dirty price = 98.42.

2) my computation differs from Luigi's bcoz I feel that the coupon is
semiannual value, so I use c=0.025/2, instead of c=0.025.
I verify this by printing out the cashflows as in the code.

The complete code is below:
// bond-tester.cpp
#include <iostream>
#include <ql/quantlib.hpp>
using namespace QuantLib;

int main() {
    Settings::instance().setEvaluationDate(Date(31,October,2004));
    std::cout << "today date: "<< Date::todaysDate() << std::endl;
 
    Calendar bondCalendar = nullCalendar();
 
    DayCounter bondDayCount = OneDayCounter();
    Integer settlementDays = 1;

        // actual market values from the evaluation date

        FixedCouponBond bond1(Date(1,November,2004),
                          Date(31,October,2004),
                          Date(31,October,2006),
                          settlementDays,
                          0.025, Semiannual,
                          bondDayCount, bondCalendar,
                          Unadjusted, 100.0);

             
             
        Real marketPrice1 = 99.203125;
        Rate marketYield1 = 0.02925;
   
        // check
        Real tolerance = 1.0e-6;
        Real price, dprice, yield;
      // calculated values

        Real cachedPrice1 = 99.204505;
       Rate cachedYield1 = 0.029257;    
   
        price = bond1.cleanPrice(marketYield1);
        dprice = bond1.dirtyPrice(marketYield1);
   
        std::cout <<  "    calculated clean price: "
                   << DecimalFormatter::toString(price,6) << "\n"
           << "    calculated dirty price: "
           << DecimalFormatter::toString(dprice,6) << "\n"
                   << "    expected:   "
                   << DecimalFormatter::toString(cachedPrice1,6) << "\n"
                   << "    error:      "
                   << DecimalFormatter::toString(price-cachedPrice1,6)
<< std::endl;
       
    std::cout << " Show cashflow: \n";
    std::vector<boost::shared_ptr<CashFlow> > cashflows = bond1.cashflows();
     
    for (Size i=0; i<cashflows.size(); ++i) {
        std::cout << cashflows[i]->date() << ": " <<
cashflows[i]->amount() <<"\n";
    }
   
    return 0;
} //main