If you are new to Stock Options Vega and Implied Volatility, I would recommend visiting the following notebooks first...
Demystifying Stock Options Vega Using Python
Calculate Implied Volatility of Stock Option Using Python
We will be using following modules to do this excercise.
- pymongo (assuming mongodb is already installed)
- opstrat Python library to calculate option geeks using Black Scholes
- Python Matplotlib
For this excercise let us look at Tesla options. We will plot
- Todays date is Jan 10, 2023
- TSLA Jan 20 2023 120 Call
Let us grab the data for the last 20 days.
importpymongoimportdatetimefrompymongoimportMongoClientclient=MongoClient('mongodb://localhost:27017')db=client['stocktwits']expirationDate=datetime.datetime.strptime("2023-01-20","%Y-%m-%d")options=list(db.tdameritrade.find({'contractName':'TSLA',\
'strike':120.0,\
'option_type':'call',\
'expirationDate':expirationDate},\
{'bid':1,'ask':1,'last':1,'daysToExpiration':1,\
'description':1,'strike':1,\
'volatility':1,r'vega':1,'added':1})\
.sort([('added',pymongo.DESCENDING)]).limit(20))
Let us look at our data now.
options[0]
{'_id': ObjectId('63bd7e41458ed237d277d4a3'), 'description': 'TSLA Jan 20 2023 120 Call', 'bid': 4.8, 'ask': 4.9, 'last': 4.85, 'volatility': 70.574, 'vega': 0.08, 'daysToExpiration': 10, 'strike': 120.0, 'added': datetime.datetime(2023, 1, 10, 14, 0, 2, 900000)}
optionsVega=[]optionsVI=[]#implied volatilitydates=[]foroptioninoptions:optionsVega.append(option['vega'])foroptioninoptions:optionsVI.append(option['volatility'])foroptioninoptions:dates.append(option['added'])
importmatplotlib.pyplotaspltimportmatplotlib.datesasmdates# Create the figure and the first axisfig,ax1=plt.subplots()ax1.plot(dates,optionsVI,'b-')ax1.set_xlabel('Date')ax1.set_ylabel('Implied Volatility',color='b')ax1.tick_params('y',colors='b')# Create the second axisax2=ax1.twinx()ax2.plot(dates,optionsVega,'r-')ax2.set_ylabel('Vega',color='r')ax2.tick_params('y',colors='r')# format the x-axisax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))fig.autofmt_xdate()# Set the interval of ticks to be displayedax1.xaxis.set_major_locator(mdates.WeekdayLocator())# Add a titleplt.title('Implied Volatility, Vega vs Date')# Show the plotplt.show()

It is hard to figure out what is going on in above the plot without overlaying the stock price. Let us do that next.
stockprices=list(db.eod_stock_data.find({'ticker':'TSLA'}).sort([('date',pymongo.DESCENDING)]).limit(20))
stockprices[0]
{'_id': ObjectId('63bdb5bd458ed2765145ff69'), 'close': 117.39, 'open': None, 'high': None, 'low': None, 'volume': 113202654, 'date': datetime.datetime(2023, 1, 10, 0, 0), 'adjusted_close': None, 'source': 'tmpvalues', 'ticker': 'TSLA', 'perchange': -1.99}
prices=[]forstockinstockprices:prices.append(stock['close'])
importmatplotlib.pyplotaspltimportmatplotlib.datesasmdates# Create the figure and the first axisfig,ax1=plt.subplots(figsize=(12,8))ax1.plot(dates,optionsVI,'b-')ax1.set_xlabel('Date')ax1.set_ylabel('Implied Volatility',color='b')ax1.tick_params('y',colors='b')# Create the second axisax2=ax1.twinx()ax2.plot(dates,optionsVega,'r-')ax2.set_ylabel('Vega',color='r')ax2.tick_params('y',colors='r')# Create the third axisax3=ax1.twinx()ax3.spines["right"].set_position(("axes",1.2))ax3.plot(dates,prices,'g-')ax3.set_ylabel('stock price',color='g')ax3.tick_params('y',colors='g')# format the x-axisax1.xaxis.set_major_formatter(mdates.DateFormatter('%Y-%m-%d'))fig.autofmt_xdate()# Set the interval of ticks to be displayed#ax1.xaxis.set_major_locator(mdates.WeekdayLocator())# Add a titleplt.title('Implied Volatility, Vega vs Date,for option Jan20, $120 call')# Show the plotplt.show()

This plot shows us how vega of the option is moving with respect to stock price. We can conclude that when the TESLA stock was at 170, at that time our Jan 20 call option of strike \$120 was in the money. At this time, the vega was around 0.08. On the other side, when the stock comes near \$120 which was in the beginning of January, the vega of option was very high but as the stock price dipped below \$120 then vega of option was low again. Therefore we can say that vega was higher when the option strike was at the money.