Introduction
Stock market prediction has always been a fascinating challenge, blending data science, economics, and technology. With high volatility and rapid fluctuations, forecasting stock prices requires powerful computational models. Our Portfolio Prediction Model leverages Long Short-Term Memory (LSTM) networks and Random Forest regressors to predict stock trends and optimize investment strategies. This blog explores how these models work, their impact on portfolio management, and how they can empower investors.
The Objective
The goal of this project was twofold:
Stock Price Prediction – Using historical data to forecast next-day stock prices with high accuracy.
Portfolio Performance Analysis – Assessing financial outcomes and profitability based on predicted prices.
By integrating LSTM networks, known for their sequential data processing capabilities, our model effectively captures trends and dependencies in stock movements, providing valuable insights for investors.
Data Preparation: Laying the Foundation
To ensure accuracy, we meticulously prepared the dataset using the following steps:
1. Data Collection
- Historical stock prices were retrieved using the yfinance library.
2. Data Cleaning & Handling Missing Values
- Missing values were forward-filled to maintain data consistency.
3. Feature Selection
- A heatmap was used to analyze correlations between features such as Open, High, Low, and Close Prices, with Close price selected as the primary target for predictions.
4. Temporal Transformation
- A sliding window approach was implemented, using data from the past 60 days to predict the next day’s price.

Figure: Closing Price History of ‘AAPL’ stock over the past 10 years.
Prediction Models: Random Forest vs LSTM
Random Forest Regressor 🌲
A robust ensemble learning method that handles non-linear trends and outliers, but struggles with time-series forecasting due to its lack of temporal memory.
📊 Performance Metrics:
- MAE: 18.22
- MSE: 735.97
- RMSE: 27.12

Figure: ‘AAPL’ Stock price prediction using Random Forest Model
🔎 Findings: While stable, Random Forest failed to effectively capture long-term trends in stock prices.
LSTM Networks 🔄
Designed for sequential data, LSTMs excel at capturing long-term dependencies and market trends. Using forget and input gates, they dynamically adjust to price fluctuations.
📊 Performance Metrics:
- MAE: 5.07
- MSE: 39.14
- RMSE: 6.30

Figure: ‘AAPL’ Stock price prediction using LSTM Model
✅ Outcome: LSTM significantly outperformed Random Forest, making it the preferred model for stock price forecasting.

Figure: Random Forest vs LSTM Model comparison
Portfolio-Level Insights 📈
We extended the model to a multi-stock portfolio consisting of:
- Apple (AAPL) – 50%
- Tesla (TSLA) – 30%
- Google (GOOGL) – 20%
Using the PortfolioPredictor class, the system computed:
- Next-Day Price Predictions for each stock.
- Profit/Loss Estimates based on predicted price changes.
- Visualization of Market Trends, comparing actual vs. predicted prices.
Key Features 🚀
1. Custom Model Classes
- StockPredictionModel: Handles LSTM model design, training, and evaluation.
- Stock: Automates data fetching, cleaning, and preparation.
- PortfolioPredictor: Manages multi-stock portfolio workflows.
2. Profit/Loss Calculation
- Computes individual stock contributions and overall portfolio performance.
3. Visualization Tools
- Generates trend charts and comparative analysis plots.

Figure: Actual vs Predicted Prices – ‘AAPL’

Figure: Actual vs Predicted Prices – ‘TSLA’

Figure: Actual vs Predicted Prices – ‘GOOGL’
Results & Insights 📊
✔ LSTM’s superior accuracy led to improved predictions and better portfolio management.
✔ The system effectively identified profitable investment opportunities and suggested rebalancing strategies.
✔ Stock price trends were successfully captured, paving the way for enhanced trading strategies.
Future Scope 🔮
🚀 Enhancing Model Complexity: Experimenting with transformer-based models (e.g., GPT, BERT) for even better forecasting.
📊 Feature Expansion: Incorporating news sentiment analysis and macro-economic indicators to refine predictions.
⚡ Real-time Implementation: Deploying the model in a live trading environment to assist real-time decision-making.
References
• GeeksforGeeks: Introduction to LSTM
• Keras: Sequential Model
• Medium: Yahoo Finance with yfinance
• YouTube: Stock Price Prediction using Python