#include "TreasuryYieldCurve.h" #include #include #include #include #include #include #include using namespace QuantLib; using namespace std; using namespace boost; #if defined(QL_ENABLE_SESSIONS) namespace QuantLib { Integer sessionId() { return 0; } } #endif int main(int, char* []) { try{ boost::timer timer; std::cout << std::endl; /////////////////***************************///////////////////////// DayCounter DMCalcCounter = Actual360(); Calendar DMCalccalendar = UnitedStates(UnitedStates::GovernmentBond); Date settlementDate(19, September, 2012); Natural settlementDays = 1; Integer fixingDays = 4; // must be a business day settlementDate = DMCalccalendar.adjust(settlementDate); Date todaysDate = DMCalccalendar.advance(settlementDate, -fixingDays, Days); cout << "Today is " << todaysDate << endl; // nothing to do with Date::todaysDate Settings::instance().evaluationDate() = todaysDate; std::vector AccuralStart; std::vector IndexRate; string dummyString, line; double dummyNumber; double continuousRate; std::ifstream AccuralStartFile, IndexRateFile; std::ofstream junk; junk.open("c:\\temp\\junk.dat"); QuantLib::Date dd; //dates for the zero curve AccuralStartFile.open("c:\\temp\\ASDateOriginal.dat"); while(true){ AccuralStartFile >> dummyString; if(AccuralStartFile.eof()) break; dd = DateParser::parse(dummyString, "mm/dd/yyyy"); AccuralStart.push_back(dd); } //original index rate for the zero curve IndexRateFile.open("c:\\temp\\IndexRateOriginal.dat"); while(true){ IndexRateFile >> dummyNumber; if(IndexRateFile.eof()) break; IndexRate.push_back(dummyNumber); } //construct a zero curve using original index rate from IndexRate.dat, the evaluation date for the zero curve is boost::shared_ptr TB13WksRateTermStructure( //this term structure used as the index rate for the floating rate note new InterpolatedZeroCurve ( AccuralStart, //the first date is the evaluation date! IndexRate, //the evaluation date for TB13WksRateTermStructure is defined DMCalcCounter)); //by the first date in ASDate.dat TB13WksRateTermStructure->enableExtrapolation(); //Define Simple Quote for the discount margin Rate Spread = 0.0012; Rate DM = 0.0012; Handle DMQuoteHandle(boost::shared_ptr(new SimpleQuote(DM))); //construct a term structure discountRateTermStructure that is (TB13WksRateTermStructure + DM) that will be used for deriving the discount factor //discountRateTermStructure will be linked to FRNEngine that is used in 'FRN.setPricingEngine(FRNEngine);' Handle zeroCurveHandle(TB13WksRateTermStructure); //handle to term structure TB13WksRateTermStructure boost::shared_ptr discountRateTermStructure(new ZeroSpreadedTermStructure(zeroCurveHandle, DMQuoteHandle)); discountRateTermStructure->enableExtrapolation(); //use ImpliedTermStructure to move evaluation date from the first date in ASDate.dat to Date(13, September, 2012) for discountRateTermStructure Handle discountRateTermStructureHandleImplied(discountRateTermStructure); boost::shared_ptr discountRateTermStructureImplied(new ImpliedTermStructure(discountRateTermStructureHandleImplied, Date(13, September, 2012))); discountRateTermStructureImplied->enableExtrapolation(); Real FRNfaceAmount = 1000000; //define a handle of the termstructure handle to be used in IborIndex. Eventually the handle will link to discountRateTermStructure RelinkableHandle TreasuryTermStructureHandle; //define FRN's index const boost::shared_ptr FRN13Wks(new USDLibor(Period(13,Weeks),TreasuryTermStructureHandle)); //link TreasuryTermStructureHandle to original zero rate curve TreasuryTermStructureHandle.linkTo(TB13WksRateTermStructure); //used by FRN13Wks that is part of FRN definition //FRN13Wks->addFixing(Date(30, July, 2012), 0.000950); //define FRN's schedule Schedule FRNSchedule( Date(31, July, 2012), Date(31, July, 2014), Period(Quarterly), UnitedStates(UnitedStates::GovernmentBond), Unadjusted, Unadjusted, DateGeneration::Backward, true); //define a floating rate note using TreasuryTermStructureHandle that basically orginal zero rate curve FloatingRateBond FRN( settlementDays, FRNfaceAmount, FRNSchedule, FRN13Wks, Actual360(), ModifiedFollowing, Natural(1), // Gearings std::vector(1, 1.0), // Spreads std::vector(1, 0.0012), // Caps std::vector(), // Floors std::vector(), // Fixing in arrears true, Real(100.0), Date(31, July, 2012)); //define the handle of the term structure that is to be used by the DiscountingBondEngine RelinkableHandle discountRateTermStructureHandle; boost::shared_ptr FRNEngine(new DiscountingBondEngine(discountRateTermStructureHandle)); FRN.setPricingEngine(FRNEngine); // Coupon pricers boost::shared_ptr FRNpricer(new BlackIborCouponPricer); // optionLet volatilities Volatility FRNvolatility = 0.0; Handle FRNvol; FRNvol = Handle( boost::shared_ptr(new ConstantOptionletVolatility( settlementDays, DMCalccalendar, ModifiedFollowing, FRNvolatility, Actual360()))); FRNpricer->setCapletVolatility(FRNvol); //void setCouponPricer(const Leg& leg, const boost::shared_ptr&); //to set the price using setCouponPricer setCouponPricer(FRN.cashflows(),FRNpricer); // Yield curve bootstrapping discountRateTermStructureHandle.linkTo(discountRateTermStructureImplied); //used by FRNEngine //print out junks junk.precision(9); for(int i=0; Date(31, July, 2012) + i*Days < Date(31, July, 2014);i++){ junk << i << " TB13WksRateTermStructure->zeroRate: " <zeroRate(Date(31, July, 2012) + i*Days ,DMCalcCounter, Simple, Daily ) << endl; junk << i << " TB13WksRateTermStructure->discount: " << Date(31, July, 2012) + i*Days << " | " << TB13WksRateTermStructure->discount(Date(31, July, 2012) + i*Days) << endl; junk << endl; } junk << endl; junk << "++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"; junk << endl; for(int i=0; todaysDate + i*Days < Date(31, July, 2014);i++){ junk << i << " discountRateTermStructureImplied->zeroRate: " << todaysDate + i*Days << " | " << discountRateTermStructureImplied->zeroRate(todaysDate + i*Days ,DMCalcCounter, Simple, Daily) << endl; junk << i << " discountRateTermStructureImplied->discount: " << todaysDate + i*Days << " | " << discountRateTermStructureImplied->discount(todaysDate + i*Days) << endl; junk << endl; } //print out the cashflow of FRN ofstream cashfile; cashfile.open("c:\\temp\\cashflow.dat"); cashfile.precision(10); std::vector > cashflows = FRN.cashflows(); //cout << "dddddddddddddddd " << cashflows.size() << endl; for (int i=0; idate() << ": " << cashflows[i]->amount() << endl; cashfile.close(); Size FRNwidths[] = { 18, 18, 18, 18 }; std::cout << std::setw(FRNwidths[0]) << "FRN Net present value: " << std::setprecision(11) << FRN.NPV() << std::endl; std::cout << std::setw(FRNwidths[0]) << "FRN Clean price: " << std::setprecision(11) << FRN.cleanPrice() << std::endl; std::cout << std::setw(FRNwidths[0]) << "FRN Dirty price: " << std::setprecision(11) << FRN.dirtyPrice() << std::endl; cout << "hello" << endl; junk.close(); system("pause"); }catch (std::exception& e) { std::cerr << e.what() << std::endl; system("pause"); return 1; } catch (...) { std::cerr << "unknown error" << std::endl; system("pause"); return 1; } system("pause"); }