how to set "take profit" and "stop loss" with ib controller?

I am programming some autotrading with AFL with IB controller. The information I got is only this web site: http://www.amibroker.com/at/

  1. Can anyone tell me how to set the “take profit price” and “stop loss price” of a already opened position?

  2. If I place a “LMT” order (via IB contoller), is there any means to set the “take profit price” at the same time?

This page:
http://www.amibroker.com/at/
includes already examples of BRACKET orders. Bracket order is the entry order plus two exit orders and that is precisely what you are asking for (the LIMIT order and STOP order).

Yes, I have found something about bracket orders below.

One follow up question: when either the take profit and stop loss order is filled, is it necessary for the program to cancel the other side? (e.g. when take profit happen, is it necessary to cancel the order of stop loss? Or will the system handle it automatically?)

This is the information I found in the document:
"4. Placing bracket order

// create instance of trading interface
ibc = GetTradingInterface(“IB”);
parentID = ibc.PlaceOrder(“MSFT”, “BUY”, 1000, “LMT”, 27, 0, “GTC”, False );
ibc.PlaceOrder(“MSFT”, “SELL”, 1000, “LMT”, 28, 0, “GTC”, False, 100, “”, parentID );
ibc.PlaceOrder(“MSFT”, “SELL”, 1000, “STP”, 26, 26, “GTC”, True, 100, “”, parentID );

Note that TRANSMIT flag is set to FALSE on all bracket orders except the last one. This ensures that orders wait until bracket order set is completed. Setting Transmit flag to TRUE on the very last one transmits entire bracket."

The documentation about all kind of orders is available via Interactive brokers web site:
https://www.interactivebrokers.com/en/index.php?f=583

The point of BRACKET orders is that they are treated as “group” so they work together and once one child is filled the others are automatically canceled.

1 Like