IBKR: Interactive Brokers
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;
await ibkr();
await ibkr({ port: IB_PORT, host: IB_HOST });
ibkr({ port: IB_PORT, host: IB_HOST }).then(started => {
if(!started){
console.log('error cannot start ibkr');
return process.exit(1);
}
})
Accounts, Summary e.t.c
const accountId = AccountSummary.Instance.accountSummary.AccountId;
const totalCashValue = AccountSummary.Instance.accountSummary.TotalCashValue;
Portfolios
const portfolios = Portfolios.Instance;
const accountPortfolios = await portfolios.getPortfolios();
ibkrEvents.on(IBKREVENTS.PORTFOLIOS, (porfolios: PortFolioUpdate[]) => {
})
Historical Data + Realtime price updates
import { HistoricalData } from 'custom-ibkr';
const historyApi = HistoricalData.Instance;
const args = {
symbol,
endDateTime = '',
durationStr = '1 D',
barSizeSetting = '1 min',
whatToShow = 'ASK'
};
const data = await historyApi.reqHistoricalData(args);
historyApi.getHistoricalData(args);
ibkrEvents.emit(IBKREVENTS.GET_MARKET_DATA, args);
ibkrEvents.on(IBKREVENTS.ON_MARKET_DATA, ({ symbol, marketData }) => {
})
import { PriceUpdates } from 'custom-ibkr';
PriceUpdates.Instance;
ibkrEvents.on(IBKREVENTS.ON_PRICE_UPDATES, (priceUpdates) => {
});
ibkrEvents.emit(IBKREVENTS.SUBSCRIBE_PRICE_UPDATES, { symbol: 'AAPL' });
ibkrEvents.emit(IBKREVENTS.UNSUBSCRIBE_PRICE_UPDATES, symbol);
Contracts
const contractDetails = await getContractDetails(ib.contract.stock("AAPL"));
const contractDetails = await getContractDetails({
currency: 'USD',
exchange: 'SMART',
multiplier: 100,
right: 'C',
secType: 'OPT',
strike: 300,
symbol: 'AAPL'
});
const contractDetails = await getContractDetails({
"symbol":"GBP",
"secType":"CASH",
"currency":"USD",
});
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,
currency: currency
}
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
ibkrEvents.on(IBKREVENTS.ORDER_FILLED, (data) => {
});
ibkrEvents.on(IBKREVENTS.ORDER_STATUS, (data) => {
});
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.
Forked from Stoqey Inc IBKR