Is there a way to use ib_async to check whether a stock is shortable, whether it is easy to borrow, and/or what its borrow rate is? #136
Replies: 3 comments 10 replies
-
You can use the tick type 236 https://interactivebrokers.github.io/tws-api/tick_types.html#shortable And/Or get them using ftp |
Beta Was this translation helpful? Give feedback.
-
I have the humble value bundle I just did a test during market hours import ib_async
ib_async.util.startLoop()
def on_shortable(time,price,size):
logger.info(f"on_shortable: {time}, {price}, {size}")
def shortable_shares(self) -> "Tickfilter":
"""Emit shortable share ticks."""
return ib_async.ticker.Tickfilter((46,89), self)
# monkey patch
ib_async.ticker.TickerUpdateEvent.shortable_shares = shortable_shares
ib = ib_async.IB()
ib.connect('tws',7499,clientId=543) # use whatever works for you
tsla_contract = ib_async.Stock('TSLA','SMART','USD')
ib.qualifyContracts(tsla_contract)
tsla_ticker = ib.reqMktData(tsla_contract,genericTickList="236")
shortable = tsla_ticker.updateEvent.shortable_shares()
shortable.connect(on_shortable)
tsla_ticker.shortableShares,tsla_ticker.hasBidAsk()
(66555313.0, True)
ib.sleep(10)
shortable.clear() log output INFO:2025-03-31 16:41:29,486:on_shortable: 2025-03-31 14:41:29.486054+00:00, 3.0, 0
INFO:2025-03-31 16:41:29,487:on_shortable: 2025-03-31 14:41:29.486054+00:00, 3.0, 0
INFO:2025-03-31 16:41:31,156:on_shortable: 2025-03-31 14:41:31.156686+00:00, -1.0, 66544738.0
INFO:2025-03-31 16:41:33,205:on_shortable: 2025-03-31 14:41:33.204920+00:00, -1.0, 66545238.0
INFO:2025-03-31 16:41:33,387:on_shortable: 2025-03-31 14:41:33.387177+00:00, -1.0, 66545138.0
INFO:2025-03-31 16:41:35,072:on_shortable: 2025-03-31 14:41:35.071950+00:00, -1.0, 66545148.0
INFO:2025-03-31 16:41:36,571:on_shortable: 2025-03-31 14:41:36.570897+00:00, -1.0, 66545146.0
INFO:2025-03-31 16:41:36,934:on_shortable: 2025-03-31 14:41:36.934368+00:00, -1.0, 66545346.0
INFO:2025-03-31 16:41:37,119:on_shortable: 2025-03-31 14:41:37.119772+00:00, -1.0, 66545246.0
INFO:2025-03-31 16:41:37,313:on_shortable: 2025-03-31 14:41:37.313484+00:00, -1.0, 66545146.0 I don't have to wait much to get data working. @mattsta after running this test, I have some... new ideas 🤗 from types import MethodType
class TickerUpdateEvent(Event):
__slots__ = ()
# Existing methods
def trades(self) -> "Tickfilter":
"""Emit trade ticks."""
return Tickfilter((4, 5, 48, 68, 71), self)
# New method to dynamically create filter methods on the instance
def create_filter(self, tick_list: tuple, name: str) -> None:
"""
Dynamically create a filter method on this instance with the given name and tick list.
Args:
tick_list (tuple): Tuple of tick identifiers to filter.
name (str): Name of the new method to create.
Raises:
ValueError: If the name conflicts with an existing attribute.
"""
if hasattr(self, name):
raise ValueError(f"Cannot create method '{name}': name already exists on this instance")
# Define the dynamic method
def dynamic_filter(self) -> "Tickfilter":
"""Emit ticks based on a custom tick list."""
return Tickfilter(tick_list, self)
# Bind it to this instance only
setattr(self, name, MethodType(dynamic_filter, self))
# Usage example
tsla_ticker.updateEvent.create_filter((46,89), "shortable_shares")
shortable_handler = tsla_ticker.updateEvent.shortable_shares() # Works on this instance only this is the concept, the implementation can be different. we might have def dynamic_filter(self) -> class_type:
"""Emit ticks based on a custom tick list."""
return class_type(tick_list, self) so we can go beyond with this we can get a handler for any tick type, which can greatly improve what it's available today. of course we will always have monkey patching. |
Beta Was this translation helpful? Give feedback.
-
Update: so there is a bug in the I was testing again and it never showed up. Tracked it down to a refactor bug. Updated the THe ohter details could be interesting, will add to the list of things to look into. Still need to run through the eventkit updates, package that along with the |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
To note, IB appears to have a "Shortable Instruments Search tool" or "Shortable Search" which seems to serve as a user interface for IBKR's Securities Loan Borrow (SLB) system. However, it is unclear to me how to access this tool and I doubt it would offer a reasonable route for automated access anyway. I also suspect that shortability, easy-to-borrow status, and borrow rate are available through scanners in Trader Workstation, though automated access seems like it would again be a challenge and I am not sure whether scanners can be used to look up the described parameters for a given stock (and if they would be the preferred approach even if they could).
Beta Was this translation helpful? Give feedback.
All reactions