Trading AI

This is an exploration in the efficacy of different machine learning models for trading on the stock market. In particular, I trained multiple types of models using data from Yahoo Finance on three different commodities, gold, crude oil, and natural gas. After exploring many different models and selecting the best one, I created a trading script that connects to the Oanda trading API and executes trades based on the predicted price of the commoditiy based on the past price history. If the predicted value of the commodity is lower than the current value, it sells some of its shares, and if the predicted value is higher than the current value, it buys some shares. Here is a link to the GitHub repository. Below is the performance of the trading algorithm using the LSTM model on the real stock market (using fake money).

image of the profit made by the trading algorithm

As you can see, the trading algorithm was able to make a profit in the short time that it was trading. However, this model was trading at a time when the market was very bullish, so it is unclear if the model would perform well in a bear market.

Throughout the testing of the different models, we compared their performance to a simple buy and hold strategy. Below are the results of these tests for different models.

LSTM with minutely, hourly, and daily data

advanced LSTM model results

Transformer

advanced transformer model results

Deep reinforcement Learning (PPO)

deep reinforcement learning model results

LSTM with daily data

simple LSTM model results

LSTM and PPO combined (promising but unreliable)

LSTM and PPO combined model with promising results

LSTM and PPO combined (poor results)

LSTM and PPO combined model with poor results

Transformer on multiple simultaneous commodities

Transformer model results using multiple commodities at once

After testing, the advanced LSTM model was used in the trading algorithm to trade on the real time stock market. The advanced LSTM model was chosen for its reliability, as some models had huge successes and catastrophic failures. Below is the trading algorithm running.

Trading algorithm actively trading using the LSTM model