Now Create an Equity Based Stop Loss Module in MQL4
If there is one thing most traders really hate, its losses and everybody is trying to avoid losses. But there’s also a saying, “Cut your losses short and let your profits run.” You might like the results you have seen in the last run of the BollingerBand EA, but you might not like the draw down here. In back testing, we cannot avoid it because we have told Metatrader to close every trade at the end of testing period and you do not see any losses between the start and the end.
In back testing, it’s absolutely no problem to wait a few seconds, but in real life, it would be hard to wait from end of February until the beginning of May. And I can tell you this trade was an open loss for more than 2 months, but humans don’t like losses, so they are going to work with a stop loss. I have created an equity based stop loss module that will take a loss of 200 dollars.
If the current loss is higher than the max loss we have defined, it will close all orders for this pair. The rest of the EA is unchanged, but we need to include another module to close the orders and here it is. If you code anything for yourself, you should use this command lines because just a few weeks after you coded it, you won’t understand your own code.
But now, you can see what each line does. It will count all the open orders, select one, check if it is the right currency pair, get the order type, and close the order. Please hit F4 and start a back test.
Remember, this was the result we had. And, with the max loss of 200 dollars, we might see the same result. So let’s change that to 20 dollars.
Remember, we want to avoid losses and now, this is the result for 20 dollars. Let’s make it 2 dollars. And this is what happens if you set your max loss to 2 dollars. You wanted to avoid losses and with the too small stop loss, you have created a big one here. So if you keep your losses short, you might create actually lots of them and the result will be a very big loss.