开云体育

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

Re: Is it possible to get pre-market bid/ask?

 

Hey Code Block,?
?
I have level 2 data and get the same output as you during non-market hours. There's also this relevant bit in the IB documentation:
?
Frozen---2---Frozen market data is the last data recorded at market close. In TWS, Frozen data is displayed in gray numbers. When you set the market data type to Frozen, you are asking TWS to send the last available quote when there is not one currently available. For instance, if a market is currently closed and real time data is requested, -1 values will commonly be returned for the bid and ask prices to indicate there is no current bid/ask data available. TWS will often show a ‘frozen’ bid/ask which represents the last value recorded by the system. To receive the last know bid/ask price before the market close, switch to market data type 2 from the API before requesting market data. API frozen data requires TWS/IBG v.962 or higher and the same market data subscriptions necessary for real time streaming data.
?
For reference, here's a link to documentation's text: https://ibkrcampus.com/campus/ibkr-api-page/twsapi-doc/#delayed-market-data


Re: Does ib_insync work with TWS 10.30?

 

No idea, but it sounds like a great time to switch over to ib_async.


Does ib_insync work with TWS 10.30?

 

Hello folks -?
I am still using ib_insync. I was using my code fine with TWS 10.19. This week I saw a message in TWS on startup asking to upgrade to TWS 10.30 as 10.19 will not be supported past March. When I start TWS 10.30 now, I see my code does not work anymore. It just sort of hangs. Below is what I get when I hit Ctrl-C to exit the program.
When I went back to TWS 10.19, the code started running again. Has anyone else tried with TWS 10.30?
?
Thanks
?
?
Traceback (most recent call last):
? File "c:\Users\rajes\tws\testib_insync.py", line 434, in <module>
? ? ib.qualifyContracts(contract)
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\site-packages\ib_insync\ib.py", line 570, in qualifyContracts
? ? return self._run(self.qualifyContractsAsync(*contracts))
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\site-packages\ib_insync\ib.py", line 318, in _run
? ? return util.run(*awaitables, timeout=self.RequestTimeout)
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\site-packages\ib_insync\util.py", line 341, in run
? ? result = loop.run_until_complete(task)
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 629, in run_until_complete
? ? self.run_forever()
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 316, in run_forever
? ? super().run_forever()
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 596, in run_forever
? ? self._run_once()
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\asyncio\base_events.py", line 1854, in _run_once
? ? event_list = self._selector.select(timeout)
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 434, in select
? ? self._poll(timeout)
? File "C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\asyncio\windows_events.py", line 783, in _poll
? ? status = _overlapped.GetQueuedCompletionStatus(self._iocp, ms)
KeyboardInterrupt
Task was destroyed but it is pending!
task: <Task pending name='Task-12' coro=<IB.qualifyContractsAsync() running at C:\Users\rajes\AppData\Local\Programs\Python\Python39\lib\site-packages\ib_insync\ib.py:1798> wait_for=<_GatheringFuture pending cb=[<TaskWakeupMethWrapper object at 0x0000023AA11C5F40>()]>>


Re: Ambiguous Contract on qualifyContracts for SPXW Option

 

I got the same issue on when my bot traded on 0DTE SPX on 20250108 (i.e. fire an option trade every morning that expires at day-end), and my bot could not trade on that day because IB had set both the expiry and last trade day to 20250109 (instead of 20250108) in the relevant contract spec. I then simply skip trading for that day and restart trading on 10 Jan, and things went normally, believing that such extra holiday would not happen too frequently.
?
However, yesterday (20250117), my bot got similar (not the same) as the above error. When I define an option contract by :
leg1 = Option('SPX', '20250117', 6035, 'C', 'SMART', currency='USD', multiplier=100, tradingClass='SPXW')
?
The contents of the above leg1 AFTER passing to ib.qualifyContracts() was (I used to print the leg1 contents for error tracing purpose) :
Option(conId=736830804, symbol='SPX', lastTradeDateOrContractMonth='20250118', strike=6035.0, right='C', multiplier='100', exchange='SMART', currency='USD', localSymbol='SPXW ?250117C06035000', tradingClass='SPXW')
?
As you can see, the returned lastTradeDateOrContractMonth became '20250118'.? Any idea why there is a discrepancy in the expiry date? and how to fix it ?
?
Thanks a million in advance


