Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interactive brokers get_last_price always returning yestreday's closing price (v 3.7.3) #554

Open
logicwar opened this issue Sep 16, 2024 · 3 comments

Comments

@logicwar
Copy link

Cannot get real last price from Interactive brokers, but instead yesterday's closing price

i.e:
2024-09-16 19:35:09,335: root: WARNING: Last price for AAPL not found. Using yesterday's closing price of 222.5 instead. reqId = 10004
2024-09-16 19:35:09,458: root: WARNING: Last price for BAC not found. Using yesterday's closing price of 38.65 instead. reqId = 10006
2024-09-16 19:35:09,461: root: WARNING: Last price for AXP not found. Using yesterday's closing price of 259.0 instead. reqId = 10007
2024-09-16 19:35:09,464: root: WARNING: Last price for KO not found. Using yesterday's closing price of 71.41 instead. reqId = 10008
2024-09-16 19:35:09,467: root: WARNING: Last price for CVX not found. Using yesterday's closing price of 140.61 instead. reqId = 10009

@logicwar
Copy link
Author

logicwar commented Sep 27, 2024

I believe I have identified the source of the issue (at least, it seems to work for me). However, as I am uncertain about the original logic behind the implementation, I am providing only an explanation of the changes I made without submitting an actual code modification.

In the file interactive_brokers.py, within the tickPrice() method, the price is consistently overwritten by yesterday's closing price because tickType == 9 always follows tickType == 4.

The root cause appears to be that self.tick is never initialized, remaining None, which causes the price already set by tickType == 4 to be overwritten.

I propose changing the check self.tick is None to self.price is None to resolve this, as illustrated below:

        # If the last price is not available, then use yesterday's closing price
        # This can happen if the market is closed
        if tickType == 9 and self.price is None and self.should_use_last_close: # Replaced self.tick with self.price
            self.price = price
            self.tick_type_used = tickType

@logicwar
Copy link
Author

@grzesir any chance to review this and integrate it in the base code if applicable ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@logicwar and others