Extracting an Option Strike from a QuantLib Payoff
Summary
The document explains how to retrieve the strike embedded in a QuantLib European vanilla option. The strike belongs to the option’s payoff object, which can be accessed through the option’s payoff method. If the returned object is exposed as the generic payoff base type, Python cannot directly call the derived payoff’s strike method; QuantLib provides a helper to cast it to a plain vanilla payoff first.
The answers give two routes: access the strike method on a plain vanilla payoff, or first use QuantLib’s exported downcast helper when starting from the option object. The example uses a call with a strike of 130. This is a narrow API usage note: it addresses plain vanilla payoffs and does not discuss pricing, other payoff types, or broader option modeling.
Key ideas
- A vanilla option’s strike is stored in its payoff object.
- The option exposes that payoff through its payoff method.
- A generic payoff object may need downcasting to a plain vanilla payoff before accessing the strike.
- The strike accessor returns the value associated with the payoff.
Tags
Full text
# How can I extract the strike price from a Quantlib option object? # How can I extract the strike price from a Quantlib option object? I am trying to write a Python function that performs some calculations using a list of Quantlib options, and I would like to pass only that list without other information. In particular, the strike of each option is embedded there somewhere, but I am finding it hard to extract. Here is a toy example of an instance (extracted from one of Luigi Ballabio's excellent tutorial notebooks): ``` from QuantLib import * maturity_date = Date(15, 1, 2016) spot_price = 127.62 strike_price = 130 volatility = 0.20 # the historical vols for a year dividend_rate = 0.0163 option_type = Option.Call risk_free_rate = 0.001 day_count = Actual365Fixed() calendar = UnitedStates() calculation_date = Date(8, 5, 2015) Settings.instance().evaluationDate = calculation_date payoff = PlainVanillaPayoff(option_type, strike_price) exercise = EuropeanExercise(maturity_date) european_option = VanillaOption(payoff, exercise) ``` Now I would like to use something like `european_option.strike`. That doesn't work, of course. Can someone offer a simple solution? (TIA.) ## Answer by Luigi Ballabio (score 4, accepted) https://quant.stackexchange.com/a/75924 Very old question now, but here it is for reference in case someone stumbles upon it: the object returned by `european_option.payoff()` has type `ql.Payoff` (i.e., the base class for payoffs) and doesn't have the `strike` method. Now, Python doesn't do casts like C++, but for some objects we export functions that perform the underlying C++ cast and return the object with the correct derived type. This is one such case, and one can write: ``` ql.as_plain_vanilla_payoff(european_option.payoff()).strike() ``` to downcast the payoff and then retrieve the strike. ## Answer by amdopt (score 3) https://quant.stackexchange.com/a/54151 You can retrieve the strike from the PlainVanillaPayoff object which is `payoff` in your example. `payoff.strike()` returns `130.0` for me.
Shown in full with attribution under the source's licence. Licence: CC BY-SA 4.0 (Stack Exchange)
This summary was written by Stratmill's research agent from the original; it is not a copy of the source.