Re: record and replay ib_async session (data only)?

 

?
After poking around I now believe that it would be fairly hard to implement a mechanism for recording and replaying an ib_async session for the purposes of capturing data.
?
For anybody who understands reasonably well how ib_async works (unlike me, apparently :-), that's probably pretty obvious. ?The basic reason is that the IB.Client and IB.Wrapper classes (which, respectively send messages to TWS and receive messages from TWS) are not so easily decoupled. ?I'd be happy to expand on this if anyone is actually interested in the details..
?
-Neal
?
?


Re: Migration from ib_insync to ib_async

 

On Wed, Jan 15, 2025 at 10:58 PM, Mel wrote:
I like having both since I have a ton of code that I don't want to delete, but may never use so why change it all and test.
Do you mean you have code that use both ib_insync and ib_async ? If the migration is so ease why import both and use both in code?
?


Re: Migration from ib_insync to ib_async

 

开云体育

I found just replacing the ib_insync with ib_async worked fine.? I use virtual environments so I created a new one, did pip install of async and copied the code I was using over.? I like having both since I have a ton of code that I don't want to delete, but may never use so why change it all and test.





-------- Original message --------
From: "pythontrader via groups.io" <ali@...>
Date: 2025-01-15 1:24 p.m. (GMT-08:00)
To: ib-async@groups.io
Subject: [ib-async] Migration from ib_insync to ib_async

Greetings,

I am planning to migrate a code base from ib_insync to ib_async.

I would like to consult with this forum to understand the scope of the migration. Specifically, should I prepare for an extensive code migration, or is it as simple as running a pip install and replacing all imports of ib_insync with ib_async?

Thank you for your insights!


Re: record and replay ib_async session (data only)?

 

?
For the record, something like the following is sufficient to record a session, that is, to record the messages passed between TWS and ib_async, presumably for later "playback", in a pickle file.
?
To implement playback via ib_async will surely take more thought. ? Ib_async would be modified so that, instead of connecting to TWS, the Connection class opens the saved pickle file, then starts playing back the saved messages from TWS to ib_async. ? The application that is using this modified ib_async would have to be built specifically to consume the data that ib_async generates from these messages, without making calls that cause ib_async to send messages to TWS . ? What I haven't yet looked into is the extent to which ib_async itself makes its own calls to TWS, and the extent to which the playback would have to synchronize itself around any such calls.
?
-Neal
?
# """ wrap connection.Connection to save TWS <-> ib_async messages, for later playback
# """

import atexit
import datetime
import inspect
import logging
import pickle
from functools import wraps
from typing import BinaryIO

import ib_async
from ib_async.connection import Connection

logger = logging.getLogger(__name__)


def _now():
return datetime.datetime.now().astimezone()


# method decorator

def
_pickle(method):

_dump_name = method.__name__

if inspect.iscoroutinefunction(method):

@wraps(method)
async def wrapper(self, *args):
pickle.dump((_now(), _dump_name, *args), self._pickle_file)
return await method(self, *args)

else:

@wraps(method)
def wrapper(self, *args):
pickle.dump((_now(), _dump_name, *args), self._pickle_file)
return method(self, *args)

return wrapper


class PickleConnection(Connection):
_pickle_file: BinaryIO

def __init__(self):
self._open_pickle_file()
super().__init__()

def _open_pickle_file(self) -> None:
timestamp = _now().strftime("%Y_%m_%d_%H_%M_%S")
filename = f"connection_{timestamp}.pkl"
atexit.register(self._close_pickle_file)
self._pickle_file = open(filename, "wb+")

