How to create an AI trading system - TuringBot (2024)

Predicting whether the price of a stock will rise or fall is perhaps one of the most difficult machine-learning tasks. Signals must be found on datasets that are dominated by noise, and in a robust way that will not overfit the training data.

In this tutorial, we are going to show how an AI trading system can be created using a technique called symbolic regression. The idea will be to try to find a formula that classifies whether the price of a stock will rise or fall the following day based on its price candles (open, high, low, close) in the last 14 days.

AI trading system concept

Our AI trading system will be a classification algorithm: it will take past data as input, and output 0 if the stock is likely to fall in the following day and 1 if it is likely to rise. The first step in generating this model is to prepare a training dataset in which each row contains all the relevant past data and also a 0 or 1 label based on what happened the following day.

We can be very creative about what past data to use as input while generating the model. For instance, we could include technical indicators such as RSI and MACD, sentiment data, etc. But for the sake of this example, all we are going to use are the OHLC prices of the last 14 candles.

Our training dataset should then contain the following columns:

open_1,high_1,low_1,close_1,...,open_14,high_14,low_14,close_14,label

Here index 1 denotes the last trading day, index 2 the trading day before that, etc.

Generating the training dataset

To make things interesting, we are going to train our model on data for the S&P 500 index over the last year, as retrieved from Yahoo Finance. The raw dataset can be found here: .

To process this CSV file into the format that we need for the training, we have created the following Python script which uses the Pandas library:

import pandas as pd# Read the CSV file into a DataFramedf = pd.read_csv('S&P 500.csv')training_data = []# Iterate through the DataFrame rowsfor i, row in df.iterrows(): if i < 13 or i + 1 >= len(df): continue # Extract features for the last 14 days features = [] for j in range(i, i - 14, -1): features.append(df.iloc[j]['Open']) features.append(df.iloc[j]['High']) features.append(df.iloc[j]['Low']) features.append(df.iloc[j]['Close']) # Add the label: 1 if the next day's close is higher, otherwise 0 label = 1 if df.iloc[i + 1]['Close'] > row['Close'] else 0 features.append(label) training_data.append(features)# Create column names for the DataFramecolumns = []for i in range(1, 15): columns.extend([f'open_{i}', f'high_{i}', f'low_{i}', f'close_{i}'])columns.append('label')# Convert the training data into a DataFrame and save it to a CSV filetraining_data_df = pd.DataFrame(training_data, columns=columns)training_data_df.to_csv('training.csv', index=False)

All this script does is iterate through the rows in the Yahoo Finance data and generate rows with the OHLC prices of the last 14 candles, and an additional ‘label’ column based on what happened the following day. The result can be found here: training.csv.

Creating a model with symbolic regression

Now that we have the training dataset, we are going to try to find formulas that predict what will happen to the S&P 500 the following day. For that, we are going to use the desktop symbolic regression software TuringBot. This is what the interface of the program looks like:

How to create an AI trading system - TuringBot (1)

The input file is selected from the menu on the upper left. We also select the following settings:

  • Search metric: classification accuracy.
  • Test/train split: 50/50. This will allow us to easily discard overfit models.
  • Test sample: the last points. The other option is “chosen randomly”, which would make it easier to overfit the data due to autocorrelation.

With these settings in place, we can start the search by clicking on the play button at the top of the interface. The best solutions found so far will be shown in real time, ordered by complexity, and their out-of-sample errors can be seen by toggling the “show cross-validation” button on the upper right.

After letting the optimization run for a few minutes, these were the models that were encountered:

How to create an AI trading system - TuringBot (2)

The one with the best out-of-sample accuracy turned out to be the one with size 23. Its win rate in the test domain was 60.5%. This is the model:

label = 1 - floor((open_5 - high_4 + open_12 + tan(-0.541879 * low_1 - high_1)) / high_13)

It can be seen that it depends on the low and high of the current day, and also on a few key parameters of previous days.

Conclusion

In this tutorial, we have generated an AI trading signal using symbolic regression. This model had good out-of-sample accuracy in predicting what the S&P 500 would do the next day, using for that nothing but the OHLC prices of the last 14 trading days. Even better models could probably be obtained if more interesting past data was used for the training, such as technical indicators (RSI, MACD, etc).

You can generate your models by downloading TuringBot for free from the official website. We encourage you to experiment with different stocks and timeframes to see what you can find.

About TuringBot

TuringBot is a desktop software for Symbolic Regression. By feeding your data in .TXT or .CSV format into the program, you can immediately start searching for mathematical formulas that connect the variables. If you want to learn more about what TuringBot can offer you, please visit our homepage.

How to create an AI trading system - TuringBot (2024)
Top Articles
What is the Dodd-Frank Act?
Dark web websites: How to access them safely - LifeLock
Asist Liberty
News - Rachel Stevens at RachelStevens.com
The Daily News Leader from Staunton, Virginia
Bluegabe Girlfriend
Barstool Sports Gif
Employeeres Ual
Gina's Pizza Port Charlotte Fl
Detroit Lions 50 50
Immediate Action Pathfinder
How Much Is Tj Maxx Starting Pay
Spartanburg County Detention Facility - Annex I
Nwi Arrests Lake County
Craigslist Farm And Garden Tallahassee Florida
Webcentral Cuny
Wicked Local Plymouth Police Log 2022
Sni 35 Wiring Diagram
Bridge.trihealth
Wgu Academy Phone Number
Quadcitiesdaily
Sea To Dallas Google Flights
Rimworld Prison Break
Rogue Lineage Uber Titles
Cor Triatriatum: Background, Pathophysiology, Epidemiology
Is Henry Dicarlo Leaving Ktla
Motorcycle Blue Book Value Honda
manhattan cars & trucks - by owner - craigslist
CohhCarnage - Twitch Streamer Profile & Bio - TopTwitchStreamers
Askhistorians Book List
Mawal Gameroom Download
To Give A Guarantee Promise Figgerits
Watchseries To New Domain
Ticketmaster Lion King Chicago
SF bay area cars & trucks "chevrolet 50" - craigslist
Heelyqutii
Hindilinks4U Bollywood Action Movies
Levothyroxine Ati Template
Paperless Employee/Kiewit Pay Statements
Colorado Parks And Wildlife Reissue List
How to Get a Better Signal on Your iPhone or Android Smartphone
Go Bananas Wareham Ma
Below Five Store Near Me
Sallisaw Bin Store
Random Animal Hybrid Generator Wheel
Greatpeople.me Login Schedule
Advance Auto.parts Near Me
Sam's Club Fountain Valley Gas Prices
Joe Bartosik Ms
Hcs Smartfind
One Facing Life Maybe Crossword
Latest Posts
Article information

Author: Annamae Dooley

Last Updated:

Views: 6198

Rating: 4.4 / 5 (65 voted)

Reviews: 80% of readers found this page helpful

Author information

Name: Annamae Dooley

Birthday: 2001-07-26

Address: 9687 Tambra Meadow, Bradleyhaven, TN 53219

Phone: +9316045904039

Job: Future Coordinator

Hobby: Archery, Couponing, Poi, Kite flying, Knitting, Rappelling, Baseball

Introduction: My name is Annamae Dooley, I am a witty, quaint, lovely, clever, rich, sparkling, powerful person who loves writing and wants to share my knowledge and understanding with you.