Python for Finance: Analyze Big Financial Data

(Elle) #1

next eight third Fridays of the month. At the end of March, there are futures with


maturities ranging from the third Friday of April to the third Friday of November. TTM in


the following pandas table represents time-to-maturity in year fractions:


In  [ 4 ]:  futures_data
Out[4]: DATE EXP_YEAR EXP_MONTH PRICE MATURITY TTM
496 2014-03-31 2014 4 17.85 2014-04-18 0.049
497 2014-03-31 2014 5 19.55 2014-05-16 0.126
498 2014-03-31 2014 6 19.95 2014-06-20 0.222
499 2014-03-31 2014 7 20.40 2014-07-18 0.299
500 2014-03-31 2014 8 20.70 2014-08-15 0.375
501 2014-03-31 2014 9 20.95 2014-09-19 0.471
502 2014-03-31 2014 10 21.05 2014-10-17 0.548
503 2014-03-31 2014 11 21.25 2014-11-21 0.644

The options data set is larger since at any given trading day multiple call and put options


are traded per maturity date. The maturity dates, however, are the same as for the futures.


There are a total of 395 call options quoted on March 31, 2014:


In  [ 5 ]:  options_data.info()
Out[5]: <class ‘pandas.core.frame.DataFrame’>
Int64Index: 395 entries, 46170 to 46564
Data columns (total 8 columns):
DATE 395 non-null datetime64[ns]
EXP_YEAR 395 non-null int64
EXP_MONTH 395 non-null int64
TYPE 395 non-null object
STRIKE 395 non-null float64
PRICE 395 non-null float64
MATURITY 395 non-null datetime64[ns]
TTM 395 non-null float64
dtypes: datetime64[ns](2), float64(3), int64(2), object(1)
In [ 6 ]: options_data[[‘DATE’, ‘MATURITY’, ‘TTM’, ‘STRIKE’, ‘PRICE’]].head()
Out[6]: DATE MATURITY TTM STRIKE PRICE
46170 2014-03-31 2014-04-18 0.049 1 16.85
46171 2014-03-31 2014-04-18 0.049 2 15.85
46172 2014-03-31 2014-04-18 0.049 3 14.85
46173 2014-03-31 2014-04-18 0.049 4 13.85
46174 2014-03-31 2014-04-18 0.049 5 12.85

As is obvious in the pandas table, there are call options traded and quoted that are far in-


the-money (index level much higher than option strike). There are also options traded that


are far out-of-the-money (index level much lower than option strike). We therefore want to


restrict the analysis to those call options with a certain (forward) moneyness, given the


value of the future for the respective maturity. We allow a maximum deviation of 50%


from the futures level.


Before we can start, we need to define a new column in the options_data DataFrame


object to store the results. We also need to import the functions from the script in


Example 3-1:


In  [ 7 ]:  options_data[‘IMP_VOL’] =   0.0
# new column for implied volatilities
In [ 8 ]: from bsm_functions import *

The following code now calculates the implied volatilities for all those call options:


In  [ 9 ]:  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
Free download pdf