RuntimeError:root没有括号

时间:2017-01-26 19:24:20

标签: python c++ quantlib

class ComputeIV
{
    public:    
        typedef std::pair<SimpleQuote,SimpleQuote> BidAsk;    

        static Volatility ComputeImpliedVol(const Date evalDate, const Date expiration, ptime quoteTime, const Option::Type optionType, 
                  const Real underlyingPrice, const Real optionPrice, const Real strike, const Rate riskFree) 
        {
            ActualActual actualActual;
            Settings::instance().evaluationDate() = evalDate;    

            Time timeToMaturity = actualActual.yearFraction(Settings::instance().evaluationDate(), expiration);

            time_duration timeOfDayDuration = quoteTime.time_of_day();
            timeToMaturity += (timeOfDayDuration.hours() + timeOfDayDuration.minutes()/60.0)/(24.0 * 365.0);               

            DiscountFactor discount = std::exp(-riskFree * timeToMaturity);

            Bisection bisection;
            Real accuracy = 0.000001, guess = .20;
            Real min = .05, max = .40;
            Volatility sigma = bisection.solve([&](const Volatility & sigma) {
                Real stdDev = sigma * std::sqrt(timeToMaturity);
                BlackCalculator blackCalculator(optionType, strike, underlyingPrice, stdDev, discount);
                    return blackCalculator.value() - optionPrice;
            }, accuracy, guess, min, max);  

            return sigma;    

        } 

    static const double& pTest(const std::string evalDateStr, const std::string expirationStr, const std::string quoteTimeStr, 
                               const int optType, const Real forwardBid, const Real forwardAsk, const Rate riskFree, const Real strike, 
                               const Real oBid, const Real oAsk)
    {
        std::cout << "Computing IV" << std::endl << std::flush;
        ActualActual actualActual;

        std::cout << evalDateStr << " " << expirationStr << " " << quoteTimeStr << std::endl << std::flush;
        std::cout << optType << " "  << riskFree << " " << forwardBid << " " << forwardAsk << " " << strike 
                  << " " <<  oBid << " " << oAsk << std::endl << std::flush;

        Date evalDate = DateParser::parseFormatted(evalDateStr.c_str(), "%d/%m/%Y");
        Settings::instance().evaluationDate() = evalDate;

        Date expiration = DateParser::parseFormatted(expirationStr.c_str(), "%d/%m/%Y");

        ptime quoteTime(from_iso_string(quoteTimeStr));
        time_duration timeOfDayDuration = quoteTime.time_of_day();

        Real price = (oBid + oAsk) / 2.0;    

        Option::Type oType = (optType > 0 ? Option::Call : Option::Put);
        Volatility *sigma = new Volatility();
        *sigma = ComputeIV::ComputeImpliedVol(evalDate, expiration, quoteTime, oType, 
                                forwardAsk, price, strike, riskFree);

        return *sigma;                        
    }

    private:        

};

当我使用这些参数调用时,

from options import ComputeIV

PUT = -1
Call = 1

evalDate = "03/01/2017"
expiration = "07/07/2017";
quoteTime = "20170103T210000"
forwardBid = 84.19 
forwardAsk = 84.20  
riskFree = .015 
strike = 45.0
oBid = 0.10
oAsk = 0.17
oType = PUT

sigma = ComputeIV.pTest(evalDate, expiration, quoteTime, oType, forwardBid, forwardAsk, riskFree, strike, oBid, oAsk)

print "from python sigma = %f" % sigma

我收到运行时错误:

Traceback (most recent call last):
  File "cboelivedata.py", line 575, in <module>
    main()
  File "cboelivedata.py", line 400, in main
    sigma = ComputeIV.pTest(evalDate, expiration, quoteTime, oType, forwardBid, forwardAsk, riskFree, strike, oBid, oAsk)
RuntimeError: root not bracketed: f[0.05,0.4] -> [-1.350000e-01,-2.434993e-02]
[idf@node3 python]$

我在这里做的不正确吗?

1 个答案:

答案 0 :(得分:3)

您正在使用黑色公式。在Black-Scholes欧洲期权框架中,期权价格是波动率的一个递增函数。

您的二等分波动率的最小和最大范围是0.050.40。它们足够好吗?您的看跌期权很低,因此需要很高的波动性。

让我们检查你的范围。转到http://www.erieri.com/blackscholes,输入信息,如我所做的:

enter image description here

您的求解程序可以给您的最大看跌期权价格约为0.0797,但您的报价为0.135 。因此,没有解决方案,你的二分根解算器是正确的告诉你。

您需要增加波动率范围。尝试:

Real min = .05, max = 1.00;

您可能不需要1.00(这太大了),但您明白了 - 您需要调整根解决范围。

尝试一下,你就会得到隐含的波动率。