Hedging orders are similar to Bracket Orders. With hedging order, a child order is submitted only on execution of the parent. Orders can be hedged by an attached forex trade, Beta hedge, or Pair Trade, just as in TWS:
Hedging Orders in TWS
For an example of a forex hedge, when buying a contract on a currency other than your base, you can attach an FX order to convert base currency to the currency of the contract to cover the cost of the trade thanks to the TWS API's Attaching Orders mechanism.
-
public static Order MarketFHedge(int parentOrderId, string action)
{
Order order = MarketOrder(action, 0);
order.ParentId = parentOrderId;
order.HedgeType = "F";
return order;
}
...
Order parent = OrderSamples.LimitOrder("BUY", 100, 10);
parent.OrderId = nextOrderId++;
parent.Transmit = false;
Order hedge = OrderSamples.MarketFHedge(parent.OrderId, "BUY");
client.placeOrder(parent.OrderId, ContractSamples.EuropeanStock(), parent);
client.placeOrder(nextOrderId++, ContractSamples.EurGbpFx(), hedge);
-
public static Order MarketFHedge(int parentOrderId, String action) {
Order order = MarketOrder(action, Decimal.ZERO);
order.parentId(parentOrderId);
order.hedgeType("F");
return order;
}
...
Order parent = OrderSamples.LimitOrder("BUY", Decimal.ONE_HUNDRED, 10);
parent.orderId(nextOrderId++);
parent.transmit(false);
Order hedge = OrderSamples.MarketFHedge(parent.orderId(), "BUY");
client.placeOrder(parent.orderId(), ContractSamples.EuropeanStock(), parent);
client.placeOrder(nextOrderId++, ContractSamples.EurGbpFx(), hedge);
-
Public Shared Function MarketFHedge(parentOrderId As Integer, action As String) As Order
'FX Hedge orders can only have a quantity of 0
Dim Order As Order = MarketOrder(action, 0)
Order.ParentId = parentOrderId
Order.HedgeType = "F"
Return Order
End Function
... 'Parent order on a contract which currency differs from your base currency
Dim parent As Order = OrderSamples.LimitOrder("BUY", 100, 10)
parent.OrderId = increment(nextOrderId)
parent.Transmit = False
'Hedge on the currency conversion
Dim hedge As Order = OrderSamples.MarketFHedge(parent.OrderId, "BUY")
'Place the parent first...
client.placeOrder(parent.OrderId, ContractSamples.EuropeanStock(), parent)
'Then the hedge order
client.placeOrder(increment(increment(nextOrderId)), ContractSamples.EurGbpFx(), hedge)
-
Order OrderSamples::MarketFHedge(int parentOrderId, std::string action){
Order order = MarketOrder(action, 0);
order.parentId = parentOrderId;
order.hedgeType = "F";
return order;
}
...
Order parent = OrderSamples::LimitOrder("BUY", DecimalFunctions::stringToDecimal("100"), 10);
parent.orderId = m_orderId++;
parent.transmit = false;
Order hedge = OrderSamples::MarketFHedge(parent.orderId, "BUY");
m_pClient->placeOrder(parent.orderId, ContractSamples::EuropeanStock(), parent);
m_pClient->placeOrder(m_orderId++, ContractSamples::EurGbpFx(), hedge);
-
2 def MarketFHedge(parentOrderId:int, action:str):
5 order = OrderSamples.MarketOrder(action, 0)
6 order.parentId = parentOrderId
...
2 parent = OrderSamples.LimitOrder(
"BUY", 100, 10)
3 parent.orderId = self.nextOrderId()
4 parent.transmit =
False
6 hedge = OrderSamples.MarketFHedge(parent.orderId,
"BUY")
8 self.placeOrder(parent.orderId, ContractSamples.EuropeanStock(), parent)
10 self.placeOrder(self.nextOrderId(), ContractSamples.EurGbpFx(), hedge)
Note that in some cases it will be necessary to include a small delay of 50 ms or less after placing the parent order for processing, before placing the child order. Otherwise the error "10006: Missing parent order" will be triggered.