Skip to content

Commit

Permalink
- dima ql 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Miguel Manese committed Apr 14, 2014
1 parent 6ca7f80 commit 822539d
Show file tree
Hide file tree
Showing 5 changed files with 222 additions and 1 deletion.
26 changes: 25 additions & 1 deletion dima/02-ql/03-dates.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
81 changes: 81 additions & 0 deletions dima/02-ql/04-calendars.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#include <iostream>
#include <ql/quantlib.hpp>
#include <boost/foreach.hpp>

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<Date> 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<Date> 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;
}
20 changes: 20 additions & 0 deletions dima/02-ql/05-daycounts.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <iostream>
#include <ql/quantlib.hpp>

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;
}
84 changes: 84 additions & 0 deletions dima/02-ql/06-schedules.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#include <iostream>
#include <ql/quantlib.hpp>
#include <vector>
#include <boost/foreach.hpp>

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<Date>& 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<Date> 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;
}
12 changes: 12 additions & 0 deletions dima/02-ql/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 822539d

Please sign in to comment.