MQL5 TUTORIAL – SIMPLE SELL TRADE EXPLAINED (in 5 minutes)

 

In this video, we are going to create a simple ExpertAdvisor that is going to open a sell trade whenever the last candle was bearish and when we have no open positions.

 

Let’s see how we can do that with MetaTrader5. With MQL5 for MetaTrader 5, we can create an Expert Advisor that will execute trades based on certain market conditions. In this video, we want to create an Expert Advisor that will sell when the last complete Candle is bearish and there are no open positions. Let’s see how we can do that. Opening Meta Editor and Creating a New File. Please click on the little button here or press F4 in Meta Trader. Now you should see the meta editor window. Here, we need to include the necessary MQL5 header file for trading operations, trade.mqh, and create an instance of the Ctrade class, which will allow us to execute trades. Defining the market data. In the onTick function, which is called every time the market data is updated, we first get the current bid price for the symbol using the symbol info double function and normalize it to the correct number of decimal places using normalized double.

 

Next, we create an array called price info to store the price data for the last three candles.

 

We then use the the array set as series function to sort the array in reverse order so that the most recent candle is at index zero. We fill the price info array with the actual price data using the copy rates function. We start from the current candle zero and copy the data for the last three candles. Checking the market condition. Now we check if the last complete candle, candle one, is a bearish candle, meaning the closing price is lower than the opening price. Executing the trade. If we don’t have any open positions, as indicated by the positions total function returning zero, we will place a sell order using the trade.sell function. This function takes several parameters. 0.10. This is the volume or lot size for the trade, in this case, 0.10 lots. Null. This is the symbol which is set to the current symbol by default. Bid. This is the current bid price, which is the price we will use to enter the short position. Bid plus 300 to a point. This is the stop loss price, which is 300 points pips above the current bid price. Bid 150 a point. This is the take profit price, which is 150 points pips below the current bid price. Null. This is an optional comment for the trade. Compiling the code. Now we can press F7 to compile the code in Meta Editor, testing the Expert Advisor. If this was too fast for you or if you don’t understand what all the code is doing, you may want to check out the premium course on our website or watch one of the basic videos first.

 

Otherwise, let’s go back to Meta Trader by pressing F4 or clicking the icon.

 

Then press CTRL+R to start the strategy tester. Select the ExpertAdvisor we just created, enable the visual mode, and start the test. You should now see the ExpertAdvisor on the chart. If you’re already a premium member and have an idea for a video like this, feel free to send us an email. We’ve just created a simple ExpertAdvisor that will sell when the last complete candle is bearish and there are no open positions, using a few lines of MQL code. Thanks for watching and I’ll see you in the next video. Okay, by now you should see the ExpertAdvisor doing its work on your chart. If everything went well, that is fine. If you have no idea what all the code does, maybe the premium course might be interesting for you. You can check it out on our website. For now, I say thank you for watching, and I will see you in the next video.