def _close_pickle_file(self):
if not self._pickle_file.closed:
self._pickle_file.close()

@_pickle
async def connectAsync(self, host: str, port: int) -> None:
await super().connectAsync(host, port)

@_pickle
def disconnect(self) -> None:
super().disconnect()

@_pickle
def sendMsg(self, msg: bytes) -> None:
super().sendMsg(msg)

@_pickle
def connection_lost(self, exc) -> None:
super().connection_lost(exc)
self._pickle_file.flush()

@_pickle
def data_received(self, data: bytes) -> None:
super().data_received(data)

# patch ib_async to use PickleConnection instead of Connection

ib_async.connection.Connection = PickleConnection
ib_async.client.Connection = PickleConnection

# SCRIPT TO DUMP PKL FILE

# # !/usr/bin/env python3
#
# import datetime
# import sys
# import pickle
#
# filename = sys.argv[1]
#
# time: datetime.datetime
# method_name: str
#
# with open(filename, "rb") as fr:
#
# try:
# while True:
# time, method_name, *args = pickle.load(fr)
# timestamp = time.strftime("%H:%M:%S:%f")[:-1]
# match method_name:
# case "data_received" | "sendMsg":
# assert len(args) == 1
# args = f"{len(args[0])}b"
# case _:
# args = ", ".join(map(str, args))
# print(f"{timestamp}: {method_name}({args})")
# except EOFError:
# pass


Re: Migration from ib_insync to ib_async

 

Your experience may be different, but when I did it there were no code changes aside from the import and nothing broke.


Migration from ib_insync to ib_async

 

Greetings,

I am planning to migrate a code base from ib_insync to ib_async.

I would like to consult with this forum to understand the scope of the migration. Specifically, should I prepare for an extensive code migration, or is it as simple as running a pip install and replacing all imports of ib_insync with ib_async?

Thank you for your insights!


Re: record and replay ib_async session (data only)?

 

?
Jason,
?
Yes. ?Also I think that what I described may be the easiest way to do that.
?
One reason is that to store level 2 data efficiently you need some kind of diff-based approach (as level 2 levels are built and change incrementally over time). ?
?
This is how level 2 data is transmitted from TWS, and ib_async is already built to maintain the level 2 levels over time from the incremental update from TWS.
?
I should add that I was easily able to overload the Connection class in connection.py to store all messages sent and received from TWS. ? I will make a separate reply summarizing that code so far.
?
-Neal


Re: record and replay ib_async session (data only)?

 

I don’t know either, but since you can request the real time level 2 data via reqMktDepth, if you were to request it and subsequently store it, would that give you what you need?

On Wed, Jan 15, 2025 at 8:41 AM, Neal Young via groups.io <youngneal=gmail.com_at_groups_io_k228j4f9pxfcc8_496s0895@...> wrote:

?
What do you gain from this approach?
?
Mainly, as far as I can tell (per ), for some data historical data is not available. ?E.g. my algo uses , and I don't know how to request historical level2 data.
?


Re: record and replay ib_async session (data only)?

 

?
What do you gain from this approach?
?
Mainly, as far as I can tell (per ), for some data historical data is not available. ?E.g. my algo uses , and I don't know how to request historical level2 data.
?


Re: record and replay ib_async session (data only)?

 

What do you gain from this approach?

If I want to backtest something, I only care about the data and my algo code; ib_async is just an interface to the brokerage and doesn’t make my algo work or not work. ?I could use ib_async to request historical data that I could save and later backtest using some replay mechanism built into the back tester.

If you are successful in recording and playing back a ‘session’, I would see you gain the ability to test whether there are bugs in ib_async or TWS/GW. ? Either of which I don’t think has anything to do with (back)testing the efficacy of your algo, does it?

