QuantLib - Python扩展
本页介绍如何通过SWIG向QuantLib的接口中添加新的功能
介绍
项目
前期准备
修改步骤
git clone https://github.com/ChinaQuants/qlengineSpreadOption
KirkSpreadOptionEngine测试
Last updated
本页介绍如何通过SWIG向QuantLib的接口中添加新的功能
git clone https://github.com/ChinaQuants/qlengineSpreadOption
KirkSpreadOptionEngineLast updated
class SpreadOption : public MultiAssetOption {
public:
class engine;
SpreadOption(const ext::shared_ptr<PlainVanillaPayoff>& payoff,
const ext::shared_ptr<Exercise>& exercise)
: MultiAssetOption(payoff, exercise) {}
};class KirkSpreadOptionEngine : public SpreadOption::engine {
public:
KirkSpreadOptionEngine(
const ext::shared_ptr<BlackProcess>& process1,
const ext::shared_ptr<BlackProcess>& process2,
const Handle<Quote>& correlation);
void calculate() const;
private:
ext::shared_ptr<BlackProcess> process1_;
ext::shared_ptr<BlackProcess> process2_;
Handle<Quote> rho_;
};%include options_ext.i#ifndef quantlib_options_ext_i
#define quantlib_options_ext_i
%include options.i
%{
using QuantLib::SpreadOption;
typedef boost::shared_ptr<Instrument> SpreadOptionPtr;
%}
%rename(SpreadOption) SpreadOptionPtr;
class SpreadOptionPtr : public boost::shared_ptr<Instrument> {
public:
%extend {
SpreadOptionPtr(
const boost::shared_ptr<Payoff>& payoff,
const boost::shared_ptr<Exercise>& exercise) {
boost::shared_ptr<PlainVanillaPayoff> stPayoff =
boost::dynamic_pointer_cast<PlainVanillaPayoff>(payoff);
QL_REQUIRE(stPayoff, "wrong payoff given");
return new SpreadOptionPtr(new SpreadOption(stPayoff, exercise));
}
}
};
#endif#ifndef quantlib_options_ext_i
#define quantlib_options_ext_i
......
#endif%include options.i%{
using QuantLib::SpreadOption;
typedef boost::shared_ptr<Instrument> SpreadOptionPtr;
%}%rename(SpreadOption) SpreadOptionPtr;
class SpreadOptionPtr : public boost::shared_ptr<Instrument> {
public:
%extend {
SpreadOptionPtr(
const boost::shared_ptr<Payoff>& payoff,
const boost::shared_ptr<Exercise>& exercise) {
boost::shared_ptr<PlainVanillaPayoff> stPayoff =
boost::dynamic_pointer_cast<PlainVanillaPayoff>(payoff);
QL_REQUIRE(stPayoff, "wrong payoff given");
return new SpreadOptionPtr(new SpreadOption(stPayoff, exercise));
}
}
};%{
using QuantLib::KirkSpreadOptionEngine;
typedef boost::shared_ptr<PricingEngine> KirkSpreadOptionEnginePtr;
%}
%rename(KirkSpreadOptionEngine) KirkSpreadOptionEnginePtr;
class KirkSpreadOptionEnginePtr
: public boost::shared_ptr<PricingEngine> {
public:
%extend {
KirkSpreadOptionEnginePtr(
const BlackProcessPtr& process1,
const BlackProcessPtr& process2,
const Handle<Quote>& correlation) {
boost::shared_ptr<BlackProcess> bsProcess1 =
boost::dynamic_pointer_cast<BlackProcess>(process1);
boost::shared_ptr<BlackProcess> bsProcess2 =
boost::dynamic_pointer_cast<BlackProcess>(process2);
return new KirkSpreadOptionEnginePtr(
new KirkSpreadOptionEngine(bsProcess1, bsProcess2, correlation));
}
}
};python setup.py installfrom QuantLib import *
payoff = PlainVanillaPayoff(Option.Call, 0.2)
maturity = Date(1, 12, 2018)
exercise = EuropeanExercise(maturity)
option = SpreadOption(payoff, exercise)
evalDate = Date(2, 8, 2018)
Settings.instance().setEvaluationDate(evalDate)
s1 = QuoteHandle(SimpleQuote(1.0))
r1 = YieldTermStructureHandle(FlatForward(evalDate, QuoteHandle(SimpleQuote(0.05)), Actual365Fixed()))
v1 = BlackVolTermStructureHandle(BlackConstantVol(evalDate, NullCalendar(), 0.5, Actual365Fixed()))
process1 = BlackProcess(s1, r1, v1)
s2 = QuoteHandle(SimpleQuote(1.0))
r2 = YieldTermStructureHandle(FlatForward(evalDate, QuoteHandle(SimpleQuote(0.05)), Actual365Fixed()))
v2 = BlackVolTermStructureHandle(BlackConstantVol(evalDate, NullCalendar(), 0.5, Actual365Fixed()))
process2 = BlackProcess(s2, r2, v2)
corr = QuoteHandle(SimpleQuote(0.0))
engine = KirkSpreadOptionEngine(process1, process2, corr)
option.setPricingEngine(engine)
print("Option NPV with 0 corretion: {0:.4f}".format(option.NPV()))
corr = QuoteHandle(SimpleQuote(1.0))
engine = KirkSpreadOptionEngine(process1, process2, corr)
option.setPricingEngine(engine)
print("Option NPV with 1 corretion: {0:.4f}".format(option.NPV()))Option NPV with 0 corretion: 0.0808
Option NPV with 1 corretion: 0.0000