In this video, we are going to create an Expert Advisor for a simple buy grid system, it will open buy trades every 50 points. So let’s find out how to do that with MQL5 .To get started, please click on a little icon here or press F4 on your keyboard.
Now, you should see the Metaeditor window and here you want to click on file, new filem, Expert Advisor from Template, continue. I will call this file simple buy grid system. Click on Continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start with an include statement to include the fire Trade.mqh. This is part of MQL5, and now we can create an instance of the class Ctrade that is called trade.
We also want to create an array for the price info data that is done by using MQLRates. And we want to declare a global variable for the signal. This is a string variable, but we don’t assign any values here. Inside of the ontick function we declare a static variable. Static variables will hold their values inside of the function for as long as the Expert Advisor is running. It’s a double variable so it can hold floating types.
And the name is next buy price. Now let’s calculate the Ask price, that can be done by using Symbolinfodouble for the current symbol on the chart. We use simple underscore ask and with normalize double and underscore digits, we automatically calculate the right number of digits behind the dot. Let’s continue and use ArraySetAsSeries to sort the price array from the current candle downwards. And now we want to use CopyRates for the current symbol on the chart and the currently selected period on that chart to fill the price array with data starting from candle zero for three candles.
Let’s actually use an underscore here. So if we have no open positions, that would be true when PositionsTotal delivers a return value of zero, the next buy price is also zero because we have no level. Now let’s check for the entry signal. We do that by calling a function that is called check entry signal here, we need to code that later on and the return value will be assigned to our signal. And one of the entry conditions is that we only want to buy if the Ask price is above or equal to the next buy price.
And if that is true, we also want to check if the signal equals buy.
In that case, we want to use trade.Buy to open a buy trade for 10 Microlot and now we set the level for the next buy price 50 points above the current Ask price.
Finally, we want to create a comment statement to output the Ask price and the next buy price on our chart. That’s it for the Ontick function. But to use this function, we need to create it now. Our function is called check entry signal. I’m using a very simplified version and check if the last candle is bullish. That is true when the close price is above the open price. And in that case we assign the word buy to our signal. In the other case, if the close price is below the open price, that would mean that the candle is bearish.
And now we assign the word sell to our signal. Finally, we return the signal to our main module and that’s about it. If this was too fast for you or if you have no idea what all the code here does, maybe you want to become a Premium Course member. You can find the Premium Course on our website. This actually was the suggestion of a current Premium Course member.
And if you are already a premium member and have an idea for a video like this one, just sent me an email. For now, please click on the Compile button or press F7 on your keyboard.
I have an additional bracket here, so let’s remove that. Recompile the code. And this time we have no errors and no warnings, and in that case, you can click on a little icon here or press F4 to go back to Metatrader. And in Metatrader, you want to click on View, Strategy tester or press Control and R, please pick the new file. Simple buy grid system.ex5 mark the option for the visual mode here and start your test.
Here we are. Our first position has been opened. The next buy price currently is set to zero and as soon as we have a bullish candle we see that it is adjusted. And here we see the next position is opened, so our little Expert Advisor is working as expected. And in this little video you have learned how to create a simple buy grid system and you have coded it yourself with a few lines of MQL5 code.