MQL4 TUTORIAL BASICS – 113 SIMPLE SELL PERCENTAGE RISK

video

 

In this video, we are creating an Expert Advisor for Sell positions, that makes it possible to use user defined Stop Loss 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 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 by creating an input variable.

 

If you mark that and press F1, you will see that input variables can be changed by the user from the program properties window without the need to recompile the code. The start value for our trading risk percentage is zero point five. That is one half of a percent. We also need another variable called max position loss. That is the maximal value that we are going to accept as a loss. Inside of the ontick function we need to check if we already have open positions.

 

So we use the function OrdersTotal. And if that returns a return value that is below one, it would mean that we have no open orders and no open positions. And if that is true, we are going to send a test sell order by using Ordersend for the current symbol on the chart. We want to sell 10 Microlot. Actually, we could also use the new syntax here, because when you mark that and press F1, you will see that you can also use _symbol. We don’t use a defined Stop Loss value here. The Take Profit is 20 points below the current Bid price. Please remember, this is just a test sell order. You wouldn’t do that on a real account. But as we need to close positions, we first need to have open positions. And actually that’s where demo accounts are good for. On your account, you could have more than one open position. So we are going to use a for loop here to go through all the open orders.

 

Now we can use orderselect for the current counter value and try to select a position. If that is successful, we will calculate the position profit by using order profit. We calculate the max position loss by getting the current account balance divided through one hundred, that would be one percent. And that will be multiplied with the percentage risk value that we have defined here. And if you wonder what that part here is about, well, the max position loss is a negative value.

 

Therefore, we use this expression to calculate the negative value. Let’s add two  print statements here. The first one is for the position profit. The second one is for the max position loss. And we will see the output in our journal. And if the position profit is below the max position loss value that we have allowed. Remember, this is a negative value – we will use order close for the current order ticket and the current order lot size to close the current order.

 

Let’s close the orderselect the For loop and to see what’s going on, I would like to add a comment statement for the current balance, the current max position loss and the percentage risk value directly on our chart. 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 videos from the basic video series first, or maybe even the Premium Course on our website might be interesting for you.

 

That’s also where you can find offers to download source codes like this one. But for now, please click on the Compile button here or press F7 on your keyboard. 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, please click on View, Strategy tester or press Control and R. Please pick the new file Simple sell percentage risk dot EX4.

 

Mark, the option for the Visual mode here and start your test, here we are. This is our first position. Now let’s click onto the journal tab. And here we see the values for the position profit. And as soon as the loss is higher than the allowed value, we should see that the position is closed. That happened right now. So our little Expert Advisor is working as expected. And in this little video, you have learned how to create an Expert Advisor that allows user defined values to close sell positions.

 

And you have coded it yourself with a few lines of MQL4 code.