How to Find the Worst-Performing Stocks in Your Portfolio Using Python

Day 72 of 100 Days Coding Challenge: Python

Yesterday, I was busy shining the spotlight on my star performers—the stocks that strutted across the stage as Broadway leads. Today, it was time to peek behind the curtain and see who was tripping over their own shoelaces. Enter: the “Worst-Performing Stock Tracker” function.

The logic is the same as yesterday’s “Best Performer” feature, but instead of crowning heroes, this one identifies the stragglers. Why bother? Because good portfolio management isn’t just about celebrating your winners—it’s also about recognizing when something is quietly dragging you down. I use it as a reminder to rebalance, pairing losses with gains. Thanks to yesterday’s groundwork, adding this feature felt like running downhill instead of uphill.

Today’s Motivation / Challenge

This project matters because no portfolio is a utopia of green arrows. Every investor has that one stock sitting in the corner sulking, ruining the vibe. By building a tool that identifies these underperformers, you can make better decisions: cut your losses, reallocate funds, or at least brace yourself with an extra cup of coffee before checking your account.

Purpose of the Code (Object)

The code checks your portfolio over the past X days (7, 14, or 30) and identifies the worst performer. It then neatly lists the bottom 10, so you can see at a glance which ones are dragging your imaginary—or real—portfolio down. Think of it as a “naughty list” for your investments.

AI Prompt

Add a function to show Worst Performer (last X days). Identify the stock dragging your (imaginary) portfolio down and display the bottom 10 over 7, 14, or 30 days.

Functions & Features

  • Add, remove, and view tracked stocks.
  • Fetch real-time prices.
  • Check best and worst performers over selectable periods.
  • Simulate portfolio performance and rebalance strategies.

Requirements / Setup

  • Python 3.9+

Install dependencies:

pip install yfinance pandas matplotlib

Minimal Code Sample

def worst_performer_last_x_days(symbols, days=7):

    results = []

    for sym in symbols:

        closes = _get_closes_1d(sym, period_days=days+5)

        if closes is None or len(closes) < days + 1:

            continue

        start, end = float(closes.iloc[-(days+1)]), float(closes.iloc[-1])

        pct = (end – start) / start * 100

        results.append({“symbol”: sym, “pct”: pct})

    return sorted(results, key=lambda r: r[“pct”])[:10]  # bottom 10

This function sorts your portfolio by performance and hands you the 10 worst offenders.

Stock Tracker

Notes / Lessons Learned

I made sure the function pulls from the same stocks.json file used by other features, keeping everything consistent. That’s like making sure all your socks match before heading out—details matter.

What surprised me? Debugging an error in my “simulate portfolio value” function. I suspect it’s tied to the line grabbing the first close price, but that’s a mystery for another day. The fun (and slightly maddening) part of coding is how today’s victory sometimes reveals tomorrow’s bug. Keeps life interesting, doesn’t it?

Optional Ideas for Expansion

  • Add a “Worst Sector” view to see which industry is letting you down the most.
  • Build a feature that pairs losers with winners for auto-rebalancing.
  • Compare your bottom 10 to the S&P 500 and see if misery loves company.

Leave a Reply

Your email address will not be published. Required fields are marked *