2014-04-23 33 views
0

我嘗試使用QuantLib和Boost構建一個pyd文件,我想要計算屏障選項的NPV。然而,QuantLib PYD拋出:Uniform1dMesher中的「end必須大於start」

class Uniform1dMesher : public Fdm1dMesher { 
public: 
    Uniform1dMesher(Real start, Real end, Size size) 
    : Fdm1dMesher(size) { 
     QL_REQUIRE(end > start, "end must be large than start"); 

     const Real dx = (end-start)/(size-1); 

     for (Size i=0; i < size-1; ++i) { 
      locations_[i] = start + i*dx; 
      dplus_[i] = dminus_[i+1] = dx; 
     } 

     locations_.back() = end; 
     dplus_.back() = dminus_.front() = Null<Real>(); 
    } 
}; 

我的C++ - 代碼如下:

RuntimeError: end must be large than start 

錯誤從以下Quantlib類uniform1dmesher.hpp起源

struct OptionInputs 
{ 
    QuantLib::Real S; 
    QuantLib::Real K; 
    QuantLib::Spread f; 
    QuantLib::Rate r; 
    QuantLib::Volatility vol; 
    QuantLib::Date maturity; 
    QuantLib::DayCounter dayCounter; 
}; 

double FxOptEx(const OptionInputs &in, 
      const QuantLib::Date &todaysDate, 
      const QuantLib::Date &settlementDate) 
{ 
    using namespace QuantLib; 

    Calendar calendar = TARGET(); 
    Settings::instance().evaluationDate() = todaysDate; 
    QuantLib::Real rebate = 0.05; 

    Size timeGird = 365; 
    Size underlyingGird = 100; 
    Size dampingSteps = 0; 
    Real theta = 0.05; 
    bool localVolatility = true; 

    boost::shared_ptr<Exercise> europeanExercise(
      new EuropeanExercise(
       in.maturity)); 
    Handle<Quote> 
    underlyingH(boost::shared_ptr<Quote>(new SimpleQuote(in.S))); 

    Handle<YieldTermStructure> 
    rTS(boost::shared_ptr<YieldTermStructure>(new FlatForward(settlementDate, 
                  in.r, 
                  in.dayCounter))); 
    Handle<YieldTermStructure> 
    fTS(boost::shared_ptr<YieldTermStructure>(new FlatForward(settlementDate, 
                  in.f, 
                  in.dayCounter))); 
    Handle<BlackVolTermStructure> 
    flatVolTS(boost::shared_ptr<BlackVolTermStructure>(new BlackConstantVol(settlementDate, 
                     calendar, 
                     in.vol, 
                     in.dayCounter))); 

    boost::shared_ptr<StrikedTypePayoff> 
    payoff(new PlainVanillaPayoff(Option::Put, 
           in.K)); 

    boost::shared_ptr<BlackScholesMertonProcess> blackScholesMertonProcess(new BlackScholesMertonProcess(
             underlyingH, 
             fTS, 
              rTS, 
             flatVolTS)); 


     BarrierOption barrierOption(
      QuantLib::Barrier::UpIn, 
      QuantLib::Option::Put, 
      rebate, 
      payoff, 
      europeanExercise); 

     barrierOption.setPricingEngine(
      boost::shared_ptr<PricingEngine>(
       new FdBlackScholesBarrierEngine (
        blackScholesMertonProcess, 
        timeGird, 
        underlyingGird, 
        dampingSteps, 
        FdmSchemeDesc::ImplicitEuler(), 
        localVolatility, 
        -Null<Real>()))); 


    return barrierOption.NPV(); 
} 

struct FXOption 
{ 
    double value; 
    void set(int S, int K, double f, double r, double vol, std::string maturity, std::string dayCounter) 
    { 
    OptionInputs in; 
    in.S=S; 
    in.K=K; 
    in.f=f; 
    in.r=r; 
    in.vol=vol; 
    in.maturity=QuantLib::DateParser::parseISO(maturity); 
    if (dayCounter == "Actual365Fixed") 
    { 
     in.dayCounter = Actual365Fixed(); 
    } 
    value = FxOptEx(in, Date(15, May, 1998), Date(17, May, 1998)); 
    } 

    double get() 
    { 
     return value; 
    } 

}; 


using namespace boost::python; 
BOOST_PYTHON_MODULE(quant) 
{ 

    class_<FXOption>("FXOption") 
     .def("get", &FXOption::get) 
     .def("set", &FXOption::set) 
    ; 
} 

任何想法爲什麼這個錯誤被拋出?

+0

很明顯,'end'不會比'start'更大。如果你發現調用'Uniform1dMesher'的構造函數的地方,這將有所幫助。你應該真的嘗試一下調試器。 – rodrigo

+0

你在哪裏使用'Uniform1dMesher'?它沒有用在您發佈的代碼中。 – ooga

+0

@ooga:我想'Uniform1dMesher'是在發佈的代碼中實例化的一些類的子類,但不知道哪一個... – rodrigo

回答

0

對不起,我遲到了派對。

很難說沒有看到實際的調用,但是可能是期權的到期日早於結算日嗎?

相關問題