Previously I shared the way to visualize daily pricing in a candle-type data format, here I would walk through how to visualize a collection of stock portfolios in a time series data format.
Tables of Content: Stock Portfolio Performance Trend Visualization Using Python
- Python Packages: matplotlib, yfinance
- Dataset required: datetime, specific pricing dataset of tickers
- Insert each ticker closing price data for plotting
- Full Python Script for visualizing stock portfolio performance trend
- Data Science & Machine Learning Couresa Course Recommendation
Python Packages: matplotlib, yfinance, pandas-datareader
Pip install finance
Pip install pandas-datareader
Pip install matplotlib
from pandas_datareader import data as pdr
import matplotlib.pyplot as plt
import yfinance as yf
Dataset required: datetime, specific pricing dataset of tickers
As well as the stock daily pricing visualization chapter, we need to implement fix-yahoo-finance library to replace broken yahoo-finance from pandas
yf.pdr_override()
Different from an individual ticker, portfolio tickers require to get the pricing dataset for each ticker. From my personal perspective, it’s great to add the security index pricing as well for adding an anchoring point. Here I add Nasdaq as my portfolio tickers are all from Nasdaq.
dataA = pdr.get_data_yahoo('NDAQ', startDate, endDate)
dataB = pdr.get_data_yahoo('TSLA', startDate, endDate)
dataC = pdr.get_data_yahoo('AMZN', startDate, endDate)
dataD = pdr.get_data_yahoo('AAPL', startDate, endDate)
Insert each ticker closing price data for plotting
Due to the different scale by each ticker, it's necessary to normalize it to 100 then insert each stock closing price data for plotting.
ax = (dataA['Close'] / dataA['Close'].iloc[0] * 100).plot(figsize=(15. , 6))
(dataB['Close'] / dataB['Close'].iloc[0]
* 100).plot(ax=ax, figsize=(15, 6))
plt.legend(['NASDAQ', 'Tesla', 'Amazon', 'Apple'], loc='upper left')
plt.show()
Full Python script for visualizing stock pricing and trading volume
- If you are interested in getting the full version of the script and vibe coding prompt mentioned in this article, please subscribe to our Youtube Channel and comment “stock trading” in the following video (Stock Trading Trend Script and Vibe Coding Prompt)
- Support our channel through PayPal (paypal.me/Easy2digital)
- Follow our Facebook page
- Sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code on Buyfromlo products and digital software