Stock Portfolio Trend Visualization Using Python, matplotlib

blog details
author


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, 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


Data Science & Machine Learning Couresa Course Recommendation


No Comment at the moment...