MQL5 TUTORIAL BASICS – 41 SIMPLE SELL BREAKEVEN STOP

video
play-sharp-fill

 

  1. Introduction to Setting a Breakeven Stop for Sell Trades (00:00 – 00:10) Introduction to the concept of setting a breakeven stop for sell trades when the price moves favorably.
  2. Starting in MetaEditor and Creating a New Expert Advisor File (00:10 – 00:45) Instructions on opening MetaEditor and creating a new expert advisor file named “Simple Sell Breakeven Stop.”
  3. Setting Up the Code Structure and Opening a Sample Trade (00:45 – 01:07) Deleting unnecessary code and explaining how to open a sample sell trade using the ‘ctrade’ instance.
  4. Getting the Bid Price and Opening a Sell Trade (01:07 – 02:00) Explanation on how to get the bid price using ‘SymbolInfoDouble’ and opening a sell trade for testing purposes.
  5. Creating the Check Sell Breakeven Stop Function (02:00 – 02:18) Instructions on creating a custom function to check and adjust the breakeven stop for sell positions.
  6. Implementing the Custom Function (02:18 – 03:09) Detailed coding of the custom function, including the use of for loops and retrieving various position details.
  7. Setting Conditions for Adjusting the Stop-Loss (03:09 – 04:15) Conditions under which the stop-loss will be moved to the breakeven point, including checking the position type and current bid price.
  8. Modifying the Position and Finalizing the Code (04:15 – 04:40) Using ‘trade.positionModify’ to adjust the stop-loss and finalizing the code with necessary loops and function closures.
  9. Compilation and Testing in MetaTrader (04:40 – 05:11) Instructions on compiling the code and testing the expert advisor in MetaTrader using the strategy tester.
  10. Demonstration of the Breakeven Stop in Action (05:11 – 05:49) Demonstrating the breakeven stop on a MetaTrader chart and showing how it adjusts with the changing market price.

 

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.