Skip to content

A machine learning-based stock trading simulation using Linear Regression, Decision Tree, Random Forest, and Neural Networks, enhanced by an ensemble Mixture of Experts strategy.

Notifications You must be signed in to change notification settings

Sahiltheram/ST-AI-Trading

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

This project uses machine learning to predict future stock prices and simulate trading strategies. It combines four models—Linear Regression, Decision Tree, Random Forest, and Neural Networks—into an adaptive ensemble (Mixture of Experts). The ensemble powers a trading bot that learns and updates weights daily based on prediction performance.

The system was tested on historical stock data from major companies (AAPL, GOOGL, AMZN, MSFT, TSLA, JPM, MCD, WMT) and simulates investment growth over a 5-year window.

  • Predicts next-day stock prices using supervised learning.
  • Compares performance using Mean Squared Error (MSE).
  • Implements a Mixture of Experts that dynamically adjusts model weights.
  • Simulates a trading bot with adjustable transaction volume (k-value).
  • Tests strategy performance across multiple real-world stocks.

How to Run

Requirements

Make sure you have Python 3.7+ and the following libraries installed:

pip install yfinance pandas numpy scikit-learn

Running the Script

python stock_ai.py

Make sure your terminal is pointed at the directory where stock_ai.py is saved.

Datasets

This project uses historical stock data obtained via the Yahoo Finance API, accessed through the yfinance Python library.

  • Data used: 5 years of daily "Open" prices
  • Stocks tested: AAPL, GOOGL, AMZN, MSFT, TSLA, JPM, MCD, WMT

Example code for fetching:

import yfinance as yf
ticker = yf.Ticker('AAPL')
aapl_df = ticker.history(period="5y")

Sample Output

Example printout from trading simulation:

For k=120: stocks=0, budget=995226.26
Mean squared error: 6.60 5.37
[0.25, 0.25, 0.25, 0.25]
...

This means that a $10,000 investment grew to $995,226 over ~1250 days at k=120.

Example Output Logic

The script prints:

  • MSE for each model
  • Dynamic model weights over time
  • Simulated ROI from the trading bot at different k values (10–200)

Results Summary

  • Best performing stock: Walmart (WMT) with test MSE of 0.76
  • Best k for trading bot: 120
  • Peak ROI: 9952.26%

Disclaimer

This is a simulated model using historical data. It does not account for transaction fees, slippage, or real-world market conditions. This is not financial advice.

Contact

Author: Sahil Thadani
📧 sahil.a.thadani@gmail.com
📍 Westborough High School, MA, USA

About

A machine learning-based stock trading simulation using Linear Regression, Decision Tree, Random Forest, and Neural Networks, enhanced by an ensemble Mixture of Experts strategy.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages