Member-only story

Easiest Way To Get Best and Worst Performing Stocks in the S&P 500 using Python and YFinance

The Scraper Guy
3 min readAug 27, 2024

--

Easy method to download each stocks data and calculate the best and worst performers by percent change of yesterdays closing price.

Lets get started, first you will need to install Python and yfinance if you have not already.

I have created a google sheet with all the S&P data we need that can be found here

https://docs.google.com/spreadsheets/d/1537b2LeQG5LJmxdYJBycVG59tpmLNny8nqpERdY4Qx0/edit?usp=sharing

Download the sheet as a CSV and import the csv.

import yfinance as yf
import pandas as pd
df = pd.read_csv('D:/Downloads/SAndPCompanies.csv')

The dataframe should look like this

For this tutorial we just need the ticker symbols. We initialize an array and add each symbol to the array

sAndP500Data = []
for i in df['Symbol']:
sAndP500Data.append(i)
sAndP500Data

To download multiple tickers data from yfinance we need to convert the list to a string, which we can do using the following function

--

--

The Scraper Guy
The Scraper Guy

Written by The Scraper Guy

Teaching You How to Scrape Using Python LinkTree - https://linktr.ee/scrapingguy

Responses (1)