How to Track Stock Prices Using Python and yFinance

Day 38 of 100 Days Coding Challenge: Python

Investing has always been a guilty pleasure of mine—right up there with overpriced coffee and late-night book binges. Back when I barely had enough money to buy lunch, I was already trying to buy tiny slices of companies like I was collecting Pokémon cards. Fast forward to today, and every bank app practically throws stock charts at you like confetti. Still, I wanted something that was mine—a tiny, personal stock tracker. One that didn’t nag me about “low balances” or “risky decisions.” So, today’s project? A DIY stock price tracker built with Python. Just me, a graph, and the ever-fluctuating mood swings of the market.

Today’s Motivation / Challenge

There’s something oddly thrilling about watching a stock price graph crawl across your screen like a timid squirrel in traffic. Whether you’re new to investing or just want to see if your gut instinct about Tesla was right last week, tracking prices can be both educational and addictive. Plus, building your own tracker means you’re not relying on whatever clunky UI your bank thinks is “innovative.”

Purpose of the Code (Object)

This simple Python program fetches historical stock prices and plots them in a neat, interactive graph. You enter the stock symbol and date range, and it shows you the closing prices over time—no logins, no ads, no “upgrade to premium” pop-ups.

AI Prompt: 

 “Write a Python script that tracks a stock’s historical closing price over a specified date range and plots it using matplotlib.”

Functions & Features

  • Accepts a stock ticker symbol and a date range
  • Fetches stock data using Yahoo Finance
  • Plots closing prices in an interactive graph
  • Lets you zoom in, pan, and even save the chart as an image

Requirements / Setup

bash

CopyEdit

pip install yfinance matplotlib pandas

Minimal Code Sample

python

CopyEdit

import yfinance as yf

import matplotlib.pyplot as plt

data = yf.download(“AAPL”, start=”2024-01-01″, end=”2024-06-30″)

plt.plot(data.index, data[“Close”])

plt.title(“AAPL Closing Price”)

plt.show()

Fetches Apple’s closing prices and plots them—simple and sweet.

Stock Price Tracker

Notes / Lessons Learned


Today’s hiccup was classic: wrong environment, right idea. I had dutifully created a virtual environment and installed everything I needed—yfinance, matplotlib, and pandas. But when I launched Visual Studio Code, my eager fingers opened the wrong project folder. I ran the code. Boom. Error. No yfinance to be found. After a brief existential crisis and a quick switch to the correct environment, the code ran beautifully. A delightful little pop-up window showed me a clean graph of stock price movements. I could zoom in, pan around, even save it as a PNG. It felt like giving my inner stock nerd a new toy to play with. And yes—I fully plan to turn this into a functional tracker for my own portfolio. No offense to my bank’s app, but mine’s cooler.

Optional Ideas for Expansion

  • Add a moving average overlay to smooth out wild price swings
  • Include volume bars below the price graph for more context
  • Let the user compare two or more stocks on the same chart