Keyboard Shortcuts
Likes
- Ib-Async
- Messages
Search
Re: ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
Sorry for the multipe email (as I can't edit my response). Just want to add that currently the output of the script is:
|
|||
Re: ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
The sequence is simply: 1. Connect to TWS 2. reqMktData() 3. Give it some time to get data. 4. If some fields are NaN or None, cancelMktData(), and retry. Below is a minimal example. When the problem happens, the bid is NaN. (Note the bid should usually be 20 some dollars. It's not a penny option where there can be no bid.) The problem can happen during market hours. After the gateway or TWS restarts, running the client the first time is always fine. But running it a second or more times can cause this. The problem is illusive. When it doesn't happen, it tends not to happen as long as I don't restart the gateway. If it happens, then it happens almost every subsequent time until I restart the gateway.
|
|||
Re: ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
I tested it. It does seem that type 2 will give me the most recent quote and price when the market is open. However, even if I use type 2, the problem I described in the original email persisted, namely, some fields are missing if the client is not connected to the gateway the first time the gateway restarts. |
|||
Re: ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
Okay, MktDataType 2 may be the answer. Parts of the documentation say it is the quotes recorded at the last closing, which is not what I want, but part of it says it is the last quotes seen by their system, which is what I want. Since the market is closed, I have to find out on Monday. Thank you for your help. There are still some pending questions that I need to investigate:
|
|||
Re: Determining which stock is bringing my portfolio into a loss
toggle quoted message
Show quoted text
-------- Original message -------- From: "Pranav Lal via groups.io" <pranav@...> Date: 2025-04-03 6:26 p.m. (GMT-08:00) Subject: [ib-async] Determining which stock is bringing my portfolio into a loss I am trying to write some code to determine which stock is bringing my portfolio into a loss. My logic is to see if the averageCost is greater than the current market price for each stock in the portfolio. The problem is that the current market price is nan. I am running this code after market close. Here is the relevant snippet. What am I missing? ib.connect('127.0.0.1', 4001, clientId=999990) positions=ib.positions() lc=0 for p in positions: [ticker] = ib.reqTickers(p.contract) print(p.contract.symbol) print("number of shares=" + str(p.position)) print("Cost per share=",ib.portfolio()[lc].averageCost) print("Current price per share=",ticker.marketPrice()) lc+=1 ib.sleep(1) Pranav |
|||
Re: Determining which stock is bringing my portfolio into a loss
开云体育Hi, ? Yes, setting the market data type to 2 solved the problem. ? Pranav From: [email protected] <[email protected]> On Behalf Of biney59 via groups.io
Sent: Friday, April 4, 2025 11:19 PM To: [email protected] Subject: Re: [ib-async] Determining which stock is bringing my portfolio into a loss ? Have a look at my reply to another topic, I think it is closely related to your issue because you are requesting after close: /g/ib-async/message/302 |
|||
Re: ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
Type 2 is the last data recorded at market close, which is not what I want. Also, "live" does not usually just mean that I will only get the data when there are new updates. It also means that I will get the most recent quote when there are no new updates. If it were to only get the data when there is an update, then all the snapshot functions wouldn't work. Also, it is not really consistent with my experience. I find that sometimes it is missing a bid and sometimes the close. Note that a market maker rarely only send the ask and not the bid when updating their quotes. Missing the close is even harder to explain. The fact that I never experienced this on the initial connection to a freshly started gateway also means that this is perhaps not the reason. |
|||
Re: ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
From my understanding, ib.reqMktData(contract) requests LIVE updates, meaning updates will be sent if changes occur after your request. Meaning if your contract is not active, there is chance it may not get populated right away. The documentation:
?
You might need to request different type of market data if you always want data, for example, set type to 2:?
|
|||
ib.reqMktData() does not get certain fields when connecting to the gateway the second time.
I am having an odd problem. Everything works as expected when my client makes an initial connection to the gateway. However, if I restart the client process, sometimes
I have logic to retry the subscription as follows, but it rarely does anything:
When this happens, the same few contracts out of 30 have this problem no matter how many times I retry (like the above). The other contracts are completely fine. This does not happen every time. Occasionally, restarting the client to make a new connection the second time is okay, and when the second time is okay, it tends to be okay for subsequent connections. Over 50% of the time, however, making the connection the second time will have the above-described problem. If the second time fails, all subsequent connections will also fail. The problem is not limited to the gateway. Connecting with TWS has exactly the same problem. Restarting the gateway fixes this problem. I have never seen this happening to the initial connection to the gateway instance. This seems to say that the gateway has to be restarted every time I restart my client, which is tedious. Has anyone encountered similar issues? |
|||
Determining which stock is bringing my portfolio into a loss
Hi all,
I am trying to write some code to determine which stock is bringing my portfolio into a loss. My logic is to see if the averageCost is greater than the current market price for each stock in the portfolio. The problem is that the current market price is nan. I am running this code after market close. Here is the relevant snippet. What am I missing? ib.connect('127.0.0.1', 4001, clientId=999990) positions=ib.positions() lc=0 for p in positions: [ticker] = ib.reqTickers(p.contract) print(p.contract.symbol) print("number of shares=" + str(p.position)) print("Cost per share=",ib.portfolio()[lc].averageCost) print("Current price per share=",ticker.marketPrice()) lc+=1 ib.sleep(1) Pranav |
|||
Re: Clicking an override button
开云体育Hi, ? I do not use as sophisticated an approach. I have just set the property for all orders. There is no harm in setting it that I have seen. ? Pranav ? From: [email protected] <[email protected]> On Behalf Of Gonzalo Saenz via groups.io
Sent: Saturday, March 29, 2025 8:24 PM To: [email protected] Subject: Re: [ib-async] Clicking an override button ? Thanks for your reply ? So if I follow you correctly,? ?
? ? ? ?
? |
|||
Re: Clicking an override button
开云体育Thanks for your replySo if I follow you correctly,?
|
|||
Re: Clicking an override button
开云体育Hi Gonzalo, ? Documentation is sketchy to say the least. See the below link. ? The rest of it was trial and error. ? Pranav |
|||
Re: Clicking an override button
开云体育Where did you get this information? Is there any documentation for this? From time to time my orders are canceled, this might help? ---- On Thu, 27 Mar 2025 17:41:35 +0100 Pranav Lal<pranav@...> wrote ----
|
|||
Re: ib.reqTickers() conflicts with ib.reqTickByTickData()
thank you for replying, I have identified the problem: the reqTickers was nested in the reqTickByTickData, so I changed the App structure. BTW do you know the function to use for nested call (even though I remember that nested asyncio and blocking was not recommended)? best |