In this video, we are going to create a sell grid system that will open sell trades when we have a falling trend. So let’s find out how to do that with MQL4. 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 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 the two comment lines here. First, we need a string variable for the signal, that will also be called signal and we do not assign a value here because we are going to calculate that later. Inside of the ontick function we create a static double variable called Next sell price. We are going to use that for the next price level and if we have no open positions, that would be the case when OrdersTotal returns the value of zero, we want to set the next sell price level to zero. To find out if we have a sell signal we are going to use a function that is called check entry signal. This function does not exist so far, so we need to code it in a few minutes. For our sell position we want to check a few conditions. First, we want to check if the Bid price is below the next sell price level that we have defined or if the next sell price is zero, the next condition we want to check is if the signal has been a sell signal.
If both conditions are true, we are going to use ordersend for the current symbol on the chart to open a sell trade for 10 Microlot. And right afterwards we are going to set the next sell price level to a value 30 points below the Bid price. Let’s add a comment statement to output the Bid price and the next sell price on our chart so we see what’s going on. So far this is it with the Ontick function. But remember, the function for the entry signal still needs to be created.
Our function will have the name check entry signal and it will return a string value to calculate the current signal. We are going to use something very easy. We want to check if the close price for candle one is bigger than the open price for candle one. If that is the case, we consider that to be a buy signal. So now we assign the work buy to our signal. In the other case, if the close price for candle one is below the open price for candle one that would be a sell signal. And in that case we would assign the word sell to our signal.
Finally, we use the return statement to return the signal to our main module, and that’s about it. Well, 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 or maybe even the Premium Course on our website might be interesting for you. That’s where you actually can also find a link for source codes like in this video. But for now, please click on the Compile button or press F7 on your keyboard.
I don’t have any errors here. 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, dot EX4 and the Visual mode here and start your test. Here we are. This is our first position. I will increase the speed a little bit and now you’ll see that we have a second position and a third one.
So our little Expert Advisor works as expected. And in this little video you have learned how to create a sell grid system for Metatrader4 and you have coded it yourself with a few lines of MQL4 code.