Hedging

View as MarkdownOpen in Claude

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.

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.

More on Hedging Orders in TWS

More on Attaching Orders

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.

1 @staticmethod
2def MarketFHedge(parentOrderId:int, action:str):
3 #FX Hedge orders can only have a quantity of 0
4 order = OrderSamples.MarketOrder(action, 0)
5 order.parentId = parentOrderId
6 order.hedgeType = "F"
7 return order
8
9# Parent order on a contract which currency differs from your base currency
10parent = OrderSamples.LimitOrder("BUY", 100, 10)
11parent.orderId = self.nextOrderId()
12parent.transmit = False
13
14# Hedge on the currency conversion
15hedge = OrderSamples.MarketFHedge(parent.orderId, "BUY")
16
17# Place the parent first...
18self.placeOrder(parent.orderId, ContractSamples.EuropeanStock(), parent)
19
20# Then the hedge order
21self.placeOrder(self.nextOrderId(), ContractSamples.EurGbpFx(), hedge)