How to Simulate Weighted Stock Investments in Python

Day 60 of 100 Days Coding Challenge: Python

When it comes to investing, I tend to lean toward the systematic approach. I like a plan. Not a “buy high, panic low” kind of plan, but a “how can I make $100 go as far as possible without needing a spreadsheet and a prayer” plan. Many stocks—especially ETFs—allow fractional share purchases, which is a blessing for anyone whose investing budget is less Wall Street and more wallet lint. That’s the beauty of modern investing: even if you don’t have a mountain of capital, you can still climb the hill.

So today I asked myself, what if my program could simulate that for me? What if I could say, “Here’s my $100,” and the program could allocate it automatically? Then I thought: what if I want to invest 25% in TSLA and only 5% in that random biotech company I’m not sure I trust? That’s where the idea of weighted allocation came in. A little money invested consistently can become something big over time—and the code now knows how to treat your dollars like they matter. Because they do.

Today’s Motivation / Challenge

You’ve probably heard some version of the advice “invest consistently, even with small amounts.” But how do you split that $100 in a smart, intentional way? Today’s challenge was about helping the program act like a friendly robo-advisor. Instead of dividing your money equally, it now lets you decide how much each stock gets. More TSLA, less risk. Or more risk, less cash. Either way, it’s your call—and the math is on your side.

Purpose of the Code (Object)

This new feature lets you simulate how much stock you could buy with a specific amount of money based on custom weight percentages. You choose the amount to invest, set the allocation weights for each stock, and the script calculates how many shares you could get, what it would cost, and how much remains. It’s budgeting, but for your imaginary portfolio.

AI Prompt: 

Create a Python function that takes a total investment amount and a custom percentage for each stock, then calculates how many shares can be purchased and the cost per stock using yfinance.

Functions & Features

  • Add or remove stock symbols from your portfolio
  • View real-time prices of all tracked stocks
  • Simulate investment allocation based on equal weights
  • Simulate investment allocation based on custom weight percentages
  • Check the price of a specific stock
  • Exit the program (option 99)

Requirements / Setup

pip install yfinance

Works with Python 3.8+. Use a virtual environment for clean setup.

Minimal Code Sample

def simulate_weighted_allocation():

    total = float(input(“How much to invest? $”))

    for symbol in stocks:

        weight = float(input(f”{symbol} weight (%): “))

        allocated = (weight / 100) * total

        shares = allocated / get_price(symbol)

        print(f”{symbol}: {shares:.4f} shares for ${allocated:.2f}”)

Takes your total investment and simulates purchasing based on custom weights.

Stock Tracker App

Notes / Lessons Learned

Initially, my simulation just split the money evenly—simple and safe. With 13 stocks in my portfolio, that reminded me of those index funds that allocate 1% to everything, like some kind of financial democracy. And while that’s a solid strategy (Vanguard would approve), I wanted control. I wanted to give TSLA a bigger slice of my $100 pie. So I added a custom weight input. Now the script lets me play mini portfolio manager.

One catch: if your percentages don’t add up to exactly 100, the program will throw a gentle tantrum. So you do have to plan ahead and know what you’re allocating. Otherwise, it’s like hosting a potluck and forgetting who brings dessert—awkward and disappointing. Still, once it worked, I thought: This is cool. It’s like mock investing with real math—and zero risk.

Optional Ideas for Expansion

  • Save your weight presets for recurring investment styles
  • Add error handling to automatically rebalance weights to 100%
  • Display a pie chart of your allocation with matplotlib

This marks the point where the stock tracker grows from a utility to a personal investing playground. And we’re only 60 days in.

Leave a Reply

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