MQL5 Tutorial – Simple Sell Stop Expert Advisor


A sell stop is an order that has been placed into the market and that will be triggered whenever the price is falling below the price level that we have defined, this one has already been triggered, so let’s see if we can create another one. Here you see that we have placed a sell stop order and as soon as the price reaches the price level that we have defined a sell position is opened.

Now how can we create an Expert Advisor that is able to place such a sell stop?

To do that please click on the little button here or press F4 in your Metatrader. Now you should see the Metaeditor window and here you want to click on: ”File/ New/ Expert Advisor (template)” from template, “ Continue”, I will call this file: “SimpleSellStop”, click on “Continue”, “Continue” and “Finish”.

Now we can delete everything above the “OnTick” function and the two comment lines here.

We are going to start by using the “include” statement to include the file “Trade.mqh”, this one comes with MQL5 and we use the included “CTrade” class to create an instance that will be called: “trade” and we are going to use it to open our sell stop later on.

Inside of the “OnTick” function we get the Bid price, that is done by using “SymbolInfoDouble” for the current symbol on the chart, we use “SYMBOL_BID” to get the Bid price and with “NormalizeDouble” and “_Digits” we make sure that we calculate the right amount of digits behind the dot, that could be 3 digits like for this currency pair or 5 digits like for this one.

Let’s get the account balance by using “AccountInfoDouble”, “ACCOUNT_BALANCE” and we also want to calculate the equity by using “AccountInfoDouble” ,“ACCOUNT_EQUITY”, so if you mark “AccountInfoDouble” and press F1 you will see examples and here are all the values that you could use. This is all account related information. 

Now If we have no open positions and no open orders, so if “PositionsTotal” equals 0 (zero) and “OrdersTotal” also equals 0 (zero) we want to open a new order for a sell stop for 10 micro lot. The difference between a position and the order is that a position is something that has already been triggered, so this is a sell position but when the position is closed we place an order below the current price level and as soon our order is triggered it becomes a position.

To do that we use “trade.SellStop” to open an order for 10 micro lot, 100 points below the current price level, we don’t use a stop loss here and the take profit should be 300 points below the current Bid price, the last parameters here are used to set an expiration or a comment but we don’t need that.

Instead we use the “Comment” function to output the current balance and the current equity directly on our chart.

Okay, that’s it. Please click on the “Compile” button or press F7, and if you don’t have any errors here please click here or press F4 to go back to Metatrader.

Now in Metatrader we click on; “View/ Strategy Tester” or we press CTRL and R, let’s pick the new file here: “SimplesellStop.ex5”, mark the visualization option and start your test.

…and here we are! This is our sell stop and it will be triggered as soon as the price level drops below this line here, that has happened now and here is our first open position.

Now you know how to code an Expert Advisor that is going to create automated sell stops and you have done it yourself with a few lines of MQL5 code.


SimpleSellStop.txt – Downloaded 17 times – 718.00 B