MQL5 TUTORIAL – SIMPLE BUY ORDER EXPLAINED

 

In this video, we are going to create a simple ExpertAdvisor that is able to create a buy trade whenever the last candle was bullish!

 

Let’s find out how we can do that with MQL5. With MQL5 for MetaTrader5, 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 buy when the last complete candle is bullish 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\Trade. Mqh, and create an instance of the C Trade 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 ask 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 array set at 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 bullish candle, meaning the closing price is higher than the opening price. Executing the trade. If we don’t have any open positions as indicated by the position’s total function returning zero, we will place a buy order using the Trade Buy 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. Ask, this is the current ask price, which is the price we will use to enter the long position. Ask – 300 _POINT. This is the stop loss price, which is 300 pips below the current ask price. Ask plus 150 _POINT, This is the take profit price, which is 150 point pips above the current ask price. Null. This is an optional comment for the trade.

 

Compiling the code!

 

Now we can press F7 to compile the code 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 Expert Advisor we just created, enable the visual mode, and start the test. You should now see the Expert Advisor 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 Expert Advisor that will buy when the last complete candle is bullish 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 running on your chart, if everything went well. I would like to say thank you for watching. And if you have an idea for a video like this one, and if you are already a premium customer, just drop me a line, otherwise maybe the premium course might be interesting for you. You can find that one on our website. Thank you for watching, and I will see you in the next video.