• C#
  • Java
  • VB
  • C++
  • Python
Contact us
Hedging

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)
    {
    //FX Hedge orders can only have a quantity of 0
    Order order = MarketOrder(action, 0);
    order.ParentId = parentOrderId;
    order.HedgeType = "F";
    return order;
    }
    ...
    //Parent order on a contract which currency differs from your base currency
    Order parent = OrderSamples.LimitOrder("BUY", 100, 10);
    parent.OrderId = nextOrderId++;
    parent.Transmit = false;
    //Hedge on the currency conversion
    Order hedge = OrderSamples.MarketFHedge(parent.OrderId, "BUY");
    //Place the parent first...
    client.placeOrder(parent.OrderId, ContractSamples.EuropeanStock(), parent);
    //Then the hedge order
    client.placeOrder(nextOrderId++, ContractSamples.EurGbpFx(), hedge);
  • public static Order MarketFHedge(int parentOrderId, String action) {
    //FX Hedge orders can only have a quantity of 0
    Order order = MarketOrder(action, Decimal.ZERO);
    order.parentId(parentOrderId);
    order.hedgeType("F");
    return order;
    }
    ...
    //Parent order on a contract which currency differs from your base currency
    Order parent = OrderSamples.LimitOrder("BUY", Decimal.ONE_HUNDRED, 10);
    parent.orderId(nextOrderId++);
    parent.transmit(false);
    //Hedge on the currency conversion
    Order hedge = OrderSamples.MarketFHedge(parent.orderId(), "BUY");
    //Place the parent first...
    client.placeOrder(parent.orderId(), ContractSamples.EuropeanStock(), parent);
    //Then the hedge order
    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){
    //FX Hedge orders can only have a quantity of 0
    Order order = MarketOrder(action, 0);
    order.parentId = parentOrderId;
    order.hedgeType = "F";
    return order;
    }
    ...
    //Parent order on a contract which currency differs from your base currency
    Order parent = OrderSamples::LimitOrder("BUY", DecimalFunctions::stringToDecimal("100"), 10);
    parent.orderId = m_orderId++;
    parent.transmit = false;
    //Hedge on the currency conversion
    Order hedge = OrderSamples::MarketFHedge(parent.orderId, "BUY");
    //Place the parent first...
    m_pClient->placeOrder(parent.orderId, ContractSamples::EuropeanStock(), parent);
    //Then the hedge order
    m_pClient->placeOrder(m_orderId++, ContractSamples::EurGbpFx(), hedge);
  • 1  @staticmethod
    2  def MarketFHedge(parentOrderId:int, action:str):
    3 
    4  #FX Hedge orders can only have a quantity of 0
    5  order = OrderSamples.MarketOrder(action, 0)
    6  order.parentId = parentOrderId
    7  order.hedgeType = "F"
    8  return order
    9 
    ...
    1  # Parent order on a contract which currency differs from your base currency
    2  parent = OrderSamples.LimitOrder("BUY", 100, 10)
    3  parent.orderId = self.nextOrderId()
    4  parent.transmit = False
    5  # Hedge on the currency conversion
    6  hedge = OrderSamples.MarketFHedge(parent.orderId, "BUY")
    7  # Place the parent first...
    8  self.placeOrder(parent.orderId, ContractSamples.EuropeanStock(), parent)
    9  # Then the hedge order
    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.