DEV Community

Cover image for Building a Stock Market Predictor: What Worked and What I'd Change
JashMehulShah
JashMehulShah

Posted on

Building a Stock Market Predictor: What Worked and What I'd Change

When I started this project, I wanted to see how far I could get with a simple linear regression model trying to forecast stock closing prices — and turn that into a basic buy/sell signal. Here's what I learned.

My Approach
Data: Historical stock/index data pulled live via the yfinance API (Yahoo Finance)
Model: Linear regression on historical price data — chosen deliberately as a baseline before reaching for anything more complex
Output: Visualized price trends, predicted future closing prices, and a simple buy/sell recommendation derived from predicted movement
Evaluation: Compared the model's predicted closing prices against actual prices to see how closely the trend line tracked reality, and checked whether the buy/sell signal would have pointed the right direction over the test period
python
`# Core idea: fit a linear regression on historical closing prices
model = LinearRegression()
model.fit(X_train, y_train)

Evaluate on held-out data

test_predictions = model.predict(X_test)
rmse = np.sqrt(mean_squared_error(y_test, test_predictions))
Results`

On a held-out test set (20% of the data, unseen during training), the model's RMSE was ~3,575 index points — against a BSE Sensex trading in the 50,000–85,000 range over this period, that's roughly a 4-5% average error. Not something you'd trade on, but a reasonable fit for a straight-line model.

The more interesting finding came from the buy/sell logic: the model recommends BUY for every timeframe I tested — 10 days, 1 month, even 1 year out. At first that looked like a bug. It isn't — it's a direct consequence of the model. Since linear regression fits a single straight line to Days vs. Close, and the Sensex trended upward across my 2020-2025 training window, the fitted slope is positive. A positive slope can only ever project further up. The model is structurally incapable of recommending SELL unless the entire historical window it was trained on was trending downward. That's a real limitation, not noise — and a useful thing to have discovered by testing it rather than just trusting the output.

Why This Matters to Me

This project is the seed of something I want to take further — I'm interested in applying ML to finance more seriously, and this was a first, honest attempt at seeing where a simple model's limits are. Building it taught me more about the limits of ML on noisy real-world data than any tutorial did.

Code for this project is on

``

Top comments (0)