James Bachini

Can ChatGPT & Keras Predict The Price Of Bitcoin

ChatGPT Bitcoin Bot

I get asked a lot if ChatGPT can be used for trading and I normally say “no, it’s a language model which isn’t designed to find market patterns in numerical price data”. ChatGPT works more like predictive text in a search engine or on your phone but what it is good at is writing code and we can use it to build out machine learning models based on openly available market data.

In this tutorial I’ll be using ChatGPT & Claude to build a data logger which will collect real time market data from cryptocurrency exchanges like Binance and Coinbase. Then I’ll ask them to write a script to train a model using Keras & Tensorflow. Tensorflow is a machine learning framework developed and open sourced by Google. Keras is a set of high level API’s built on top which makes the process simple. Finally I’ll test these models by doing some mock trading using real time market data. All code from this experiment is open-source and available with installation instruction in the Github repository.

  1. Data Logging
  2. Training A Model
  3. Mock Trading

Data Logging

AI in it’s current form can be thought of as big computation on big data sets. So the first thing we need is a lot of data to train a model. I’m going to use the following prompt to generate a data-logger

Create a data logger using python and ccxt to collect data from the following cryptocurrency exchanges. Binance, Coinbase, Kraken, OKX, Bybit. It should query the exchanges asynchronously and log data points including Bitcoin price in USD or USDT, order book data with bids and asks, and recent trading volume.

The result was this code: https://github.com/jamesbachini/Bitcoin-Prediction-Model/blob/main/data-logger.py

This was fairly good from the outset, my only criticism would be with the inconsistency of the timings. It waits a second between queries but also waits for responses to come back from all exchanges which means it’s logging data roughly every 5 seconds.

You could potentially improve this with web sockets and dig into the exchange API’s to make it more efficient but for what we want it will do the job.

Install Python and run it for 24hrs and you’ll get a 5mb csv file full of crypto market information.

git clone https://github.com/jamesbachini/Bitcoin-Prediction-Model.git
cd Bitcoin-Prediction-Model
pip install -r requirements.txt
python data-logger.py

Training A Model

From the same chat history we can now ask ChatGPT (and Claude) to generate a script to build and train a model.

Write a python script using Keras to take the features from the data logger above, train and test a Keras sequential model based on time series data to predict the future price of Bitcoin. It should be designed to take in relevant data and find patterns which indicate future price movements while avoiding overfitting.

You can see the variations between the originals here:

I tweaked prodded and experimented to come up with the following model:

https://github.com/jamesbachini/Bitcoin-Prediction-Model/blob/main/ltf-model.py

model = Sequential([
    Conv1D(filters=64, kernel_size=3, activation='relu', input_shape=(SEQ_LENGTH, x.shape[2])),
    Bidirectional(LSTM(128, return_sequences=True)),
    Dropout(0.2),
    LSTM(128, return_sequences=True),
    Dropout(0.2),
    GRU(64, return_sequences=False),
    Dropout(0.2),
    Dense(50, activation='relu', kernel_regularizer=l1_l2(l1=1e-5, l2=1e-4)),
    Dense(1)
])

This model is a sequential neural network tailored for time series data analysis. The model starts with a Conv1D layer, which uses a one dimensional convolution to process the sequence data and extract features. This layer applies 64 filters with a kernel size of 3 and uses the ReLU activation function. The input shape is defined to accommodate the sequence length and the number of features per time step.

Following the convolutional layer, a Bidirectional LSTM (long short term memory) layer is employed to capture dependencies in both forward and backward directions within the sequence. This layer contains 128 units and is set to return the full sequence for subsequent layers to process. To prevent overfitting, a Dropout layer with a dropout rate of 20% is added after the Bidirectional LSTM.

The model continues with an additional LSTM layer and a GRU (gated recurrent unit) layer, both configured to return sequences, with the GRU layer only returning the last output. Each of these recurrent layers is followed by a Dropout layer to maintain regularization and prevent overfitting.

Finally, the model includes a Dense layer with 50 units and ReLU activation, incorporating L1 and L2 regularization to further mitigate overfitting. The output layer is a Dense layer with a single unit, suitable for regression or binary classification tasks.

This combination of convolutional and recurrent layers, along with dropout and regularization techniques, equips the model to effectively handle and learn from sequential data.

Run it in python and it will train over 1000 epochs to create a .keras file which we can import into our trading engines.

Training a keras model to predict Bitcoin price

Mock Trading

So now we have a nice model which has been trained on the previous days market data. Is there any alpha in it and can it predict future Bitcoin price movements?

To test this we first created a prediction model to collect data from the exchanges and run the trained model across this data to create real time predictions.

Using the model and data logger above can you write a python script which collects data in real time and passes it to the model we just trained. It should output a prediction for the future price of Bitcoin

The code here ran into some issues relating to the MinMaxScaler which were easily fixed by pasting in the errors from the console and asking ChatGPT to come up with a fix.

The other issue I ran into was that it was constantly predicting a price above the current price. I think this was due to price movement since the time the model was trained. This was remedied by creating an array of past predictions and then using the mean of these values as a baseline for if the model thinks price is going up or down. This was a short term band aid and a better solution would be to clean the data better and work with anchored prices i.e. Coinbase is +$1.03 over Binance, Volume is 150% above the mean etc prior to training the model.

The final step was to add a mock trading function which would log the prices when signals are fired and record trades to see if it was profitable. The final code for this is here:

https://github.com/jamesbachini/Bitcoin-Prediction-Model/blob/main/mock-trading.py

Did it make any money?

Well yes and no, in a mock trading environment it was finding profitable trades and some alpha in the market data. Both long and short positions were profitable across 500+ trades.

The catch is that it was trading on an extremely low time frame, not holding trades open for long and the profits wouldn’t cover the execution costs.

If I was going to develop this into a production level trading bot there would be execution costs which would be larger than the profits such as exchange trading fees and slippage.

Where this could be useful is to get better low time frame entries for higher time frame strategies. It could be used as part of a cog in a larger system to get better execution. You could develop these models further and incorporate them for whatever asset you are trading to improve your entry/exit efficiencies, something I’ve tested before here without ML models:

So the bottom line is that ChatGPT is a language model and not designed to predict numerical data. It can however assist in creating the code to build neural networks which are better designed for this task. You wont get rich overnight but it can dig out a little bit of alpha from the market data which is openly available for crypto markets.



Get The Blockchain Sector Newsletter, binge the YouTube channel and connect with me on Twitter

The Blockchain Sector newsletter goes out a few times a month when there is breaking news or interesting developments to discuss. All the content I produce is free, if you’d like to help please share this content on social media.

Thank you.

James Bachini

Disclaimer: Not a financial advisor, not financial advice. The content I create is to document my journey and for educational and entertainment purposes only. It is not under any circumstances investment advice. I am not an investment or trading professional and am learning myself while still making plenty of mistakes along the way. Any code published is experimental and not production ready to be used for financial transactions. Do your own research and do not play with funds you do not want to lose.