Files
strategy-lab/to_explore/pyquantnews/115_PandasMarketDataAnalysis.ipynb
David Brazda e3da60c647 daily update
2024-10-21 20:57:56 +02:00

198 KiB

No description has been provided for this image

Load historical stock data for a given ticker symbol

We start by loading historical stock data for NVIDIA (ticker symbol "NVDA") from Yahoo Finance. This helps us get the historical prices of the stock to perform further analysis.

In [12]:
import math
import pandas as pd
import yfinance as yf
import matplotlib.pyplot as plt
In [2]:
data = yf.download("NVDA", start="2023-01-01")
/Users/jason/anaconda3/envs/newsletter/lib/python3.10/site-packages/yfinance/utils.py:775: FutureWarning: The 'unit' keyword in TimedeltaIndex construction is deprecated and will be removed in a future version. Use pd.to_timedelta instead.
  df.index += _pd.TimedeltaIndex(dst_error_hours, 'h')
[*********************100%%**********************]  1 of 1 completed

This code uses the yfinance library to download stock data for NVIDIA starting from January 1, 2023. The data is stored in a pandas DataFrame named data. This DataFrame contains various columns such as 'Open', 'High', 'Low', 'Close', 'Volume', and 'Adj Close', which represent different aspects of the stock's daily performance.

Display the first few rows of the data

Next, we display the first few rows of the data to understand its structure and the kind of information it holds.

In [3]:
data.head()
Out[3]:
<style scoped=""> .dataframe tbody tr th:only-of-type { vertical-align: middle; } .dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; } </style>
Open High Low Close Adj Close Volume
Date
2023-01-03 14.851 14.996 14.096 14.315 14.305580 401277000
2023-01-04 14.567 14.853 14.241 14.749 14.739294 431324000
2023-01-05 14.491 14.564 14.148 14.265 14.255615 389168000
2023-01-06 14.474 15.010 14.034 14.859 14.849223 405044000
2023-01-09 15.284 16.056 15.141 15.628 15.617717 504231000

The head() function in pandas displays the first five rows of the DataFrame by default. This allows us to quickly inspect the data and verify that it has been loaded correctly. We can see the dates as the index and the columns representing different metrics such as opening price, closing price, highest price, lowest price, and volume for each trading day.

Calculate moving averages and other indicators

Now, we calculate the 20-day and 50-day simple moving averages (SMA) of the stock's closing price. We also calculate the historical volatility and the percentile rank of the closing prices.

In [4]:
data['SMA20'] = data['Close'].rolling(window=20).mean()
In [5]:
data['SMA50'] = data['Close'].rolling(window=50).mean()
In [16]:
data['vol'] = data['Close'].pct_change().rolling(window=21).std() * math.sqrt(252)
In [7]:
data['pct_rank'] = data['Close'].rolling(window=21).rank(pct=True)

We calculate the 20-day and 50-day simple moving averages by using the rolling method with a specified window and then applying the mean function. These moving averages help identify trends in the stock price. We compute historical volatility by taking the standard deviation of the closing prices over a 21-day window and then annualizing it using the square root of 252 (number of trading days in a year). Finally, the percentile rank of the closing prices over a 21-day window is calculated using the rank method with the pct parameter set to True, giving us a value between 0 and 1.

Plot the stock's closing price and moving averages

Next, we plot the stock's closing price along with the 20-day and 50-day simple moving averages to visualize trends and patterns.

In [8]:
to_plot = ["Close", "SMA20", "SMA50"]
In [9]:
data[to_plot].plot()
Out[9]:
<Axes: xlabel='Date'>
No description has been provided for this image

We store the columns to be plotted (closing price, 20-day SMA, and 50-day SMA) in a list named to_plot. Using pandas' plot method, we create a line plot for these columns. This visual representation helps us see how the stock's price moves relative to its short-term and long-term trends defined by the moving averages.

Plot historical volatility

We then plot the historical volatility of the stock to understand its risk and price fluctuations over time.

In [20]:
ax = data["Close"].plot()
data.vol.plot(secondary_y=True, ax=ax)
plt.show()
No description has been provided for this image
In [ ]:
 
In [ ]:
 

This code creates a line plot showing both the closing price and the computed historical volatility. By plotting these together, we can see how the stock's volatility changes over time and how it correlates with the price movements. Higher volatility indicates higher risk and larger price swings, while lower volatility suggests more stable price behavior.

Plot the percentile rank over time

Finally, we plot the percentile rank of the closing prices to see how the current price compares to its historical range.

In [11]:
data['pct_rank'].plot()
Out[11]:
<Axes: xlabel='Date'>
No description has been provided for this image

We use the plot method to visualize the percentile rank of the closing prices. This plot shows how the current price ranks within a specified window of past prices. A high percentile rank indicates that the current price is relatively high compared to its historical range, while a low percentile rank suggests that it is relatively low. This can help identify overbought or oversold conditions in the stock.

Your next steps

To deepen your understanding, try changing the stock ticker symbol to analyze a different company's data. You can also experiment with different time windows for the moving averages and volatility calculations to see how these changes affect the results.

PyQuant News is where finance practitioners level up with Python for quant finance, algorithmic trading, and market data analysis. Looking to get started? Check out the fastest growing, top-selling course to get started with Python for quant finance. For educational purposes. Not investment advise. Use at your own risk.