How to Get One Cancels the Other (OCO) Orders on MetaTrader 5

One cancels the other on MetaTrader 5

When you buy something through one of the links on our site, we may earn an affiliate commission.

I started using MetaTrader back in about 2008.

Unfortunately, it hasn’t changed much, even with the launch of MT5.

That means it still doesn’t have the “One Cancels the Other” order function. Some people, including myself, prefer to call this “Order Cancels Order.”

Whatever you call it, now we can now easily add this order type to MT5 with the help of AI.

You don’t need to know how to code. 

In this tutorial, I’ll show why OCO is so useful and how to use ChatGPT to easily create your own OCO EA for MetaTrader 5.

One cancels the other on MetaTrader 5

How OCO Works

“One Cancels the Other” (OCO) trading orders are a type of conditional order where two pending orders are placed simultaneously, but only one can be executed.

When one of the orders is triggered and filled, the other is automatically canceled.

This can be very useful for taking advantage of different trading opportunities.

OCO Example

Here’s an example where you can use an OCO order to take advantage of a potentially profitable situation.

AUDCAD chart

This Pin Bar is also an Engulfing Bar, which could signal a top.

The previous bar had very high volume, showing that this trend could be coming to an end because traders appear to be bailing out.

Now I could enter this trade now, but there is no guarantee that price will drop as expected.

So I can set up 2 pending orders, one sell limit above the current price (higher blue line) and a sell stop below the current price (lower blue line).

The first thing that could happen is price could retrace before moving back down.

If price does retrace, I will enter the trade at a slightly better price and make a little more money. The second trade will be cancelled and I won’t take a duplicate trade.

Even if price retraces and hits a stop loss above the candle, the loss will be smaller than if I entered at the close of the current candle.

Another thing that could happen is price could just head straight down, as expected. In this case, the sell stop (lower blue line) will be hit and the retracement trade will be cancelled.

This pending order will get me into the market if goes in the direction that I expect, but doesn’t retrace. It’s the best of both worlds.

If price doesn’t go as I planned, I’ll lose less money because the sell limit will get hit instead of this sell stop.

As you can see, using a OCO order can be extremely useful.

The amount of money I save by doing this can really add up over time.

MT5 EA Design

Before we talk to ChatGPT, we have to put a little thought into how we will design this MT5 EA.

There are only 2 things that we have to account for.

First, there might be other pending trades in the same market, so we would have to account for that and only cancel orders that we mark for cancellation, and not any others.

Second, we only want to cancel trades in the same market of the chart that the EA is attached to.

This will rule out any trades in other markets that happen to have the same marker.

In MetaTrader 5, we can use the comment field to easily mark trades that we want to apply the OCO logic to.

It takes a little getting used to because in order to use the comment field, the order has to be entered via the right-click menu and not the one-click button.

But it’s not a big deal.

The ChatGPT Prompt to Get OCO in MT5

So for this EA, I’m going to prompt ChatGPT with the following:

Please create an EA for MetaTrader 5 that looks for open orders with a comment in the market of the chart that the EA is attached to. When an open order is found, cancel all pending orders with a matching comment, in the market of the chart that the EA is attached to. Look for any trade with a comment and close the pending orders that have a matching comment. Do not provide an input field for the comment to be searched for. Automatically match the comment fields.

Feel free to copy and paste that prompt and use it for yourself.

But remember that I’m NOT liable for whatever happens if you choose to use that prompt. 

ChatGPT or another AI might give you a different result than what I got, depending on which model you use, so you always have to test an EA thoroughly before using it in a live account. 

Keep reading to learn how to test it.

Create the Expert Advisor in MT5

Once you get the code from ChatGPT, simply hit the Copy link in the upper right corner of the code window to copy the code.

Copy link in ChatGPT

Then open MT5 and click on the IDE button at the top.

IDE button in MT5

This will open the MQL5 Editor.

Click on the New button in the upper left corner of the screen.

A window will come up with a wizard.

  • On the first screen, select: Expert Advisor (template)
  • Click Next
  • Give the EA a name
  • Keep clicking Next until you get to the end of the wizard

The rest of the settings don’t matter because you’re just going to copy in the code from ChatGPT anyway.

When you see the EA template that MT5 created for you, click anywhere on the screen and it Control+A to select all the text.

Then hit Backspace or Delete on your keyboard to delete everything.

Now hit Control+V to paste the ChatGPT code into the blank area.

Finally, click on the Compile button on the top of the screen to check the code.

If it compiles without errors, congratulations!

Skip down to the installation section below.

Otherwise, use the next section to fix the errors.

Troubleshooting

ChatGPT might not get it right the first time.

When you compile the code, there’s a good chance that you’ll get some errors or warnings like this.

MT5 errors

When that happens, simply highlight all of the error messages in MT5, by using Shift+left mouse click and right-click > copy to copy them.

Then go over to ChatGPT and ask it to fix it.

For example:

Telling ChatGPT to fix compile errors

You might have to do this a few times, but it usually figures it out eventually.

Remember that is only a syntax test.

Now you have to check the logic of the program.

Put it into a demo account and test until you’re confident in the results.

If you’re having problems that you cannot correct, check to see that you’re using MT5.

The code for MT4 and MT5 are different, so you cannot use this EA in MT4.

How to Install the EA

Once the code compiles, go back to the main MT5 window and look in the Navigator window.

If the Navigator section is not available, then go to: View > Navigator to open the window.

Go to the Expert Advisors section and click and drag the EA you created onto the chart.

Add EA to chart

Once it is on the chart, you’ll see the name of the EA in the upper right corner of the chart.

The hat icon should be blue to show that it’s running.

MT5 icon

If it’s not blue, then click the Algo Trading button at the top of the screen to make sure that the EA is activated.

Test the EA

Now it’s time to test your new EA.

Be sure to test this in a DEMO account, just in case something goes wrong. 

To take a trade, right click on the chart you want to trade on.

Go to: Trading > New Order

Comment

In the comment field, enter anything you want to identify the order.

It could be something like “999” or whatever is easy to remember.

Enter the details for the pending order, then open a new pending order and enter the same comment text in the comment field.

To test this quickly, set 2 orders that are very close to the current price, so one of them gets executed right away.

You can also see how the EA treats multiple pending orders with the same comment.

Try every kind of weird situation you can think of to be sure that the EA is working as expected.

Test it until you are 100% confident that it’s working correctly.

More Troubleshooting

If there are issues with the logic of the EA, then go back to ChatGPT to work them out.

At some point, you’ll have to start understanding the code, so now is a good time to start.

You don’t have to know how to code, but you should know how each part of the code works.

Luckily, you can get ChatGPT to explain it to you.

It’s actually quite good at explaining.

Keep asking it questions until you understand how the program works.

Remember that ChatGPT might sound all professional, but it doesn’t know as much as you think.

Tell it exactly what you want and don’t let it insert logic that it thinks is good, but isn’t in line with what you want. 

Like any other skill, building with ChatGPT can be a little frustrating in the beginning.

But once you get the hang of it, it’s extremely powerful and help you build all the trading tools that you’ve always wanted.

Final Thoughts

So that’s how to create an OCO EA for MT5.

But this is just the beginning.

What else can you make?

Use your creativity and let your imagination run wild.

You might just create something amazing…and super profitable.

 

 

The post How to Get One Cancels the Other (OCO) Orders on MetaTrader 5 appeared first on Trading Heroes.

Original source: https://www.tradingheroes.com/one-cancels-the-other-oco-mt5/

You May Also Like