MQL5 TUTORIAL BASICS – 41 SIMPLE SELL BREAKEVEN STOP

video

In this video, we are going to find out how to set a breakeven stop for sell trades when the price is running into our direction. You see the little red line here that is the breakeven stop that has been placed, so let’s find out how to do that with mql5.
To start please click on the little icon here or press F4 in your Metatrader, now you should see the Metaeditor window and here you would want to click on file, new file, expert advisor form 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 need to use the include statement to include the file trade dot mqh. This one comes with mql5 and it will give us some simple trading functions that we need to open a sample trade.
We create an instance of the class ctrade that will be called trade.
Inside of the on tick function, we first need to get the bid price because we are going to open a sell trade. To get the bid price we use symbol info double, for the current symbol on the chart, the parameter here is called symbol underscore bid – all in capital letters – and I also use normalize double and underscore digits to automatically calculate the right number of digits behind the dot.
And if we have no open positions, so if positions total equals zero, we use trade dot sell to open a sell trade for ten micro lot. You wouldn’t do that on a real account, but we need something to test.
And now we are going to call a function called check sell breakeven stop and pass the bid price as a parameter.
So far, this function does not exist, so we need to code it now.
Our function will not have any return value. We start with the for loop and go through all the positions. For each position, we want to find out the ticket number. That is done by using the position get integer, and the parameter to get the ticket number is position underscore ticket – all in capital letters.
We also want to know the open price, or in my case I have called it buy price.
That can be done by using position get double, position underscore price underscore open.
To get the position stop loss we use position get double, and we use position underscore sl.
And that is also the same for the take profit, we use position get double, and this time position underscore tp.
To find out if it’s a sell position, we need to find out the position type by using position get integer, position underscore type.
And to get the position symbol we use position get symbol for the current counter value, because in the next step we are going to check out if the current symbol on the charge equals the position symbol, and if that is true, we check if the position type equals position type sell, so that’s a sell position.
We only want to set a breakeven stop if the current position stop loss still is above the position buy price, and in my case I want to set the stop loss if the current bid price is at least thirty points below the position buy price.
And if all those conditions here are true, we can now modify the stop loss. That is done by using trade dot position modify for the current position ticket. I want to place the breakeven stop four points below the position buy price, and we leave the position take profit value as it is.
Finally, we need to close the for loop and 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 one of the other videos in this basic course, or maybe even the premium course on the website is interesting for you.
For now, please click on the compile button here or press F7. We did not get any errors here, and if that is the case, you can click on the 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 breakeven stop dot ex5, mark the visual mode here and start a test.
Here is our first position, and just now the breakeven stop has been placed, so our little expert advisor is working and in this little video you have learnt how to place a breakeven stop for sell trades when it’s going into your direction, and you have coded it yourself with a few lines of mql5 code.