In this video, we are going to create a sell grid system that will open a sell position when the next sell price is reached. So let’s speed that up a little bit and you see that we have several positions. So let’s find out how to create something like this with MQL5. To do that, 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 file, Expert Advisor from template, Continue. I will call this file Simple sell Grid System. Click on Continue, continue and finish. Now you can delete everything above the Ontick function. And let’s also remove the two comment lines. We start with an include function to include the file Trade.mqh. This one will enable us to create an instance of the class Ctrade. It gives us simplified trading functions and it will be called trade.
We also want to create an array for the price data, that is called price info and it is created by using MQLRates. And let’s create a string variable for the signal, that is also called signal, but we don’t assign a value here because we are going to calculate that later. Inside of the ontick function we want to start by creating a static double variable for the next sell price. Static is a modifier that will remain the value for as long as the Expert Advisor is running, even if we leave the function. Now let’s calculate the Bid price. That is done by using Symbolinfodouble for the current symbol on the chart, we are using symbol underscore bid and with normalize double and underscore digits, we make sure that we automatically calculate the right number of digits behind the dot.
Because some currency pairs have five digits behind the dot and the other ones have three digits behind the dot. Now let’s use arraysetasseries for the price and for array to sort the current array from the current candidate downwards.
And with CopyRates, we can now fill the array with data for the current symbol on the chart and the currently selected period on that chart, starting from candle zero for three candles. We store the data and the price info array, and we can also use underscores here like we did in this line. Now, if we have no open positions, that would be true when PositionsTotal has a value of zero, we want to set our next sell price to zero.
That is a reset. Afterwards, we want to check our entry signal, by calling a function that is called check entry signal. That function doesn’t exist so far, so we need to create it in a few minutes, but before we do that, we want to check if the Bid price currently is below the next sell price or if the next sell price equals zero. If that is the case, we want to open a sell position, that is done by using trade.Sell for 10 Microlot.
And right after we have done that, we want to set the next sell price 50 points below the current sell price level. Let’s also add a comment statement to create an output for the Bid price and for the next sell price right on our chart. So far, so good. That’s the ontick function. But now we need to create this function here. It is called check entry signal and it will return a string value. Because when the close price for the last candle in our price info array is above the open price for candle one in our price info array, we consider that to be a buy signal.
So now we assign the word buy to our signal. Otherwise, when the close price for candle one in our price info array is bigger than the open price for candle one in our price info array that would be a sell signal. And now we assign the word sell to our signal. Finally, we want to return our signal to the main module and close this function. 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 basic videos in this video series. And on our website, you can now find a link for all these videos, including the source codes in our shop. For now, let’s enable the toolbar and click on compile or press F7. You shouldn’t get any errors. And if that is the case, you can click on a little button 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 Sell Grid System.ex5, mark the option for the visual mode. If you don’t see that it, is below here and start a test. Now you should see the first sell trade is opened. Here is the second one and every 50 points we will see a new sell trade. So our little sell grid system works as expected and you have coded it yourself with a few lines of MQL5 code.