0
$\begingroup$

I'm brand new to QuantLib and have the following class that I'm using to price European options:

import QuantLib as ql

class European_Option:
    def __init__(self, underlying_price, strike_price, maturity_date, risk_free_rate, implied_volatility, calculation_date):
        self.underlying_price = underlying_price
        self.strike_price = strike_price
        self.maturity_date = ql.Date(maturity_date, '%d-%m-%Y')
        self.risk_free_rate = risk_free_rate
        self.implied_volatility = implied_volatility
        self.calculation_date = ql.Date(calculation_date, '%d-%m-%Y')
        
        day_count = ql.Actual365Fixed()
        calendar = ql.UnitedStates()
        
        dividend_rate = 0.01

        option_type = ql.Option.Call
    
        payoff = ql.PlainVanillaPayoff(option_type, self.strike_price)
        exercise = ql.EuropeanExercise(self.maturity_date)
        european_option = ql.VanillaOption(payoff, exercise)
    
        spot_handle = ql.QuoteHandle(
            ql.SimpleQuote(self.underlying_price))

        flat_ts = ql.YieldTermStructureHandle(
            ql.FlatForward(self.calculation_date, 
                           self.risk_free_rate, 
                           day_count))
        
        dividend_yield = ql.YieldTermStructureHandle(
            ql.FlatForward(self.calculation_date, 
                           dividend_rate, 
                           day_count))
        
        flat_vol_ts = ql.BlackVolTermStructureHandle(
            ql.BlackConstantVol(self.calculation_date, 
                                calendar, 
                                self.implied_volatility, 
                                day_count))
        
        bsm_process = ql.BlackScholesMertonProcess(spot_handle, 
                                                   dividend_yield, 
                                                   flat_ts, 
                                                   flat_vol_ts)
        
    def get_price(self):
        european_option.setPricingEngine(ql.AnalyticEuropeanEngine(bsm_process))
        bs_price = european_option.NPV() / spot_price
        return bs_price
    
    def get_delta(self):
        delta = european_option.delta()
        return delta
    
    def get_gamma(self):
        gamma = european_option.gamma()
        return gamma
    
    def get_vega(self):
        vega = european_option.vega()
        return gamma
    
    def get_theta(self):
        theta = european_option.theta()

Next, I instantiate an object named foo as follows:

foo = European_Option(400,
                      500,
                      '15-06-2022',
                      0.01,
                      0.375,
                      '20-01-2022'
                        )

Then, I call the get_delta() method:

foo.get_delta()

I'm seeing the following error:

NameError: name 'european_option' is not defined

I have european_option defined as:

european_option = ql.VanillaOption(payoff, exercise)

So, I'm confused as to what's happening here.

My goal is to be able to use this class to be able to get the price and Greeks of thousands of options in a csv file (by creating a Pandas dataframe and then adding columns to the dataframe with the price and Greeks).

Can anyone help me with this?

Thanks!

$\endgroup$

1 Answer 1

1
$\begingroup$

This is not really a QuantLib related error.

You defined european_option locally in your __init__ function and then you are trying to access european_option locally in your get_delta() function, so they are not the same variable.

Use self.european_option = ql.VanillaOption(payoff, exercise) to define this variable in the scope of the class instance and then refer to it as self.european_option in your functions.

$\endgroup$
1
  • $\begingroup$ Are you suggesting that self.european_option = ql.VanillaOption(payoff, exercise) should be indented 4 spaces (i.e. not part of the __init__ function? If this is the case, then do all of the other variables that are currently under the self.x variables have to be moved out of the __init__ function, too? (sorry, I'm new to OOO paradigm) $\endgroup$
    – equanimity
    Jan 26, 2022 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.