node-ib icon indicating copy to clipboard operation
node-ib copied to clipboard

Bracket Orders

Open aziz92 opened this issue 8 years ago • 4 comments

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 avatar Sep 30 '16 14:09 aziz92

@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 avatar Jan 15 '17 22:01 lewisdawson

@lewisdawson I'm interested; could you kindly post your example?

intercalations avatar Feb 05 '17 23:02 intercalations

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 avatar Feb 06 '17 01:02 intercalations

@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();
})

lewisdawson avatar Feb 17 '17 03:02 lewisdawson