MQL4 TUTORIAL - HOW TO USE CHATGPT AI IN METATRADER TO CREATE A FUNCTION

Опубликовано: 22 Февраль 2025
на канале: MQL4Tutorial
4,047
51

https://mql4tutorial.com/?s=AI

In this video, we are going to use the AI solution that stands behind ChatGPT to create a function for automated trading.

Actually, AI tools have become very popular, and one of the recent changes is that we can now use it inside of Metaeditor. To do that, please click on the IDE button here. Now you should see the Metaeditor window, and here we want to click on View Toolbar. Here we are. It looks very familiar, but there is this little guy, and when you hover over it with your mouse, you will see that you now can use Copilot for AI programming tasks. If you haven't done so far, you need to click on Tools, Options and enable an OpenAI API here. You can click on this link to create one. And GPT 3.5 Turbo is the default option for ChatGPT. There are better models called GPT 4 and GPT 4 turbo, but those are more expensive. We have already covered that in the last video. So let's go back to MetaEditor. And you see we have a function here that is able to calculate the current equity. So let's compile the source code and we shouldn't get any errors. Let's go back into MetaTrader and press Control and R. Here is my copilot function. Now let's click on Start.

And we can see that the equity is drawn on the chart. So let's stop that, go back into Metaeditor. I would like to copy this comment, and below the current function, I paste it in, mark the whole line. Now, I click on to the little guy for copilot, and it should create the same function again, which is nice. It's a little bit different. Let's undo that and change the description here for the current balance. And when I mark the line again and click on the copilot button, we should get a very similar function like the one above. Now, you might notice that we have a few differences here. This is a double and this is a float variable. Here the function is camel case calculate balance. This one has a lower C and an underscore. So let's first get rid of the comments here. You can use a shortcut on the English keyboard layout, but in Germany, it's very hard. So we click on the advanced option here and select uncomment Lines. Now the comments are gone. Let's use a proper name for the function, and we will make both results a double variable. Now, what you might notice is that so far, the onTick function will only use the comment statement to output the equity.

Let's see if we can change that and say, change the following function to also output balance. Now I have to mark the line, click on the copilot button. And now something strange is happening. The function is not changed, but we got an additional function here that is called print account info. That's not what we wanted, and that's wrong for two reasons. The first one is we wanted to use the comment function and not the print function, because the print function is just outputting stuff in the journal and not on the screen. But the AI says this is an example of how we can modify the function, and that's one of the main problems with AI programming. You will notice that, yes, it might be helpful, but it isn't able to do everything right away. Let's try a different description here. Now I will mark the whole function and the comment and send this one to copilot. This time it inserted the onTick function below the current one. This one looks better. Let's mark the outcommented new function. Click on Edit, Advanced, Uncomment Lines, or you can press Control and the semicolon. That looks good.

But wait, we still need to change the function name here because that was renamed in the last task. Let's try, please use the the proper function name. Now I will mark all the lines below, send those to copilot, and it didn't change anything. So that's not what we wanted. I have to change the function name myself. Now let's compile the code. We have zero errors, so let's go back to MetaTrader and restart the test. And now indeed, we get the equity and the balance shown on the screen. So this was a simplified example on how to use Copilot to create AI-created functions. I say thank you for watching, and I will see you in the next video.