Calculating Implied Volatilities

I have been teaching myself finance using Python with the book Finance For Python.

I am stuck on chapter 3: Using a BSM Model and stock Options to create analytical Data.

One of the things that I am calculating is implied volatility but I am having issue with my syntax. I have tried editing my code, I have tried searching for answers, but my error code is primarily saying that my code was expecting a 'Boolean Array'.

I'm not sure how to solve this, and I've looked up and down the web, and I have tried editing my syntax, but nothing seems to give.

tol = 0.5 # Tolerance level for Moneyness
for option in options_data.index:
    # Iterating over all option quotes
    forward = futures_data[futures_data['MATURITY'] == 
                           options_data.loc[option]['MATURITY']['PRICE'].values[0]
        # picking the right futures value
    if (forward * (1- tol) < options_data.loc[option]['STRIKE']
                        < forward * (1 + tol)):
        # only for options with moneyness within tolerance
        imp_vol = bsm_call_imp_vol(
                V0, #VSTOXX value
                options_data.loc[option]['STRIKE']
                options_data.loc[option]['TTM']
                r, #short rate
                options_data.loc[option]['PRICE']
                sigma_est=2,
                it=100)
        options_data['IMP_VOL'].loc[option] = imp_vol

File "<ipython-input-82-705e5cdb6e01>", line 8
    < forward * (1 + tol)):
                          ^
SyntaxError: invalid syntax

^My ERROR, However it's not the syntax, because I've taken away the colon, the parenthesis and once I do that then the issue becomes my

imp_vol = bsm_call_imp_vol(

Line as a Syntax Error, which I know it's not. And yesterday trying to solve this issue Jupyter Notebooks was declaring that it was a Boolean Array issue dealing with this part of my script

if (forward * (1- tol) < options_data.loc[option]['STRIKE']
                            < forward * (1 + tol)):

as far as an input, trying to plot my data in matplotlib then returns this. Here is my train of thought followed by computers Response

import matplotlib.pyplot as plt
%matplotlib inline
plt.figure(figsize=(8, 6))
for maturity in maturities:
    data = plot_data[options_data.MATURITY == maturity]
        # SELECT DATA FOR THIS MATURITY
    plt.plot(data['STRIKE'], data['IMP_VOL'],
            label=maturity.date(), lw=1.5)
    plt.plot(data['STRIKE'], data['IMP_VOL'], 'r.')
plt.grid(True)
plt.xlabel('strike')
plt.ylabel('implied volatility of volatility')
plt.legend()
plt.show()

<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]
<ipython-input-81-20d894c2707c>:5: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
  data = plot_data[options_data.MATURITY == maturity]


DATE    EXP_YEAR    EXP_MONTH   TYPE    STRIKE  PRICE   MATURITY    TTM IMP_VOL
46170   2014-03-31  2014    4   C   1.0 16.85   2014-04-18  0.049   0.0
46171   2014-03-31  2014    4   C   2.0 15.85   2014-04-18  0.049   0.0
46172   2014-03-31  2014    4   C   3.0 14.85   2014-04-18  0.049   0.0
46173   2014-03-31  2014    4   C   4.0 13.85   2014-04-18  0.049   0.0
46174   2014-03-31  2014    4   C   5.0 12.85   2014-04-18  0.049   0.0



<class 'pandas.core.frame.DataFrame'>
Int64Index: 395 entries, 46170 to 46564
Data columns (total 8 columns):
 #   Column     Non-Null Count  Dtype         
---  ------     --------------  -----         
 0   DATE       395 non-null    datetime64[ns]
 1   EXP_YEAR   395 non-null    int64         
 2   EXP_MONTH  395 non-null    int64         
 3   TYPE       395 non-null    object        
 4   STRIKE     395 non-null    float64       
 5   PRICE      395 non-null    float64       
 6   MATURITY   395 non-null    datetime64[ns]
 7   TTM        395 non-null    float64       
dtypes: datetime64[ns](2), float64(3), int64(2), object(1)
memory usage: 27.8+ KB


from Recent Questions - Stack Overflow https://ift.tt/3e0xQAL
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Spring Elasticsearch Operations

Network Error and Timeout on Authorize.net JS

Object oriented programming concepts (OOPs)