ANN: IbPy 0.2 - Interactive Brokers Python API

Troy Melhase
Mon, 29 Sep 2003 12:12:43 -0800

IbPy - Interactive Brokers Python API
IbPy 0.2 Released 29 September 2003

What is IbPy?
IbPy is a third-party implementation of the API used for accessing the
Interactive Brokers on-line trading system.  IbPy implements functionality
that the Python programmer can use to connect to IB, request stock ticker data,
submit orders for stocks and options, and more.

Where can I get IbPy?
IbPy is available for download from

Project page:

How do I use IbPy?
In order to use IbPy, the TWS application provided by IB must be installed and
running.  See the note "What is TWS?" for more information.

IbPy is used in the same manner as all socket clients supplied by Interactive
Brokers.  The typical sequence of operations is:

    1.    Start the browser-based or stand-alone TWS application
    2.    In TWS, enable socket clients via the Configure -> API menu
    3.    Connect to TWS from external application
    4.    In TWS, accept the incoming connection
    5.    External application requests ticker updates, sends orders, receives
          account data, portfolio data, etc.

To connect to TWS and interact with the brokers trading system, the Python
developer defines methods or functions to act as callbacks and associates these
with an Ib.Socket.SocketConnection object.  The following example is similar to
the module included with the release:

    import Ib.Message
    import Ib.Socket
    import Ib.Type

    class Example:
        def account_value_updated(self, acct_msg):
            fs = "Broker reports account data: %s is %s"
            print fs % (acct_msg.key, acct_msg.value, )

        def ticker_updated(self, tick_msg):
            fs = "Broker reports ticker data: ticker id %s - %s is %s"
            print fs % (tick_msg.ticker_id, tick_msg.field, tick_msg.value)

    example = Example()
    connection =

    connection.register(Ib.Message.Account, example.account_value_updated)
    connection.register(Ib.Message.Ticker, example.ticker_updated)

    connection.connect(("localhost", 7496))

    ## request account updates from the broker

    ## request data for two different tickers
    tickers = [(100, "MERQ"), (101, "MXIM"), ]
    for ticker_id, symbol in tickers:
        contract = Ib.Type.Contract(symbol=symbol)
        connection.request_market_data(ticker_id, contract)

In lieu of IbPy documentation, developers are referred to the IbPy source code
and the documentaion supplied by Interactive Brokers.

What are the requirements?
IbPy requires Python 2.2 or newer.

TWS requires a web browser capable of executing Sun(R) Java(tm) applets.
TWS can also be started directly with Sun(R) Java(tm) and the stand-alone
package supplied by Interactive Brokers.

What is Interactive Brokers?
>From the "About The Interactive Brokers Group" page

The Interactive Brokers Group is a group of electronic brokerage and market 
making firms at the forefront of the electronic brokerage industry. We have 
revolutionized the securities and derivatives markets by providing investors 
with high-speed, direct access to stock, options and futures products 
worldwide. We specialize in providing investors with technology that offers 
efficient, fast access to the world's markets at the lowest possible cost.

What is Python?
>From the "What is Python?" page (

Python is an interpreted, interactive, object-oriented programming language. 
It is often compared to Tcl, Perl, Scheme or Java.

Python combines remarkable power with very clear syntax. It has modules, 
classes, exceptions, very high level dynamic data types, and dynamic typing.  
There are interfaces to many system calls and libraries, as well as to various
windowing systems (X11, Motif, Tk, Mac, MFC). New built-in modules are easily 
written in C or C++. Python is also usable as an extension language for 
applications that need a programmable interface.

The Python implementation is portable: it runs on many brands of UNIX, on 
Windows, DOS, OS/2, Mac, Amiga... If your favorite system isn't listed here, 
it may still be supported, if there's a C compiler for it. Ask around on 
comp.lang.python -- or just try compiling Python yourself.

The Python implementation is copyrighted but freely usable and distributable, 
even for commercial use.

What is TWS?
>From the page "Trader Execution and Clearing System Features" 

* IB's Trader Workstation can be run directly from your browser, or you can 
install it on your PC and run it from your desktop. This Java based 
application allows traders to quickly enter orders and see results.

* Direct Access: Unlike other "online" brokers that send your order through 
an e-mail type system, IB offers a direct-access order entry system that 
transmits your order to the proper market center, usually within one second of

* Real-Time Streaming Quotes: Display real-time, streaming quotes for markets 
of interest to the investor. Quotes are live and change continuously as the 
markets change, no periodic manual update or "refresh" is required.

What else?
IbPy is not a product of Interactive Brokers, nor am I affiliated with IB.  I
am a satisfied IB customer, of course.

IbPy is installed with distutils.  Refer to the Python distutils documentation
for more information.  The digest version is:

        # tar xzf IbPy-0.2.tar.gz
        # cd IbPy-0.2
        # python install

The TWS demo system is available here:

The stand-alone TWS and other API software is available from IB:

IbPy is licensed under the BSD License.

I'm very interested in your experience with IbPy.  Please drop me an note with
any feedback you have.

Troy Melhase