In this video, we are going to create an Expert Advisor for Sell positions, that is able to close positions when a defined risk percentage value has been reached. So let’s find out how to do that with MQL5. 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 Sell percentage risk.
Click on Continue, continue and finish. Now you can delete everything above the Ontick function and the two comment lines here. We start with an include statement to include the trade Library. This is the file Trade.mqh. It is part of MQL5 and it will help us to use some functions to open trades. Therefore, we create an instance of the class Ctrade that is called Ctrade, our goal for this Expert Advisor is to create an input variable for the user that will be called percentage risk value.
This is the start value and it can be changed by the user without Recompilation. Because if you mark input and press F1, you will see that values of input variables can be changed by the user in the properties window of the Expert Advisor. Now let’s add another variable called Max Position Loss. This is the max loss that we are going to accept. Inside of the ontick function we start by calculating 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 can automatically calculate the right number of digits behind the dot. The second thing I want to calculate is the balance. That can be done by using account info double and we are going to use account underscore balance in this case. If you mark that and press F1, you will see that you can get all kinds of information about your account. And as we want to close sell position, we need to have a test position. So we check if PositionsTotal is less than one, that would mean we have no open positions and now we can use trade.Sell to open a 10 Microlot position. You wouldn’t do that on a real account, but that’s what demo accounts are for. And without a position, we cannot close anything. On a real account we could have more than one position, so we would use a for loop to go through all the open positions. We use buy position getticket for the current counter value to get the ticket number for the position.
Afterwards, we use position get double to get the position profit, because if we have the position profit, we also know the position loss and now we can calculate the max position loss.
We are going to divide the balance by 100 to get one percent. That will be multiplied with the percentage risk value. And if you wonder what that part is for, we need to have a negative value because a position loss as a negative value. Let’s add two print outputs, so we can see what’s going on in the journal. And if the position profit is below the max position loss that we have defined, or in other words, if the position loss is bigger than the accepted loss, we use trade position close for the current ticket to close the current position.
Let’s end the for loop here and to see what’s going on, I would like to add a comment statement that will output the balance, the max position loss and the percentage risk value directly on our chart. 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 on our website might be interesting for you.
That’s also the place where you can find offers to download source codes like this one. But for now, please click on View, Toolbar. Now click on the Compile button or press F7 on your keyboard. You shouldn’t see 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 inside of Metatrader we want to click on View, Strategy tester or Press Control and R. Please pick the new file. Simple percentage risk.ex5, mark the option for the Visual mode and test start your test. Here we have a sell position, it’s currently in the profit, but as soon as the loss is getting too big, we should see that the position will be closed. So let’s speed that up a little bit here. Now we have a bigger loss and now the position was closed. Let’s click onto the journal tab, here we see the current position profit and the max position loss.
So our little Expert Advisor works as expected. And in this little video, you have learned how to set a user defined percentage value to close positions when they have reached a user defined loss. And you have coded it yourself with a few lines of MQL5 code.