Building bars using IB's API

Discussion in 'Automated Trading' started by RoughTrader, Apr 12, 2007.

  1. Hi all,

    I'm trying to build a program that will construct chart bars in real time from IB's data feed.

    For example, once the program has established a connection to IB's feed and is actively listening for raw streaming ticks, I'd like to build the following OHLC bars:

    1) x-minute bars
    2) y-tick bars
    3) z-share bars

    I'd also like to create an integer variable called "SessionMarker" which takes the value "1" at the close of the first bar of the session, and "0" for all other bars.

    For example:

    Instrument: ESM07
    1) 5-minute bars
    2) 100-tick bars
    3) 500-share bars

    Does anyone have any experience with the code needed to do this?


  2. yes. you need an event stream processor such as Esper or Coral8 to collect the ticks, aggregate them over a specific time frame, and output after X seconds/minutes/ticks. Obviously there are other ways of doing this, but it's not worth reinventing the wheel.
  3. Hmmm ... It's only about 30 lines of code - probably less. I know nothing about Esper or Coral8, but one should be really careful about decisions to tie ones future to some specific framework without some careful thought about the matter.

    The OP is obviously fairly new at this, so roll your own may well be a good educational experience.
  4. What programming language are you using ?
  5. You don't NEED an event stream processor to do this. As was said, there are other ways of doing this that have been around a lot longer than event stream processing and are simpler for your specific purposes.

    A simple tick compressor is all that is needed. However, you do not get ticks from IB's feed and therefore tick-based bars are not really possible.

    You can approach it this way:

    1) Model a Bar type/class which acts as a container for values of a given bar e.g. OHLCV or whatever is appropriate for your bars.

    2) Create a "tick compressor" that receives incoming ticks and caches the most recent data. When x minutes have passed, or when y ticks have come in etc. - create a Bar object that represents that data. Then, start with the next Bar object and so on.

    What you do with your Bar objects is up to you: hold them in memory as a time series or persist them to a file or database etc.

    If you specify your language of choice, I'm sure someone here will provide more concrete example code.
  6. Hi dcraig,

    I'd like to use java. Do you have any code that might help me?


  7. Hi TraderMojo,

    If IB's feed does not provide ticks, I'm wondering how other charting apps like sierra or multicharts is able to do it...

    Like I mentioned, I'm thinking to use java for this project.

    I use tradestation as my primary datafeed for the e-minis, so another route to take for this project would be to use a dll file to send OHLC data at the close of each bar to a java app. I have absolutely no idea how to do this though....

  8. IB's data feed is not true tick by tick. Instead it uses a snapshot mechanism to keep up to date with the market. This is an oft discussed issue on ET for example:

    Programs like Sierra etc. take the ticks provided by IB but they may not be the true ticks .

    If you're using Java, then I'm sure dcraig will be along in a moment to help you out...

    Seriously though, what kind of level are you at with Java? Are you able to connect to the IB API with Java? That would be the first step.

    You may wish to consult the IB forums:

    Or the twsapi Yahoo! group for example code: