Fetching the Top Stock Movers for Free (with Python)

B/O Trading Blog
3 min readJun 17, 2022

--

Photo by Adam Smigielski on Unsplash.com

This post is a step-by-step tutorial on how to fetch the top daily stock movers for free using Python. This information has a variety of uses, for example to create trading ideas, generate notifications on specific stock activities or to build a top mover information page in your automated trading system.

This story is solely for general information purposes, and should not be relied upon for trading recommendations or financial advice. Source code and information is provided for educational purposes only, and should not be relied upon to make an investment decision. Please review my full cautionary guidance before continuing.

The Data Source

For this exercise were are going to fetch the data on the TradingView.com Top Movers and Gainers page from the ‘Overview’ tab.

As you can see we can fetch the following pieces of information:

  • Stock Ticker
  • Company Name
  • Last Price in USD
  • Change %
  • Change in USD
  • Technical Rating (Buy/Sell recommendation)
  • Volume
  • Volume Price
  • Market Cap
  • P/E
  • EPS and
  • Employees.

As you can see this information may be quite useful. For example, the buy/sell recommendations could be used to automatically trigger a buy or sell request using the ticker symbol. Not saying this is a good idea. Please do your own research.

For more premium content, check out my blog ‘The Algorithmic Trading Blog’.

Implementation

You can download the complete script from my blog ‘The Algorithmic Trading Blog’ here.

First we need to import the relevant Python libraries into our script. In case you haven’t already installed these packages, you can do so using the ‘pip’ package manager.

import requests
from bs4 import BeautifulSoup
import datetime
import pandas as pd

The next function fetches the webpage using the Python requests package and parses it using the Beautiful Soup package.

The data is parsed and placed into a DataFrame.

def fetch_top_stock_movers():
fetch_url = 'https://www.tradingview.com/markets/stocks-usa/market-movers-most-volatile/'
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:101.0) Gecko/20100101 Firefox/101.0',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
'Accept-Language': 'en-US,en;q=0.5',
'Accept-Encoding': 'gzip, deflate, br'
}
# Send GET request
request = requests.get(fetch_url, headers=headers)
soup = BeautifulSoup(request.text, 'lxml')
# Parse data
data = []
for item in soup.find_all('tr', class_='listRow'):
row = [
item.find_all('td')[0].getText(separator=' ').split(item.find('sup').text)[0].strip(), # Ticker
item.find('sup').text.replace('\"', ''), # Name
item.find_all('td')[1].text.replace('USD',''), # Last Price USD
item.find_all('td')[2].text.replace('%', ''), # Change %
item.find_all('td')[3].text.replace('USD', ''), # Change USD
item.find_all('td')[4].text, # Technical Rating
item.find_all('td')[5].text, # Volume
item.find_all('td')[6].text, # Volume Price
item.find_all('td')[7].text.replace('USD', ''), # Market Cap
item.find_all('td')[8].text.replace('USD', ''), # P/E
item.find_all('td')[9].text.replace('USD', ''), # EPS
item.find_all('td')[10].text # Employees
]
data.append(row)
df = pd.DataFrame(data, columns=['Ticker', 'Name','Last Price USD','Change %','Change USD', \
'Technical Rating','Volume','Volume Price','Market Cap', \
'P/E', 'EPS','Employees'])
return df

The next function just stores the DataFrame into a file with the current date as a file name.

def save_data(df):
today = datetime.date.today()
today_str = today.strftime("%Y-%m-%d")
file_name = f"{today_str}-top-stock-movers.csv"
df.to_csv(file_name)

The last function is the main() function, which calls the previous two functions in sequence.

# MAIN
if __name__ == '__main__':
# Fetch data
df = fetch_top_stock_movers()
# Save data
save_data(df)
print(f'Done fetching top movers.')

Output

Once assembled and executed, the script fetches the top 100 stock movers from the tradingview.com website along with the different pieces of information.

The output format is a DataFrame compatible csv, which you can easily reload using pd.read_csv() for further processing.

Here an example of what the output looks like:

And that’s it. Now you are able to fetch the top stock mover information automatically. Sweet!

I hope you found this post interesting and informative.

You can support my writing for free using this link.
Don’t miss another story — subscribe to my stories by email.

Have a great day!

--

--

B/O Trading Blog

Blogging about algorithmic trading, Python utilities and passive income opportunities.