In this video we want to find out how to set a breakeven stop, that’s the red line here, actually, it’s a little bit above the buying price so let’s find out how to do that with mql4.
To get started please click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor and here you want to click on file, new, expert advisor from template, continue, I will call this file simple buy breakeven stop, click on continue, continue and finish.
Now you can remove everything above the on tick function and the two comment lines here, we start by checking if we have no open orders, that would be the case if the function orders total delivers a return value of zero, and if that is the case we want to open a test position, you wouldn’t do that on a real account but for the strategy tester we need an open position so that we can set the breakeven stop.
We use order send to open a buy trade here and afterwards we want to check if the breakeven stop for the buy trade is okay.
So far this function does not exist so we need to code it now, our custom function does not return any value, it is called check buy breakeven stop, we use orders total to get the number of all the orders and this loop here will count down until there are no orders left, we check if the selected order is actually an open position, we do that by using order select for the current number of the position, we use select by position – all in capital letters – and we use mode underscore trades. If you mark that and press the F1 key you will learn that mode underscore trades will be used to find open and pending orders, while mode history would be used if we need cancelled and closed orders, so if we have an open position we want to check if the order symbol actually is equal to the currency pair on our chart, and we want to know if the order type equals op underscore buy and if that is the case we actually have a buy position.
We also want to know if the order stop loss is below the order open price because it doesn’t make much sense to set a breakeven stop when the current stop loss would be far above the open price and finally we need to check if the current ask price is above the order open price – in my case actually I want to set the breakeven stop when it’s thirty points above the open price – and only if all those conditions are true we want to use order modify to move the stop loss to four points above the open price for the current order ticket that was opened for the order open price, we set the stop loss to order open price plus four points, the order take profit will remain unchanged, that’s also true for the last two parameters, we don’t have an expiration value so we set it to zero, and we use clr underscore none because we don’t need any color, please don’t forget to set two curly braces here, one is to close the for loop and the other one to the function and that’s about it.
If you don’t know what all the code here does or if this was too fast for you maybe you want to watch the other videos in this basic video series or maybe even the premium course on the website is interesting for you, for now, please click on the compile button or press F7, you shouldn’t get any errors here, we only have one warning, we could check the return value for order modify but for this simple example we don’t need to do that, so if you don’t have any errors please click on a little button here or press F4 to go back to Metatrader.
And in Metatrader we click on view, strategy tester or press control and r, please pick the new file simple buy breakeven stop dot ex4, mark the visual mode here and start a test.
So let’s zoom into the chart, we have opened a buy trade and here comes the breakeven stop, so in this little video, you have learned how to automatically set a breakeven stop once your buy trade is going into the right direction and you have coded it yourself with a few lines mql4 code.