MQL4 TUTORIAL BASICS – 112 SIMPLE BUY PERCENTAGE RISK

video

 

In this video, we are going to create an Expert Advisor that is able to close positions based on a user defined percentage value. 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 buy percentage risk. 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. We start with an input double variable. Input is a modifier that makes it possible to be changed by a user from the program properties. And this is the percentage risk value, zero point five would be a half percent. We also need a variable for the max position loss value. Inside of the ontick function we check if we have no open orders.

 

So if OrdersTotal returns a value that is below one, we would open a buy ticket or a buy order. This is just a test position. You wouldn’t do that on a real account, but we need something to close. So we now use Ordersend to open a buy trade for 10 Microlot on our demo account. Now let’s talk about the interesting part. We use For loop to go through all the open orders here. In our case, we will have only one order.

 

But on a real account, you would now go through each and every trade for every currency pair, select the order.

 

And afterwards we would calculate the profit for the position. That can be done by using order profit. It says that it will return the profit of the currently selected order. Well, that is not totally correct because it will calculate the profit without swaps and commissions. But for this simple example, it is good enough. Now let’s continue and calculate the max position loss. We use account balance divided by one hundred. So that would be one percent. And then we want to multiply it with percentage risk value.

 

That is the user defined value that the person in front of the screen has defined.

 

And if you wonder about this part, well, we need to calculate the negative value because actually a position loss is a negative thing. Now, let’s add two print statements. They will show us the position profit and the value for max position loss in our journal tab. And if the position profit is below the allowed max position loss, we would use order close for the current ticket and the current order lot size and close the order. This is the slippage value and the last value here is for the color.

 

Now let’s close the select part and the for loop here. And at the end of our little Expert Advisor, I would like to add a comment that will output the balance, the value for max position loss and the user selected percentage risk value on our chart. Well, that’s about it. If this was too fast for you or if you have no idea what all the code is good for, 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. This was once again a suggestion of a German Premium Course member. And as soon as this video is finished, I will send him the source code. So if you are a Premium Course member and have an idea for a video like this one, just let me know. For now, please click on the Compile button or press F7 on your keyboard, that should work without any errors here.

 

And if that is the case, you can click the little button here or press F4 to go back to Metatrader. And in Metatrader, we want to click on View, Strategy tester or Control and R.

 

Please pick the new file, simple buy percentage risk dot EX4. Click on expert properties. In my case, I will use the one percent risk value. For four hundred US dollars, that would be four dollars, so let’s click on OK, mark the option for the Visual mode and start your test. Here is our Expert Advisor. It is running. Now let’s click on the journal tab and we can see the current position loss. And as soon as it is bigger than four US dollars, our position will be closed here.

 

So our little Expert Advisor works as expected. And in this little video you have learned how to close buy positions based on user defined risk value and you have coded it yourself with a few lines of MQL4 code.