MQL5 TUTORIAL BASICS – 40 SIMPLE BUY BREAKEVEN STOP

video

In this video we are going to create break-even stops for buy trades, this is the initial stop-loss for this trade and when the price rises as soon as it passes the buying price you will see the little red line appears on the chart so let’s find out how to do that with mql5.
We start by clicking on this little icon here or we can also press F4 on the keyboard, now you should see the Metaeditor window and here you can click on file, new file, expert advisor from template, continue, I will call this file simple buy break-even stop, click on continue, continue and finish.
Now you can delete everything above the on tick function and the two comment lines here, we start by including the trade dot mqh file that comes with mql5 and we will use an instance of ctrade called trade to open a test position. You wouldn’t do that on a real account but we need to open a test position so we can set the break-even stop.
For a buy position we need to find out the ask price, that is done by using symbol info double for the current symbol on the chart, we use symbol underscore ask – all in capital letters – and with normalize double and underscore digits we automatically calculate the right number of digits behind the dot.
I only need one test position so I check if the return value for the function positions total equals zero and if that is true we use trade dot buy to open the test position for ten micro lot.
We also set a stop loss and take profit value here and at the end of the on tick function, we want to call another function that shall be called check buy break-even stop, we passed the ask price as a parameter and this function doesn’t exist so far so we need to code it now.
Our custom function has no return value and it will take the ask price as a parameter, we will use a for loop, our counter is an integer for the number of the position and we will count down from positions total until there are no other positions left.
For each position we need to get the ticket number that is done by using position get integer, position underscore ticket, that value will be stored in a variable called position ticket, that’s an unsigned long variable and that’s a little bit crazy because we use position get integer so you would expect an integer value here but when you mark position ticket and press F1 you will see that it actually returns a long value here.
We also need to get the position buy price or maybe I should say the open price for the position that is done by using position get double and we use position underscore price, underscore open, let’s repeat that two more times because we also want to know the position stop loss and the position take profit value, both can be calculated by using position get double and to get the direction for the position we use position get integer for position underscore type, that will give us the direction because we need to know if it’s a buy or a sell position.
To make sure that the position belongs to the current chart we use position get symbol for the current position number and now we can check the conditions, we only want to move the stop-loss if the current symbol on the chart equals the symbol of the position, we also want to make sure that the position type is equal to position underscore type, underscore buy, that is the case when we have a buy trade, we only want to move the break-even stop if the current position stop-loss is below the position buy price.
And finally, I want to know if the current ask price is above the position buy price plus thirty points that’s when I want to set my breakeven stop.
If all those conditions are true I use trade dot position modify to modify the current position for the current position ticket and want to move the current stop-loss to four points above the position buy price, that’s the price that we paid when we opened the buy position and in this case I leave the position take profit value unchanged. Let’s reformat the code here, we need to close the for loop and we need to end the function that should be it.
Well, if you have no idea 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 first or maybe even the premium course on the website might be interesting for you, for now please click on the compile button, I didn’t get any errors here, just one warning about possible data loss because of a type conversion and if that is also the case for you you can click on a little button here or you can 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 break-even stop dot ex5, mark the visualization option here and start a test.
Here is the expert advisor, let’s zoom into the chart, the price is far below the buy price so we don’t see any break-even stop here but now you could see that the little red line here is the break-even stop so the expert advisor really works and you have coded it yourself with a few lines of mql5 code.