Stock Price Prediction Application

Inspiration

This project was inspired by the challenge of predicting stock prices in a volatile market, particularly for high-growth or speculative stocks like CELH, CVNA, UPST, ALT, and FUBO. I wanted to explore how combining technical indicators, fundamental data, macroeconomic factors, and news sentiment could improve forecasting accuracy. The idea stemmed from a desire to create a practical tool for investors or traders, blending my interest in machine learning with real-world financial applications.

What I Learned

  • Hybrid Modeling: I learned to integrate LSTM (for time-series data) with dense layers (for static fundamental features) using PyTorch, creating a HybridModel that leverages multiple data types.
  • Feature Engineering: Calculating technical indicators (e.g., RSI, MACD, Bollinger Bands) and incorporating sentiment analysis from news taught me the importance of diverse feature sets.
  • API Integration: Working with yfinance and fredapi showed me how to fetch and align real-time financial and economic data.
  • Streamlit: Building an interactive dashboard was my first hands-on experience with Streamlit, making complex predictions accessible to users.
  • Challenges of Stock Prediction: The mixed accuracy results (e.g., 90% for UPST vs. 14% for CVNA) highlighted the unpredictable nature of stock markets and the limits of historical data alone.

How I Built It

  1. Data Collection:

    • Used yfinance to download stock price data and fundamentals (e.g., PE ratio, debt-to-equity) for a specific period (Jan 21, 2024 - July 21, 2024).
    • Fetched macroeconomic data (interest rates, inflation) via fredapi.
    • Extracted news sentiment using yfinance’s news feature, processed with TextBlob and VADER.
  2. Feature Engineering:

    • Calculated technical indicators like moving averages, volatility, RSI, and MACD using Pandas.
    • Added macroeconomic features (e.g., interest rates, VIX) and sentiment scores to enrich the dataset.
  3. Modeling:

    • Built a HybridModel in PyTorch combining LSTM (for technical time-series) and dense layers (for fundamentals).
    • Trained the model on sequences of technical data (30-day windows) and static fundamental features, using MSE loss and Adam optimizer.
    • Saved trained models to disk for reuse.
  4. Prediction & Visualization:

    • Generated predictions with the trained HybridModel and saved them as CSV files.
    • Created a Streamlit dashboard to let users select stocks, trigger predictions, and view results with Plotly charts.
  5. Structure:

    • Modularized the code into data_loader.py (data fetching), features.py (feature engineering), models.py (model definitions), train.py (training), and predict.py (inference), with a main script to tie it all together.

Challenges Faced

  • Data Alignment: Ensuring price data, news, and macroeconomic indicators aligned correctly over the date range (Jan 21 - July 21, 2024) was tricky. I used Pandas’ reindex and forward-fill to handle gaps.
  • Model Accuracy: The huge variance in prediction accuracy (e.g., CVNA at 14.7% vs. UPST at 90.1%) suggested overfitting or missing key features for volatile stocks. CVNA’s wild deviation was a puzzle—possibly due to a market event my model didn’t capture.
  • Column Naming: The Streamlit app crashed due to a KeyError: 'predicted' because my predict_stock function named the output column prediction. I had to adapt the app to handle flexible column names.
  • Feature Availability: Some fundamental data (e.g., PE ratio) was missing for certain stocks, requiring imputation with defaults, which might have skewed predictions.
  • Computational Resources: Training the hybrid LSTM model for multiple stocks with 100 epochs was resource-intensive, pushing me to optimize batch sizes and validation splits.

Next Steps

  • Improve CVNA/CELH Predictions: Investigate market events or add more features like social media sentiment (e.g., X posts) to capture volatility.
  • Ensemble Refinement: Weight models by performance or integrate Prophet/RandomForest more effectively with the LSTM.
  • Real-Time Data: Extend the app to fetch live data for ongoing predictions.
  • User Experience: Enhance the Streamlit UI with date range selectors and more detailed error messages.

This project was a deep dive into financial machine learning, blending theory with practical application. While the results aren’t perfect, they’ve given me a solid foundation to iterate and improve!

Built With

  • bollinger-bands
  • csv-predictions
  • dense-(fundamentals)
  • ensemble-learning.-feature-engineering:-ma
  • file
  • fred
  • hybrid-model
  • languages-&-frameworks:-python
  • macd
  • macroeconomic-factors
  • manual
  • nan-handling-(ffill())
  • newsapi-(placeholder).-architecture:-lstm-(technical)
  • nltk
  • no-model-version-control
  • numpy
  • pandas
  • placeholder-sentiment
  • prophet
  • pytorch
  • random-forest
  • rsi
  • scikit-learn
  • sentiment-analysis.-infrastructure:-.pth-model-files
  • textblob.-data-sources-&-apis:-yahoo-finance
  • venv-for-dependencies.-technical-debt:-limited-news-api
Share this project:

Updates