Day 66 of 100 Days Coding Challenge: Python
I’ve added a new trick to my stock tracker: the moving average. Think of it as the “Zen master” of stock analysis—calm, collected, and uninterested in daily drama. I set it up so I can see both the short-term (7-day) moving average, which catches the mood swings, and the long-term (30-day) moving average, which reveals the bigger picture. Short-term trends tell you if a stock is suddenly sprinting, while long-term averages show if it’s been strolling uphill—or slowly rolling downhill—over time.
In finance, we do this on all sorts of timelines: monthly, quarterly, yearly. I’m not a day trader, so this isn’t about chasing every market twitch. Instead, this helps me filter out noise and focus on the broader trend—much like ignoring the gossip at the office and paying attention to the quarterly earnings report. In the future, I’d like to use this to spot “support” and “resistance” levels—that is, the invisible floor and ceiling where prices tend to bounce.
Today’s Motivation / Challenge
Daily stock prices are a roller coaster. Fun if you like screaming every 3 seconds, exhausting if you actually want to get somewhere. Moving averages smooth out the ride, letting you see if the track is going up, down, or looping sideways.
Purpose of the Code (Object)
This feature takes the recent stock prices for each symbol, calculates the 7-day and 30-day averages, and compares them to the most recent closing price. The result: a quick glance at short-term momentum versus long-term direction, without getting blinded by daily price noise.
AI Prompt
“Add a function to track the 7-day and 30-day moving averages of listed stocks, comparing each to the last closing price, and display the results clearly.”
Functions & Features
- Calculates 7-day and 30-day simple moving averages (SMA).
- Compares latest price to SMA values.
- Displays trends for quick decision-making.
Requirements / Setup
pip install yfinance pandas
Minimal Code Sample
closes = hist[‘Close’]
last = float(closes.iloc[-1]) # Get most recent closing price
sma7 = closes.rolling(window=7).mean().iloc[-1]
sma30 = closes.rolling(window=30).mean().iloc[-1]
Calculates the last closing price and both moving averages.
Notes / Lessons Learned
The first time I ran this, Pandas wagged its finger at me with a “FutureWarning” about float(series). Apparently, in Pandas 2.2+, turning a whole Series into a single float is too vague. Imagine handing a librarian a 300-page book and saying, “Turn this into a number.” Which page, line, or word? Pandas now demands you be explicit:
- .iloc[0] → picks the first element.
- .item() → converts it directly to a Python number.
Once I cleaned up the code and restarted, it ran like a charm. And honestly, seeing my own program spit out these averages feels much better than refreshing some finance site.
Optional Ideas for Expansion
- Add a chart to visualize SMA crossovers.
- Let the user set custom time windows for moving averages.
- Include alerts when the short-term average crosses the long-term average.

