stoqey / ibkr

Interactive Brokers wrapper 🚩
MIT License
59 stars 11 forks source link

Emit all tickTypes by default when typeType is not explicitly specified #58

Closed ellis closed 3 years ago

ellis commented 3 years ago

This commit builds on https://github.com/stoqey/ibkr/pull/57 as follows:

ellis commented 3 years ago

Here is some sample code to show what happens when it runs on my system:

import {IBKRConnection} from './src/connection';
import {IBKREVENTS} from './src/events';
import ibkr, {IbkrEvents} from './src/index';
import {sleep} from 'sleepjs';
import {PriceUpdates} from './src/realtime';

async function main(): Promise<void> {
    Error.stackTraceLimit = Infinity;
    process.on('unhandledRejection', (up) => {
        console.log('ERROR:', up);
        throw up;
    });

    const ibkrEvents = IbkrEvents.Instance;

    await ibkr({port: 7496, host: 'localhost'});
    console.log('Connected');

    const ib = IBKRConnection.Instance.getIBKR();
    ib.reqMarketDataType(4); // marketDataType (1=live, 2=frozen, 3=delayed, 4=delayed/frozen)

    // Get Price Data
    PriceUpdates.Instance; // init

    // subscribe for price updates
    ibkrEvents.on(IBKREVENTS.ON_PRICE_UPDATES, (priceUpdates) => {
        console.log('priceUpdates:', priceUpdates);
    });

    // Request price updates
    ibkrEvents.emit(IBKREVENTS.SUBSCRIBE_PRICE_UPDATES, {
        contract: 'AAPL',
    });
}

main();

When I run this with DEBUG=\* npx ts-node -T ellis_test, the output is something like this:

...
Connected
2020-12-25T12:30:07.707Z ibkr:info contract before getting details AAPL
priceUpdates: { contract: 'AAPL' }
2020-12-25T12:30:07.845Z ibkr:info PriceUpdates.subscribe AAPL is successfully subscribed
2020-12-25T12:30:08.168Z ibkr:info error Requested market data is not subscribed. Displaying delayed market data...
2020-12-25T12:30:08.454Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=66/DELAYED_BID, price=-1 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.454Z ibkr:info PriceUpdates.tickPrice DELAYED_BID:PRICE AAPL => $-1 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_BID',
  symbol: 'AAPL',
  price: null,
  date: 2020-12-25T12:30:08.454Z
}
2020-12-25T12:30:08.455Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=67/DELAYED_ASK, price=-1 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.455Z ibkr:info PriceUpdates.tickPrice DELAYED_ASK:PRICE AAPL => $-1 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_ASK',
  symbol: 'AAPL',
  price: null,
  date: 2020-12-25T12:30:08.455Z
}
2020-12-25T12:30:08.457Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=68/DELAYED_LAST, price=131.99 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.457Z ibkr:info PriceUpdates.tickPrice DELAYED_LAST:PRICE AAPL => $131.99 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_LAST',
  symbol: 'AAPL',
  price: 131.99,
  date: 2020-12-25T12:30:08.457Z
}
2020-12-25T12:30:08.457Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=72/DELAYED_HIGH, price=133.46 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.457Z ibkr:info PriceUpdates.tickPrice DELAYED_HIGH:PRICE AAPL => $133.46 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_HIGH',
  symbol: 'AAPL',
  price: 133.46,
  date: 2020-12-25T12:30:08.457Z
}
2020-12-25T12:30:08.458Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=73/DELAYED_LOW, price=131.1 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.458Z ibkr:info PriceUpdates.tickPrice DELAYED_LOW:PRICE AAPL => $131.1 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_LOW',
  symbol: 'AAPL',
  price: 131.1,
  date: 2020-12-25T12:30:08.458Z
}
2020-12-25T12:30:08.458Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=75/DELAYED_CLOSE, price=130.96 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.458Z ibkr:info PriceUpdates.tickPrice DELAYED_CLOSE:PRICE AAPL => $130.96 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_CLOSE',
  symbol: 'AAPL',
  price: 130.96,
  date: 2020-12-25T12:30:08.458Z
}
2020-12-25T12:30:08.458Z ibkr:info PriceUpdates.tickPrice tickerId=24381, tickType=76/DELAYED_OPEN, price=0 {"s":"AAPL","ticker":24381}
2020-12-25T12:30:08.458Z ibkr:info PriceUpdates.tickPrice DELAYED_OPEN:PRICE AAPL => $0 tickerId = 24381
priceUpdates: {
  tickType: 'DELAYED_OPEN',
  symbol: 'AAPL',
  price: 0,
  date: 2020-12-25T12:30:08.458Z
}

Personally, I'm happy with this behavior of getting all tickType messages. What do you think @ceddybi?

There is, however, a bug in the above output that I wasn't able to track down. It's not related to this patch, since I noticed it before as well. At the top of the output, we see the line priceUpdates: { contract: 'AAPL' }. For some reason, the event handler for IBKREVENTS.ON_PRICE_UPDATES is being called in ContractDetails.ts after ib.reqContractDetails(reqId, contractArg) but before handleContract(). Any ideas?

ceddybi commented 3 years ago

For some reason, the event handler for IBKREVENTS.ON_PRICE_UPDATES is being called in ContractDetails.ts after ib.reqContractDetails(reqId, contractArg) but before handleContract(). Any ideas?

@ellis can you please show me an example, I've failed to understand,

ellis commented 3 years ago

For some reason, the event handler for IBKREVENTS.ON_PRICE_UPDATES is being called in ContractDetails.ts after ib.reqContractDetails(reqId, contractArg) but before handleContract(). Any ideas?

@ellis can you please show me an example, I've failed to understand,

I'll create a separate issue for it, since it's not directly related to this PR.