MQL5 TUTORIAL BASICS 97 – SIMPLE REVERSE POSITION

video

 

In this video, we are going to create an Expert Advisor that is able to change the course direction for every trade. So let’s find out how to code that with MQL5. To do that, 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 reverse position. Click on continue, continue and finish.

 

Now you can remove everything above the Ontick function and the two comment lines here. We start with an include statement to include the file Trade.mqh. This one comes with simplified trading functions for MQL5 to use it, we now need to create an instance of the Class Ctrade.

 

We will call it trade. Let let’s also create some variables here. The first one is a string variable for the direction, the initial value will be buy for this simple example, the next variable is called take profit points. This one has a modifier that is called input. And this input modifier will make it possible to change the variable settings without the need to recompile the code. We can do it from the Expert Advisor settings. Now let’s create another one.

 

This one is called Stop Loss Points. We will set this one to thirty points. Inside of the ontick function we need to calculate the ask price, that is done by using symbolinfodouble for the current symbol on the chart. We use symbol underscore ask as we want to calculate the ask price and I like to use normalize double and underscore digits to automatically calculate the right number of digits behind the dot.

 

Now we will repeat the whole thing for the bid price. This is almost the same, except that we are going to use symbol underscore bit in this case. Now let’s check if we have no open position, that can be done by using positions total and if the return value is zero, we have no open position. And we also want to check if the current direction to trade is set to buy and if both conditions are true, we use trade.Buy to buy 10 microlot, for the current symbol.

 

Let’s actually say that. We want to buy for the current ask price. The next expression here is the stop loss. That will be set to the value that we have defined here, followed by the parameters for the take profit. That’s the value here. And the last variable here could be a comment. We don’t need that. So we sell it to Null. And as we want the next trade to go into the other direction, we will now change the direction to sell.

 

That’s it for the buying part, the selling part is very similar. First, we need to check if we have any open positions and if we don’t have, we want to check if the direction is set to sell and if both conditions are true, we use trade.Sell to sell 10 microlot for the current symbol on the chart. To sell, we need to use the bid price and of course, when we sell, we need to set the stop loss points to a value above the current price, and the take profit value will be below the current price.

 

Afterwards, we change the direction to buy.

 

Now, you might ask yourself, why would anybody do something like this? Well, it’s a good idea to test other components in your system. And actually, if you do this exercise and change the values for take profit and stop loss, you will probably learn something that you didn’t expect. I would like to see the current direction. So I’m going to add a comment statement here that will output the text: “the current direction is”, followed by the direction.

 

OK, 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 our basic video series or maybe even the Premium Course on our website might be interesting for you. Actually we currently also have the starter course on the website for just one dollar. But for now, please click on the Compile button or press F7 on your keyboard, you shouldn’t get any errors here. And if that is the case, you can click on the 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 file simplereverseposition.ex5 mark the option for the visual mode.

 

If you can’t see it, you might need to move the lower panel a little bit.

 

Here is the option, so please mark it, click on start to start your test and now we see the first trades here. That direction is changing from buy to sell, and we can see that the stop loss is a little bit bigger than the take profit value here. So let’s stop the Expert Advisor, click on inputs. Now, we can change the value for the stop loss or for the take profit points.

 

I have set those values to 30 here. Let’s restart the test, and this time we can see that both values are equal. So our little Expert Advisor works as designed. And in this little video, you have learned how to create an Expert Advisor that is able to change the trading direction for each position. And you have created yourself with a few lines of MQL5 code.