Use pip (recommended)
pip install swigibpy
Alternatively download a release, extract it and run
python setup.py install
TWS or IB Gateway must be running. To use swigibpy simply import the
swigibpy module into your code, define an
EWrapper sub-class and create
Use the methods of your
swigibpy.EPosixClientSocket instance to send
requests to Interactive Brokers. All requests are asynchronous and any
responses, notifications and warnings are handled by the methods of your
In the following simple example a request is made to Interactive Brokers for some historical data for the GOOG ticker, and the response is printed to the console.
from datetime import datetime import swigibpy class MyEWrapper(swigibpy.EWrapperVerbose): def historicalData(self, reqId, date, open, high, low, close, volume, barCount, WAP, hasGaps): if date[:8] == 'finished': print("History request complete") else: date = datetime.strptime(date, "%Y%m%d").strftime("%d %b %Y") print(("History %s - Open: %s, High: %s, Low: %s, Close: " "%s, Volume: %d") % (date, open, high, low, close, volume)) myWrapper = MyEWrapper() tws = swigibpy.EPosixClientSocket(myWrapper, reconnect_auto=True) tws.eConnect("", 7496, 42) contract = swigibpy.Contract() contract.exchange = "SMART" contract.symbol = "GOOG" contract.secType = "STK" contract.currency = "USD" today = datetime.today() tws.reqHistoricalData(2, contract, today.strftime("%Y%m%d %H:%M:%S %Z"), "1 W", "1 day", "TRADES", 0, 1, None)
See the examples for some more simple demos of using swigibpy. For a more in-depth introduction Rob Carver has written a nice series of blog posts on getting started with swigibpy and the Interative Brokers API.
For documentation on the methods of
other API reference refer to the C++ API documentation.
Note that unlike the C++ API swigibpy will automatically poll TWS for messages, see Message Polling for more about this.
If TWS reports an error then the
winError will be called as described in the TWS C++ API documentation.
Additionally swigibpy augments
EWrapper with an extra error handling
def pyError(self, type, value, traceback)
which will be called if an exception is raised during execution of one of your
EWrapper Python methods. The default behaviour is to print the exception to
standard error, but you can override the
pyError method to implement your own
handling. See the python docs for sys.exc_info() for details on the
EWrapper Utility Classes
EWrapper means having to tiresomely provide an
implementation for every method defined by
EWrapper. Happily swigibpy
EWrapper subclasses which can help.
EWrapperVerbose implements every
EWrapper method and by default just
prints a message to standard out every time one of its methods is invoked. The
message printed includes the arguments that were passed. Useful for development
EWrapperQuiet implements every
EWrapper method and silently ignores
any calls that have not been implemented by you. Useful if you are not
interested in defining every
swigibpy can automatically reconnect to TWS / IB Gateway in case of
connection loss or restart. To enable this behaviour use the
argument added to
tws = EPosixClientSocket(mywrapper, reconnect_auto=True)
Auto-reconnect is disabled by default.
yield parameter in
CommissionReport clashes with a Python reserved
keyword so it is renamed to
By default swigibpy will create a background thread (
to automatically poll TWS for messages. If you wish to disable this behaviour
and handle polling yourself use the
poll_auto argument added to
tws = EPosixClientSocket(mywrapper, poll_auto=False)
tws = EPosixClientSocket(mywrapper) ... tws.poll_auto = False
The TWS C++ API performs non-blocking socket I/O to communicate with TWS, swigibpy's background thread uses socket select to poll for incoming messages.
Apart from a few trivial patches to aid compilation and interoperability with Python swigibpy does not alter the TWS C++ API code in any way.
swigibpy is open source so feel free to get involved. If something doesn't work, or you'd like to add a feature, example or some documentation please create a pull request, if you need help open an issue.
For development switch to the swigibpy code directory and build the extension in the current dir.
python setup.py build_ext --inplace
The TWS API included in the repository has already been patched and the repository already includes the SWIG generated code but if you modify the interface file or need to rerun these steps the commands are
python setup.py swigify
to regenerate the SWIG wrappers (SWIG 3.0+ required), and
python setup.py patchify
to reapply the patches to the TWS API (specify the option
-r if you want to
un-apply the patches and get back to unaltered TWS code).
swigibpy provides a wrapper around the TWS C++ API so it must be compiled for your target platform during installation. While this should 'just work' for Linux and OSX, Windows users might need to do some extra work.
Only some basic tips are given here, for more see Installing Python Modules in the official documentation.
To get pip to use MinGW as the compiler edit or create a
[PYTHON LOCATION]\Lib\distutils where
[PYTHON LOCATION] is the path to your Python install, e.g.
Add the following to
then use the pip command given above in Installation and with a bit of luck, you're done!
Alternatively you can download a release and build the package directly. To build and install manually use
python setup.py build -c mingw32 python setup.py install
This has been verified to work using MinGW and Python 2.7 on Windows 7, Vista, and XP.
Visual Studio Compilation
Several users have reported success building swigibpy with Visual Studio, with a few caveats:
- Distutils has issues building with anything later than Visual Studio 2008 (version 9).
- Visual Studio 11 doesn't like the
/MDcompile flag, which distutils adds. For a workaround see here.
swigibpy original code is free software under the New BSD license.
Interactive Brokers propriety C++ API is copyright Interactive Brokers LLC. swigibpy is in no way supported or endorsed by Interactive Brokers LLC.