pilwon / node-ib

Interactive Brokers TWS API client library for Node.js
382 stars 133 forks source link

Combo / BAG example #40

Open claude2 opened 8 years ago

claude2 commented 8 years ago

Could you supply a combo example please?

contract/combo.js only takes a symbol, but how do you define the legs?

jem890 commented 6 years ago

Hi @claude2 , did you find the way to achieve this? Many thanks

mr-pascal commented 5 years ago

hey @claude2 @jem890
currently also searching for it, did one of you succeded to do option combos?

mr-pascal commented 5 years ago

Hey guys, @claude2 @jem890 after doing quite a bit of research in this repository and at the official interactivebrokers API documentation I found out how to do it. Following a minimalistic code example to create a 1000/900 Bull Put Spread in AMZN. you just have to take care regarding the orderid, since it is currently just hardcoded into code ;-)

var _ = require('lodash');
var chalk = require('chalk');

var contractDetails = [];
var maxContracts = 2;

var ib = new(require('ib'))({
    // clientId: 0,
    // host: '127.0.0.1',
    port: 7497
}).on('contractDetails', function (reqId, contract) {
    contractDetails.push(contract);

    if (contractDetails.length >= maxContracts) {
        placeOrders(contractDetails);
    }
});

ib.once('nextValidId', function (orderId) {
    // define options
    var shortStrike = ib.contract.option('AMZN', '20190315', 1000, 'P');
    var longStrike = ib.contract.option('AMZN', '20190315', 990, 'P');

    // get Contracts
    ib.reqContractDetails(23, shortStrike);
    ib.reqContractDetails(24, longStrike);
});

ib.connect()
    .reqIds(1);

function placeOrders(cds) {

    var comboContract = {
        symbol: 'AMZN',
        secType: 'BAG',
        currency: 'USD',
        exchange: 'SMART',
        comboLegs: [{
                conId: cds[0].summary.conId,
                ratio: 1,
                action: 'SELL',
                exchange: 'SMART'
            },
            {
                conId: cds[1].summary.conId,
                ratio: 1,
                action: 'BUY',
                exchange: 'SMART'
            }
        ]
    };

    ib.placeOrder(573849, comboContract, ib.order.limit('BUY', 1, 0.2));
}