1. Introduction to Creating a Sell Order on First Red Candle in MQL5 (00:01 – 00:15)
    • Introduction to the tutorial on creating an automated sell order in MQL5 when the first red candle appears on the chart.
  2. Opening MetaEditor and Creating a New File (00:15 – 00:37)
    • Instructions on opening MetaEditor and creating a new Expert Advisor file named “SimpleSellOrder”.
  3. Setting Up the Code Structure (00:37 – 00:45)
    • Removing unnecessary code above the OnTick function and preparing the code structure.
  4. Creating an Instance of CTrade and Importing Trade.mqh (00:45 – 01:02)
    • Including the ‘Trade.mqh’ file and creating an instance of CTrade for simplified trade functions.
  5. Calculating the Bid Price and Normalizing It (01:02 – 01:52)
    • Using ‘SymbolInfoDouble’ to calculate the Bid price and normalizing it with ‘_Digits’ for correct digit formatting.
  6. Setting Up MqlRates and Price Information Array (01:52 – 02:33)
    • Using ‘MqlRates’ to store price, volume, and spread information in the ‘PriceInfo’ array.
  7. Determining Conditions for a Red Candle (02:33 – 03:07)
    • Checking if the close price of the previous candle is below its open price, indicating a red candle.
  8. Checking for Open Positions and Placing a Sell Order (03:07 – 03:30)
    • Ensuring no open positions exist and then using ‘trade.Sell’ to place a sell order.
  9. Configuring Sell Order Parameters (03:30 – 04:20)
    • Setting parameters for the sell order, including lot size, currency pair, Bid price, stop loss, and take profit.
  10. Compiling the Code and Testing in MetaTrader (04:20 – 04:53)
  • Compiling the code and testing the Expert Advisor in MetaTrader using the Strategy Tester.
  1. Observing the Automated Sell Trade on First Red Candle (04:53 – 05:29)
  • Watching the Expert Advisor execute a sell trade on the appearance of the first red candle in the test.


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.


SimpleSellOrder.mq5 – Downloaded 73 times – 1.02 KB