The following script shows how to retrieve a value indicating if a stock has shares available to borrow to sell short in the Interactive Brokers (IBKR) system.
First, make sure the IBKR API for Python is installed:
$ pip install ibapi
We assume Trader Workstation (TWS) is running locally and allows connections from EClients.
The script takes the ticker symbol as input.
The class extending EClient and EWrapper allows us to implement an event handler: tickGeneric(). We will subscribe to the tick indicating short share availability and the data will be received via this callback.
Note the value for genericTickList is “236” in the call to reqMktData() to subscribe to the short shares available tick.
Note that there will be a delay before the generic tick event fires and future updates are not frequent.
Use CTRL-C to exit.
Complete script:
import threading import time from ibapi.client import EClient, Contract from ibapi.wrapper import EWrapper # Extend IBKR scanner base class. class IBapi(EClient, EWrapper): def __init__(self): EClient.__init__(self, self) def tickGeneric(self, reqId, tickType, value): if tickType == 46: print("Value (short shares available indicator): " + str(value)) # Indicator value interpreted according to the docs: # https://interactivebrokers.github.io/tws-api/tick_types.html if value <= 1.5: print("Not available for short sale.") if value > 1.5 and value <= 2.5: print("Hard to borrow. Locate required.") if value > 2.5: print("Easy to borrow. At least 1000 shares available.") # Main script. TWS_HOST = "127.0.0.1" TWS_PORT = 7497 CLIENT_ID = 12345 ticker = input("Ticker symbol: ") app = IBapi() app.connect(TWS_HOST, TWS_PORT, CLIENT_ID) threading.Thread(target=app.run).start() time.sleep(5) # Allow time for subscription. contract = Contract() contract.symbol = ticker contract.secType = "STK" # Stocks. contract.exchange = "SMART" contract.currency = "USD" app.reqMarketDataType(3) # Delayed streaming data. app.reqMktData(reqId=100, # Arbitrary ID. contract=contract, genericTickList="236", # Includes borrow availability. snapshot=False, regulatorySnapshot=False, mktDataOptions=[] )
Example input and output:
Ticker symbol: TRIB Value (short shares available indicator): 3.0 Easy to borrow. At least 1000 shares available.
References
https://interactivebrokers.github.io/tws-api/tick_types.html