How to Create a cBot With ChatGPT¶
Generative AI is on the rise and creating a cBot without having to write a single line of code is not a dream anymore: it is reality. In this article and its corresponding video, we will show you how you can create a cBot using ChatGPT. We will also demonstrate how you can evaluate cBot code provided by ChatGPT and make sure your AI-generated bot is operational in just a few minutes.
Creating the Correct Prompt¶
For the purposes of this video, we will be using the GPT-3.5 model. It is free to everyone and, despite lacking in features compared to newer GPT models, it produces great results.
The first part of creating a great cBot with ChatGPT is typing the correct prompt. Here are a few rules for what makes a good prompt:
- Encourage ChatGPT to try to act the part of a professional cBot developer.
You can achieve this by typing an initial prompt with the words
"Let's play or game where you are a qualified algo developer for cTrader..." or something like
Pretend to be a professional developer of cBots for cTrader....
- Be specific.
The more details you provide to ChatGPT, the better. Prompts such
Create a profitable cBot for me... would result in a lot of confusion on the part of the AI.
We recommend specifying each part of your cBot as precisely as possible. Consider what parameters it should have, what conditions it should react to, and what trading operations it should perform. If you are creating a custom trading panel, consider what UI elements it should have and where they should be placed.
- Be polite and respectful.
This might sound surprising but research has shown that being polite to ChatGPT actually improves output quality. Including phrases such as
"I would appreciate it if...",
"It would be nice if..." or something similar.
Creating a Colour-Trading cBot¶
For starters, we just want to create a simple cBot that places a new market order on each bar open. The direction of the order depends on whether the previous bar was green or red. If the bar was red, we want to place a sell order and it it was green, we want to place a buy order.
Here is how we can do it by providing the correct prompts.
- Starting sentence:
"Pretend to be a professional developer of cBots for cTrader, Write very simple and understandable code"
- Specific requirements:
"Create a cBot. On every bar, it should place a new market order with no protection mechanisms. The order volume (in units) should be a customisable parameter. A buy order should be placed if the previous bar was green. A sell order should be placed if the previous bar was red"
"I would really appreciate it if you do this task, thank you!"
The exact code ChatGPT outputs may vary but in any case we can simply copy and paste it into the code editor window to see whether it is correct and to see whether it implements our desired logic.
Usually, there are only a couple of non-critical errors that need fixing. In our case, the only thing that required fixing was changing the
Symbol object in the
ExecuteMarketOrder method to the
SymbolName string. Here is the final code we got after doing all of this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
As usual, we save and build the bot. In the 'Trade' application, we attach the instance of the bot to a chart and see if it works as intended. The bot works flawlessly and reacts to the colour of past bars by placing new orders. The order volume is also a customisable parameter.
Note that we did not even have to read the code outputted by ChatGPT. We simply fixed every warning displayed in the code editor and then launched the bot.
Creating a Trend Trading cBot¶
While the earlier cBot worked great, it used simple logic and did not pay attention to market trends. We will try creating something more complex using the same approach to prompting as before.
- Starting sentence:
"Let's imagine you are a skilled cBot developer for cTrader. I will be your client"
- Specific requirements:
"Create a cBot. On every bar, it should check whether the 50-day moving average is above or below the 200-day moving average. If it is above, the bot should place a buy order. If it is below, the bot should place a sell order. The bot should not place an order if there already exists an open position in the same direction. The order volume should be a customisable parameter".
"Thank you for the help!"
Once again, we will simply copy and paste the code given to us by ChatGPT into the code editor window and try to build the bot. Here are all the build errors we get.
- The code is using the outdated
MarketSeriesAPI member. We can replace it with the
- We once again have to replace the
- We should also delete some unnecessary arguments from the
ExecuteMarketOrdermethods as they do not fit any of the available method overloads.
We can also take a look at how our logic was realised. In the
OnStart() method, we initialise our moving averages. ChatGPT made moving average periods as customisable parameters even though we did not ask it to. We can leave it as it is without threatening the core logic.
However, in the
OnBar() method, we can see that the bot only places new orders when there are no open positions (
Positions.Count ==0). This is not what we want as we also want to check for the position direction. We can slightly modify the code so that we end up with the following bot.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
We have used the
Positions.FindAll(string label) method to get all positions with a given label and calculate their count.
When we build the bot, we can add it to a chart. We will also add 50 and 200-day moving averages so that we can evaluate cBot behaviours. After we wait a bit, we can see that the bot functions as expected. New orders are placed when needed but no more than one open position exists in a given direction.
ChatGPT can be a valuable tool when you need to quickly create a cBot with precisely defined logic. While ChatGPT produces some errors, they are usually insignificant and can be fixed quickly. We hope that you found this article and video helpful; subscribe to our YouTube channel to be notified every time we publish a new video.