¿ªÔÆÌåÓý

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

highest bid higher than lowest ask

 

¿ªÔÆÌåÓý

Hello all,

In trading the MES/ES futures, very often it happens that the highest bid is higher than than the lowest ask. This is usually no more than 0.25 or 0.50 points for a short period of time. Is there something wrong in my data processing, or do other developers witness the same thing? What could be going on, shouldn't this be handled by the exchange?

At times when there's an important news item, it's extreme, look at the attached screenshot during the exact moment the CPI numbers were released earlier this week.



It took 19 seconds(!) to settle back to normal, highest bid lower than lowest ask.



Hope to hear from your experience, take care,
Raoul


Re: non-simultaneous clients w/ different ID#s won't receive price ticks

 

¿ªÔÆÌåÓý

I think your claim is rather too strong.

?

There may well be a bug as you suggest (I need to do my own test in another language to confirm it ¨C I¡¯m not equipped for Python), but it¡¯s a very restricted circumstance. Your sample code is asking for delayed frozen data. Then it¡¯s disconnecting as soon as it receives the very first price tick, without even checking what the tick type is.

?

So it¡¯s a totally unrealistic scenario.

?

Perhaps you can narrow it down a bit more. Does it still happen if you disconnect after the second tick is received? If not, then that is useful evidence, and would be helpful to people trying to assess whether there¡¯s a problem here that might affect them. If it does, then what about the third tick, or if you wait for say 5 millisecs, 10 millisecs, 1 second, before disconnecting? Similarly what happens if you disconnect immediately when you receive the first tick, but then delay for a short time before actually exiting?

?

I can certainly confirm that the normal usage of requesting non-delayed non-frozen data, making some sort of sensible use of the stream, and then disconnecting, works just fine with multiple clients either simultaneously or serially.

?

So as far as I¡¯m concerned, with what you¡¯ve said so far, there is nothing in this to prevent the vast majority of API users working with the 10.nn versions.

?

Nevertheless a bug is a bug, so it probably should be fixed assuming it can be confirmed.

?


Re: Using old versions of TWS

 

Generally speaking I think the advice in this thread is pretty good. That said, it's actually possible that new versions of TWS/IBGW regress and introduce bugs that didn't exist before. Developers aren't perfect; not even those at IB.

In this case, if e.g. you've installed the self-updating version, you may wake up one morning to alarm bells sounding because an update happened that borks your workflow in an unexpected way. Or worse, you'll blindly update without realizing your trading is going down the tubes because some feature of TWS/IBGW that you relied on broke in a subtle way.

So, the more and more important your trading system becomes to you, you'll certainly want to consider sophisticated upgrade strategies that include archiving old versions, the ability to perform some system integration testing of new versions, and the ability to roll-back in case a regression error is found.

