我正在尝试实现QuantLib的Vanilla Swap,但遇到了Thread 1: EXC_BAD_ACCESS (code=1, address=) error
。我正在使用Xcode
来实现以下代码。从网站上下载quantlib
时,基本上我只是遵循“示例”文件夹中随附的示例。
#include <iostream>
#include <vector>
#include <ql/qldefines.hpp>
#include <boost/shared_ptr.hpp>
#include <ql/quantlib.hpp>
#include <ql/settings.hpp>
int main(int argc, const char * argv[])
{
QuantLib::Singapore sgCalendar(QuantLib::Singapore::Market::SGX);
QuantLib::Date settlementDate(10, QuantLib::Month::April, 2010);
// adjust to business day
settlementDate = sgCalendar.adjust(settlementDate);
// today date
QuantLib::Integer fixingDays = 2;
QuantLib::Date todayDate = sgCalendar.advance(settlementDate, -fixingDays, QuantLib::Days);
QuantLib::Settings::instance().evaluationDate() = todayDate;
// deposit
QuantLib::Rate d1wQuote = 0.0328;
QuantLib::Rate d1mQuote = 0.0470;
QuantLib::Rate d3mQuote = 0.0510;
QuantLib::Rate d6mQuote = 0.0631;
QuantLib::Rate d9mQuote = 0.0782;
QuantLib::Rate d1yQuote = 0.0896;
//FRA
QuantLib::Rate fra3x6Quote=0.037125;
QuantLib::Rate fra6x9Quote=0.037125;
QuantLib::Rate fra6x12Quote=0.037125;
//futures
QuantLib::Real fut1Quote=96.2875;
QuantLib::Real fut2Quote=96.7875;
QuantLib::Real fut3Quote=96.9875;
QuantLib::Real fut4Quote=96.6875;
QuantLib::Real fut5Quote=96.4875;
QuantLib::Real fut6Quote=96.3875;
QuantLib::Real fut7Quote=96.2875;
QuantLib::Real fut8Quote=96.0875;
//swaps
QuantLib::Rate s2yQuote=0.037125;
QuantLib::Rate s3yQuote=0.0398;
QuantLib::Rate s5yQuote=0.0443;
QuantLib::Rate s10yQuote=0.05165;
QuantLib::Rate s15yQuote=0.055175;
// deposits
boost::shared_ptr<QuantLib::Quote> d1wRate(new QuantLib::SimpleQuote(d1wQuote));
boost::shared_ptr<QuantLib::Quote> d1mRate(new QuantLib::SimpleQuote(d1mQuote));
boost::shared_ptr<QuantLib::Quote> d3mRate(new QuantLib::SimpleQuote(d3mQuote));
boost::shared_ptr<QuantLib::Quote> d6mRate(new QuantLib::SimpleQuote(d6mQuote));
boost::shared_ptr<QuantLib::Quote> d9mRate(new QuantLib::SimpleQuote(d9mQuote));
boost::shared_ptr<QuantLib::Quote> d1yRate(new QuantLib::SimpleQuote(d1yQuote));
// FRAs
boost::shared_ptr<QuantLib::Quote> fra3x6Rate(new QuantLib::SimpleQuote(fra3x6Quote));
boost::shared_ptr<QuantLib::Quote> fra6x9Rate(new QuantLib::SimpleQuote(fra6x9Quote));
boost::shared_ptr<QuantLib::Quote> fra6x12Rate(new QuantLib::SimpleQuote(fra6x12Quote));
// futures
boost::shared_ptr<QuantLib::Quote> fut1Price(new QuantLib::SimpleQuote(fut1Quote));
boost::shared_ptr<QuantLib::Quote> fut2Price(new QuantLib::SimpleQuote(fut2Quote));
boost::shared_ptr<QuantLib::Quote> fut3Price(new QuantLib::SimpleQuote(fut3Quote));
boost::shared_ptr<QuantLib::Quote> fut4Price(new QuantLib::SimpleQuote(fut4Quote));
boost::shared_ptr<QuantLib::Quote> fut5Price(new QuantLib::SimpleQuote(fut5Quote));
boost::shared_ptr<QuantLib::Quote> fut6Price(new QuantLib::SimpleQuote(fut6Quote));
boost::shared_ptr<QuantLib::Quote> fut7Price(new QuantLib::SimpleQuote(fut7Quote));
boost::shared_ptr<QuantLib::Quote> fut8Price(new QuantLib::SimpleQuote(fut8Quote));
// swaps
boost::shared_ptr<QuantLib::Quote> s2yRate(new QuantLib::SimpleQuote(s2yQuote));
boost::shared_ptr<QuantLib::Quote> s3yRate(new QuantLib::SimpleQuote(s3yQuote));
boost::shared_ptr<QuantLib::Quote> s5yRate(new QuantLib::SimpleQuote(s5yQuote));
boost::shared_ptr<QuantLib::Quote> s10yRate(new QuantLib::SimpleQuote(s10yQuote));
boost::shared_ptr<QuantLib::Quote> s15yRate(new QuantLib::SimpleQuote(s15yQuote));
/*********************
*** RATE HELPERS ***
*********************/
// RateHelpers are built from the above quotes together with
// other instrument dependant infos. Quotes are passed in
// relinkable handles which could be relinked to some other
// data source later.
// deposits
QuantLib::DayCounter depositDayCounter = QuantLib::Actual360();
boost::shared_ptr<QuantLib::RateHelper> d1w(new QuantLib::DepositRateHelper(QuantLib::Handle<QuantLib::Quote> (d1wRate),
1*QuantLib::Weeks, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true,depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> d1m(new QuantLib::DepositRateHelper(QuantLib::Handle<QuantLib::Quote> (d1mRate),
1*QuantLib::Months, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true,depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> d3m(new QuantLib::DepositRateHelper(QuantLib::Handle<QuantLib::Quote> (d3mRate),
3*QuantLib::Months, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true,depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> d6m(new QuantLib::DepositRateHelper(QuantLib::Handle<QuantLib::Quote> (d6mRate),
6*QuantLib::Months, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true,depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> d9m(new QuantLib::DepositRateHelper(QuantLib::Handle<QuantLib::Quote> (d9mRate),
9*QuantLib::Months, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true,depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> d1y(new QuantLib::DepositRateHelper(QuantLib::Handle<QuantLib::Quote> (d1yRate),
1*QuantLib::Years, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true,depositDayCounter));
//setup FRA
boost::shared_ptr<QuantLib::RateHelper> fra3x6(new QuantLib::FraRateHelper(QuantLib::Handle<QuantLib::Quote> (fra3x6Rate),
3, 6, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> fra6x9(new QuantLib::FraRateHelper(QuantLib::Handle<QuantLib::Quote> (fra6x9Rate),
6, 9, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
boost::shared_ptr<QuantLib::RateHelper> fra6x12(new QuantLib::FraRateHelper(QuantLib::Handle<QuantLib::Quote> (fra6x12Rate),
6, 12, fixingDays, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
//setup futures
QuantLib::Integer futMonths = 3;
QuantLib::Date imm = QuantLib::IMM::nextDate(settlementDate);
boost::shared_ptr<QuantLib::RateHelper> fut1(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut1Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut2(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut2Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut3(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut3Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut4(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut4Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut5(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut5Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut6(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut6Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut7(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut7Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
imm = QuantLib::IMM::nextDate(imm + 1);
boost::shared_ptr<QuantLib::RateHelper> fut8(new QuantLib::FuturesRateHelper(QuantLib::Handle<QuantLib::Quote> (fut8Price),
imm, futMonths, sgCalendar, QuantLib::ModifiedFollowing,
true, depositDayCounter));
//setup swap
QuantLib::Frequency swFixedLegFrequency = QuantLib::Annual;
QuantLib::BusinessDayConvention swFixedLegConvention = QuantLib::ModifiedFollowing;
QuantLib::DayCounter swFixedLegDayCounter = QuantLib::Thirty360(QuantLib::Thirty360::European);
boost::shared_ptr<QuantLib::IborIndex> swFloatingLegIndex(new QuantLib::Euribor6M);
boost::shared_ptr<QuantLib::RateHelper> s2y(new QuantLib::SwapRateHelper(QuantLib::Handle<QuantLib::Quote> (s2yRate),
2 * QuantLib::Years, sgCalendar, swFixedLegFrequency, swFixedLegConvention,
swFixedLegDayCounter, swFloatingLegIndex));
boost::shared_ptr<QuantLib::RateHelper> s3y(new QuantLib::SwapRateHelper(QuantLib::Handle<QuantLib::Quote> (s3yRate),
3 * QuantLib::Years, sgCalendar, swFixedLegFrequency, swFixedLegConvention,
swFixedLegDayCounter, swFloatingLegIndex));
boost::shared_ptr<QuantLib::RateHelper> s5y(new QuantLib::SwapRateHelper(QuantLib::Handle<QuantLib::Quote> (s5yRate),
5 * QuantLib::Years, sgCalendar, swFixedLegFrequency, swFixedLegConvention,
swFixedLegDayCounter, swFloatingLegIndex));
boost::shared_ptr<QuantLib::RateHelper> s10y(new QuantLib::SwapRateHelper(QuantLib::Handle<QuantLib::Quote> (s10yRate),
10 * QuantLib::Years, sgCalendar, swFixedLegFrequency, swFixedLegConvention,
swFixedLegDayCounter, swFloatingLegIndex));
boost::shared_ptr<QuantLib::RateHelper> s15y(new QuantLib::SwapRateHelper(QuantLib::Handle<QuantLib::Quote> (s15yRate),
15 * QuantLib::Years, sgCalendar, swFixedLegFrequency, swFixedLegConvention,
swFixedLegDayCounter, swFloatingLegIndex));
/*********************
** CURVE BUILDING **
*********************/
// Any DayCounter would be fine.
// ActualActual::ISDA ensures that 30 years is 30.0
QuantLib::DayCounter termstructureDayCounter = QuantLib::ActualActual(QuantLib::ActualActual::ISDA);
double tolerance = 1.0e-15;
//depo-swap curve
std::vector<boost::shared_ptr<QuantLib::RateHelper>> depoSwapInstrument;
depoSwapInstrument.push_back(d1w);
depoSwapInstrument.push_back(d1m);
depoSwapInstrument.push_back(d3m);
depoSwapInstrument.push_back(d6m);
depoSwapInstrument.push_back(d9m);
depoSwapInstrument.push_back(d1y);
depoSwapInstrument.push_back(s2y);
depoSwapInstrument.push_back(s3y);
depoSwapInstrument.push_back(s5y);
depoSwapInstrument.push_back(s10y);
depoSwapInstrument.push_back(s15y);
boost::shared_ptr<QuantLib::YieldTermStructure> depoSwapTermStructure(new QuantLib::PiecewiseYieldCurve<QuantLib::Discount, QuantLib::LogLinear>
(settlementDate, depoSwapInstrument, termstructureDayCounter, tolerance));
//depo-futures-swap curve
std::vector<boost::shared_ptr<QuantLib::RateHelper>> depoFutSwapInstrument;
depoFutSwapInstrument.push_back(d1w);
depoFutSwapInstrument.push_back(d1m);
depoFutSwapInstrument.push_back(fut1);
depoFutSwapInstrument.push_back(fut2);
depoFutSwapInstrument.push_back(fut3);
depoFutSwapInstrument.push_back(fut4);
depoFutSwapInstrument.push_back(fut5);
depoFutSwapInstrument.push_back(fut6);
depoFutSwapInstrument.push_back(fut7);
depoFutSwapInstrument.push_back(fut8);
depoFutSwapInstrument.push_back(s3y);
depoFutSwapInstrument.push_back(s5y);
depoFutSwapInstrument.push_back(s10y);
depoFutSwapInstrument.push_back(s15y);
boost::shared_ptr<QuantLib::YieldTermStructure> depoFutSwapTermStructure(new QuantLib::PiecewiseYieldCurve<QuantLib::Discount, QuantLib::LogLinear>
(settlementDate, depoFutSwapInstrument, termstructureDayCounter, tolerance));
//depo-FRA-swap curve
std::vector<boost::shared_ptr<QuantLib::RateHelper>> depoFRASwapInstrument;
depoFRASwapInstrument.push_back(d1w);
depoFRASwapInstrument.push_back(d1m);
depoFRASwapInstrument.push_back(d3m);
depoFRASwapInstrument.push_back(fra3x6);
depoFRASwapInstrument.push_back(fra6x9);
depoFRASwapInstrument.push_back(fra6x12);
depoFRASwapInstrument.push_back(s2y);
depoFRASwapInstrument.push_back(s3y);
depoFRASwapInstrument.push_back(s5y);
depoFRASwapInstrument.push_back(s10y);
depoFRASwapInstrument.push_back(s15y);
boost::shared_ptr<QuantLib::YieldTermStructure> depoSWAPSwapStructure(new QuantLib::PiecewiseYieldCurve<QuantLib::Discount, QuantLib::LogLinear>
(settlementDate, depoFRASwapInstrument, termstructureDayCounter, tolerance));
// Term structures that will be used for pricing:
// the one used for discounting cash flows
QuantLib::RelinkableHandle<QuantLib::YieldTermStructure> discountingTermStructure;
// the one used for forward rate forecasting
QuantLib::RelinkableHandle<QuantLib::YieldTermStructure> forecastingTermStructure;
/*********************
* SWAPS TO BE PRICED *
**********************/
QuantLib::Real nominal = 1000000;
// fixed leg
QuantLib::Frequency fixedLegFrequency(QuantLib::Annual);
QuantLib::BusinessDayConvention fixedLegConvention = QuantLib::BusinessDayConvention::Unadjusted;
QuantLib::BusinessDayConvention floatingLegConvention = QuantLib::BusinessDayConvention::ModifiedFollowing;
QuantLib::DayCounter fixedLegDayCounter = QuantLib::Thirty360(QuantLib::Thirty360::European);
QuantLib::Rate fixedLegRate = 0.04;
QuantLib::DayCounter floatingLegDaycounter = QuantLib::Actual360();
//floating leg
QuantLib::Frequency floatingLegFrequency = QuantLib::Semiannual;
boost::shared_ptr<QuantLib::IborIndex> euriborIndex(new QuantLib::Euribor6M(forecastingTermStructure));
QuantLib::Spread spread = 0.0;
QuantLib::Integer lengthInYear = 5;
QuantLib::VanillaSwap::Type swapType = QuantLib::VanillaSwap::Type::Payer;
QuantLib::Date maturity = settlementDate + lengthInYear * 5;
QuantLib::Schedule fixedSchedule(settlementDate, maturity, QuantLib::Period(fixedLegFrequency),
sgCalendar, fixedLegConvention, fixedLegConvention, QuantLib::DateGeneration::Forward, false);
QuantLib::Schedule floatingSchedule(settlementDate, maturity, QuantLib::Period(floatingLegFrequency),
sgCalendar, floatingLegConvention, floatingLegConvention, QuantLib::DateGeneration::Forward, false);
QuantLib::VanillaSwap spot5YearSwap(swapType, nominal, fixedSchedule, fixedLegRate, fixedLegDayCounter,
floatingSchedule, euriborIndex, spread, floatingLegDaycounter);
return 0;
}
尝试初始化spot5YearSwap
时发生错误。