MQL5 TUTORIAL BASICS – 13 HOW TO OPEN A SIMPLE SELL TRADE

In this video we are going to find out how to create a sell order, whenever the first red candle appears on the chart, so let’s find out how to do that with MQL5.
First please click on the little button here or press F4 on your keyboard, 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: “SimpleSellOrder”, click on “Continue”, “Continue” and “Finish”.
Now you can remove everything above the “OnTick” function and let’s also remove the two comment lines here.
We start by creating an instance of “CTrade” so we include the file: “Trade.mqh”, this one comes with MQL5 and it provides simplified trade functions, so let’s create an instance called: “trade” that we are going to use later on to open our sell trade.
When we sell something we need to find out the Bid price, that is done with “SymbolInfoDouble” for the current symbol on the chart; this one is from the last tutorial, actually we need “SYMBOL_BID” here, you see this capital letters here are constants and when I mark “SYMBOL_BID” and press F1 we learn that we will get the sell offer and that’s what we need.
“NormalizeDouble” and “_Digits” is used because we have some currency pairs with three digits behind the dot while others have five digits behind the dot, “NormalizeDouble” is a way to tell MQL5 how many digits we want and “_Digits” is a way to automatically calculate the number of digits that we need for this particular currency pair.
Let’s continue with “MqlRates” and “MqlRates” has all the information about prices, volumes and spread and we will store it in an array that is called: “PriceInfo”, let’s use “ArraySetAsSeries” to sort the array (PriceInfo) from the current candle downwards and now we can use “CopyRates” to fill our array (PriceInfo) with price data for the current symbol on the chart and the currently selected period, we start with candle 0 (zero) – that’s the current candle – and we use price data for 3 candles and store it in the “PriceInfo” array.
For this very simple example we only need data for candle 1 because we are going to find out if the close price for candle 1 in our price info array (PriceInfo) is below the open price for candle 1 in the price info array (PriceInfo), that’s the case here; the opening price was higher than the close price so the candle is red.
Let’s check another condition because we only want to open a new position when we have no other open positions, so if “PositionsTotal” equals 0 (zero) – that would mean that we have no other open positions – and in that case we are going to sell.
To do that we use “trade.Sell”, remember this is the instance that we have created here, we need to pass a few parameters.
The first one is for the lot size, how much we are going to sell.
The second one is all capital letters for the current currency pair.
The next parameter is the current Bid price, we use the Bid price because we are going to sell, if we would like to buy something we would use the Ask price and I would like to have a stop loss that is 300 points above the Bid price, that’s parameter 4 and I would like to have the take profit 150 points below the Bid price.
The last parameter has no value in my case, you could use it as a comment and pass a string variable, I rarely do that but if you use several Expert Advisors on the same chart that might be interesting.
Okay, that’s it so far. Please click on the “Compile” button here and now you should get no errors and no warnings and in that case please click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we click on: “View/ Strategy Tester” or press CTRL and R, let’s pick the new file: “SimpleSellOrder.ex5”, mark the visualization option here and start a test.
Here we are, so far only bullish candles, here is the first red one and now we have an automated sell trade, so our little Expert Advisor works and in this short video you learned how to use MQL5 to open an automated sell trade when the first red candle on the chart appears and you have coded it yourself with a few lines of MQL5 code.