Find the highest moving (hidden) stocks of the day with Python
By: Dorien Herremans
Tags: Data Science, Python, Stock Market, Coding, Stocks

Image from unsplash.

Find the highest moving (hidden) stocks of the day with Python

There are more stocks than just the SP500! In this article we will explore Python code that will unveil fast moving small gem stocks…

The stock market is a fast moving place, especially in these volatile times. While we often hear about the most popular stocks (the so called SP500), there are many other stocks out there. Some small, some fast rising, some are new IPOs, and some are fast crashing… In this article, I will share some useful Python code that I’ve written to dive deep into the most unknown stocks and find those fast movers / high risers…

We will make use of a library called yfinance, that will provide us with historic stock data and import pandas.

# imports

!pip install yfinance

import yfinance as yf

from pandas_datareader import data as pdr

import pandas as pd

As we don’t just want to explore the most well known stock, we will load a complete list of all Nasdaq stocks:

# list all stocks

url = “ftp://ftp.nasdaqtrader.com/SymbolDirectory/nasdaqlisted.txt"

df=pd.read_csv(url, sep=”|”)

print(df.head())

print(df['Symbol'].head())

print(len(df['Symbol']))

This has provided us with a variable (df) which contains 3,549 stock symbols!

So how do we get the high movers of today? Let’s start by retrieving recent history for each stock in our list and storing it in movementlist

I first define a helper function that will allow us to quickly lookup values from our dataframe with error checking.

def lookup_fn(df, key_row, key_col):

try:

return df.iloc[key_row][key_col]

except IndexError:

return 0

I look back 5 days. You can change the time period in the command hist = thestock.history(period=”5d”)

We populate the list of movement (stock symbol, low over the time period, high over the time period).

movementlist = [] for stock in df['Symbol']:

# get history

thestock = yf.Ticker(stock)

hist = thestock.history(period="5d") # print(stock) low = float(10000)

high = float(0) # print(thestock.info) for day in hist.itertuples(index=True, name='Pandas'): if day.Low < low:

low = day.Low

if high < day.High:

high = day.High



deltapercent = 100 * (high - low)/low

Open = lookup_fn(hist, 0, "Open") # some error handling:

if len(hist >=5):

Close = lookup_fn(hist, 4, "Close")

else :

Close = Open if(Open == 0):

deltaprice = 0 else:

deltaprice = 100 * (Close - Open) / Open print(stock+" "+str(deltapercent)+ " "+ str(deltaprice))

pair = [stock, deltapercent, deltaprice]

movementlist.append(pair)

Now that we have populated our list of stock movement, we can move on to filtering out the highest movers. If you just want to get a quick list of those who moved more then 100%:

for entry in movementlist:

if entry[1]>float(100):

print(entry)

If we are interested in exploring some of these stocks a bit more in detail we’ll want some more info, like the sector they belong too. Here is how we properly clean up the list:

# High risers:

def lookup_stockinfo(thestock):

try

return thestock.info

except IndexError:

return 0 cutoff=float(80) for entry in movementlist:

if entry[2]>cutoff:

print("

"+ str(entry))

thestock = yf.Ticker(str(entry[0])) if entry[0]=='HJLIW':

print("no info")

else:

a = lookup_stockinfo(thestock) if a == 0:

print("no info")

else:

if a is None:

print("no info")

else:

if a == "":

print("no")

else:

print(a)

print('Up '+ str(entry[2]) + "%")

print(str(a['sector']))

print(str(a['longBusinessSummary']))

print("year high "+ str(a['fiftyTwoWeekHigh']))

The result is a detailied overview with some information on the highest movers of the last 5 days. Very interesting to detect some fast moving stocks for speculators or just for anyone wanting to find a hidden gem stock!