MQL4 TUTORIAL BASICS 41 SIMPLE SELL BREAKEVEN STOP

video

In this little video, we are going to find out how to set a breakeven stop for sell trades, this breakeven stop will be placed whenever the price runs into our direction, so let’s find out how to set the breakeven stop with mql4.
To do that please click on the little button here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new, expert advisor from template, continue, I will call this file simple sell breakeven stop, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here.
First, we want to use orders total to find out if we have no open orders, that would be the case if the return value is zero and if that is the case we want to open a sell ticket, that is a test position because we need an open position to set the breakeven stop.
You wouldn’t do that on your real account but for the strategy test we use order send to open a sell trade for ten micro lot and afterwards we want to check if we need to set a breakeven stop for our sell trade, that will be done by calling a function that is called check sell breakeven stop, it doesn’t exist so far so we need to code it now.
Our function will not have a return type and we use a for loop to go through all the open orders, we count down from orders total until we have no orders left and for each of the orders we use order select to find out if it’s a current position, that is done by using order select and the parameter mode underscore trades.
Mode trades is for open and pending orders while mode history is for closed and cancelled orders.
In the next step we check if the order symbol equals the current symbol on the chart and we also want to find out if the order type equals op sell, in that case, it’s a sell trade and we want to set a breakeven stop only if the current order stop loss is above the order open price and finally I would like to know if the current bid price is at least thirty points below the order open price and only if all those conditions are true we want to use order modify for the current order ticket with the current order open price to set the breakeven stop six points below the order open price.
We will not change the order take profit value here, we don’t need a time for expiration and we don’t need a color and that’s about it. We just need to close a few loops here and the function.
Well if this was too fast for you or if you don’t know what all the code here does maybe you want to watch the other videos in this basic series first or maybe even the premium course on the website is interesting for you, for now please click on the compile button or press F7, we don’t have 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, we click on view, strategy tester or press ctrl and r, please pick the new file simple sell breakeven stop dot ex4, mark the visual mode here and start a test.
Here we are! And in a few seconds, we should see the first breakeven stop appear on our chart, so our little expert advisor is working and in this little video you have learned how to place an automated breakeven stop for sell trades and you have coded it yourself with a few lines of mql4 code.