On Wed, Jan 15, 2025 at 6:58 AM, Neal Young via groups.io <youngneal=gmail.com_at_groups_io_k228j4f9pxfcc8_496s0895@...> wrote:

?
How hard would it be to modify ib_async to record a session for playing back later? ?The motivation is that it would give a general mechanism for recording data for later use, such as backtesting. ?
?
Specifically, I would like to have a client that runs ib_async (suitably modified) for a day, subscribing to various data feeds such as ticks and level2 data (but not making any trades or other active requests that modify account state). ?I would like the modified ib_async to record all incoming messages from TWS in a file, so that later I could play the session back. ?That is, run a different client, with ib_async simulating the incoming TWS messages recorded in the file, instead of actually connecting to TWS.
?
How hard would it be to modify ib_async to allow this? ?It seems like it _might_ be relatively easy, mainly by modifying the connection class.
?
More generally, one could imagine recording a session that also included activities such as trading, and being able to replay those verbatim, with some kind of inspector that allowed one to zoom in and inspect critical moments in a session.
?
Or perhaps someone has already tried this?
?
-Neal


record and replay ib_async session (data only)?

 

?
How hard would it be to modify ib_async to record a session for playing back later? ?The motivation is that it would give a general mechanism for recording data for later use, such as backtesting. ?
?
Specifically, I would like to have a client that runs ib_async (suitably modified) for a day, subscribing to various data feeds such as ticks and level2 data (but not making any trades or other active requests that modify account state). ?I would like the modified ib_async to record all incoming messages from TWS in a file, so that later I could play the session back. ?That is, run a different client, with ib_async simulating the incoming TWS messages recorded in the file, instead of actually connecting to TWS.
?
How hard would it be to modify ib_async to allow this? ?It seems like it _might_ be relatively easy, mainly by modifying the connection class.
?
More generally, one could imagine recording a session that also included activities such as trading, and being able to replay those verbatim, with some kind of inspector that allowed one to zoom in and inspect critical moments in a session.
?
Or perhaps someone has already tried this?
?
-Neal


Re: how to unset goodAfterTime ?

 

?
It seems this is not possible with TWS API. ?More generally, the only way to "pause" and "resume" a standing limit order seems to be to raise and lower the limit price. ?See the following topic at the TWS group for more info: /g/twsapi/topic/conditional_order_conditioned/110515714
?
-Neal


how to unset goodAfterTime ?

 

?
I am able to set the goodAfterTime field as expected for a standing order, e.g. to "20250113 15:59:00 US/Eastern", and it seems to work as expected. ?
?
However, I would also like to be able to reset the field to "", so as to remove the goodAfterTime constraint, allowing the order to fill immediately. ?I can't get this to work. ?I am able to reset the field to "", and to re-place the order, but the goodAfterTime field on the standing order is not reset. ?(See code below.)
?
(The order stays in "presubmit" status, and in the TWS GUI, if I examine the fields via the order-modification panel, it still shows the original goodAfterTime value.)
?
Checking the "audit trail" of messages through the TWS GUI, I see that the goodAfterTime field seems to transmit as a numeric field number 168.
?
As far as I can tell, even after I reset goodAfterTime, that field is not being transmitted with the new value of "".
?
I've been banging my head against this for several hours now. ? What am I doing wrong?
?
help!? ?thank you,
?
-Neal
?
Code to set the field:
order.goodAfterTime = "20250113 15:59:00 US/Eastern"
order.tif = "GTC"
ib.placeOrder(contract, order)
Code to unset it:
    order.goodAfterTime = ""
order.tif = "GTC"
ib.placeOrder(self.contract, self.order)


Is it possible to get pre-market bid/ask?

 

I'm having some trouble getting bid/ask/last information on pre-market.
The bid/ask come as -1, and the last sometimes is not present.
I've been trying

