img

Use ChatGPT To Build A Powerful Crypto Trading Bot In 5 Easy Steps

img
valuezone 12 April 2023

Use ChatGPT To Build A Powerful Crypto Trading Bot In 5 Easy Steps

ChatGPT might not be able to predict the next 100x crypto gem (yet), but it has some very powerful use cases.

One of them is its ability to write code.

So why not build your own trading strategy, backtest it in TradingView and tweak its performance to find what works best?

That’s exactly what we’ll be doing in this guide.

First, let’s choose what strategy we want to code and test.

1. Choose trading strategy

With ChatGPT, you can write code to test the performance of any crypto indicator or a combination of indicators. In this post, we’ll ask ChatGPT to code a simple Bollinger Bands strategy.

To avoid fixing a bunch of error codes, the best is to get a functioning indicator from TradingView and ask ChatGPT to code an entry strategy.

2. Get source code

1. Open the BTC/USDT chart on TradingView and click on Indicators.

2. Type Bollinger Bands in the search bar and click on the first Community script by Madrid.


BTC/USDT chart on TradingView

3. Hover over the indicator in the top left corner of your chart (where it says MBB close 34 2 and click on the Source code icon which looks like this {}. This will open the Pine Editor.

4. Click on the More button (three horizontal dots in the upper right corner of the Pine Editor) and click on Save as. This way, you will create a copy of the script which you’ll be able to edit.


BTC/USDT chart on TradingView

5. Enter a name for your script and click Save.

6. Click on the three dots again and on Convert to v4. Repeat this step to convert to v5.

3. Let ChatGPT do its magic

Now that we have the code for the indicator we want to test, we can ask ChatGPT to create a trading strategy.

First, let’s define the rules for our strategy. We assume we’re trading in trend conditions, so we want to buy when the price closes above the upper Bollinger band, and sell when the price closes below the lower Bollinger band.

1. Copy the following rules and the script code into ChatGPT.

2. We don’t want an indicator anymore, we want a full trading strategy, so in the script, change the word indicator into strategy.

3. Press enter.

Here is the whole text to copy-paste into ChatGPT:

Code a trading strategy for this pine script v5. Enter a long position when the price closes above the upper Bollinger band, and a short position when the price closes below the lower Bollinger band. Close the long position when the short condition is met, and close the short position when the long condition is met.

//@version=5
// Bollinger Bands: Madrid : 14/SEP/2014 11:07 : 2.0
// This displays the traditional Bollinger Bands, the difference is
// that the 1st and 2nd StdDev are outlined with two colors and two
// different levels, one for each Standard Deviation

strategy(shorttitle='MBB', title='Bollinger Bands', overlay=true)
src = input(close)
length = input.int(34, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)

basis = ta.sma(src, length)
dev = ta.stdev(src, length)
dev2 = mult * dev

upper1 = basis + dev
lower1 = basis - dev
upper2 = basis + dev2
lower2 = basis - dev2

colorBasis = src >= basis ? color.blue : color.orange

pBasis = plot(basis, linewidth=2, color=colorBasis)
pUpper1 = plot(upper1, color=color.new(color.blue, 0), style=plot.style_circles)
pUpper2 = plot(upper2, color=color.new(color.blue, 0))
pLower1 = plot(lower1, color=color.new(color.orange, 0), style=plot.style_circles)
pLower2 = plot(lower2, color=color.new(color.orange, 0))

fill(pBasis, pUpper2, color=color.new(color.blue, 80))
fill(pUpper1, pUpper2, color=color.new(color.blue, 80))
fill(pBasis, pLower2, color=color.new(color.orange, 80))
fill(pLower1, pLower2, color=color.new(color.orange, 80))

This is the code returned by ChatGPT:

//@version=5
// Bollinger Bands: Madrid : 14/SEP/2014 11:07 : 2.0
// This displays the traditional Bollinger Bands, the difference is
// that the 1st and 2nd StdDev are outlined with two colors and two
// different levels, one for each Standard Deviation

strategy(shorttitle='MBB', title='Bollinger Bands', overlay=true)

// Define inputs and variables
src = input(close)
length = input.int(34, minval=1)
mult = input.float(2.0, minval=0.001, maxval=50)

basis = ta.sma(src, length)
dev = ta.stdev(src, length)
dev2 = mult * dev

upper1 = basis + dev
lower1 = basis - dev
upper2 = basis + dev2
lower2 = basis - dev2

// Define plot colors
colorBasis = src >= basis ? color.blue : color.orange

pBasis = plot(basis, linewidth=2, color=colorBasis)
pUpper1 = plot(upper1, color=color.new(color.blue, 0), style=plot.style_circles)
pUpper2 = plot(upper2, color=color.new(color.blue, 0))
pLower1 = plot(lower1, color=color.new(color.orange, 0), style=plot.style_circles)
pLower2 = plot(lower2, color=color.new(color.orange, 0))

fill(pBasis, pUpper2, color=color.new(color.blue, 80))
fill(pUpper1, pUpper2, color=color.new(color.blue, 80))
fill(pBasis, pLower2, color=color.new(color.orange, 80))
fill(pLower1, pLower2, color=color.new(color.orange, 80))

// Define entry and exit rules
long_condition = crossover(close, upper2)
short_condition = crossunder(close, lower2)

// Define strategy entries and exits
if long_condition
strategy.entry("Long", strategy.long)
if short_condition
strategy.entry("Short", strategy.short)

if strategy.position_size > 0 and short_condition
strategy.close("Long")
if strategy.position_size < 0 and long_condition
strategy.close("Short")
  1. Replace the old code in Pine Editor with this one.
  2. Click Save.
  3. If you get an error, prefix crossover and crossunder with ta. to have ta.crossover and ta.crossunder.
  4. Click Save again and Add to chart.

4. Analyze performance

Once you add the strategy to the chart, you can analyze its performance in the Strategy Tester tab.

You can change timeframes to see on which the strategy is performing best.

In our case, the strategy has the best performance on the daily chart where it generates a $53 thousand profit which is approximately 5%. On the other hand, it loses money on very low and the weekly timeframes.


BTC/USDT chart on TradingView

Bear in mind that the Strategy tester supposes you’re trading with $1 million of initial capital which you can see in the Properties tab.

The strategy tester is a very powerful tool as it lets you analyze the list of all trades, see the maximum drawdown you would incur, the backtesting date range, and much more.

5. Improve results

Let’s tweak a few parameters in our code to significantly improve the performance of our strategy.

First, trading with $1 million is unrealistic for most, so let’s change it to $10 000.

Let’s also tell our bot to reinvest 100% of our profits. This way, profits will compound and our overall gain will be much higher.

To do this, replace the first line of the code with this:

strategy(shorttitle='MBB', title='Bollinger Bands', overlay=true, initial_capital = 10000, default_qty_type = strategy.percent_of_equity, default_qty_value = 100)

This time, the performance is staggering 975% on the daily chart which means we would have made $97 000!


BTC/USDT chart on TradingView

However, the maximum drawdown is over $148 000 which is not great, so there’s still lots of room for improving this strategy.

Try it yourself!

Now that you know how, you can use ChatGPT to test other indicators or to code strategies from scratch using several indicators.

Remember that you should always ask it to code in Pine Script v5 as this is the programming language used by TradingView.

If you get errors, you can even copy-paste the error messages in ChatGPT and ask it to fix them for you!

Try different timeframes and crypto pairs to see where your strategy is performing best.

Keep in mind that we’ve just playing around here! Before you consider plugging any strategy into an exchange and use it to trade real money, you should do extensive tweaking and backtesting.

Bottom line

With ChatGPT you can now easily backtest any trading strategy that pops into your head.

This is absolutely mind-blowing, because just a few months ago, you would need to hire a developer or learn to code to be able to achieve this.

Take advantage of this incredible revolution we’re going through and build something amazing!