1. Creating Break-Even Stops for Buy Trades (00:01 – 00:18) Introduction to creating break-even stops for buy trades, demonstrating the initial stop-loss and its adjustment as the price rises.
  2. Starting in MetaEditor and Creating a New Expert Advisor File (00:18 – 00:53) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Buy Break-Even Stop.”
  3. Setting Up the Code Structure and Opening a Test Position (00:53 – 01:20) Deleting unnecessary code and explaining how to open a test position using the ‘ctrade’ instance and ‘’.
  4. Getting the Ask Price and Setting Initial Parameters (01:20 – 02:06) Explanation on how to get the ask price using ‘SymbolInfoDouble’ and setting up initial stop loss and take profit values.
  5. Creating the Check Buy Break-Even Stop Function (02:06 – 02:29) Instructions on creating a custom function to check and adjust the break-even stop for buy positions.
  6. Implementing the Custom Function (02:29 – 03:36) Detailed coding of the custom function, including the use of for loops and retrieving various position details.
  7. Getting Position Details and Setting Conditions (03:36 – 04:28) Retrieving position details such as open price, stop loss, and take profit, and setting conditions for modifying the stop loss.
  8. Adjusting the Stop-Loss Conditionally (04:28 – 05:14) Conditions under which the stop-loss will be moved to the break-even point, including checking the position type and current ask price.
  9. Modifying the Position and Finalizing the Code (05:14 – 06:10) Using ‘trade.positionModify’ to adjust the stop-loss and finalizing the code with necessary loops and function closures.
  10. Compilation and Testing in MetaTrader (06:10 – 06:50) Instructions on compiling the code and testing the expert advisor in MetaTrader using the strategy tester.
  11. Demonstration of the Break-Even Stop in Action (06:50 – 07:31) Demonstrating the break-even stop on a MetaTrader chart and showing how it adjusts with the changing market price.


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.