MQL5 Tutorial – Simple Order Cancel Order EA

video
play-sharp-fill
Today we want to talk about a system that is called OCO that is the short term for Order Cancels Order.
If you look on our chart we have a sell stop here and we have a buy stop there and here is where the price is and if the price moves to the sell stop that would trigger a short position.
And if the price rises and hits the buy stop we would have a long position. Now what’s the difference between an order and a position?
You use an order if you want to open a position so you can place your orders in the market and if the price moves and hits your pending order it will become a position.
You see that we have cancelled the other order now. It’s no longer here, so let’s find out how to create an Expert Advisor that can cancel pending orders.
To do that please click on the little button here or press the F4 key and once you have done that piece click on File, New, Expert Advisor from Template, Continue.
I will call it Simple Cancel Order, Continue, continue and finish and now you want to remove everything above the OnTick function and the two comment lines here.
And to have anything to close we first need to import the file trade mqh by using the include function.
We want to create an instance of the Class Ctrade and that will have the name trade.
In the first step we need to get the account balance that is done by using account info double, Account_Balance.
Now we need to get the equity by using account info double, Account_Equity and we also need the ask prize here we use symbol info Double_Symbol for the current chart and the constant Symbol_Ask.
We also use normalized Double_Digits because we have currency pairs with five digits and other currency pairs with three digits and now we do exactly the same to get the bid price, but this time we use Symbol_Bid.
Now if we have no open position and no order, so if positions total equals zero and if orders total also equals zero, we need to open a pending order for a buy stop and another one for the sell stop, the position size is ten micro lot.
In both cases the buy stop order will be placed one hundred points above the ask price and the sell stop order will be placed one hundred points below the bid price.
For the current symbol we have not defined a stop loss, the take profit is three hundred points above the ask price and two hundred, no let’s also use three hundred points below the bid price.
We use other time GTC that means the order will stay until we cancel it, so we have no expiration time here.
If we would use an expiration time we would use this parameter to define it and the last parameter here is for a comment but we don’t need that.
If we are trading right now – that would mean that the balance has different value than our equity.
We want to cancel the open orders so let’s create a function called cancel order.
It’s a void type because it doesn’t need to return any kind of value and here we use For Loop.
It starts by using orders total minus one and until the value for the counter is zero that means as long as we have open orders it will go through all the open orders.
We use order get ticket for the current order to find out the ticket number and it now will delete the pending orders by using Trade.OrderDelete for the order ticket that we have calculated here.
And the last thing we need to do is to close the For Loop and the cancel order function.
Now let’s compile the code by clicking the little button here or pressing the F7 and that did actually work without any errors.
So now please click here or press F4 to go back into Meta Trader. And in MetaTrader you want to click on View, Strategy Tester or press control and R, and here you want to select the Simple Cancel Order.ex5 file, select any currency pair and enable the visualisation mode.
After you have done that please start your test. And here you see two pending orders; one is a buy stop and another one is a sell stop so let’s zoom in and see how it works.
That’s a little bit slow so let’s speed it up here.
Now you can see the pending order is gone, we have an open position and every time the open position is closed, two new pending orders will be opened here and as soon as the first order is triggered the other one will be cancelled.
Okay now you know how you can create a very simplified version of the order cancel order Expert Advisor and you have coded it yourself with a few lines of MQL 5 code.

Download “MQL5 - Simple Order Cancel Order EA from MQL5 Tutorial”

SimpleCancelOrder.txt – Downloaded 436 times – 1.55 KB