Skip to content

Python

How to Identify Trending Stocks Using Python?

20 February 20254 min readPython
FabTrader author portrait

FabTrader

If you’re a trader or investor, identifying trending stocks can give you an edge in the market. Stocks in a strong uptrend often continue their momentum, while stocks in a downtrend may keep falling. In this guide, we'll show you how to find the top trending stocks using Python by calculating their trend slopes.

Understanding Stock Trends

A stock trend is determined by the direction in which its price is moving over a period of time. A positive slope indicates an uptrend (bullish momentum), while a negative slope signals a downtrend (bearish momentum). By using Linear Regression, we can calculate the trend slope for each stock and identify the top 10 uptrending and downtrending stocks.

Steps to Find Trending Stocks

  1. Fetch stock data using the yfinance library.
  2. Use linear regression to calculate the slope of closing prices over the last 30 days.
  3. Sort stocks based on their slope values.
  4. Filter out non-trending stocks (only consider positive slopes for uptrends and negative slopes for downtrends).
  5. Extract the top 10 uptrending and downtrending stocks.

More from Python