I want to get all the recent data (past 15 most recent transactions) from the time and sales table in IBKR using the ib_insync library. The problem is I can't get the most recent data while also getting the information if the past traders were transactions to buy or to sell. I've tried this:
endDateTime = datetime.datetime.now()
startDateTime = endDateTime - datetime.timedelta(seconds=15)
time_and_sales_data = self.ib.reqHistoricalTicks(self.STOCK_CONTRACT, numberOfTicks=15, whatToShow='TRADES', useRth=False, startDateTime=startDateTime, endDateTime=endDateTime)
data = util.df(time_and_sales_data)
print(data)
This is the output:
time tickAttribLast price size exchange specialConditions
0 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=False) 118.8400 100.0 ISLAND
1 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 55.0 ISLAND I
2 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 13.0 BATS I
3 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 78.0 DRCTEDGE I
4 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 24.0 DRCTEDGE I
5 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 50.0 MEMX I
6 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 23.0 PEARL F I
7 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 78.0 DRCTEDGE I
8 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 1.0 BATS F I
9 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 86.0 BATS I
10 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=False) 118.8400 147.0 DRCTEDGE F
11 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 1.0 BATS I
12 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 3.0 DRCTEDGE I
13 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 7.0 EDGEA I
14 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 7.0 BYX I
15 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 93.0 EDGEA F I
16 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=False) 118.8350 100.0 FINRA
17 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=False) 118.8300 100.0 ISLAND
18 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 7.0 MEMX I
19 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8400 7.0 MEMX I
20 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 67.0 DRCTEDGE I
21 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 5.0 DRCTEDGE I
22 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 40.0 DRCTEDGE I
23 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 7.0 ISLAND F I
24 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 53.0 MEMX I
25 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 6.0 ISLAND I
26 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 48.0 ISLAND I
27 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 52.0 ISLAND I
28 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 6.0 DRCTEDGE F I
29 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8599 1.0 FINRA I
30 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8350 6.0 FINRA I
31 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 1.0 ISLAND I
32 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8300 9.0 ISLAND I
33 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=False) 118.8201 200.0 FINRA
34 2023-10-26 16:27:12+00:00 TickAttribLast(pastLimit=False, unreported=True) 118.8200 10.0 FINRA I
The problem with this is that the data is delayed by a couple of seconds (at least 9, which is way too much) and I can't see if the transactions were to buy or to sell.
I've then tried this:
def process_time_and_sales_data_event(msg) -> None:
print(msg)
self.ib.reqMktData(contract=self.STOCK_CONTRACT, genericTickList="233")
self.ib.pendingTickersEvent += process_time_and_sales_data_event
self.ib.sleep(1)
This is the output:
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 29, 59, 983990, tzinfo=datetime.timezone.utc), minTick=0.01, bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 29, 59, 984988, tzinfo=datetime.timezone.utc), minTick=0.01, lastExchange='P', bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), minTick=0.01, last=118.97, lastSize=100.0, lastExchange='P', ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=4, price=118.97, size=100.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=5, price=118.97, size=100.0)], bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), minTick=0.01, last=118.97, lastSize=100.0, lastExchange='P', volume=373991.0, high=121.64, low=118.35, close=121.39, ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=8, price=-1.0, size=373991.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=6, price=121.64, size=0.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=7, price=118.35, size=0.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=9, price=121.39, size=0.0)], bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), minTick=0.01, last=118.97, lastSize=100.0, lastExchange='P', volume=373991.0, open=120.56, high=121.64, low=118.35, close=121.39, ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 986980, tzinfo=datetime.timezone.utc), tickType=14, price=120.56, size=0.0)], bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 29, 59, 987961, tzinfo=datetime.timezone.utc), minTick=0.01, bid=118.95, bidSize=1800.0, bidExchange='JKPQVNUH', ask=118.97, askSize=900.0, askExchange='PQVNUH', last=118.97, lastSize=100.0, lastExchange='P', volume=373991.0, open=120.56, high=121.64, low=118.35, close=121.39, ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 987961, tzinfo=datetime.timezone.utc), tickType=1, price=118.95, size=1800.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 29, 59, 987961, tzinfo=datetime.timezone.utc), tickType=2, price=118.97, size=900.0)], bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 30, 0, 129873, tzinfo=datetime.timezone.utc), minTick=0.01, bid=118.95, bidSize=1800.0, bidExchange='JKPQVNUH', ask=118.97, askSize=900.0, askExchange='PQVNUH', last=118.97, lastSize=100.0, lastExchange='P', volume=373991.0, open=120.56, high=121.64, low=118.35, close=121.39, bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 30, 0, 130892, tzinfo=datetime.timezone.utc), minTick=0.01, bid=118.95, bidSize=1800.0, bidExchange='JKPQVNUH', ask=118.97, askSize=900.0, askExchange='PQVNUH', last=118.97, lastSize=100.0, lastExchange='K', volume=373991.0, open=120.56, high=121.64, low=118.35, close=121.39, bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 30, 0, 131470, tzinfo=datetime.timezone.utc), minTick=0.01, bid=118.96, bidSize=100.0, bidExchange='H', ask=118.97, askSize=500.0, askExchange='PQVN', last=118.96, lastSize=100.0, lastExchange='K', prevBid=118.95, prevBidSize=1800.0, prevAskSize=900.0, prevLast=118.97, volume=373992.0, open=120.56, high=121.64, low=118.35, close=121.39, ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 131470, tzinfo=datetime.timezone.utc), tickType=4, price=118.96, size=100.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 131470, tzinfo=datetime.timezone.utc), tickType=8, price=-1.0, size=373992.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 131470, tzinfo=datetime.timezone.utc), tickType=1, price=118.96, size=100.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 131470, tzinfo=datetime.timezone.utc), tickType=3, price=118.97, size=500.0)], bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 30, 0, 271049, tzinfo=datetime.timezone.utc), minTick=0.01, bid=118.96, bidSize=200.0, bidExchange='QH', ask=118.97, askSize=200.0, askExchange='P', last=118.96, lastSize=100.0, lastExchange='K', prevBid=118.95, prevBidSize=100.0, prevAskSize=500.0, prevLast=118.97, volume=373992.0, open=120.56, high=121.64, low=118.35, close=121.39, ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 271049, tzinfo=datetime.timezone.utc), tickType=0, price=118.96, size=200.0), TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 271049, tzinfo=datetime.timezone.utc), tickType=3, price=118.97, size=200.0)], bboExchange='9c0001', snapshotPermissions=3)}
{Ticker(contract=Stock(symbol='AMZN', exchange='SMART', currency='USD'), time=datetime.datetime(2023, 10, 26, 16, 30, 0, 733911, tzinfo=datetime.timezone.utc), minTick=0.01, bid=118.96, bidSize=200.0, bidExchange='QH', ask=118.97, askSize=200.0, askExchange='P', last=118.96, lastSize=11.0, lastExchange='K', prevBid=118.95, prevBidSize=100.0, prevAskSize=500.0, prevLast=118.96, prevLastSize=100.0, volume=373992.0, open=120.56, high=121.64, low=118.35, close=121.39, vwap=119.79368522, rtVolume=374004.0, rtTime=datetime.datetime(2023, 10, 26, 16, 29, 59, 601000, tzinfo=datetime.timezone.utc), ticks=[TickData(time=datetime.datetime(2023, 10, 26, 16, 30, 0, 733911, tzinfo=datetime.timezone.utc), tickType=48, price=118.96, size=11.0)], bboExchange='9c0001', snapshotPermissions=3)}
The problem with this is: I again, don't know how to see if the transactions were to buy or to sell. The good thing is, the data is updated in real time.
The question is: How do I get the past 15 transactions made on the time and sales while also knowing if they were transactions to buy or to sell ?