I'm currently struggling with this to some degree because IB has retired the 9.x branch and unfortunately there appears to be a regression in the 10.x branch that hasn't been fixed (which I've posted about here). Obviously I'm holding off on upgrading until the issue is fixed and weighing my alternatives in terms of workarounds.

Just a reminder and word of caution... blindly updating to the "latest and greatest" is not a panacea either.


Re: Collecting tick-by-tick price data

 

Helpful, thanks!


Re: Collecting tick-by-tick price data

 

The post you pointed to is 15 years old, and, at that point, only reqMktData existed. The API Reference Guide chapter ""? describes how sampling/aggregation is handled today. So the post is still relevant for reqMktData (even if sampling is a little faster and smarter now).

was introduced only a few years back and is not sampled or aggregated. Consequently, data volume is huge and the limits of how many simulations subscriptions can exist are much tighter, Every trade (ask/bid) is reported to you as it happens.

If you subscribe, for example, via reqMktData for an instrument, you will never get more that 4 to 6 updates per second even if that instrument is traded heavily. If you subscribe to TickByTiclLast for the same instrument, you might get hundreds if not thousands of ticks in very busy seconds. And you will see every price change including the "102" the post in your link refers to.

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

On Wed, Sep 14, 2022 at 03:37 PM, buddy wrote:

´³¨¹°ù²µ±ð²Ô makes extremely valid points. If you're seeing more data in the TWS log file than your Python code is collecting, then you're probably not trapping everything you think you are. Otherwise, a modern computer is quite fast and should generally have enough memory to deal with TWS/IBGW... unless there's something like a memory leak in your code or some inefficient O(exp(x)) algorithm you've written.

As far as the link I provided, I think it's helpful to be aware of in case you are comparing TWS data to another provider's data (I wasn't sure what "log" you were referring to). That said, I'm not sure why it would pertain to reqMktData and not to tickByTickAllLast, perhaps you can elaborate on that ´³¨¹°ù²µ±ð²Ô? It could be helpful for future reference.


Re: Collecting tick-by-tick price data

 

´³¨¹°ù²µ±ð²Ô makes extremely valid points. If you're seeing more data in the TWS log file than your Python code is collecting, then you're probably not trapping everything you think you are. Otherwise, a modern computer is quite fast and should generally have enough memory to deal with TWS/IBGW... unless there's something like a memory leak in your code or some inefficient O(exp(x)) algorithm you've written.

As far as the link I provided, I think it's helpful to be aware of in case you are comparing TWS data to another provider's data (I wasn't sure what "log" you were referring to). That said, I'm not sure why it would pertain to reqMktData and not to tickByTickAllLast, perhaps you can elaborate on that ´³¨¹°ù²µ±ð²Ô? It could be helpful for future reference.


Re: Collecting tick-by-tick price data

 

Thank you for reply, ´³¨¹°ù²µ±ð²Ô!

I ran my code for whole day and then checked my python list it had 40k entries and then I analyzed log for the same day. The log had 400k price points. I ran nothing else, but my single piece of code. This is how I know.?

  • This is good idea to review call path. I am also going to run my code for my much shorter time frame (minute or two) and check how much data I have in log vs python code. Hopefully, this way I can pinpoint what data points are missing
  • There is really nothing time consuming in my code. The only deficiency I see right now it that I used a = a + [some_value] on Python list, instead of a.append(some_value). Append should be faster.?
  • I do have a sleep in my main thread. I put my main thread to sleep until the end of trading day. However, I do not think it should affect the thread with app.?
  • Yes, my client is on the same machine as IB gateway. There are no network issues.

This is a bit off topic, but decrypting 300MB api-log file takes about 3 hours. This is on Google compute instance with 2 cores. It seems too long.?

Regards


Re: Collecting tick-by-tick price data

 

That link is not relevant to your problem. It relates to and not to TickByTick data that you are asking for.

When you say you see 400k ticks in the log file, is that the TWS/IBG API log file with market data included? And how do you know your client only saw 40k ticks?

We cannot fix your code for you by email here in the group, but here a few things that come to mind that you could check:
  • You should review all call paths in your client to make sure that tickByTick callbacks properly handle all data presented to them. Maybe add a simple counter so that you know exactly how often the API calls them.
  • Does your code contain any time consuming operations, that prevent it from handling callbacks on a timely basis?
  • You use time.sleep after the connection call. Does your client use sleep anywhere else? Calling time.sleep (in any API language) is a really bad idea and shall not be done.
  • Is your client co-located with TWS/.IBGW in the same server, or is there a network there them?

Your client (even in Python) should be able to handle 400k ticks without breaking a sweat. Our client (while implemented in Java) handles the data streams from 80+ instruments, plus five 15 TickByTick streams (five times Last, BidAsk, and Midpoint each) plus several Level II Market Book feeds. We see between 40Mio and 90Mio ticks per day (500per second average over 24hrs, with peaks of 15k ticks per second and more).

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



On Wed, Sep 14, 2022 at 02:27 PM, GreenGreen wrote:
Thank for the link, buddy!

This is very interesting. I think this is next level for me. For now I would be happy to reconcile why api-log has more data than my python code.


Re: Collecting tick-by-tick price data

 

Thank for the link, buddy!

This is very interesting. I think this is next level for me. For now I would be happy to reconcile why api-log has more data than my python code.


Re: Collecting tick-by-tick price data

 

I believe you're suffering from the issue discussed here.


Collecting tick-by-tick price data

 

Below is my code to collect tick-by-tick daily data for SPY. When I run it I get around 40k trades for SPY, but when I check log file I get around 400k trades a day. So some of the data is missing. My first thought is that my code is running too slow and because of api buffer overflow some data gets dumped. Is this accurate guess? Is there way to confirm it? How else would you go about collecting tick-by-tick data?
Minor detail: for illustration purposes I showed only last price request for one ticker, but when I run actual code, there is also bid and ask request for another ticker along with last price request.?

from ibapi.client import EClient
from ibapi.wrapper import EWrapper
from ibapi.contract import Contract
from ibapi.ticktype import TickTypeEnum
import time
import threading

class TestApp(EWrapper, EClient):
def __init__(self):
EClient.__init__(self, self)
self.last_exchange = []
self.last_market_time = []
self.last_price = []
self.last_size = []

def error(self, reqId, errorCode, errorString):
print("Error: ", reqId, " ", errorCode, " ", errorString)

def tickByTickAllLast(self, reqId, tickType, time, price, size, tickAtrribLast,
exchange, specialConditions):

super().tickByTickAllLast(reqId, tickType, time, price, size, tickAtrribLast,
exchange, specialConditions)


self.last_exchange = self.last_exchange + [exchange]
self.last_market_time = self.last_market_time + [time]
self.last_price = self.last_price + [price]
self.last_size = self.last_size + [size]

app = TestApp()

contract = Contract()
contract.symbol = "SPY"
contract.secType = "STK"
contract.exchange = "SMART"
contract.currency = "USD"
contract.primaryExchange = "ARCA"

app.connect("127.0.0.1", 4002, 0)
time.sleep(1)
app.reqTickByTickData(1 ,contract, "Last", 0, False)
api_thread = threading.Thread(target=app.run)
api_thread.start()




Re: No historical market data for AAL/STK@VALUE Last 1d

 

Maybe a different issue

Try emptying the contract, (including symbol)
Just keep conId (and maybe exchange=SMART)
I found out that Conflicting info in Contract when conId is specified lead to strange result
I am not surprised by what your report as looking worst than if you nicely specify the primaryExchange without specifying conId.


Re: No historical market data for AAL/STK@VALUE Last 1d

 

Get same issue using exchange="SMART" , But as you point it out it works when exchange = primaryExchange.

IMHO
Raising the question about what you look for when looking historical,
Otherwise said, historical yes, but from which exchange ?

How did IB compound a bar across multiple exchange ?
Did they even compound it ? (half ligit) or resolve that for us ?
Does IB require now that you specify where do you want your data from ?

Suggestion for this thread
Avoid using SMART and set exchange = primaryExchange,
primary is a decent source for historical. Just never had reasons to grab historical from alternate exchange.


Re: Credentials for demo account?

 

Yeah, that defunct demo account was exactly what I was needing.

I already have a paper-trading account, but it follows regular trading hours. If I code on the weekend, then I cannot test any of my code until the week.

I might have to create something on my own.

Thanks.

-Ray.


On Tuesday, September 13, 2022 at 12:06:22 p.m. PDT, Richard L King <rlking@...> wrote:
There used to be a thing called the ¡®demo account¡¯, which made use of generated (rather than real data), and provided this data around the clock, including at weekends. You didn¡¯t need an account with IB to use it, and it would do sort of simulated trades, but it really wasn¡¯t an accurate representation of how the live system works (whereas the paper-trading system is a very close approximation in nearly every respect).

?

In the days before the paper trading account was introduced (around 2005-6 if I remember correctly), the demo account was the only way to test placing orders without risking real money, so it had some limited uses for API developers, but I think it¡¯s real purpose was to show potential customers what TWS was like.

?

The demo account was abandoned three or four years ago: it had a username of ¡®edemo¡¯ and a password of ¡®demouser¡¯. I think if you try to log in to it now, you just get politely brushed off.

?

The free trial accounts you can get now are basically the paper-trading system, so they¡¯re pretty realistic and use live data, but they¡¯re time-limited: after a certain period (I don¡¯t actually know how long) you can¡¯t log in any more.

?

Richard


Re: Credentials for demo account?

 

¿ªÔÆÌåÓý

There used to be a thing called the ¡®demo account¡¯, which made use of generated (rather than real data), and provided this data around the clock, including at weekends. You didn¡¯t need an account with IB to use it, and it would do sort of simulated trades, but it really wasn¡¯t an accurate representation of how the live system works (whereas the paper-trading system is a very close approximation in nearly every respect).

?

In the days before the paper trading account was introduced (around 2005-6 if I remember correctly), the demo account was the only way to test placing orders without risking real money, so it had some limited uses for API developers, but I think it¡¯s real purpose was to show potential customers what TWS was like.

?

The demo account was abandoned three or four years ago: it had a username of ¡®edemo¡¯ and a password of ¡®demouser¡¯. I think if you try to log in to it now, you just get politely brushed off.

?

The free trial accounts you can get now are basically the paper-trading system, so they¡¯re pretty realistic and use live data, but they¡¯re time-limited: after a certain period (I don¡¯t actually know how long) you can¡¯t log in any more.

?

Richard

?

?

From: [email protected] <[email protected]> On Behalf Of buddy
Sent: 13 September 2022 17:46
To: [email protected]
Subject: Re: [TWS API] Credentials for demo account?

?

Demo account?!?! I don't understand... do you mean a "free trial" account? You should be able to open one of those here:


Re: Adding timezone to request

 

Looks fixed in 10.19.0e: the date part is no longer dropped.


non-simultaneous clients w/ different ID#s won't receive price ticks

 

I've noticed what appears to be a bug in the 10.x versions of TWS/IBGateway.

After signing into the IBGateway (or TWS) application, a client program can't get ticks for a stock unless subsequent invocations use the same client ID as the first instance which requested those ticks. This wasn't how things worked in the 9.x versions.

Indeed, the documentation claims that up to 24 clients can be connected at the same time. This would lead me to believe that there shouldn't be any problems with clients that connect and disconnect one after another.

This is happening when requesting both live and delayed/frozen data. Also, it's happening with real money as well as demo accounts.

Basically, it always happens in these circumstances: if your client connects with ID x, requests market data for a particular stock, and disconnects as soon as it gets a price tick. Then, the next client to connect won't be able to get any price ticks for that particular stock unless it also connects with the same ID #.

Has anyone else noticed the same problem? There's some example code written in Python located but the problem also happens in C++ (and I assume all the other supported languages).

This makes me think there's a buggy Java destructor in the 10.x branch of the API implementation... any thoughts?


Re: Credentials for demo account?

 

Demo account?!?! I don't understand... do you mean a "free trial" account? You should be able to open one of those here:

https://www.interactivebrokers.com/en/trading/free-trial.php


Re: No historical market data for AAL/STK@VALUE Last 1d

 

Because the earliest data points to a delisted name (LCC/US Airways), notice the @VALUE in callbacks in ´³¨¹°ù²µ±ð²Ô's post. Delisted names data isn't offered by IB, for whatever reason.


Re: No historical market data for AAL/STK@VALUE Last 1d

 

AAL merged with LCC, so I think the earliest point in data refers to LCC instead, which is of course delisted now. This seems to be an IBKR issue quite clearly.
Data problems are a plenty now it seems. For example, I cannot get any tickByTick data for ABNB for example, whether specifying the primary exchange or not.