In this video, we are going to create an Expert Advisor that is able to close positions based on a risk percentage value. So let’s find out how to do that with MQL5.


To get started, please click on the 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 buy 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 and include statement to include the file Trade.mqh. That will make it possible to create an instance of the class  Ctrade, that is called trade.


We also want to create an input variable that is a user changeable variable for the percentage risk. In our case, we have set a percentage of zero point five percent based on the balance and we also need to define a max position, loss variable. So let’s continue with the calculation. First, we need to calculate the Ask price. That is done by using Symbolinfodouble for the current symbol on the chart, we use simple underscore ask. And with normalize double and Underscore digits, we make sure to automatically calculate the right number of digits behind the dot.


We also need to calculate the current account balance. We can do that by using account info double. And we use the parameter account underscore balance here. Now we need to open a test position. Of course we only do this on a demo account for testing purposes.


So when we have no open positions, we want to open one buy position. Let’s change that to 10 Microlot here so it will work on most accounts. Some brokers do not support one Microlot position sizes. And this will be our test position and we want to close it as soon as the loss is higher than the percentage value that we have defined here. So let’s create a for loop that will go through all the open positions until we have no positions left.


We use position get ticket for the current counter value of the For loop to get the position ticket number, and afterwards we will calculate the position profit. That can be done by using position get double and we use position underscore profit here. If you mark that and press F1 you will see that it says it will give you the current profit. That is not really the profit because you might also want to include position swap in the calculation if you hold positions for longer than one day.


But for this simple example, it’s good enough. Now let’s calculate the maximum position loss. We use the current balance. One percent of the balance would be  balance divided by one hundred. And we multiply it with the percentage risk value that we have defined here. In our case, it’s zero point five percent. And if you wonder about this part here, well we need to calculate the negative value for this variable, because a loss is actually a negative value.


Now, let’s add to print statements. That will output the position profit and the max position loss each time a new tick comes in. And if the position profit is below the max position loss, that would mean that we have a greater minus value here than the negative value for the max position loss. That’s when we use trade position close for the current ticket number. Please don’t forget to close the For loop and to see what’s going on, I would like to add a comment statement to output the balance, the max position loss and the percentage risk value that we have defined. 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 basic videos or maybe even the Premium Course on our website might be interesting for you. This actually was a suggestion of a German Premium Course member. And if you are a Premium Course member and have an idea for a video like this one, please just let me know. On our website we have also offers for source codes like this one.


I will send the source code for this video to the requester as soon as this video is finished. So for now, please click on View, toolbar and click on the Compile button here. Or press F7 on your keyboard. You shouldn’t get 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, please click on View, Strategy tester or press Control and R. Please pick the new file simple by percentage risk, mark the option for the visual mode here and start your test. Here we are. We see that trades have been opened and closed. We have a very low balance here, so let’s change that. I will use one thousand US dollars and on the inputs tab I will pick one percent. Now let’s restart the test. Here we are.


We have one position. If we click into the journal, we can see that the position profit changes with each tick. Now, let’s speed that up a little bit, and as soon as the loss is higher than 10 dollars here, we should see that the position is closed automatically. In this case, it doesn’t seem to work because it takes forever. Let’s speed that up a little more. Look into the Ctrade history. And here is our position close, we can see the position profit in the journal, and whenever the loss is bigger than the percentage value that we have defined here, we should see that the position is closed.


So in this little video, you have learned how to close Buy positions based on a percentage risk value that you have defined and you have coded it yourself with a few lines of MQL5 code.