¿ªÔÆÌåÓý

ctrl + shift + ? for shortcuts
© 2025 Groups.io

Possible bug in TWS API when requesting auction price, volume, and imbalance data for ASX-listed securities


 

Hi,

When requesting auction price, volume, and imbalance data for ASX-listed securities in the pre-auction period, I'm getting the same number returned to me for both auction volume and auction imbalance, even though the tick type is different. I've put together an MWE to help anyone else look into this possible bug. I also just ran this MWE during the pre-close period today and I've pasted some of the output produced below so you can see what I mean. Anyway, the code is:
from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.order import Order
from ibapi.order_state import OrderState

import threading
import time
from datetime import datetime
import queue
class IBapi(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
def request_auction_data_single(self, idnum: int, contract1: Contract):
self.reqMktData(idnum, contract1, 'mdoff,225', False, False, []) #225 is tick id for auction data of volume, price, and imbalance
return None
def tickPrice(self, reqId, tickType, price, attrib):
# tick price data sent by TWS is received by this function
attrib_as_string = str(attrib)
print("tickPrice triggered. reqId=%d, tickType=%d, price=%f, attribasstr=%s" % (reqId, tickType, price, attrib_as_string))
return None
def tickSize(self, reqId, tickType, size):
# tick size data sent by TWS is received by this function
print("tickSize triggered. reqId=%d, tickType=%d, size=%d" % (reqId, tickType, size))
return None
def error(self, id, errorCode, errorString):
errormessage = "TWS error. id = %d. errorcode = %d. msg = %s" % (id, errorCode, errorString)
print(errormessage)
def build_contract(self, mkt: str, scy: str, sectype: str, currency: str):
cc1 = Contract()
cc1.exchange = mkt
cc1.symbol = scy
cc1.secType = sectype
cc1.currency = currency
return cc1

def run_loop():
app.run()

app = IBapi()
app.connect("127.0.0.1", 7496, 0)

# Start the app in a thread
api_thread = threading.Thread(target=run_loop, daemon=True)
api_thread.start()

time.sleep(1) # Sleep interval to allow time for connection to server

# Request auction data
id1 = app.reqIds(-1)
contract1 = app.build_contract("ASX","NAB","STK","AUD")
app.request_auction_data_single(id1, contract1)


On my machine, this snippet will start printing auction price, volume, and imbalance data to the terminal for NAB (National Australian Bank) - which is one of the big banks listed on the ASX. I ran the snippet in the pre-close period today (just after 4pm Sydney time) and here is a snippet of the output produced:
tickSize? triggered. reqId=5, tickType=36, size=897951
tickPrice triggered. reqId=5, tickType=35, price=30.160000, attribasstr=CanAutoExecute: 0, PastLimit: 0, PreOpen: 0
tickSize? triggered. reqId=5, tickType=61, size=0
tickSize? triggered. reqId=5, tickType=34, size=912196
tickSize? triggered. reqId=5, tickType=36, size=912196
tickSize? triggered. reqId=5, tickType=34, size=915148
tickSize? triggered. reqId=5, tickType=36, size=915148
tickPrice triggered. reqId=5, tickType=35, price=30.290001, attribasstr=CanAutoExecute: 0, PastLimit: 0, PreOpen: 0
tickSize? triggered. reqId=5, tickType=34, size=915178
tickSize? triggered. reqId=5, tickType=36, size=915178
tickSize? triggered. reqId=5, tickType=34, size=915190
tickSize? triggered. reqId=5, tickType=36, size=915190
tickSize? triggered. reqId=5, tickType=34, size=916392
tickSize? triggered. reqId=5, tickType=36, size=916392
tickPrice triggered. reqId=5, tickType=35, price=30.250000, attribasstr=CanAutoExecute: 0, PastLimit: 0, PreOpen: 0
tickSize? triggered. reqId=5, tickType=34, size=916618
tickSize? triggered. reqId=5, tickType=36, size=916618
tickSize? triggered. reqId=5, tickType=34, size=917042
tickSize? triggered. reqId=5, tickType=36, size=917042
tickSize? triggered. reqId=5, tickType=34, size=917851
tickSize? triggered. reqId=5, tickType=36, size=917851
tickSize? triggered. reqId=5, tickType=34, size=917863
tickSize? triggered. reqId=5, tickType=36, size=917863
My understanding is that tickType 34 is supposed to be the volume, and tickType 36 is the imbalance. However, as can be seen here, they are both returning exactly the same number each time. Note, I've verified this behaviour across multiple securities on multiple days.

Cheers all, any help would be most appreciated.

Colin


 

I just thought I'd add to my original message: I'd be very interested in hearing whether anyone has experienced this bug on exchanges other than the ASX, and also whether anyone has experienced this bug using languages other than Python? I've joined the github project for TWS API, so if I could confirm that this behaviour only happens on Python that would certainly narrow things down.

I also should have added to the original post that the auction imbalance displays correctly in the TWS GUI, which suggests fairly strongly that the problem is occurring somewhere in the API.

Cheers,

Colin


Nick
 

You could enable the api log and see what TWS is sending over the socket.

I haven't looked at the latest api but earlier versions did not inform TWS of the language used to implement the client api. In other words TWS does not know whether a client is python or Java or C++.

If the data is correct in the api log but incorrect in python then that is useful. If the data is wrong in the api log it would be wrong in all languages.

On 4/10/2022 7:35 PM, colintbowers@... wrote:
I also should have added to the original post that the auction imbalance displays correctly in the TWS GUI, which suggests fairly strongly that the problem is occurring somewhere in the API.


 

Great suggestion thanks Nick. I'll try that and report the results back to this thread.

Cheers,

Colin


On Mon, 11 Apr 2022 at 10:26, Nick <news1000@...> wrote:
You could enable the api log and see what TWS is sending over the socket.

I haven't looked at the latest api but earlier versions did not inform
TWS of the language used to implement the client api. In other words TWS
does not know whether a client is python or Java or C++.

If the data is correct in the api log but incorrect in python then that
is useful. If the data is wrong in the api log it would be wrong in all
languages.


On 4/10/2022 7:35 PM, colintbowers@... wrote:
> I also should have added to the original post that the auction
> imbalance displays correctly in the TWS GUI, which suggests fairly
> strongly that the problem is occurring somewhere in the API.







 

Following Nick's suggestion, I made some requests in the pre-close period today and examined separately the TWS log file and the API log file. As it turns out, examining the API log file was unnecessary, since we can already see the bug in the TWS log file, before the data ever reaches the API. Here is a short excerpt:
```
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Sending tick size.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:DET] [2;6;2070488;34;37031]
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Tick size sent.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Sending tick size.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:DET] [2;6;2070488;36;37031]
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Tick size sent.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:1:6:INFO] Sending tick price.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:1:6:DET] [1;6;2070488;35;0.70999998;non-eligible;0;0;0]
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Sending tick size.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:DET] [2;6;2070488;61;0]
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Tick size sent.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Sending tick size.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:DET] [2;6;2070561;34;200000]
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Tick size sent.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Sending tick size.
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:DET] [2;6;2070561;36;200000]
2022-04-11 16:04:40.267 [UN] INFO? [JTS-afarmDispatcherS8-111S8-112] - [0:151:151:1:0:2:6:INFO] Tick size sent.
```
As can be seen, tick types 34 and 36 are both sending the same number, even though the imbalance number in the GUI is most definitely not the same number as the auction volume.

So it seems that the bug is in the TWS section of the codebase itself, rather than in the Python API, which is a bit unfortunate because I probably could have done something about it via github if it was in the Python API. So I guess this probably has to be lodged as a ticket with the IB TWS team. Does anyone have any experience with this? Are they particularly responsive or is this one of those things that could take years for them to address?

Cheers,

Colin


 

Hi all,

Just thought I would add that I've verified that the bug is definitely in TWS itself. My earlier assertion that the numbers in the GUI were correct was actually incorrect. I was looking at imbalance percentage rather than raw imbalance. When you compare raw imbalance and auction volume in TWS GUI for ASX listed stocks pre-auction, those two fields are always identical (which obviously they shouldn't be). I've submitted a bug report to IB directly with a screenshot and we'll see how responsive they are.

