In this video, we are going to create an Expert Advisor that is able to set buy prices as a grid system when we have an uprising trend. So let’s find out how to do that with MQL4. To get started. Please click on a little button here or press F4 on your keyboard. Now you should see the Metaeditor window. And here you want to click on file, new file, 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 let’s also delete the two comment lines here, we start by creating a global variable called signal. This is a string variable so it can hold text values, but we don’t assign any values here because we are going to calculate that later. Inside of the ontick function we want to create a static variable. Static is a modifier that will exist during the whole lifetime of the MQL4 program. We use double because we need floating types.
And the name of the variable is next buy price, we are going to use it to set the next buy price level for the grid system and we want to check if OrdersTotal equals zero. That would be the case when we have no open positions. In that case, we set the buy price to zero. Afterwards we want to call a function that is called check entry signal. This function doesn’t exist so far. We need to create it later. And that will check if we have a buy or a sell signal, one of the conditions to open a buy trade would be if the Ask price is above or equal to the next buy price.
The second condition is that we want to check if the signal equals buy and if both conditions are true, we are using Ordersend to open a buy trade for 10 Microlot and we also want to set the next buy price level. So now we are going to calculate the next buy price buy using the current Ask price and adding 30 points. Let’s add a comment statement. That will output the current Ask price and the next buy price directly on our chart. This function doesn’t exist so far, so we need to create it now. The name of the function will be check entry signal. It’s a string function. That means it will return a string value. We use a very simple way to find out if we want to buy or sell. In our case, we want to check if the last candle is bullish. That would be the case when the close price for candle one is bigger than the open price for candle one. If that is true, we assign the word buy to our signal.
In the other case, when the close price for candle one is below the open price for candle one, that would be a sell signal. And now we assign the word sell to our signal. Finally, we want to return the signal to the 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 watch one of the other videos in the basic video series.
Or maybe you want to buy the Premium Course, you can find that one on our website. This was actually suggested buy a Premium Course member. And if you are already a premium member and have an idea for such a video, please let me know. For now, we want to click on the Compile button or press F7. I don’t have any errors here. And if that is the case, we can click on a little button here or press F4 to go back to Metatrader.
And in Metatrader we want to click on View, Strategy tester or press Control and R. Please pick the new file simple buy grid system dot EX4, mark the option for the visual mode here. And start your test. Here we are. The Expert Advisor is running and once we see a rising trend, we should see additional positions. That happened right now, so our little Expert Advisor works 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 MQL4 code.