Hello everyone, welcome to my channel. Today, I’m going to show you how I personally take advantage of ChatGPT in order to create a piece of software that I use in real life. I will explain the approach I take to minimize errors in the logic generated by the AI, which, as you might already know, is far from precise.
I also want to ensure that my logic is correct and validate it. I will show you what I usually do in different contexts, especially in algorithmic trading.
In this episode, we will ask ChatGPT to help us create a grid bot expert advisor to be used with the MetaTrader4 platform.
Before getting into the details, let’s quickly understand how the strategy works. Basically, we want to create a grid of equally spaced pending orders given the price range to use and the number of orders to create. The expert advisor should be capable of computing the range automatically given some specifications and then divide it into a grid based on the number of orders to allocate.
The expert advisor will check the current price and create sell limit orders above it and buy limit orders below it until the whole range has been filled. The range should be computed by getting the lows and highs of the previous week when the expert advisor starts.
As soon as two orders are executed at market, the expert advisor should open another limit order at the price of the penultimate one. It will always check the current price and create the type of order accordingly (sell limit if the price is above the current one, buy limit if the price is below the current one). If the number of executed orders at market is higher than two for some reason, the expert advisor will reset everything by deleting the remaining pending orders and recreating them again according to the same range.
Now, let’s ask ChatGPT to help us create an indicator that computes and shows on the chart the daily trading range. It’s important to note that ChatGPT does not make any checks or validations on the code it produces, so we need to inspect it for errors on both syntax and logic.
After several tries, I finally got the indicator working correctly by showing the trading range as two horizontal red lines. I had to fix the logic provided by the AI, as it confused the support and resistance levels for some reason. This highlights the importance of being careful with the code provided by ChatGPT.
If you’re not familiar with the programming language, you can ask ChatGPT to refine the code for you. However, this might require more iterations. In that case, my suggestion is to always provide ChatGPT with the current version of the code and ask it to fix small pieces one by one. The more precise you are in describing what you want to fix or change, the higher the chances of getting the correct code.
Now, let’s ask ChatGPT to help us create a trading bot. According to the trading range we computed previously and the current price, the bot will place pending orders equally spaced above and below the ladder.
I am going to proceed iteratively, asking ChatGPT to improve the logic step by step. I will also provide my current updated code to ChatGPT every time I want to add or change something. This is important because otherwise, ChatGPT will assume you want to continue working on the last code it provided, which may be wrong or not fully precise.
Now that I have the correct order creation logic, I can add the code to manage the orders during the trade. In particular, I’m going to ask ChatGPT to help me store the order tickets in an array and check at every new bar if some of the pending orders have been executed. I want the expert advisor to update the array accordingly.
By checking the code, we can spot some errors in the logic again. For example, ChatGPT does not take into account the distance between the orders, placing them all at the same price. After fixing this error, let’s improve the bot logic even more.
Now, we want ChatGPT to help us add some more rules to the trading logic. In particular, we want to be able to reset the pending orders when a new day starts and rebuild the grid according to the updated trading range.
Alright, that’s it for now. Stay tuned for the next episode!