Day 75 of 100 Days Coding Challenge: Python
Today marked the final lap of my stock tracker project. To finish strong, I added one last function: checking my portfolio against the S&P 500. Think of it like racing your homemade go-kart against a Formula 1 car—suddenly, you know where you actually stand.
Most people use the ticker ^GSPC as their yardstick for the S&P 500. It’s a tidy snapshot of large-cap U.S. stocks. But here’s the trick: it only makes sense to compare apples to apples. If your portfolio is tilted toward bonds, emerging markets, or your cousin’s hot crypto tip, the S&P might not be your best match. Personally, I balance stocks and bonds between 70/30 and 50/50, depending on the season (and, frankly, my mood).
If your portfolio consists solely of ETFs, you could use SPY (an ETF that tracks the S&P 500). Want to check your bonds? Try a bond index ticker like AGG. The idea is simple: know what game you’re playing before you brag about winning.
Today’s Motivation / Challenge
Why bother with this comparison? Flying blind in investing is like bragging about your marathon time without realizing you ran half the distance. Benchmarking grounds you. It tells you whether your “brilliant strategy” is actually brilliant—or if you’d have been better off just buying the index and going fishing.
Purpose of the Code (Object)
This function aligns your portfolio’s performance with a benchmark (such as the S&P 500) from a date you choose. It then tells you how much you gained, what your growth rate looks like over time, and how painful your worst dip was. Finally, it plots a simple graph so you can see who’s winning: you or the index.
AI Prompt
Add to show the portfolio’s performance with a benchmark.
Functions & Features
- Compare your portfolio against a benchmark (default: S&P 500).
- Print stats: total return, growth rate, and max drawdown.
- Plot both curves on a chart starting at the same baseline.
- Flexible benchmark (SPY, AGG, or any valid ticker).
Requirements / Setup
- Python 3.10+
Install dependencies:
pip install yfinance matplotlib pandas
Minimal Code Sample
# Compare portfolio vs benchmark
port_ret = portfolio / portfolio.iloc[0] – 1
bench_ret = benchmark / benchmark.iloc[0] – 1
plt.plot(port_ret.index, (1 + port_ret) * 100, label=”Portfolio”)
plt.plot(bench_ret.index, (1 + bench_ret) * 100, label=”Benchmark”)
plt.legend()
plt.show()
Both lines start at 100, so you can see the race clearly.
Notes / Lessons Learned
This project was an absolute joy. Finance has fascinated me ever since my university days, when I was the nerd reading investment books for fun. Writing this code felt like merging that old passion with my new one: programming. There’s a strange comfort in watching your ideas become actual functions—little digital tools that reflect your understanding of the markets. I’m not a professional investor, but I can proudly say this project carries bits of my life’s learning stitched into its code.
Optional Ideas for Expansion
- Add dividends into the calculation for a more realistic comparison.
- Try benchmarking against multiple indexes at once (stocks, bonds, international).

