#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 // par-rate approximation Rate parRate(const YieldTermStructure& yts, const std::vector& dates, const DayCounter& resultDayCounter) { QL_REQUIRE(dates.size() >= 2, "at least two dates are required"); Real sum = 0.0; Time dt; for (Size i=1; i0.0, "unsorted dates"); sum += yts.discount(dates[i]) * dt; } Real result = yts.discount(dates.front()) - yts.discount(dates.back()); return result/sum; } int main(int, char* []) { try{ boost::timer timer; std::cout << std::endl; /////////////////***************************///////////////////////// Natural settlementDays = 1; Integer fixingDays = 1; DayCounter TYUCounter = Actual360(); Calendar TYUcalendar = UnitedStates(UnitedStates::GovernmentBond); Date settlementDate(19, September, 2012); // must be a business day settlementDate = TYUcalendar.adjust(settlementDate); Date todaysDate = TYUcalendar.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; std::ifstream AccuralStartFile, IndexRateFile; std::ofstream junk; junk.open("c:\\temp\\junk.dat"); QuantLib::Date dd; AccuralStartFile.open("c:\\temp\\ASDate.dat"); while(true){ AccuralStartFile >> dummyString; if(AccuralStartFile.eof()) break; dd = DateParser::parse(dummyString, "mm/dd/yyyy"); AccuralStart.push_back(dd); //junk << dd << endl; } cout << "AccuralStartSize = " << AccuralStart.size() << endl; IndexRateFile.open("c:\\temp\\IndexRate.dat"); while(true){ IndexRateFile >> dummyNumber; if(IndexRateFile.eof()) break; IndexRate.push_back(dummyNumber); //junk << dummyNumber << endl; } cout << "IndexRateFileSize = " << IndexRate.size() << endl; //junk.close(); //term structure constructed using data from TYU.xlsm boost::shared_ptr TB13WksRateTermStructure( new InterpolatedZeroCurve ( AccuralStart, IndexRate, TYUCounter)); TB13WksRateTermStructure->enableExtrapolation(); //print out the zero curve for TB13WksRateTermStructure for(int i=0;Date(31, July, 2012)+i*Days < Date(31, July, 2014);i++){ junk << i << " " << Date(31, July, 2012)+i*Days << " | " << TB13WksRateTermStructure->zeroRate(Date(31, July, 2012) + i*Days ,TYUCounter, Simple, Quarterly ) << endl; } //Simple quote defined by the spread of TYU.xlsm Rate Spread = 0.0012; Rate DM = 0.0012; Handle DMQuoteHandle(boost::shared_ptr(new SimpleQuote(Spread))); //construct a term structure discountRateTermStructure that is (TB13WksRateTermStructure + spread) Handle zeroCurveHandle(TB13WksRateTermStructure); //handle to term structure TB13WksRateTermStructure boost::shared_ptr discountRateTermStructure(new ZeroSpreadedTermStructure(zeroCurveHandle, DMQuoteHandle)); discountRateTermStructure->enableExtrapolation(); //print out the zero curve for TB13WksRateTermStructure + spread for(int i=0;Date(31, July, 2012)+i*Days < Date(31, July, 2014);i++){ junk << i << " " << Date(31, July, 2012)+i*Days << " ^^^ " << discountRateTermStructure->zeroRate(Date(31, July, 2012) + i*Days ,TYUCounter, Simple, Quarterly ) << endl; } 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)); //fixing the index using the data from TYU.xlsm and print them out TreasuryTermStructureHandle.linkTo(TB13WksRateTermStructure); //used by FRN13Wks that is part of FRN definition //FRN13Wks->maturityDate(Date(31, July, 2014)); junk << AccuralStart.size() << endl; for(int i=0;iisValidFixingDate(AccuralStart[i]) && AccuralStart[i] < settlementDate){ FRN13Wks->addFixing(AccuralStart[i], IndexRate[i]); junk << AccuralStart[i] << " +++ " << FRN13Wks->fixing(AccuralStart[i]) << " +++ " << IndexRate[i] << endl; } if(FRN13Wks->isValidFixingDate(AccuralStart[i]) && AccuralStart[i] >= settlementDate){ junk << AccuralStart[i] << " ``` " << FRN13Wks->forecastFixing(AccuralStart[i]) << " ``` " << IndexRate[i] << endl; } } junk.close(); //use forecast fixing date for the fixings later than the Settlement date //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); FloatingRateBond FRN( settlementDays, FRNfaceAmount, FRNSchedule, FRN13Wks, Actual360(), ModifiedFollowing, Natural(2), // 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, TYUcalendar, ModifiedFollowing, FRNvolatility, Actual365Fixed()))); 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(discountRateTermStructure); //used by FRNEngine //print out the cashflow of FRN ofstream cashfile; cashfile.open("c:\\temp\\cashflow.dat"); std::vector > cashflows = FRN.cashflows(); //cout << "dddddddddddddddd " << cashflows.size() << endl; for (int i=0; idate() << ": " << cashflows[i]->amount() << endl; cashfile.close(); Size FRNwidths[] = { 18, 10, 10, 10 }; std::cout << std::setw(FRNwidths[0]) << "FRN Net present value" << std::setw(FRNwidths[3]) << FRN.NPV() << std::endl; std::cout << std::setw(FRNwidths[0]) << "FRN Clean price" << std::setw(FRNwidths[2]) << FRN.cleanPrice() << std::endl; cout << "hello" << endl; system("pause"); }catch (std::exception& e) { std::cerr << e.what() << std::endl; return 1; } catch (...) { std::cerr << "unknown error" << std::endl; return 1; } }