Creating a ChatGPT crypto trading bot

michelonpython

MichelOnPython

Posted on May 26, 2024

Creating a ChatGPT crypto trading bot

Does it make sense to create a LLM-driven trading bot ?

Looking at the recent improvement in LLM, I decided to check by myself.

I recently realized that beyond the usual "I'm an AI model, I can't give price predictions", LLM like ChatGPT (and most AI models) can actually be surprisingly good at predicting a price.

So here is what I did:

Ask for data analysis, not financial predictions

I will use ChatGPT (gpt-4o to be accurate) for the rest of this article as it is the most famous one.

If you give candle data to ChatGPT and ask if the price will go up or down, you get this answer I'm sorry, but I can't predict the future price of Bitcoin or any other financial asset. ...., which makes sense.

asking ChatGPT for a simple price prediction does not work

However, things are different when giving the LLM data to work with and ask for statistics based on those data. Here is what I get when feeding it with the latest price:

asking ChatGPT for a price prediction by feeding it input data works

The full answer is:

Based on the provided data, it appears that the price has been fluctuating with some periods of increase and decrease. The latest closing price is 68843.3.

Given the recent trend, where the price has seen both ups and downs but with a slight downward trend in the last few data points, I predict the next movement will be down with a 60% confidence.

Now things are different ! It turns out that ChatGPT is now willing to make a prediction.

Special thanks to www.octobot.cloud and their open source bot where I found this trick. It looks like they are already using for some of their strategies and it seems to work pretty well.

And OctoBot ChatGPT based strategy example

So I decided to try creating my bot.

My ChatGPT trading bot script

Here is the ChatGPT trading bot script I created. I'm currently testing. I'll put it here in case someone has ideas to make it better, I would really like to get some feedback on it so if you have improvement ideas in the main algorithm, please let me know !

It's very simplistic, but the main ideas are there:

  1. Use different crypto at the same time
  2. Ask the LLM
  3. Trade accordingly


import json
import openai
import ccxt.async_support
import asyncio


OPENAI_KEY = ''
API_KEY = None
API_SECRET = None


async def _bot_iteration(binance, openai_client, symbol):
    btc_price = await binance.fetch_ohlcv(symbol, "4h", limit=50)

    messages = [
        {
            'role': 'system',
            'content': 'Predict: {up or down} {confidence%} no other info'
        },
        {
            'role': 'user',
            'content': json.dumps(btc_price)
        }
    ]
    completions = await openai_client.chat.completions.create(model='gpt-4o', messages=messages)
    prediction = completions.choices[0].message.content     # ex: 'down 70%'

    should_buy = "up" in prediction
    should_sell = "down" in prediction
    confidence = float(prediction.split(" ")[1].split("%")[0])

    # only trade when confidence is at least 70%
    if confidence >= 70:
        if should_buy:
            await binance.create_market_buy_order(symbol, 0.001)
        elif should_sell:
            await binance.create_market_sell_order(symbol, 0.001)


async def bot():
    symbols = ["BTC/USDT", "ETH/USDT"]
    binance = ccxt.async_support.binance({'apiKey': API_KEY, 'secret': API_SECRET})
    openai_client = openai.AsyncOpenAI(api_key=OPENAI_KEY)

    while True:
        # trigger evaluations
        await asyncio.gather(
            *(_bot_iteration(binance, openai_client, symbol) for symbol in symbols)
        )
        await asyncio.sleep(4 * 3600)   # wakeup every 4 hours


asyncio.run(bot())


Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
michelonpython
MichelOnPython

Posted on May 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related