ib_insync

repository·master·Indexed 25 days ago

https://github.com/erdewit/ib_insync

A Python library designed to simplify working with the Interactive Brokers Trader Workstation (TWS) API. It provides a linear programming style and an asynchronous framework based on asyncio to keep data in sync with TWS or IB Gateway. The library supports interactive development in Jupyter notebooks and includes functionality for downloading historical data, requesting scanner data, calculating option implied volatility, accessing market depth, and managing orders.

Tokens
10K
Snippets
40
Records
67
Agent score
85%

What's inside ib_insync

  1. Explore IB-insync using Jupyter Notebooks

    master
    IB-insync supports fully interactive and exploratory development using live data within Jupyter notebooks. You can use various recipe notebooks to learn how to handle specific tasks such as contract details, option chains, market data, and ordering.
  2. Fetch consecutive historical data

    master

    To fetch a continuous stream of historical data (e.g., 1-minute bars) from a specific point in time back to the beginning, use a while loop. Start with the current time and use the endDateTime parameter in reqHistoricalData to request data in chunks, updating the endDateTime with the date of the earliest bar received in the previous request until no more data is returned.

    import datetime
    from ib_insync import *
    
    ib = IB()
    ib.connect('127.0.0.1', 7497, clientId=1)
    
    contract = Stock('TSLA', 'SMART', 'USD')
    
    dt = ''
    barsList = []
    while True:
        bars = ib.reqHistoricalData(
            contract,
            endDateTime=dt,
            durationStr='10 D',
            barSizeSetting='1 min',
            whatToShow='MIDPOINT',
            useRTH=True,
            formatDate=1)
        if not bars:
            break
        barsList.append(bars)
        dt = bars[0].date
        print(dt)
    
    # save to CSV file
    allBars = [b for bars in reversed(barsList) for b in bars]
    df = util.df(allBars)
    df.to_csv(contract.symbol + '.csv', index=False)
  3. Handle short-lived connections gracefully

    master

    The IB socket protocol is designed for long-lived connections. If you are using the library for short-lived tasks (e.g., firing a few orders and exiting), add a small delay (e.g., ib.sleep(1)) before calling ib.disconnect(). This ensures that any pending data in the buffer is flushed to the server.

    ib = IB()
    ib.connect()
    
    ...  # create and submit some orders
    
    ib.sleep(1)  # added delay
    ib.disconnect()
  4. Install ib_insync

    master

    Install the library using pip:

    pip install ib_insync

    Requirements

    • Python 3.6 or higher
    • A running TWS (Trader Workstation) or IB Gateway application (version 1023 or higher).
    • The API port must be enabled in your TWS/IB Gateway settings.
    • 'Download open orders on connection' must be checked in your TWS/IB Gateway settings.

    Note: The official ibapi package from Interactive Brokers is not required.

  5. Prefer current state methods over requests

    master

    To improve performance, use 'current state' methods instead of 'request' methods. Request methods (prefixed with req) involve network round-trips and are slower, whereas current state methods (e.g., ib.positions()) access data already synchronized in memory and are available immediately.

    Examples:

    • Use ib.positions() instead of ib.reqPositions()
    • Use ib.openOrders() instead of ib.reqOpenOrders()
  6. Setup ib_insync in Jupyter Notebooks

    master

    To use ib_insync in a Jupyter Notebook, import all members and start the event loop using util.startLoop(). This ensures the notebook remains live-updated with data from the IB API.

    Note: util.startLoop() is designed specifically for notebooks and will not work in regular Python scripts.

    from ib_insync import *
    util.startLoop()
  7. Get live updates for historical bars

    master

    To receive live updates for a historical data subscription, call ib.reqHistoricalData with endDateTime='' and keepUpToDate=True.

    You can subscribe to updates by attaching a callback function to the updateEvent of the returned bars object. The callback receives (bars, hasNewBar).

    Warning: Using keepUpToDate=True may cause the API to become inoperable if a network interruption occurs.

    contract = Forex('EURUSD')
    
    # Request bars with keepUpToDate enabled
    bars = ib.reqHistoricalData(
            contract,
            endDateTime='',
            durationStr='900 S',
            barSizeSetting='10 secs',
            whatToShow='MIDPOINT',
            useRTH=True,
            formatDate=1,
            keepUpToDate=True)
    
    def onBarUpdate(bars, hasNewBar):
        print(f"New bar: {bars[-1]}")
    
    # Subscribe to updates
    bars.updateEvent += onBarUpdate
    
    # To stop the subscription
    ib.cancelHistoricalData(bars)
  8. Connect to TWS or IB Gateway

    master

    Use the IB class to establish a connection to a running Trader Workstation (TWS) or IB Gateway application.

    Troubleshooting Connection Failures:

    • Verify that the API port is enabled in your TWS/IBG settings.
    • Double-check the hostname and port.
    • For IB Gateway, the default port is typically 4002.
    • Ensure the clientId provided is not already in use by another session.
    ib = IB()
    ib.connect('127.0.0.1', 7497, clientId=10)
  9. Construct and qualify multiple option contracts

    master

    Once you have an option chain, you can programmatically build a list of Option contracts based on specific criteria like strike price increments, expiration dates, and rights ('P' for Put, 'C' for Call). After creating the list of Option objects, use ib.qualifyContracts(*contracts) to fill in missing details like conId and ensure they are valid for trading.

    # Example: Building contracts based on filtered strikes and expirations
    strikes = [strike for strike in chain.strikes if strike % 5 == 0]
    expirations = sorted(chain.expirations)[:3]
    rights = ['P', 'C']
    
    contracts = [Option('SPX', expiration, strike, right, 'SMART', tradingClass='SPX')
            for right in rights
            for expiration in expirations
            for strike in strikes]
    
    # Qualify the contracts
    contracts = ib.qualifyContracts(*contracts)
  10. Configure ib_insync requirements

    master

    To use ib_insync, ensure you meet the following requirements:

    • Python: Version 3.6 or higher.
    • TWS or IB Gateway: A running instance of Trader Workstation (TWS) or IB Gateway (version 1023 or higher).
    • API Settings:
      • The API port must be enabled in your TWS/Gateway settings.
      • The option 'Download open orders on connection' must be checked.

    Note: The official ibapi package from Interactive Brokers is not required.

  11. Connect to IB via ib_insync

    master

    To start using ib_insync, initialize the IB object, start the event loop using util.startLoop(), and connect to your IB gateway or TWS instance.

    Warning: Using live accounts will place real orders. It is recommended to use a paper trading account during testing.

    from ib_insync import *
    util.startLoop()
    
    ib = IB()
    ib.connect('127.0.0.1', 7497, clientId=13)