|
From: Ashish B. <ash...@gm...> - 2022-12-06 16:52:02
|
Hi Elric,
What a coincidence, that I sent a query related to this same error for my
scenario. I have seen this error in cases where the code is not able to
solve for the right IV to match the price. In my case the option was
trading with exactly 0 time value i.e. the option premium was dot matching
with the intrinsic value for put options. I don't see that scenario for you
but not sure what else is breaking the IV code here.
I suggest you option.NPV to calculate the price of the option using your
code and passing different volatility values to see if you are able to
achieve the target premium with any reasonable volatility.
Thanks
Ashish
On Tue, 6 Dec 2022 at 09:19, Elric StormBringer <
elr...@gm...> wrote:
>
> I actually used the code below for a test-case (Up-and-Out Barrier
> Option). I know the arguments to specify the trade is correct, because I
> can get 'PV', and 'greeks'. But when I tried to get the 'Implied
> Volatility', it fails with the following error : return _QuantLib.
> BarrierOption_impliedVolatility(self, targetValue, process, accuracy,
> maxEvaluations, minVol, maxVol)
>
> RuntimeError: root not bracketed: f[0.0001,4] -> [-nan(ind),1.127331e+01]
>
>
>
> Below is the python code : ...... Not sure if it is due to 'arguments' being insufficient, or the solver cannot solve for the Implied-vol, or something else.
>
>
> # Import required library
>> from QuantLib import *
>
>
> # Barrier Option: Up-and-Out Call
> # Strike 100, Barrier 150, Rebate 50, Exercise date 4 years
>
> #Set up the global evaluation date to today
> today = Date(28,February,2020)
> Settings.instance().evaluationDate = today
>
> # Specify option
> # ql.BarrierOption(barrierType, barrier, rebate, payoff, exercise)
> # the option below sets Up-and-Out barrier, with Barrier Level of 150, European Vanilla Call as the underlying payoff
> # of European Call Strike and European Expiry date in exercise
> option = BarrierOption(Barrier.UpOut, 150.0, 50.0,
> PlainVanillaPayoff(Option.Call, 100.0),
> EuropeanExercise(Date(29, February, 2024)))
>
> # We will now pass the market data: spot price : 100, risk-free rate: 1% and sigma: 30%
> # Underlying Price
> u = SimpleQuote(100)
> # Risk-free Rate
> r = SimpleQuote(0.01)
> # Sigma
> sigma = SimpleQuote(0.30)
>
> # Build flat curves and volatility
> riskFreeCurve = FlatForward(0, TARGET(), QuoteHandle(r), Actual360())
> volatility = BlackConstantVol(0, TARGET(), QuoteHandle(sigma), Actual360())
>
> # Build the pricing engine by encapsulating the market data in a Black-Scholes process
> # Stochastic Process
> process = BlackScholesProcess(QuoteHandle(u),
> YieldTermStructureHandle(riskFreeCurve),
> BlackVolTermStructureHandle(volatility))
>
> # Build the engine (based on an analytic formula) and set it to the option for evaluation
> option.setPricingEngine(AnalyticBarrierEngine(process))
>
> # Change the market data to get new option pricing.
> # Set initial value and define h
> u0 = u.value(); h=0.01
> P0 = option.NPV()
> print('the initial option price is', np.round(P0, 4))
>
> # Bump up the price by h
> u.setValue(u0+h)
> P_plus = option.NPV()
> print('the bumped up option price is', np.round(P_plus, 4))
>
> # Bump down the price by h
> u.setValue(u0-h)
> P_minus = option.NPV()
> print('the bumped down option price is', np.round(P_minus, 4), '\n')
>
> # Set the price back to its current value
> u.setValue(u0)
>
> # Calculate Greeks: Delta, Gamma, Vega, Theta, Rho
> delta = (P_plus - P_minus)/(2*h)
> gamma = (P_plus - 2*P0 + P_minus)/(h*h)
>
> # Update quote for rho calculation
> r0 = r.value(); h1 = 0.0001
> r.setValue(r0+h); P_plus = option.NPV()
> r.setValue(r0)
>
> # Rho
> rho = (P_plus - P0)/h1
>
> # Update quote for sigma calculation
> sigma0 = sigma.value() ; h = 0.0001
> sigma.setValue(sigma0+h) ; P_plus = option.NPV()
> sigma.setValue(sigma0)
>
> # Vega
> vega = (P_plus - P0)/h
>
> # Update quote to calculate theta
> Settings.instance().evaluationDate = today+1
> P1 = option.NPV()
> h = 1.0/365
>
> # Theta
> theta = (P1-P0)/h
>
> print(f'OptionPrice: {P0: .2f}, Delta: {delta: .2f}, Gamma: {gamma: .4f}, Theta: {theta: .2f}, \
> Vega: {vega: .2f}, Rho: {rho: .2f}')
>
> option.impliedVolatility(22.06, process)
>
>
> ---------
>
>
>
>
> On Fri, 2 Dec 2022 at 21:01, Ashish Bansal <ash...@gm...>
> wrote:
>
>> Specific to IV, i didn't calculate IV for barriers so not sure. The IV is
>> not implied for all the option types like for averaging options, it doesn't
>> work. However, i do see the same being implemented for Barriers:
>>
>> https://github.com/lballabio/QuantLib/blob/2536f0e3db681f4cb8f4972f09d561e8f085b5ea/ql/instruments/barrieroption.cpp#L51
>> Volatility BarrierOption::impliedVolatility(
>> Real targetValue,
>> const ext::shared_ptr<GeneralizedBlackScholesProcess>& process,
>> Real accuracy,
>> Size maxEvaluations,
>> Volatility minVol,
>> Volatility maxVol)
>>
>> What error are you getting? Try to enter more arguments in it and try.
>>
>> Regards
>> Ashish
>>
>> On Fri, 2 Dec 2022 at 17:15, Elric StormBringer <
>> elr...@gm...> wrote:
>>
>>> thanks everyone, very grateful for all the helpful responses.
>>>
>>> perhaps to be a bit more clear; I was having problems with the FX Touch
>>> Barrier Options
>>> 1. When I specified the option is Vanilla European : option =
>>> ql.VanillaOption(payoff, exercise)
>>> -> using the option.impliedVolatility(premium, process) *** works ***
>>> [see screenshot below[/
>>>
>>> 2. But when I specify the product type to be anything else, it no longer
>>> works, and I just get error messages...
>>> -> so, is it we need more special arguments for non-European-Vanilla? Or
>>> does the Implied Volatility work *** ONLY *** for European Vanilla Options?
>>>
>>> [image: image.png]
>>>
>>>
>>>
>>>
>>> [image: image.png]
>>>
>>>
>>> On Fri, 2 Dec 2022 at 17:41, Ashish Bansal <ash...@gm...>
>>> wrote:
>>>
>>>> Hi Elric,
>>>>
>>>> If you want a basic code for barrier then I wrote 1 in this thread:
>>>> https://sourceforge.net/p/quantlib/mailman/message/37670218/
>>>>
>>>> can also refer to the examples of barrier options in this test suite if
>>>> you are comfortable with C++:
>>>>
>>>> https://github.com/lballabio/QuantLib/blob/master/test-suite/barrieroption.cpp
>>>>
>>>> Thanks
>>>> Ashish
>>>>
>>>> On Wed, 30 Nov 2022 at 18:21, Jonathan Sweemer <sw...@gm...>
>>>> wrote:
>>>>
>>>>> Hi Kiann,
>>>>>
>>>>> I'm surprised that the error you're seeing isn't related to the
>>>>> arguments you pass to BlackScholesProcess. You should be passing term
>>>>> structure objects instead of numerical values.
>>>>>
>>>>> See these links for more information:
>>>>>
>>>>> 1.
>>>>> https://quantlib-python-docs.readthedocs.io/en/latest/stochastic_processes.html#blackscholesprocess
>>>>> 2.
>>>>> https://github.com/lballabio/QuantLib-SWIG/blob/master/SWIG/stochasticprocess.i#L116-L121
>>>>> 3.
>>>>> https://stackoverflow.com/questions/4891490/calculating-europeanoptionimpliedvolatility-in-quantlib-python
>>>>>
>>>>>
>>>>>
>>>>> On Wed, Nov 30, 2022 at 8:13 PM Elric StormBringer <
>>>>> elr...@gm...> wrote:
>>>>>
>>>>>> Hi there, a noob using/investigationg QuantLib library via python.
>>>>>>
>>>>>> Great job there guys!
>>>>>>
>>>>>> I've been browsing the online documentation, but still having problem
>>>>>> trying to find the 'arguments' and 'fields' inside each.
>>>>>> For example, after I've defined the trade details/market-data/engine
>>>>>> for : ql.BarrierOption.impliedVolatility
>>>>>> -> I am trying to use .ImpliedVolatility.
>>>>>> -> However, I am getting errors from my input fields
>>>>>> : option_.impliedVolatility(0.01, ql.BlackScholesProcess(1.0, 0.0, 0.3))
>>>>>>
>>>>>> Can I check, what are the syntax of the data-fields to be used? The
>>>>>> error message is : impliedVolatility() missing 2 required positional
>>>>>> arguments: 'targetValue' and 'process'
>>>>>>
>>>>>> Kind regards
>>>>>> Kiann
>>>>>> _______________________________________________
>>>>>> QuantLib-users mailing list
>>>>>> Qua...@li...
>>>>>> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>>>>>>
>>>>> _______________________________________________
>>>>> QuantLib-users mailing list
>>>>> Qua...@li...
>>>>> https://lists.sourceforge.net/lists/listinfo/quantlib-users
>>>>>
>>>>
|