question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Repair after hours `.marketPrice()` logic

See original GitHub issue

https://github.com/erdewit/ib_insync/blob/ddf86d398bef88a3877309fb74f37a08f493a0da/ib_insync/ticker.py#L151-L156

During non-market-hours the bid and ask are both -1 (or nan), so the range check fails, then the .midpoint() also fails (because bid and asks are combinations of -1 and nan), so .marketPrice() returns the previous day close instead of the most recent last price seen (example: currently for Sunday night, .marketPrice() is returning the Thursday close price because of how the IBKR quote/ticker system updates open/close only at new market sessions).

Looks like the logic could be updated to either:

  • just change the nan override to .last instead of .close
  • or check for full accuracy:
    • if (bid is nan or -1) and (ask is nan or -1), use last
    • if bids and asks are active, and if price is outside the spread, return synthetic .midpoint()

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:9 (9 by maintainers)

github_iconTop GitHub Comments

1reaction
erdewitcommented, May 9, 2022

@mattsta What I notice is that you still get a last value even without bid/ask. So the last value is still useful by itself and I will update the logic to take this into account.

When I try today (Monday morning, European servers), I get a ticker with only ticker.close=157.28. This is visible in TWS as well, there’s no grayed-out bid/ask anymore.

0reactions
mattstacommented, May 8, 2022

also for reference using .reqTickers():

>>> ib.connect(port=4001)
<IB connected to 127.0.0.1:4001 clientId=1>
>>> ib.reqMarketDataType(2)
>>> contract = Stock('AAPL', 'SMART', 'USD')
>>> [ticker] = ib.reqTickers(contract)
>>> print(f'{ticker.bid=}, {ticker.ask=}, {ticker.last=}, {ticker.close=}, {ticker.marketPrice()=}')
ticker.bid=-1.0, ticker.ask=-1.0, ticker.last=157.15, ticker.close=156.54, ticker.marketPrice()=156.54
Read more comments on GitHub >

github_iconTop Results From Across the Web

Does After-Hours Trading Affect Stock Prices? - Investopedia
Trading stocks during after-hours trading sessions can have a big effect on the price that an investor will pay.
Read more >
(FIX) Latest After Hours Trades - Nasdaq
After Hours Time (ET), After Hours Price, After Hours Share Volume. 16:14:07, $116, 20. 16:12:32, $116, 424. 16:12:32, $116, 248. 16:12:16, $116, 198....
Read more >
What Is After-Hours Trading? – Forbes Advisor
Can You Buy Stocks After Hours? Yes. After-hours trading allows for stocks to be traded after the stock market's regular hours. However, ...
Read more >
After-Hours Trading: Understanding the Risks - SEC.gov
The prices of some stocks traded during the after-hours session may not reflect the prices of those stocks during regular hours, either at...
Read more >
After-Hours Trading: What It Is and How It Works
After -hours trading takes place after the trading day for a stock exchange, and it allows you to buy or sell stocks outside...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found