node-ib
node-ib copied to clipboard
Bracket Orders
Is there a way to create bracket orders? Currently there's no parentId field on orders to associate child orders to parents. I'm not sure if there's another way or if this functionality is missing all together?
@aziz92 There is a way to create bracket orders. If you're still interested, I can post code that shows how to do it.
@lewisdawson I'm interested; could you kindly post your example?
Never mind; I figured it out. The official IB API documentation was really helpful here: https://interactivebrokers.github.io/tws-api/bracket_order.html#gsc.tab=0
@intercalations Sorry it took me a while. For anyone else wondering in the future, the key is assigning the parentId to the orders. The example below uses market order, which is not always ideal:
.on('nextValidId', function(orderId) {
var parentId = orderId,
limitId = parentId + 1,
stopLossId = parentId + 2,
contract,
parent,
limit,
stopLoss;
console.log('parentId -- %s | limitId -- %s | stopLossId -- %s', parentId, limitId, stopLossId);
contract = ib.contract.future('ES', '201703', 'USD', 'GLOBEX');
parent = {
id: parentId,
contract: contract,
order: ib.order.market('BUY', 1, false)
};
limit = {
id: limitId,
contract: contract,
order: ib.order.limit('SELL', 1, 1884.0, false)
};
limit.order.parentId = parent.id;
stopLoss = {
id: stopLossId,
contract: contract,
order: ib.order.stop('SELL', 1, 1874.0, true)
};
stopLoss.order.parentId = parent.id;
ib.placeOrder(parent.id, parent.contract, parent.order);
ib.placeOrder(limit.id, limit.contract, limit.order);
ib.placeOrder(stopLoss.id, stopLoss.contract, stopLoss.order);
ib.reqOpenOrders();
})