MQL5 TUTORIAL BASICS – 12 HOW TO OPEN A SIMPLE BUY ORDER

In this video we want to find out how to open a simple buy position when a bullish candle appears on the chart, in this case we have already opened this buy trade here and now we are going to find out how to code that in MQL5.
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: “SimpleBuyOrder” click on “Continue”, “Continue” and “Finish”.
Now we can delete everything above the “OnTick” function and let’s also remove the two comment lines.
We start by creating an instance of “CTrade”, “CTrade” will help us to use simple buy and sell functions, to use it we need to include the “Trade.mqh” library, the “Trade.mqh” library comes with MQL5 and now we use it to create an instance called: “trade” that we are going to use later on to open a position.
As our trade is going to be a buy position we first need to find out the Ask price, that is done by using the “SymbolInfoDouble” function, for the current symbol we use “SYMBOL_ASK” to get the Ask price and I prefer to use “NormalizeDouble”, “_Digits” to get the right number of digits behind the dot because some currency pairs have five digits behind the dot while others only have three digits behind the dot, so this line will give us the Ask price.
With “MqlRates” we create a “PriceInfo” array for the price data, with “ArraySetAsSeries” we sort the price info array (PriceInfo) from the current candle downwards and now we use “CopyRates” to copy the price data into our “PriceInfo” array and we do it for the current symbol and the current period – in my case the current symbol is Euro against US Dollar and the current period is the 1-minute chart – actually we use “_Symbol” here, so let’s do that also in this line. “_Symbol” and “_Period” actually do the same, I don’t know why they did it, we start with candle 0 (zero) and copy the prices for 3 candles in our price info array (PriceInfo) and now we can use it to find out if our last candle is bullish, that would be the case when the close price for candle 1 in our price info array (PriceInfo) is above the open price for our candle 1 in the price info array (PriceInfo).
I also want to check another condition; I want to know if we have no other open positions that can be done by using “PositionsTotal” and if that equals 0 (zero) we now can actually open a position and that is where we use the trade instance that we have created here, now use “trade.Buy” to buy 10 micro lot, the second parameter is for the symbol; “NULL”, all capital letters will give us the current symbol on the chart, we need to pay the Ask price, our stop loss will be 300 points below the current price, this parameter here is our take profit, so when the price rises 150 points above the current Ask price we will close the position for a profit, and the last parameter here is used to pass a comment, I don’t need one, but if we cut the last parameter here and set a comma you will see that you could use a “string” to pass a comment and that might be helpful if you want to use several Expert Advisors on one and the same chart but I don’t do that.
Actually we could separate the whole buy statement here to make it more readable, let’s add a few comments here, this is the take profit and the last one would be the comment and that’s it.
When you are done you can click on the “Compile” button or press F7, you shouldn’t get any errors here and in that case you can click on the little button here or press F4 to go back to Metatrader.
In Metatrader you want to click on: “View/ Strategy Tester” or press CTRL and R please pick the “SimpleBuyOrder.ex5” file, enable the visual mode here and start the test.
…and now you should see the first bullish candle opens a buy trade, so our little Expert Advisor is working as expected and now you know how to open a buy position for Metatrader and you have coded it yourself with a few lines of MQL5 code.