Economic InsightEconomic InsightEconomic Insight
Notification Show More
Font ResizerAa
  • Home
  • Business News
  • Economics
  • Finance
  • Investment
  • Stock Market
  • Trading
Reading: How to Get One Cancels the Other (OCO) Orders on MetaTrader 5
Share
Font ResizerAa
Economic InsightEconomic Insight
  • About Us
  • Privacy Policy
  • Terms of Use
  • Disclaimer
  • Contact
Search
  • Home
  • Business News
  • Economics
  • Finance
  • Investment
  • Stock Market
  • Trading
Have an existing account? Sign In
Follow US
Economic Insight > Blog > Trading > How to Get One Cancels the Other (OCO) Orders on MetaTrader 5
How to Get One Cancels the Other (OCO) Orders on MetaTrader 5
Trading

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

EC Team
Last updated: May 17, 2025 1:41 am
EC Team
Published May 17, 2025
Share
SHARE

If you purchase something through one of our site’s links, you may win an affiliate committee.

I started using Metatrader around 2008.

Unfortunately, the release of the MT5 has not changed much.

In other words, it’s still “One cancels the other“Order function. Some people, including me, prefer to call this “cancel order for an order.”

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

You don’t need to know how to code.

This tutorial shows you why OCOs are so useful and how to easily create your own OCOEA for Metatrader 5 using ChatGpt.

How OCO works

A “one cancels the other” (OCO) trading order is a type of conditional condition that allows two pending orders to be placed at the same time, but only one can be executed.

Once one of your orders is triggered and filled, the other is automatically cancelled.

This is extremely useful for taking advantage of various trading opportunities.

Examples of OCO

Here is an example of how you can use OCO orders to take advantage of potentially profitable situations:

audcad chart

This pinbar is also an encircling bar that allows you to signal the top.

Previous bars were incredibly large and seemingly bailed out by traders, indicating that this trend could end.

Now I’ve now been able to enter this transaction, but there’s no guarantee that the price will drop as expected.