```
contract = Stock('AAPL', 'OVERNIGHT', 'USD')
ticker = ib.reqMarketData(contract)

# The result
Ticker(contract=Stock(symbol='AAPL', exchange='OVERNIGHT', currency='USD'), time=datetime.datetime(2025, 1, 9, 14, 25, 30, 913270, tzinfo=datetime.timezone.utc), minTick=0.01, bid=-1.0, bidSize=0.0, ask=-1.0, askSize=0.0, close=242.7, bboExchange='9c0001', snapshotPermissions=3)
```
I think to get the "last", I use the specific exchange, at least looking at my previous logs I see
```
# using OVERNIGHT
Ticker(contract=Stock(symbol='RS', exchange='OVERNIGHT', currency='USD'), time=datetime.datetime(2025, 1, 8, 13, 37, 45, 829020, tzinfo=datetime.timezone.utc), minTick=0.01, bid=-1.0, bidSize=0.0, ask=-1.0, askSize=0.0, volume=49.0, close=270.11, ticks=[TickData(time=datetime.datetime(2025, 1, 8, 13, 37, 45, 829020, tzinfo=datetime.timezone.utc), tickType=8, price=-1.0, size=49.0), TickData(time=datetime.datetime(2025, 1, 8, 13, 37, 45, 829020, tzinfo=datetime.timezone.utc), tickType=9, price=270.11, size=0.0)], bboExchange='a60001', snapshotPermissions=3)

# using NYSE
Ticker(contract=Stock(symbol='RH', exchange='NYSE', currency='USD'), time=datetime.datetime(2025, 1, 8, 14, 8, 9, 599540, tzinfo=datetime.timezone.utc), minTick=0.01, bid=-1.0, bidSize=0.0, ask=-1.0, askSize=0.0, last=408.0, lastSize=1.0, volume=61.0, close=403.82, bboExchange='a60001', snapshotPermissions=3)
```

Do I need to do this some other way?
I haven't bought a level 2 subscription, do I need to buy that, and then I would have to call a different function to get the bid and ask?

Thanks for any pointer.


Re: Ambiguous Contract on qualifyContracts for SPXW Option

 

Yeah this is one of those edge cases that we all dread... My bot had the same issue and could not run.
?
Notice the localSymbol code allows you to break the ambiguity: one is for today, the other for tomorrow, but I don't think it can be traded since the market will be closed, so your only choice is today.


Re: Ambiguous Contract on qualifyContracts for SPXW Option

 

This is because IB screwed something up and marked the AM expiration as SPXW instead of SPX. Today was the first time I have ever seen this. IB support told me they were aware of the issue and were working on it. Not sure if they resolved it today or not?


On Wed, Jan 8, 2025, 2:59?PM Pratik Babhulkar via <praditik=gmail.com@groups.io> wrote:
Interesting observation. Would love to have your opinions.?
?
I have been using this script to validate the option contracts using `qualifyContracts`. Today, I am getting ambiguous contracts error for this Option Contract. I know about few days when monthly and weekly expiries coincide. But, this is a different case. Both of the possible contracts suggested are SPXW trading class and the only difference is in the contract ID. Is it possible somehow that this has some relation with AM vs PM settlement? I am noticing this for first time.
?
?
Ambiguous contract: Option(symbol='SPX', lastTradeDateOrContractMonth='20250108', strike=5895.0, right='PUT', multiplier='100', exchange='SMART', currency='USD', tradingClass='SPXW'), possibles are [
Contract(secType='OPT', conId=749800369, symbol='SPX', lastTradeDateOrContractMonth='20250108', strike=5895.0, right='P', multiplier='100', exchange='SMART', currency='USD', localSymbol='SPXW ?250108P05895000', tradingClass='SPXW'),
Contract(secType='OPT', conId=749800737, symbol='SPX', lastTradeDateOrContractMonth='20250108', strike=5895.0, right='P', multiplier='100', exchange='SMART', currency='USD', localSymbol='SPXW ?250109P05895000', tradingClass='SPXW')]
?
Regards,
Pratik
?
?