In this video we are going to find out how to open a simple buy position whenever we have a candle that is bullish and 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 one: “SimpleBuyOrder”, click on “Continue”, “Continue” and “Finish”.
Now we can remove everything above the “OnTick” function and let’s also delete the two comment lines here.
Okay. Inside the “OnTick” function we first want to check if we have no open orders that can be done by using the function “OrdersTotal”, it returns the number of market and pending orders and we want to find out if it is 0 (zero) so that would mean that we have no open positions and we don’t have any orders that might be triggered later.
In this simple example we want to check for one other condition and that is if the close price for candle 1 was above the open price for candle 1. Candle 1 is the last finished candle – that’s this one in our case – right now it’s early in the morning so there is not much movement here. When we mark the “close” statement and press F1 we learn that it is actually an array that contains close prices for each bar on the current chart, so we just have to put the candle number inside of the square braces here and if the close price for the last candle 1 is above the open price for the last candle 1 we want to buy.
Opening a position is a little bit complicated if you have never seen it before, in our case it looks like this. We use the “OrderSend” function and that requires a few parameters; we could have used everything in one line but to explain it I separated each parameter with a new line here.
The first one is for the current symbol – that’s the currency pair on the chart. We are suing Australian Dollar against US Dollar here. The second parameter is what we want to do – in our case we want to open a buy order, you could also do something like “OP_SELL” to open a sell order and there are pending orders like: buy limit, sell limit, buy stop and sell stop. “OP_BUY” is going to buy immediately, this parameter is for how much we want to buy – in my case I’m going to buy 10 micro lot or 0.10 lot and I am going to buy it for the current Ask price. If you double click one of the currency pairs here you will always see two price lines; a red one and a blue one. If you want to buy you will get the lower price and if you want to sell you must pay the higher price, the difference between those two prices is the spread, so we want to buy at the Ask price.
This is the so called slippage, we only want to buy if we don’t have a big price movement and in our case we are just going to buy if the price didn’t move more than 3 points.
The next parameter is the stop loss and as you might have guessed the one that comes after the stop loss is the take profit – that’s when our position will be closed either when we made a loss or when we made a profit.
We could also use a comment, I don’t do that, and otherwise you could use a “string” value here.
If you are willing to use different kinds of Expert Advisors on one and the same chart that would be helpful. The parameter after the comment is called a magic number. This one is not magic at all, it’s like a license plate on a car, you just give it a unique number to identify it.
This parameter would be the expiration; for a pending order you could set the order and set a deadline, so let’s say you don’t want to buy anything after a week has past and the last parameter “green” is used for the arrow that is drawn on the chart as soon as you open a new position.
So let’s compile the code. This worked without any errors so we can click on the little button here or press F4 to go back to Metatrader.
And in Metatrader we want to click on: “View/ Strategy Tester” or press CTRL and R, now we need to pick the new file: “SimpleBuyOrder.ex4” and start the test.
Here we are and as soon as we have the first bullish candle our buy trade is opened and now you know how to open a buy position for the first bullish candle that appears on the chart and you have coded it yourself with a few lines of MQL4 code.


SimpleBuyOrder.mq4 – Downloaded 3623 times – 764.00 B