OCO (One Cancels the Other) Order
Overview
An OCO (One Cancels the Other) order is a combination of two independent entry orders that are linked together by the OCO logic.
If one order is executed, the other order is automatically canceled. It allows traders to enter the market at different price levels and manage their risk exposure.
To create an OCO order using the FCLite API, you need to use the IOrdersManager.createOcoOrder method.
This method allows you to specify two entry orders: either a stop entry order and a limit entry order, or two stops or two limits.
With OCO orders, you can create two independent entry orders that are linked by the OCO logic. This means that if one order is executed, the other order is automatically canceled. You can use OCO orders to:
- Bound the market: You can set two entry orders at different price levels and execute whichever one is reached first by the market. For example, you can say: if the price drops, sell; or if the price increases, buy.
- Stop/limit to close a position: You can also use OCO orders to close an existing position by setting a stop order and a limit order at opposite directions. For example, you can say: if the price reaches my profit target, close the position; or if the price reaches my loss limit, close the position.
- Completely different: You can also create OCO orders for different symbols, directions, or quantities. For example, you can say: if oil rises, buy oil; but if gold decreases, sell gold.
OCO orders give you more flexibility and control over your trading strategy. However, they do not guarantee execution, so you should always monitor your positions and adjust your orders accordingly.
These orders are incredibly useful for managing risk and optimizing trading strategies.
Here is how to create OCO (One Cancels the Other) orders using the FCLite API.
Here are the steps:
Step 1: Create a Market Order
To begin, you'll need to create a market order using the FCLite API. A market order executes immediately at the current market price.
Step 2: OCO Order MANUAL Setup
An OCO (One Cancels the Other) order is a combination of two orders: a take profit order and a stop loss order. These orders allow you to set a target profit level and a maximum loss level for your trade. When one order is executed, the other is automatically canceled. This way, you can manage your risk and optimize your trading strategy.
To set up an OCO order, follow these steps:
- Define two separate orders: one for take profit and another for stop loss.
- Associate these orders with the same trading position.
- Specify the desired take profit price and stop loss price.
- Ensure that when one order is executed, the other is automatically canceled.
Here is an example of how to create an OCO order using the FCLite API:
// Create a market order to buy EUR/USD at 1.1000
var market_order = OpenOrderFactory.createMarketOrder("EUR/USD", 1000, "buy");
// Create a take profit order to close the position at 1.1100
var take_profit_order = OpenOrderFactory.createCloseMarketOrder(market_order.position_id, "sell", 1000);
take_profit_order.price = 1.1100;
// Create a stop loss order to close the position at 1.0900
var stop_loss_order = OpenOrderFactory.createCloseMarketOrder(market_order.position_id, "sell", 1000);
stop_loss_order.price = 1.0900;
// Send the orders to the server
market_order.send();
take_profit_order.send();
stop_loss_order.send();
// Monitor the execution of the orders
if (take_profit_order.isExecuted()) {
// Cancel the stop loss order
stop_loss_order.cancel();
} else if (stop_loss_order.isExecuted()) {
// Cancel the take profit order
take_profit_order.cancel();
}
However, if you prefer to use the createOcoOrder method, you can do so by following these steps:
- Create an IOrdersManager object, which is a helper class that manages your orders.
- Use the createOcoOrder method to create an OCO order with two entry orders: either a stop entry and a limit entry, or two stops or two limits.
- Specify the order parameters, such as the symbol, quantity, price, and direction.
- Send the OCO order to the server.
- Here is an example of how to use the createOcoOrder method in Javascript:
// Create an IOrdersManager object
var orders_manager = new IOrdersManager();
// Create an OCO order with two entry orders
var oco_order = orders_manager.createOcoOrder();
// Set the order parameters for the first entry order
oco_order.order1.symbol = "EUR/USD";
oco_order.order1.quantity = 1000;
oco_order.order1.price = 1.1050;
oco_order.order1.direction = "buy";
// Set the order parameters for the second entry order
oco_order.order2.symbol = "EUR/USD";
oco_order.order2.quantity = 1000;
oco_order.order2.price = 1.0950;
oco_order.order2.direction = "sell";
// Send the OCO order to the server
oco_order.send();
When creating these two orders, you need to ensure that you associate them with the open position that you are monitoring.
You should also in your coding, create a listener that listens for changes in your order status, so that when it changes to closed, you can immediately remove the opposing order.
Note
Unfortunately, the FCLite API does not directly support OCO orders out of the box. However, you can manually manage this by monitoring the execution of one order and canceling the other accordingly.
Remember that OCO orders are powerful tools, but they require careful planning and monitoring. Always test your implementation thoroughly before deploying it in a live trading environment.
For more information on each order type, please refer to the following sections of the documentation:
| Table of Contents | |
|---|---|
| Order Types This page will explain each of the different types of orders in the system. |
|
| Get All Orders The article teaches how to get a list of all orders. |
|
| Modify an Order The article teaches how to modify an order |
|
| Create a Market Order The article teaches how to create a market order |
|
| Create an Entry Order API Command used to create an entry order |
|
| Create a Close Market Order The article teaches how to create a close 'market order' |
|
| Create an OCO Order API Command used to create an OCO order |
|
| Create a Stop and/or Limit Order This article provides information about how to add a stop/limit to an order |
|
| Cancel an Order This article provides information about how to cancel an order |