diff --git a/dima/02-ql/03-dates.cpp b/dima/02-ql/03-dates.cpp index 14208fa..95c7842 100644 --- a/dima/02-ql/03-dates.cpp +++ b/dima/02-ql/03-dates.cpp @@ -43,9 +43,33 @@ void DateTesting2() { cout << "Serial number: " << serialNum << endl; } +void DateTesting3() { // static functions + cout << "Today: " << Date::todaysDate() << endl; + cout << "Min Date: " << Date::minDate() << endl; + cout << "Max Date: " << Date::maxDate() << endl; + cout << "Is leap year?: " << Date::isLeap(2011) << endl; + cout << "End of month: " << Date::endOfMonth(Date(4,Aug,2009)) << endl; + cout << "Is month end?: " << Date::isEndOfMonth(Date(29,Sep,2009)) << endl; + cout << "Is month end?: " << Date::isEndOfMonth(Date(30,Sep,2009)) << endl; + cout << "Next week day: " << Date::nextWeekday(Date(1,Sep,2009), Friday) << endl; + cout << "nth week day: " << Date::nthWeekday(3, Wed, Sep, 2009) << endl; +} + +void DateTesting4() { + Date d = Settings::instance().evaluationDate(); + + cout << "Eval date (default): " << d << endl; + + Settings::instance().evaluationDate() = Date(5, Jan, 1995); + d = Settings::instance().evaluationDate(); + cout << "Eval date (after): " << d << endl; +} + int main(int argc, char *argv[]) { //DateTesting1(); - DateTesting2(); + //DateTesting2(); + //DateTesting3(); + DateTesting4(); return 0; } diff --git a/dima/02-ql/04-calendars.cpp b/dima/02-ql/04-calendars.cpp new file mode 100644 index 0000000..af69d0d --- /dev/null +++ b/dima/02-ql/04-calendars.cpp @@ -0,0 +1,81 @@ +#include +#include +#include + +using namespace std; +using namespace QuantLib; + +// calendar functions: +// isBusinessDay(const Date& d) +// isHoliday(const Date& d) +// isWeekend(Weekday w) +// isEndOfMonth(const Date& d) +// Date endOfMonth(const Date& d) + +void CalendarTesting1() { + Calendar frankfCal = Germany(Germany::FrankfurtStockExchange); + Calendar saudiCal = SaudiArabia(); + Date nyEve(31, Dec, 2009); + + cout << "Is business day? (Frankfurt): " << frankfCal.isBusinessDay(nyEve) << endl; + cout << "Is holiday? (Frankfurt): " << frankfCal.isHoliday(nyEve) << endl; + cout << "Is weekend? (Saudi Arabia): " << saudiCal.isWeekend(Saturday) << endl; + cout << "Is last business day? (Frankfurt): " << frankfCal.isEndOfMonth(Date(30,Dec,2009)) << endl; + cout << "Last business day? (Frankfurt): " << frankfCal.endOfMonth(nyEve) << endl; +} + +// customizing calendars +// addHoiday(const Date& d) +// removeHoliday(const Date &d) +// std::vector holidayList(const Calendar& calendar, const Date& from, const Date& to, bool includeWeekends) + +void CalendarTesting2() { + Calendar frankfCal = Germany(Germany::FrankfurtStockExchange); + Date d1(24, Dec, 2009), d2(30, Dec, 2009), d3(31, Dec, 2009); + + frankfCal.addHoliday(d2); + frankfCal.removeHoliday(d3); + + cout << "Is business day?: " << frankfCal.isBusinessDay(d2) << endl; + cout << "Is business day?: " << frankfCal.isBusinessDay(d3) << endl; + + std::vector holidays = frankfCal.holidayList(frankfCal, d1, d2, false); + cout << "------------------------------" << endl; + BOOST_FOREACH(Date d, holidays) cout << d << endl; +} + +// BusinessDayConvention enum(?) +// * Following +// * ModifiedFollowing +// * Preceding +// * ModifiedPreceding +// * Unadjusted +// +// Calendar functions +// Date adjust(const Date&, BusinessDayConvention convention) +// Date advance(const Date&, const Periond&, DayConvention, bool endOfMonth) +// BigInteger businessDaysBetween(const Date& from, const Date& to, +// bool includeFirst, bool includeLast) - # of business days + +void CalendarTesting3() { + Calendar frankfCal = Germany(Germany::FrankfurtStockExchange); + + Date d1(31,Oct,2009), d2(1,Jan,2010); + + cout << "Date 2 adjust (Preceding): " << frankfCal.adjust(d2, BusinessDayConvention(Preceding)) << endl; + cout << "Date 2 adjust (ModifiedPreceding): " << frankfCal.adjust(d2, BusinessDayConvention(ModifiedPreceding)) << endl; + + Period mat(2, Months); + cout << "Date 1 month adv: " << frankfCal.advance(d1, mat, BusinessDayConvention(Following), false) << endl; + cout << "Date 1 month adv: " << frankfCal.advance(d1, mat, BusinessDayConvention(ModifiedFollowing), false) << endl; + + cout << "Business days b/w: " << frankfCal.businessDaysBetween(d1, d2, false, false) << endl; +} + +int main(int argc, char *argv[]) { + //CalendarTesting1(); + //CalendarTesting2(); + CalendarTesting3(); + + return 0; +} diff --git a/dima/02-ql/05-daycounts.cpp b/dima/02-ql/05-daycounts.cpp new file mode 100644 index 0000000..faf2787 --- /dev/null +++ b/dima/02-ql/05-daycounts.cpp @@ -0,0 +1,20 @@ +#include +#include + +using namespace std; +using namespace QuantLib; + +void dayCounterTesting1() { + DayCounter dc = Thirty360(); + Date d1(1, Oct, 2009); + Date d2 = d1 + 2*Months; + + cout << "Days between d1/d2: " << dc.dayCount(d1, d2) << std::endl; + cout << "Year fraction d1/d2: " << dc.yearFraction(d1, d2) << std::endl; +} + +int main(int argc, char *argv[]) { + dayCounterTesting1(); + + return 0; +} diff --git a/dima/02-ql/06-schedules.cpp b/dima/02-ql/06-schedules.cpp new file mode 100644 index 0000000..d41548b --- /dev/null +++ b/dima/02-ql/06-schedules.cpp @@ -0,0 +1,84 @@ +#include +#include +#include +#include + +using namespace std; +using namespace QuantLib; + +// DateGeneration::Rule +// Backward - backward from termination date to effective date +// Forward - forward from effective date to termination date +// Zero - no intermediate dates b/w effective & termination dates +// ThirdWednesday - all dates in b/w eff & term dates are on 3rd Wed of +// the month (forward calc) +// Twentieth - used for CDS schedules in emerging markets, all dates b/w eff +// & term dates are 20th of the month +// TwentiethIMM - used for CDS schedules, all dates b/w eff & term dates are +// 20th of an IMM month + +// Schedule(...) constructor arguments: +// const Date& effectiveDate - +// const Date& terminationDate - +// const Period& tenor - freq of the schedule (e.g. 3*Months) +// const Calendar& calendar - +// BusinessDayConvention convention - for in b/w dates +// BusinessDayConvention terminationDateConversion - for term date only +// DateGeneration::Rule rule - +// bool endOfMonth - if eff date is end of month, enforce sched dates +// to be end of month too (e.g. April 30th, May 31st, ...) +// const Date& firstDate = Date() - odd first +// const Date& nextToLastDate = Date() - odd last + +// Schedule functions +// Size size() - # of dates +// const Date& operator[](Size i) - indexer +// Date previousDate(const Date& refDate) - +// Date nextDate(const Date& refDate) - +// const std::vector& dates() - + +void scheduleTesting1(DateGeneration::Rule rule) { + Date begin(30, Sep, 2009), end(15, Jun, 2012); + Calendar cal = Japan(); + + BusinessDayConvention bdc = BusinessDayConvention(Following); + + Period tenor(6, Months); + + Schedule sched(begin, end, tenor, cal, bdc, bdc, rule, true); + + std::vector dates = sched.dates(); + + BOOST_FOREACH(Date d, dates) cout << d << endl; +} + +void scheduleTesting3() { + Date begin(30, Sep, 2009), end(15, Jun, 2012); + Calendar cal = Japan(); + + BusinessDayConvention bdc = BusinessDayConvention(Following); + + Period tenor(6, Months); + + DateGeneration::Rule rule = DateGeneration::Forward; + + Schedule sched(begin, end, tenor, cal, bdc, bdc, rule, true); + + Date refDate(3, Aug, 2010); + cout << "Date: " << refDate << endl; + cout << "Next date: " << sched.nextDate(refDate) << endl; + cout << "Prev date: " << sched.previousDate(refDate) << endl; +} + +int main(int argc, char *argv[]) { + + // last date should be 2012-06-15, but I'm producing 2012-06-29!! wtf! + //cout << "Forward: \n"; + //scheduleTesting1(DateGeneration::Forward); + + //cout << "\n\nBackward: \n"; + //scheduleTesting1(DateGeneration::Backward); + + scheduleTesting3(); + return 0; +} diff --git a/dima/02-ql/Makefile b/dima/02-ql/Makefile index f5c77f6..0dcc020 100644 --- a/dima/02-ql/Makefile +++ b/dima/02-ql/Makefile @@ -14,5 +14,17 @@ dates.exe: 03-dates.cpp g++ -c -Wall -I${BOOST} -I${QUANTLIB_H} $< -o $(patsubst %.exe,%.o,$@) g++ -L${QUANTLIB_LIB} $(patsubst %.exe,%.o,$@) -o $@ -lQuantLib +calendars.exe: 04-calendars.cpp + g++ -c -Wall -I${BOOST} -I${QUANTLIB_H} $< -o $(patsubst %.exe,%.o,$@) + g++ -L${QUANTLIB_LIB} $(patsubst %.exe,%.o,$@) -o $@ -lQuantLib + +daycounts.exe: 05-daycounts.cpp + g++ -c -Wall -I${BOOST} -I${QUANTLIB_H} $< -o $(patsubst %.exe,%.o,$@) + g++ -L${QUANTLIB_LIB} $(patsubst %.exe,%.o,$@) -o $@ -lQuantLib + +schedules.exe: 06-schedules.cpp + g++ -c -Wall -I${BOOST} -I${QUANTLIB_H} $< -o $(patsubst %.exe,%.o,$@) + g++ -L${QUANTLIB_LIB} $(patsubst %.exe,%.o,$@) -o $@ -lQuantLib + clean: rm -f *.exe