- Introduction to Order Cancels Order (OCO) System (00:00 – 00:09)
- Introduction to the concept of OCO, where one order’s execution cancels the other.
- Visualization of sell stop and buy stop orders on the chart.
- Understanding Orders and Positions (00:09 – 00:30)
- Explanation of the difference between an order and a position.
- How orders become positions when triggered by price movements.
- Creating an Expert Advisor in MetaEditor (00:30 – 01:01)
- Instructions on opening MetaEditor and starting a new Expert Advisor project.
- Naming the project ‘Simple Cancel Order.’
- Setting Up the Expert Advisor (01:01 – 01:30)
- Removing unnecessary lines and importing the ‘trade.mqh’ file.
- Creating an instance of the CTrade class.
- Initializing Trading Parameters (01:30 – 02:05)
- Getting account balance, equity, ask, and bid prices.
- Normalizing prices for different currency pairs.
- Creating Buy Stop and Sell Stop Orders (02:05 – 03:28)
- Conditions for opening pending orders when no positions or orders exist.
- Setting up buy stop and sell stop orders with defined take profit points.
- Implementing Order Cancellation Logic (03:28 – 04:49)
- Checking for active trading and differing balance and equity values.
- Creating a function to cancel open orders.
- Compiling and Testing the Expert Advisor (04:49 – 05:32)
- Compiling the code in MetaEditor.
- Instructions for testing the Expert Advisor in MetaTrader.
- Visualizing the Expert Advisor’s Functionality (05:32 – 06:06)
- Observing the behavior of the Expert Advisor with pending orders in MetaTrader.
- Explanation of how the Expert Advisor cancels one order when the other is triggered.
- Conclusion and Summary (06:06 – 06:06)
- Recap of how to create a simple OCO Expert Advisor in MQL5.
- Emphasis on the ease of coding this functionality with a few lines of MQL5 code.
Today we want to talk about a system that is called OCO that is the short term for Order Cancels Order.
Download “MQL5 - Simple Order Cancel Order EA from MQL5 Tutorial”
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 445 times – 1.55 KB