Build a Portfolio Performance Checker in Python

Day 71 of 100 Days Coding Challenge: Python

Today, I added a feature to crown the “best performer” from my portfolio. Think of it as a mini award ceremony where only stocks are invited and there’s no acceptance speech—just numbers. The function itself is simple: it checks which stocks in my holdings have gained the most over the last 7, 14, or 30 days. I don’t need this every day; daily stock swings are more like background noise than useful insight. In fact, when I first started, I used to check every single day—bad idea. The constant up-and-down did nothing but feed my anxiety. Now, I prefer to take a step back and do a weekly review, usually on weekends. That’s when I reflect, think strategically, and make any adjustments.

I’m more of a mid-term investor, not a hyperactive day trader, so this tool fits right into my rhythm. It’s like glancing at the scoreboard after a few innings rather than obsessing over every pitch.

Today’s Motivation / Challenge


The challenge was to build a function that helps me zoom out. Beginners (my past self included) often get caught up in every twitch of the market. But sometimes, what you need is a bird’s-eye view: Which of my stocks has been pulling its weight lately? This function answers that without the clutter of daily drama.

Purpose of the Code (Object)


This code identifies which stock in your portfolio had the biggest gain over a chosen period—7, 14, or 30 days. It’s essentially a quick performance check-up for your holdings, designed to keep you focused on trends rather than noise.

AI Prompt


Use the file uploaded, and add the function to see Best Performer (last X days): Identify the top gainer over the last 7, 14, or 30 days.

Functions & Features

  • Add and remove stock symbols from your portfolio
  • Track daily changes and simulate allocations
  • Plot historical charts and moving averages
  • Check diversification and sector exposure
  • New: Spot the best performer over 7, 14, or 30 days

 You’ll need Python 3 and a few common libraries:

pip install yfinance pandas matplotlib

Minimal Code Sample

winner, results = best_performer_last_x_days(symbols, days=7)

print(f”Best performer (7 days): {winner[‘symbol’]} {winner[‘pct’]:.2f}%”)

This snippet finds the top gainer in your current portfolio over the past 7 days.

Stock Tracker

Notes / Lessons Learned


Today, I learned how a tiny inconsistency can wreak havoc. My script is now over 1,300 lines, and I initially got nothing but “no results” because the function was pulling symbols from the wrong place. I mistakenly used get_symbols() from portfolio.json, while the rest of the app stores tickers in stocks.json through load_stocks(). Naturally, the poor function came up empty. The quick fix was to swap in load_stocks(). It reminded me of those group projects where one person keeps using a different spreadsheet version—chaos ensues. Moral of the story: keep your files consistent, keep your sanity intact.

Optional Ideas for Expansion

  • Add a “Worst Performer” twin function, so you know which stock deserves detention.
  • Compare your portfolio’s best performer against the S&P 500 for context.
  • Create a simple chart that ranks the top 5 movers for a more visual snapshot.

Leave a Reply

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