MQL5 Tutorial – Simple trend based sell stop

video
play-sharp-fill

In this video we are talking about a sell stop that is triggered whenever we have a trend that is going down.
That is called it a short trend and if this value is negative we will create a sell stop here that is triggered when the price crosses the line we have set for our pending sell position.
Now how can we create an Expert Advisor that will open a pending sell stop when we have a short trend and when we have no other orders or positions?
Well, to do that please click on the little button here or press F4 in your Meta Trader and now you should see the Meta Editor and here you want to click on File, New, Expert Advisor from Template, Continue.
I will call it Simple Sell Stop, Continue, Continue and finish and now you can delete everything above the OnTick function.
To open our Sell Stop we need to include the trade.mqh file.
That’s a library that is included with MQL 5 and it will enable us to create an instance of the Class Ctrade.
In this case it will have the name trade and we will use this one to open our Sell Stop.
Let’s remove the two comment lines here and in the OnTick function we are going to create an array for several prices.
We want to use a simple moving average, that’s the red line here and that can be done by using the function iMA for the current symbol on the chart and the currently selected period on the chart.
Our moving average will be calculated for one hundred candles.
It will have shift value of zero, we will use Mode SMA because Mode_SMA stands for a Simple Moving Average and we use Price_Close because we want our iMA to be calculated for the close price of the candles.
Now that we have done that we want to sort the price array from the current candle downwards by using array set as series for the moving average array we have created here.
With Copybuffer we want to fill the moving average array according to the moving average definition that we have created here, for the first and only buffer, starting from the current candle zero, for ten candles.
And one more thing, to open a sell stop position we need the bid price and we get the bid price by using Symbol Info Double for the current symbol and we use Symbol_Bid and with NormalizeDouble and _Digits we get the right number of digits behind the dot, because depending on the currency pair you will have either three digits or five digits.
Now let’s calculate the long term trend, it’s a double value. We will call it Trend100 and we will calculate it by taking the value of the current candle and subtracting the value of the candle before.
And if that is negative that would mean the trend is falling, because Trend One hundred is below zero.
We want to check another condition, because we only want to open another pending order if no open positions or orders exist.
That can be calculated by checking if the result of PositionsTotal() equals zero, and checking if the result of OrdersTotal() also equals zero.
And if that is true we use trade.sellstop for ten micro lots. The sell stop will be one hundred points below the current bid price, for the current symbol, we have not defined a stop loss and the take profit will be three hundred points below the current bid price.
We use ORDER_TIME_GTC because ORDER_TIME_GTC is good until we cancel the order manually so it doesn’t expire.
So the expiration time here is zero and the last parameter is also zero. This one would stand for a comment but we don’t need one.
That’s almost it. To see what’s going on we want to use the comment function to output the text, “Trend100” and the calculate trend on our chart
Now let’s click on compile or press the F7 key and the compilation process worked without any errors, so now you can click on the little button here or press F4 to go back to MetaTrader.
And in MetaTrader you want to click on View, Strategy Tester or you can also press control and R on your keyboard.
And in the Strategy Tester you want to select the SimpleSellStop.ex5 file, select a currency pair and enable the visualisation mode.
Now let’s start your test and this is our code in action, you can see we have a short trend here and the sell stop is already placed and it will be triggered in a few seconds.
Now that happened, let’s select another currency pair and it also works for the Euro/US dollar.
Here is our Sell Stop and now the Expert Advisor will do it’s work completely automated.
Okay now you know how to create an Expert Advisor that will open pending Sell Stops whenever there is a short trend and you have coded it yourself with a few lines of MQL5 code.

Download “MQL5 - Simple Trend Based Sell Stop from MQL5 Tutorial”

SimpleSellStop.txt – Downloaded 632 times – 1.26 KB