- Introduction to Creating a Trailing Stop for Sell Trades (00:00 – 00:14)
- Introduction to the video’s goal: creating a trailing stop for sell trades in MQL5, demonstrated by the trailing stop adjusting automatically as the price falls.
- Starting with MetaEditor and Creating a New File (00:14 – 00:36)
- Instructions on opening MetaEditor, creating a new Expert Advisor file named “Simple Sell Trailing Stop”.
- Setting Up the Expert Advisor (00:36 – 00:44)
- Steps to delete unnecessary parts of the code and include the ‘Trade.mqh’ file for simplified trading functions.
- Creating an Instance of the CTrade Class (00:44 – 00:59)
- Creating an instance of the CTrade class named ‘trade’ for executing trades.
- Calculating the Bid Price and Checking for Open Positions (00:59 – 01:51)
- Using ‘SymbolInfoDouble’ to calculate the bid price and checking if there are no open positions to create a new sell position.
- Creating and Calling the Check Trailing Stop Function (01:51 – 02:04)
- Creating and calling a custom function ‘checkTrailingStop’ to manage the trailing stop.
- Implementing the Check Trailing Stop Function (02:04 – 04:10)
- Implementing the function to adjust the trailing stop for sell trades, including looping through open positions and modifying the stop loss.
- Compilation and Testing in MetaTrader (04:10 – 04:43)
- Instructions on compiling the Expert Advisor and testing it in MetaTrader using the Strategy Tester.
- Setting Up the Chart for Testing (04:43 – 05:09)
- Preparing the chart in MetaTrader for testing the Expert Advisor, including saving a template for the Strategy Tester.
- Running the Strategy Tester and Observing the Trailing Stop (05:09 – 05:32)
- Running the Strategy Tester to observe the trailing stop moving down as the price decreases.
In this video we are going to create a trailing stop for sell trades, you can see when the price falls that the trailing stop is adjusted automatically and now we want to find out how to do that in mql5.
To start please click on the little icon here or press F4 on your keyboard, now you should see the Metaeditor window and here you want to click on file, new file, expert advisor from template, continue, I will call this file: simple sell trailing 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 include the file trade dot mqh that one will give us simplified trading functions, and it includes a class called c trade, we will create an instance of that class called trade that is what we are going to use later on to open a new trade.
To do that we first need to find out the bid price that is done by using symbol info double, for the current symbol on the chart and we use symbol underscore bid – all in capital letters.
I also like to use normalize double and underscore digits because that will automatically calculate the number of digits behind the dot depending on the currency pair that could be 3 or 5 digits.
So let’s first find out if we have any open positions, and if positions total equals zero that would mean that we don’t have any positions. So on a demo account, we would now use trade dot sell, to sell 10 micro lot because we need a position otherwise, we cannot find out if the trailing stop works and afterwards we want to call the function check the trailing stop and pass the bid price.
Well, this function does not exist, so we now need to create it.
I use void because I don’t need to return any values, I just want to check and adjust the trailing stop.
Our function will take the bid price as a parameter and I would like to have the stop loss 150 points above the bid price. So let’s go through all the positions that is done with a for loop, positions total will give us the number of open positions and we want to count down until we have no open positions left.
First we need to get the position symbol for the current position because we only want to continue if the current symbol on the chart and the position symbol are equal and we also want to check if the position type is order type sell – all in capital letters – because if you mark that and press F1 you will see that this kind of order type is for a market sell order and that’s what we need.
If this is the case we want to get the position ticket – that’s kind of a license plate that will identify one particular position – and we get it by using position get integer.
Please use position underscore ticket – once again all in capital letters – afterwards we want to calculate the current stop loss that is done by using position get double and position underscore sl – also in capital letters – and if the current stop loss is above the stop loss that we want to have we use trade dot position modify for the current position ticket to adjust the current stop loss by 10 points.
Finally, we need to close the loops and that’s about it.