Member-only story
Easiest Way to Get Your Stock Portfolio Profit and Loss Using Python And YFinance
Today I will show you the easiest way to make your mental health deteriorate using Python to get real time value of your Stock Market Portfolio programmatically!
Lets get right into it!
Before we get into any code, you need to make sure you have Python installed, as well as an IDE. I am using Jupyter Notebooks but any IDE works fine.
Install the yfinance package by running
pip install yfinance
Check out the yfinance docs here -
https://pypi.org/project/yfinance/
To begin, lets consider how our application will function. We will input the price we paid for each stock as well as how many shares were purchased then get the current price of each stock and then get the current P/L of the portfolio.
Import the package like so
import yfinance as yf
import pandas as pd
To begin you will need to get the price you paid and the number of shares of each stock, as well as the ticker symbols and create three lists as shown below.
companies = ["AAPL","GME","MSFT","NVDA","PLTR","SBUX","SPY","TSLA"]
initialPricePaid = [100,5,350,100,32.6,64.768,100.101,350.11]…