In this video, we want to create an Expert Advisor that is able to close all the open orders for a currency pair at the same time and you see that we have already placed a few orders here.
As soon as the price rises high enough all the open orders here are closed, because we just made a profit. For this test, we opened five new test orders here, the price rises and now they are closed.
Now, how can we create an Expert Advisor in MQL4 that is able to close all open orders for one currency pair at the same time?
To do that please click on the little button here or press F4 on your keyboard and now you should see the Metaeditor, and here you want to click on “File, New, Expert Advisor from template. Continue. I will call it: Simple Order Close EA. Continue. Continue and finish.
Now you can remove everything above the OnTick function here and please also delete the two comment lines.
Okay, in the first step we want to check if we have less than five open orders.
That can be done by using the function: “OrdersTotal” and if the result is below 5 we want to send a buy order and in this case, we are going to open a test order.
That is done by using the function: “OrderSend”. For the current symbol on the chart we are using “OP_BUY” because it’s a buy order. We want to have a lot size of 10 micro lots.
We need to buy for the current Ask price. The Ask price is what you see when click on one of the currency pairs. The red line here the Ask price, the blue line here is the Bid price and if we buy something we have to pay the Ask price and when we sell it we will get the Bid price.
We only want our order to be valid if it is executed before the price has moved more than 3 pips. Each time the price moves here – this is at least one pip…
We have not defined a Stop Loss here, the Take Profit should be 10 points above the Ask price, we don’t use a comment, we also don’t need a magic number or an expiration date and we want a green arrow to appear on our chart as soon as we place the order.
If the account equity is above the account balance that would mean we have made a profit and now we want to close all trades for this pair.
So, now we need to create a function that can do that, that will be called: “CloseAllTradesThisPair” and now we need to go through all the open orders.
That is done by the “for” loop from the total number of open orders. We will count down to zero as long as we have other open orders.
This is done by decrementing the counter variable here, so let’s select a trade and if OrderSelect for the current position is true we want to check if the trade belongs to the current chart.
OrderSymbol will deliver the symbol name of the currently selected order and the function: “Symbol” returns the name of the symbol on the current chart.
And if those conditions are true we want to close the current trade order so we use OrderClose for the OrderTicket and the lot size of the order.
We close for the Bid price of the OrderSymbol, we only allow a deviation of 5 pips and we want to place a red arrow on the chart.
In the last step, we need to end the “for” loop and the function.
When you ready you can click on: “Compile” or press F7 and 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 select the file: SimpleOrderCloseEA.ex4, mark the visualization mode and start your test.
Now you should see that five orders have been opened immediately, let’s zoom in to the chart and speed the process up a little bit and all these orders will be closed at once as soon as the price rises high enough to create a profit.
Now you know how to create an Expert Advisor that can automatically close all open orders for a currency chart and you have programmed it yourself with a few lines of mql4 code.
Download “MQL4 TUTORIAL - SIMPLE ORDER CLOSE EA”SimpleOrderCloseEA.txt – Downloaded 3343 times – 871.00 B