MQL4 TUTORIAL BASICS 97 – SIMPLE REVERSE POSITION

video

 

In this video, we are going to create an Expert Advisor that is able to change the buy or sell direction for each position, 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 reverse position, click on Continue, continue and finish.

 

Now you can delete everything above the ontick function and let’s also remove the two comment lines here. First, we want to create a variable for the direction. This is a string variable called direction. And the initial value will be buy, for the simple example. Now let’s add an input variable. An input variable can be modified by the user without recompilation. It can be done by simply changing the Expert Advisor properties in the strategy tester or on real charts.

 

This variable will be an integer variable called take profit points and we are going to set the initial value to 50. Now let’s do that again, this time for the stop loss points. We are going to use 30 points for the initial stop loss. And inside the ontick function, we want to check if OrdersTotal equals zero. If that is true, we would have no open position. And we also want to check if the direction equals buy and if that is true, we want to open a buy ticket, that is done by using ordersend for the current symbol on the chart, we use OP_BUY because we want to open up by trade. For 10 Microlot, for the ask price, the tolerance value is three and this value here will set the stop loss for us to the value of points that we have created in the variable here, the next expression will set the take profit points. This could be a comment that we don’t need. We also don’t need a magic number and we do not need to set an expiration date.

 

The last parameter is for the color of the line, in this case, we use green. And because we want to change the direction every time, we are now going to set the direction variable to sell. That’s it for the buying part. For the selling part, it is almost equal.

 

We check if orders total delivers a value of zero and if the direction equals sell. And if that is true, we use OrderSend for the current symbol. This time we use OP_Sell because we want to sell 10 microlots. And when we sell, we need to use the Bit price, the tolerance value is the same. This will set the Stop-Loss above the current sell trade and this will set the take profit below the current sell trade. The rest is the same, except for the fact that we are using red lines for sell trades.

 

And after we have opened the ticket, we set the direction for the next trade to buy. Well, why would anybody do stuff like that? It’s a good way, for example, to check if the other components of a trading system work well. And when you do this here and set the values for the take profit and for the stop loss to different point values, you will likely learn something that you didn’t expect. Now, let’s add a comment statement for the text.

 

The current direction is followed by the direction. 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 even the Premium Course might be interesting for you, you can find that one on our Web page where we also offer a starter course for just one dollar right now. But for now, please click on the Compile button 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 reverse position dot .ex4 mark the option for the visual mode here and start your test. Here we are. This is the first position you can see that the take profit value is bigger than the stop loss value.

 

Now let’s click on Stop, Expert Properties and change the value to thirty. Click on OK, restart the test. And this time we can see that the values are equal. The current direction is buy. Let’s speed that up a little bit and you should see that it buys and sells as expected. So our little Expert Advisor is working and you have learned how to code an Expert Advisor that is able to change the trading direction for each trade. And you have coded it yourself with a few lines of MQL4 code.