npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

custom-ibkr-muly

v1.8.22

Published

NodeJS Interactive Brokers wrapper & utilities - Custom Version

Downloads

25

Readme

Run IBKR in style

This is an event-based ibkr client for node | | Feature | | :---: | --------------------------------------------- | | ✅ | Accounts | | ✅ | Portfolios | | ✅ | Orders (stocks/forex/options/index .e.t.c) | | ✅ | Historical Data | | ✅ | Realtime price updates | | ✅ | Contracts (stocks/forex/options/index .e.t.c) | | ✅ | Mosaic Market scanner | | ⬜️ | News |

1. Install

npm i custom-ibkr

2. Usage

Initialize

import ibkr, { AccountSummary, IBKREVENTS, IbkrEvents, PortFolioUpdate, getContractDetails } from 'custom-ibkr';

const ibkrEvents = IbkrEvents.Instance;


// 0. Using env process.env.IB_PORT and process.env.IB_HOST
await ibkr();

// 1. Async 
await ibkr({ port: IB_PORT, host: IB_HOST });

// 2. Callback
ibkr({ port: IB_PORT, host: IB_HOST }).then(started => {
  
    if(!started){
          // Error IBKR has not started
          console.log('error cannot start ibkr');
        //   Not to proceed if not connected with interactive brokers
          return process.exit(1);
    }

    // Your code here

})

Accounts, Summary e.t.c

const accountId = AccountSummary.Instance.accountSummary.AccountId;
const totalCashValue = AccountSummary.Instance.accountSummary.TotalCashValue;

Portfolios


// Get current portfolios
const portfolios = Portfolios.Instance;
const accountPortfolios = await portfolios.getPortfolios();

// Subscribe to portfolio updates
ibkrEvents.on(IBKREVENTS.PORTFOLIOS, (porfolios: PortFolioUpdate[]) => {
      // use porfolios  updates here
})

Historical Data + Realtime price updates

  • Market data
import { HistoricalData } from 'custom-ibkr';

// 1. Init
const historyApi = HistoricalData.Instance;

const args = {
  symbol,
  // contract: ib.contract.stock("AAPL"),
  endDateTime = '',
  durationStr = '1 D',
  barSizeSetting = '1 min',
  whatToShow = 'ASK'
};

// 2. Get market data async promise
const data = await historyApi.reqHistoricalData(args);

// OR 

// 3.1 Request for market data using events
historyApi.getHistoricalData(args);
ibkrEvents.emit(IBKREVENTS.GET_MARKET_DATA, args); // the same

// 3.2. Subscribe to market data results
ibkrEvents.on(IBKREVENTS.ON_MARKET_DATA, ({ symbol, marketData }) => {
    //  Use the data here
})
  • Real-time price updates
import { PriceUpdates } from 'custom-ibkr';

PriceUpdates.Instance; // init

// subscribe for price updates
ibkrEvents.on(IBKREVENTS.ON_PRICE_UPDATES, (priceUpdates) => {
     // use the price updates here
 });

//  Request price updates
ibkrEvents.emit(IBKREVENTS.SUBSCRIBE_PRICE_UPDATES, { symbol: 'AAPL' });
// Unsubscribe from price updates
ibkrEvents.emit(IBKREVENTS.UNSUBSCRIBE_PRICE_UPDATES, symbol);

Contracts

 
const contractDetails = await getContractDetails(ib.contract.stock("AAPL"));

//  or e.g options
const contractDetails = await getContractDetails({
    currency: 'USD',
    exchange: 'SMART',
    multiplier: 100,
    right: 'C',
    secType: 'OPT',
    strike: 300,
    symbol: 'AAPL'
});

// e.g forex
const contractDetails = await getContractDetails({
    "symbol":"GBP",
    "secType":"CASH",
    "currency":"USD",
     // "localSymbol":"GBP.USD",
});


// or with just a symbol, defaults to stocks
 const contractDetails = await getContractDetails('AAPL');

Orders

import { Orders, Order, Contract } from 'custom-ibkr';

const orderTrade = Orders.Instance;

const myStockOrder: Order = { ... }
const myStockContract: Contract = { ... }

const placedOrder = await orderTrade.placeOrder(myStockContract, myStockOrder);
          

Order

const stockOrderBuyOut: Order = {
    action: OrderAction.BUY,
    orderType: OrderType.LMT,
    lmtPrice: 200
    totalQuantity: 5
}

const stockContractBuyOut: Contract = {
    symbol: symbol,
    secType: SecType.STK,
    exchange: exchange, // 'SMART'
    currency: currency // 'USD'
}

type

  • limit -> LMT (action, quantity, lmtPrice) like in example above
  • market -> MKT (action, quantity, transmitOrder, goodAfterTime, goodTillDate)
  • marketClose -> MOC (action, quantity, price, transmitOrder)
  • stop -> STP (action, quantity, price, transmitOrder, parentId, tif)
  • stopLimit -> STP_LMT (action, quantity, limitPrice, stopPrice, transmitOrder, parentId, tif)
  • trailingStop -> TRAIL (action, quantity, auxPrice, tif, transmitOrder, parentId)

Order events

  • Order filled
ibkrEvents.on(IBKREVENTS.ORDER_FILLED, (data) => {

});
  • Order status
ibkrEvents.on(IBKREVENTS.ORDER_STATUS, (data) => {

});
  • Open Orders updates
ibkrEvents.on(IBKREVENTS.OPEN_ORDERS, (data) => {

});

Mosaic Scanner

import { MosaicScanner } from 'custom-ibkr';
const mosaicScanner = new MosaicScanner();

const scannerData = await mosaicScanner.scanMarket({
      instrument: 'STK',
      locationCode: 'STK.US.MAJOR',
      numberOfRows: 10,
      scanCode: 'TOP_PERC_GAIN',
      stockTypeFilter: 'ALL'
})

see any .test.ts file for examples

3. Debug

We use debug library for logging. Run with DEBUG=ibkr:* to see all logs, or DEBUG=ibkr:info for less verbose logs.

See change log for updates