So you can set two pending orders, one selling limit (over the current price (higher blue line) and a selling outage below the current price (lower blue line).

The first possibility that prices may recede before they recede.

If prices go backwards, I will join the trade at a slightly better price and make a little more money. The second transaction will be cancelled and no duplicate transactions will occur.

Even if the price hits a stop loss on top of the candle, the loss is smaller than if I had entered the end of the current candle.

Another thing is that as expected, prices can go straight. In this case, a cell stop (the blue line below) will hit and the retracement trade will be cancelled.

This pending order will refer me to the market if I am moving in the direction I expect, but it will not retreat. It’s the best of both worlds.

If prices don’t go as planned, I lose less money as the selling restrictions take a hit instead of this selling stop.

As you can see, using OCO orders is very convenient.

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

MT5 EA Design

Before talking to ChatGpt, we need to think a bit about how we design this mt5 ea.

There are only two things to explain.

First of all, there may be other pending transactions in the same market, so we should consider that and only cancel orders that mark for cancellation.

Secondly, we would like to cancel only transactions in the same market on the chart with the EA attached.

This excludes transactions in other markets that happen to have the same marker.

In Metatrader 5, you can use Comment Field To easily mark transactions that you want to apply OCO logic to.

I’m a bit used to it as I have to enter my order via the right-click menu instead of the one-click button to use the comment field.

But that’s not a big deal.

ChatGPT prompt to get OCO in MT5

So for this EA, I encourage ChatGpt with:

Create a Metatrader 5 EA. This should look for open orders with comments in the market on the chart attached with EA. If an open order is found, cancel all pending orders with matching comments in the market on the chart with EA attached. Find a deal in the comments and close pending orders with matching comments. Do not provide an input field where comments are searched. Automatically match comment fields.

Copy and paste that prompt and use it yourself.

But remember that if you choose to use that prompt, I will not be responsible for what happens.

CHATGPT or another AI can have different results than what I got depending on the model I use, so you should always thoroughly test your EA before using it on a live account.

Continue reading and learn how to test it.

Create an Expert Advisor in MT5

Once you get the code from chatgpt, copy Copy the code by linking to the top right corner of the code window.

Copy link with chatgpt

Next, open MT5 and click IDE button at the top.

MT5 IDE button

This will open the MQL5 editor.

Click new A button in the top left corner of the screen.

The window comes up with a wizard.

  • On the first screen, select: Expert Advisor (Template)
  • click Next
  • Name the EA
  • Please click Next Until you reach the end of the wizard

The rest of the settings aren’t important as you just copy the code from ChatGpt anyway.

Once the EA template created by MT5 appears, click anywhere on the screen to control +A to select all text.

Then it hits Back space or erase With the keyboard to remove everything.

It’s a hit now Control +v Paste the ChatGPT code into a blank area.

Finally, click compile The button at the top of the screen checks the code.

Congratulations if you want to compile without errors!

Skip to the installation section below.

Otherwise, use the following section to correct the error.

troubleshooting

ChatGpt may not be able to understand that correctly for the first time.

Compiling your code is more likely to result in errors and warnings like this.

MT5 error

If that happens, just highlight it using all the error messages in MT5 Shift + left mouse click and Right click > copy Copy them.

Next, go to ChatGpt and ask them to fix it.

for example:

Tell chatgpt to fix compilation errors

You may have to do this several times, but usually you will understand it in the end.

Remember that this is just a syntax test.

Now you need to check the logic of your program.

Please put it in demo Account and test until you are confident in the results.

If you have issues that you cannot fix, make sure you are using MT5.

You cannot use this EA on MT4 as the codes for MT4 and MT5 are different.

How to install EA

Once the code is compiled, go back to the main MT5 window and Navigator window.

in the case of Navigator Sections are not available. Move to: View > Navigator Open the window.

I’m going to Expert Advisor Click on the section and drag the created EA onto the chart.

Add EA to the chart

When you appear on a chart, the name of the EA will appear in the top right corner of the chart.

The hat icon must be blue to indicate that it is running.

MT5 icon

If it is not blue, click Argo’s trade The button at the top of the screen confirms that the EA is active.

Test the EA

Now it’s time to test your new EA.

Test this with a demo account just in case something goes wrong.

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

Move: trading > New orders

comment

Enter all that identifies your order in the comment field.

It might be something that’s easy to remember, such as “999.”

Enter the details of the pending order, then open and enter a new pending order Same comment text Comment field.

To test this quickly, we set up two orders that are very close to the current price, so one of them will run immediately.

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

Try any kind of weird situation you can think of to make sure your EA is working as expected.

Test until you are sure it’s working properly.

More troubleshooting

If you have problems with the logic in your EA, go back to ChatGpt and solve them.

At some point, you need to start understanding the code, so now is the good time to start.

You don’t need to know how to code, but you need to know how each part of your code works.

Luckily, you can get ChatGpt and explain it.

It’s actually pretty good to explain.

Continue with the questions until you understand how the program works.

ChatGpt may sound professional for all, but don’t forget that you don’t know as much as you think.

Tell exactly what you want and don’t let them insert logic that it seems good.

Like any other skill, building using ChatGpt can be a bit frustrating at first.

But once you get it, it is very powerful and will help you build all the trading tools you’ve always wanted.

Final Thoughts

This is how you can create an OCOEA for MT5.

But this is just the beginning.

What else can I make?

Use your creativity to run your imagination into the wild.

You might just create something amazing…and very profitable.

You Might Also Like

The Volume Weighted Moving Average Strategy Guide

10 Benefits of Forex Hedging Most Traders Don’t Know About

Professor Boring and his Fantabulous Scan-traption-and why I’m not a Systems Trader

Trading Psychology: Conquering Overconfidence | MarketBulls

Trade Strategy 5.30.25 | Polaris Trading Group for Stocks and Futures Traders

TAGGED:CancelsMetaTraderOCOOrders
Share This Article
Facebook Email Print
Leave a Comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Follow US

Find US on Social Medias
FacebookLike
XFollow
YoutubeSubscribe
TelegramFollow

Weekly Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Popular News
Shriram Finance shares dip 4.77% amid mixed analyst outlooks
Business News

Shriram Finance shares dip 4.77% amid mixed analyst outlooks

EC Team
EC Team
April 28, 2025
Expanding The Child Tax Credit
The secret to Warren Buffett’s stock-picking success: He knew how to change his mind
Nearing Financial Independence with Just ONE “Luxury” Rental
Warren Buffett says Greg Abel should take over as Berkshire Hathaway CEO by year’s end
- Advertisement -
Ad imageAd image

Categories / Tags

  • Business News
  • Finance
  • Investment
  • Economics
  • Stock Market
  • Trading
  • stock
  • Trading
  • Market
  • Stocks

About US

Founded with the belief that economic understanding should be accessible to all, we strive to decode complex market movements, break down financial trends, and spotlight business developments that matter — all in a clear, digestible format.
Quick Link
  • Home
  • Blog
  • Contact
Important Links
  • About Us
  • Privacy Policy
  • Terms of Use
  • Disclaimer
  • Contact

Subscribe US

Subscribe to our newsletter to get our newest articles instantly!

© Foxiz News Network. Ruby Design Company. All Rights Reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?