#include #include #include #include using namespace QuantLib; #if defined(QL_ENABLE_SESSIONS) namespace QuantLib { Integer sessionId() { return 0; } } #endif int main(int, char* []) { try { ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// // calculate Yield to maturity of Fixed Rate Bond ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// //////////BondSchedule Class Declarations////////////////// Date bondDatedDate(15,June,2010); Date SettlementDate(15,June,2011); Date bondMaturityDate(15,June,2011); Frequency bondCouponFrequency = Annual; Calendar bondCalendar = NullCalendar(); BusinessDayConvention bondBusinessDayConvention = Unadjusted; //////////////////////////////////////////////////////////// Settings::instance().evaluationDate()= bondDatedDate; //Added For initiating //////////FixedRateBond Class Declarations////////////////// Integer bondSettlementDays = 0; Real faceAmount = 60.00 ; //60.0; Real bondCoupon = 0.05; //interest rate Fixed DayCounter bondDayCountConvention = Thirty360(Thirty360::BondBasis); Real bondRedemption = faceAmount ;//60.00; //Change Made //100 Date bondIssueDate(15,June,2010); Real Clean_Price = 30.00; //30.0 //////////////////////////////////////////////////////////// //Method 1 Schedule bondSchedule(bondDatedDate, bondMaturityDate, Period(bondCouponFrequency), bondCalendar,bondBusinessDayConvention, bondBusinessDayConvention, DateGeneration::Forward,false); boost::shared_ptr bond( new FixedRateBond (bondSettlementDays, faceAmount, bondSchedule, std::vector(1,bondCoupon), bondDayCountConvention, bondBusinessDayConvention, bondRedemption, bondIssueDate)); /* //Method 2 Period MyInterval(1,Years); boost::shared_ptr bond( new FixedRateBond (bondSettlementDays, bondCalendar, faceAmount, bondIssueDate, bondMaturityDate, MyInterval, std::vector(1,bondCoupon), bondDayCountConvention));/* Following,Following,100, //Defaults... Date(),Date(), DateGeneration::Backward,false)); */ //Clean Price Required... std::cout << "----\n"; Rate MyFactor =100; Rate NonPercent; //1.0e-8 //Real accuracy NonPercent = bond->yield(Clean_Price,bondDayCountConvention,Compounded,bondCouponFrequency); std::cout << "So Yield to maturity is " << NonPercent ; //* MyFactor << " % \n"; std::cin.get(); return 0; } catch (std::exception& e) { std::cerr << e.what() << std::endl;\ std::cin.get(); return 1; } catch (...) { std::cerr << "unknown error" << std::endl; std::cin.get(); return 1; } }