Membuat Bracket Order dengan IB NPM Wrapper

Mungkin ada yang ingin membuat automated trading menggunakan wrapper Node.Js untuk Interactive Brokers

Sample code di bawah adalah untuk membuat “Bracket Order” di mana anda boleh terus membuat order beserta dengan arahan profit taker dan stop loss.

Code ini di gunakan bersama Trader Work Station dan pekej ini:
https://www.npmjs.com/package/ib

var ib = new (require('ib'))({
    clientId: 0,
    host: '127.0.0.1',
    port: 4001
  })
  
  ib.on('error', function (err) {
    console.error('error --- %s', err.message);
  })
  
  ib.on('result', function (event, args) {
    console.log('%s --- %s', event, JSON.stringify(args));
  })

  ib.once('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);
    let stkCode = 'HAS';
    let quantity = 100; 
    contract = ib.contract.stock(stkCode);
    

    parent = {
        id: parentId,
        contract: contract,
        order: ib.order.limit('BUY', quantity, 0.02)
       
    };

    limit = {
        id: limitId,
        contract: contract,
        order: ib.order.limit('SELL', quantity, 0.03)
    };
    limit.order.parentId = parent.id;

    stopLoss = {
        id: stopLossId,
        contract: contract,
      
        order: ib.order.limit('SELL', quantity, 0.01)
    };
    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);
    //transmit
    ib.reqOpenOrders();
})
  .once('openOrderEnd', function () {
   console.log('Order Successfully Submitted')
    ib.disconnect();
  })
   
  ib.connect()
  ib.reqIds(1);

  

Nik Izwan Kamel is an Entrepreneur who venture in multiple businesses in different industries including IT, Blockchain, E-Commerce, Security, Retail and F&B. A cryptocurrency enthusiast and cooking computer codes at night.

Leave a reply:

Your email address will not be published.

Site Footer