- GuruFinance Insights
- Posts
- VIX and Stock Investment: Data Analysis with Python
VIX and Stock Investment: Data Analysis with Python
Dub app raises $17M for first regulated copy trading platform
On Dub, you don't pick the stocks you want to invest in. You pick the people you want to copy, with portfolios based on hedge fund managers, investing experts, and even law makers.
When you copy a portfolio on Dub, you execute the same trades as them automatically. Dub’s team hopes this will make investing more transparent and accessible.
Dub is SEC-registered, member FINRA. All deposits are SIPC-insured.
Not investment advice. Full disclosures here.
Exciting News: Paid Subscriptions Have Launched! 🚀
On September 1, we officially rolled out our new paid subscription plans at GuruFinance Insights, offering you the chance to take your investing journey to the next level! Whether you're just starting or are a seasoned trader, these plans are packed with exclusive trading strategies, in-depth research paper analysis, ad-free content, monthly AMAsessions, coding tutorials for automating trading strategies, and much more.
Our three tailored plans—Starter Investor, Pro Trader, and Elite Investor—provide a range of valuable tools and personalized support to suit different needs and goals. Don’t miss this opportunity to get real-time trade alerts, access to masterclasses, one-on-one strategy consultations, and be part of our private community group. Click here to explore the plans and see how becoming a premium member can elevate your investment strategy!
VIX refers to the Volatility Index (VIX Index) for options trading on the S&P 500, which is calculated and published by the Chicago Board Options Exchange (CBOE). It is commonly referred to as a “investor fear gauge” because it reflects investor sentiment, such as a sense of caution about the future of the market. This article explores how VIX can be utilized for stock investment using Python.
We are going to answer the following questions through this article.
How many days overall does the VIX spike occur?
What happens to stock prices when the VIX rises?
How good is the performance of an investment based on the VIX?
Photo by m. on Unsplash
1. Environment Setup
First install yfinance.
pip install yfinance
import yfinance as yf
import pandas as pd
import datetime
import matplotlib.pyplot as plt
%matplotlib inline
BofA says +80% of young, wealthy investors want this asset—now it can be yours.
A 2024 Bank of America survey revealed something incredible: 83% of HNW respondents 43 and younger say they currently own art, or would like to.
Why? After weathering multiple recessions, newer generations say they want to diversify beyond just stocks and bonds. Luckily, Masterworks’ art investing platform is already catering to 60,000+ investors of every generation, making it easy to diversify with an asset that’s overall outpaced the S&P 500 in price appreciation (1995-2023), even despite a recent dip.
To date, each of Masterworks’ 23 sales has individually returned a profit to investors, and with 3 illustrative sales, Masterworks investors have realized net annualized returns of +17.6%, +17.8%, and +21.5%
Past performance not indicative of future returns. Investing Involves Risk. See Important Disclosures at masterworks.com/cd.
2. Download Dataset
Here we download historical data of S&P500 and VIX using yfinance. We use all S&P 500 and VIX data since 1990 to today, when the VIX is available.
# duration
start = datetime.date(1990,1,1)
end = datetime.date.today()
# ticker
codelist=["^GSPC","^VIX"]
# display name
displist=["S&P500","VIX"]
# get data
data_stock = yf.download(codelist, start=start, end=end)["Adj Close"]
# data_stock.to_csv("VIX.csv")
# data_stock = pd.read_csv("VIX.csv", index_col=["Date"], parse_dates=True)
3. VIX index and market psychology: the emotion behind volatility
VIX generally rises when markets are in turmoil or when the economy is in a downturn. On the other hand, when stock prices are rising and no major fluctuations are expected, the VIX index tends to decline or remain stable at low levels. In other words, the VIX index is negatively correlated with stock price movements.
For example, in March 2020, when market sentiment deteriorated following the spread of the new coronavirus, the VIX index rose to a record high of 82.69.
The value of the VIX can be used to infer investor sentiment.
0–15
Generally, the market is optimistic15–20
Generally indicates market conditions are normal20–25
Generally indicates increasing market concern25–30
Generally indicates market turmoil30+
Generally indicates that the market is very turbulent
Now we check the proportion of each VIX value range.
# probability of each VIX value range
data_stock[codelist[1]].value_counts(bins=[0, 15, 20, 25, 30, 100], normalize=True)
The probability of the VIX being above 30 is 8.2%, which means that on average the market is in turmoil two days a month.
4. Plot Relationship between S&P500 and VIX
Draw a time-series plot of the VIX and the S&P 500 simultaneously to see how stock prices move when the VIX soars.
vix_num=30
fig, ax1 = plt.subplots(figsize=(20, 6),facecolor="white")
ax2 = ax1.twinx()
ax1.plot(data_stock[codelist[0]].dropna(),
color="red", label=displist[0],linewidth=2,alpha=0.5)
ax2.plot(data_stock[codelist[1]].dropna(),
color="blue", label=displist[1],linewidth=2,alpha=0.5)
handler1, label1 = ax1.get_legend_handles_labels()
handler2, label2 = ax2.get_legend_handles_labels()
ax1.legend(handler1 + handler2, label1 + label2, loc=2, borderaxespad=0.5)
ax1.tick_params(axis='x', labelrotation=45)
ax1.set_yscale("log")
ax1.set_ylim(data_stock[codelist[0]].min()*0.5,data_stock[codelist[0]].max()*1.1)
ax2.set_ylim(data_stock[codelist[1]].min()*0.9,data_stock[codelist[1]].max()*3.0)
ax2.axhline(y=vix_num, color="gray")
df=data_stock[data_stock[codelist[1]] >= vix_num]
for item in df.index:
ax1.axvline(item,color='limegreen',linewidth=1 ,alpha=0.1)
plt.show()
It can be seen that the VIX soars when there is a sharp drop in stock prices. Also, at a rough glance, the timing of the VIX spike looks pretty good.
6. Performance of buying S&P 500 when the VIX soared
If you had bought the S&P 500 on August 5, when the VIX was most recently above 30, you would have received a 12.8% return.
data_stock.tail(1)['^GSPC'].values / df.tail(1)['^GSPC'].values
# output: array([1.12819665])
So do we get the same good returns on other VIX soaring days?
We will examine the performance of the S&P 500 in a similar manner for the 722 days that the VIX has been above 30 so far. In the following, we calculate the performance of S&P500 one year after the day when VIX was over 30.
# 1 years later of date when VIX > 30
one_year_later = df.index + datetime.timedelta(days=365)
i = 0
one_year_later_adjusted = []
def check_date(date, trial=0):
if not date in data_stock.index:
if trial <= 4:
return check_date(date + datetime.timedelta(days=1), trial + 1)
else:
return date
for d in one_year_later:
d = check_date(d)
if d:
one_year_later_adjusted.append(d)
performance_one_year_later = data_stock.loc[one_year_later_adjusted]['^GSPC'].values / df.head(len(one_year_later_adjusted))['^GSPC'].values
pd.DataFrame(pd.Series(performance_one_year_later.ravel()).describe()).transpose()
plt.hist(performance_one_year_later, bins=10)
plt.ylabel("Frequency")
plt.xlabel("Performance (1 years)")
plt.show()
Surprisingly, an average return of 22.1% can be expected. The maximum return is 74. 7%, and the maximum loss is -41.5%.
Over a five-year period, the average return is 70.3%. VIX-aware investing is one of wise strategies.