BMWise

BMW Stock Prediction & Trading Simulator

This project uses machine learning and financial modeling to analyze BMW stock data, predict future prices, and simulate investment strategies. It helps understand stock trends, optimize trading decisions, and evaluate how a portfolio would perform over time. πŸ”— View the Project Here: GitHub Link

Why Does This Project Matter?

This project helps investors and traders make smarter decisions by:
βœ… Using AI to predict stock trends instead of guessing.
βœ… Testing different investment strategies before putting in real money.
βœ… Finding the best stock allocations to maximize profits while reducing risk.

How It Works

Understanding the Data

The program starts by loading historical stock price data for BMW and preparing it for analysis:

  • Rolling Averages: Calculates the 50-day and 200-day moving averages, which smooth out price fluctuations and show trends.

    • A moving average (MA) is a way to track stock prices over time by calculating the average price over a set number of days. It helps investors see if a stock is going up or down without getting distracted by daily price changes.

      • 50-Day Moving Average: Shows the average price of BMW stock over the last 50 days.

        • Short-Term Trend: Reacts quickly to price changes.

        • Helps traders decide when to buy or sell in the short term.

      • 200-Day Moving Average: Shows the average price over the last 200 days.

        • Long-Term Trend: Moves slower but gives a bigger picture of the stock’s long-term trend.

        • Helps investors understand if BMW’s stock is growing or falling over time.

  • Lag Features: Introduces a time delay in the dataset, allowing the model to learn from previous price movements.

    • The model can understand how stock prices change over time and use that knowledge for predictions.

  • Scaling: Uses MinMaxScaler (tool used in machine learning) to normalize data, ensuring better performance by keeping all values in the same range.

    • BMW stock might be $100, but another stock might be $1,500. A machine learning model might get confused by these big numbers, so Min-Max Scaler shrinks all numbers to a small, fixed range (0 to 1), so the model can understand them better.

Predicting Stock Prices

To forecast BMW’s future stock prices, the program uses three types of models, each with a unique approach:

  • Linear Regression

    • A model that finds patterns in past prices and predicts future values.

    • It assumes that price changes follow a straight-line trend.

  • ARIMA (AutoRegressive Integrated Moving Average)

    • A statistical method that analyzes patterns in time series data (BMW stock prices over time).

    • It learns from past trends, cycles, and seasonality to make predictions.

    • Uses Statsmodels (Python library used for statistics and data analysis) for implementation.

  • LSTM (Long Short-Term Memory)

    • A neural network-based model that excels at time series forecasting.

    • Unlike traditional models, LSTM can remember past stock price trends and patterns to make smarter predictions.

    • Implemented using TensorFlow/Keras.

      • Both are models that can recognize patterns, make predictions, and learn from data. Keras is a simpler tool built inside TensorFlow. For example, TensorFlow is the car engine and Keras is the steering wheel that makes it easy to drive.

These models provide different insights into future price movements, allowing us to compare their accuracy and reliability.

Simulating Stock Trading

Once stock price predictions are generated, the program simulates real-world trading:

  • It buys stocks when prices are expected to rise and sells when prices are expected to fall.

  • The program starts with an initial cash investment and tracks how the portfolio grows over time.

  • It evaluates different buy-and-sell strategies to determine which would have been the most profitable.

Portfolio Optimization & Investment Strategy Testing

A portfolio is a collection of investments. In my case, my project focuses on BMW stock but could be expanded to include multiple stocks.

Key financial concepts used:

  • Returns: The profit made on an investment over time.

    • Example: If BMW stock grows from $100 to $110, the return is 10%.

  • Risk (Covariance & Volatility): Measures how much the stock price fluctuates.

    • High risk means the price moves up and down a lot.

    • Low risk means the price stays relatively stable.

  • Sharpe Ratio: A risk-adjusted return metric that shows how much profit is made relative to risk.

    • A higher Sharpe Ratio means a better investment.

  • Portfolio Optimization Using the Efficient Frontier

    • The program allocates investments strategically using PyPortfolioOpt.

    • It finds the best balance between risk and return, ensuring investors get the highest possible return with the least amount of risk.

Performance Evaluation: Backtesting Strategies

To measure how well the trading strategy performs, the program uses:

  • Mean Squared Error (MSE) – Measures how accurate stock price predictions are.

  • Portfolio Performance Metrics – Calculates returns, risk, and Sharpe ratio to evaluate investment success.

  • Volatility Analysis – Shows how much the stock price fluctuates over time.

This ensures that investment decisions are backed by data and analytics, not just intuition.

What Was Used to Build This?

  • Programming Languages: Python, Javascript, CSS

  • Machine Learning & Deep Learning

    • Scikit-Learn: Used for Linear Regression and model evaluation.

    • TensorFlow/Keras: Builds and trains LSTM models for deep learning-based forecasting.

    • Statsmodels: Implements ARIMA for statistical time series forecasting.

  • Data Analysis & Processing

  • Pandas: Loads, preprocesses, and manipulates stock market datasets.

  • NumPy: Handles complex mathematical operations and optimizations.

  • Data Visualization

  • Matplotlib & Seaborn: Generate charts and graphs for stock trends, predictions, and portfolio performance.

  • Financial Modeling & Portfolio Optimization

  • PyPortfolioOpt – Optimizes stock allocation using risk-return metrics like the Sharpe ratio.

  • Statistical Metrics – Calculates returns, risk (covariance & volatility), and investment performance.

Scaling & Performance Optimization

  • MinMaxScaler (Scikit-Learn) – Normalizes stock price data for better model accuracy.

  • Rolling Averages & Lag Features – Helps capture historical trends for more accurate forecasting.