Cheers,

Colin


 

Which TWS version shows this behavior, Colin? And is this in a paper or real account?

We had issues with all version 10 "latest" TWS versions in combination with a paper account (including 10.15 versions) and have stayed with the "stable" TWS 981.3 for the paper account for now.

´³¨¹°ù²µ±ð²Ô


 

¿ªÔÆÌåÓý

Hi Jurgen
Thanks for responding.

Yes I read your other thread about 10.x and double checked what version I was on, but I¡¯m still in 9.x. Also this was on a live account not paper.

I think this bug has been around for a while. The imbalance field is supposed to be red or green depending on bid or ask surplus, but now that I think about it, I have never in the last few years seen it be any colour other than green for ASX stocks, which fits with the narrative that they are accidentally putting the volume in that field instead (which will always be positive).

Cheers,

Colin


On 13 Apr 2022, at 5:00 pm, ´³¨¹°ù²µ±ð²Ô Reinold via groups.io <TwsApiOnGroupsIo@...> wrote:

?Which TWS version shows this behavior, Colin? And is this in a paper or real account?

We had issues with all version 10 "latest" TWS versions in combination with a paper account (including 10.15 versions) and have stayed with the "stable" TWS 981.3 for the paper account for now.

´³¨¹°ù²µ±ð²Ô