MQL5 TUTORIAL – COPILOT AI PROGRAMMING (WITH SOME ISSUES)

 

In this video, we are going to use artificial intelligence in Metaeditor to build a simple Candle Counter Expert Advisor!

 

So let’s see how we can do that with MQL5. To do that, please open MetaTrader and click on the little button here or press F4 on your keyboard. That should open the Meta Editor window. And if you don’t see the Toolbar, please click on View, Toolbar. Now we want to click on File, New File, ExpertAdvisor from Template, Continue. I will call this file, Simple CandleCounter. Click on continue, continue, and Finish. Here we are. Now we can delete everything above the OnTick function, and I also delete the two comment lines here. So this is how we have done it so far, manually. But this time I would like to use this little icon here. This will send some text to a solution that is called Copilot. Basically, Copilot is a coding AI that also can be used for other programming languages like Python. And to use it, we first need to activate it.

 

So you need to get an OpenAI API key!

 

You can click on this link and you will be brought to the OpenAI website where you can create as many keys as you like. I I have already created my key. They also show you the monthly usage, and so far, the most expensive day was about $1. I’m using this API for AI programming with Python because I do AI coding for companies to create AI servers that will help them in their daily business. So if you have a business, that might be interesting for you. And once you get your key, you can paste it here. For this simple example, we are going to use GPT 3.5 turbo. You could also use a GPT 4 model. Those are better, but also a lot more expensive. The maximum tokens amount here is basically a restriction on about how much you are going to allow to spend. A thousand tokens is about 750 words. And this variability slider here is set to 20% because I don’t want my solution to be very creative. If you would like to have something about fairy tales and unicorns, you could increase this value here. Now, basically, what this thing does is it sends whatever you say to the artificial intelligence. And this one is trying to make suggestions. So let’s see if it works in our case. Create a function that calculates and returns the number of candles on the chart. I can now mark this line of text, click on the button here, and it should be sended to Copilot, which actually worked this time. In the German video, we had several timeouts, and I had to restart MetaTrader all over again. In case you click on the button and nothing happens, you can click on View, Toolbox, and check out the journal here. Here is the error that we had, and this time we spend a few cents for 74 tokens.

 

So now we have our function. Let’s try to create an output!

 

I say: use the comment function to output the result for the get number of candles function. Let’s mark that and send it over to copilot. And here is the result. And you might notice that we said we want to use the comment function, but it returned the print function. So let’s try on the screen, remove the output, and mark this and try it again. And still, it uses the print function instead of the comment function. So for now, I’m going to change that manually. These are all comment lines, so let’s click on Edit, Advanced, and you can uncomment a few lines by clicking on this menu item or by pressing Control and the plus. Let’s change that to comment. And remove the empty lines. Once you’re done, you can click on the Compile button. So what’s happening now? Okay, this should be a comment line that is correct. And this time, the compilation was successful. Actually, in the German version, it forgot that we have to pass two parameters here because when you click on the ibas function and press F1, you will see that we have to pass the symbol and the time frame, which would be period in my case. So let’s recompile the code. And if you don’t get any errors, you can click on the little button here or press F4 to go back to MetaTrader. This It was a very simple example. And if you have no idea what all the code here does, maybe the premium course on our website might be interesting for you.

 

For now, let’s go back to MetaTrader!

 

Click on View, Strategy Tester, or press Control and R. Pick the simple candle counter that we have created. Mark the visual mode here and start your test. And yes, indeed, we get the number of candles. It is counting, and we see it on the life chart in the strategy tester. So our little expert advisor works. This was a very simple example, and you might notice that from time to time, you get code that is not working right away, like we have seen with the comment statement and the print function. So for now, I consider AI to be helpful but not perfect. But that might change in the near future when we get better models.