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

Connecting to Interactive Brokers REST API (v 3.8.12) #636

Open
logicwar opened this issue Nov 24, 2024 · 5 comments
Open

Connecting to Interactive Brokers REST API (v 3.8.12) #636

logicwar opened this issue Nov 24, 2024 · 5 comments

Comments

@logicwar
Copy link

Hi,

Just tried to switch from the Interactive Brokers Legacy connection to the new method. Getting the following error messages. It seems that it is looking for a running docker ? Nothing is mentioned in the documentation about it ? I only added the IB_USERNAME and IB_PASSORD variables in .env. Without adding ACCOUND_ID and IB_API_URL as mentioned as optional. Am I missing something ?

https://lumibot.lumiwealth.com/brokers.interactive_brokers.html

2024-11-24 16:21:30 | root | INFO | Connecting to Interactive Brokers REST API...
docker: error during connect: this error may indicate that the docker daemon is not running: Post "http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.24/containers/create?name=lumibot-client-portal": open //./pipe/docker_engine: The system cannot find the file specified.
See 'docker run --help'.
2024-11-24 16:21:50 | root | INFO | Not connected to API server yet. Waiting for Interactive Brokers API Portal to start...
2024-11-24 16:21:50 | root | INFO | Waiting for another 10 seconds before checking again...

@logicwar logicwar changed the title Connecting to Interactive Brokers REST API Connecting to Interactive Brokers REST API (v 3.8.12) Nov 24, 2024
@Al4ise
Copy link
Collaborator

Al4ise commented Nov 30, 2024

Hi, when running locally (not on Render), you need to have docker installed and running for it to run properly. You're right, we should probably add it to the docs, I'm putting it on my To Do list. Let me know if you face any further issues, I'd be happy to help :)

@logicwar
Copy link
Author

Thank you for your feedback, and apologies for the delay in getting back to you. I only just had the chance to review it—needed a bit of holiday time to recharge! 😊

Even with Docker running, I encountered an issue. The latest package I downloaded from PyPI appears to be missing the "resource" directory, which includes the configuration file required for the Docker image. It seems something might be missing in your deployment script.

I was able to resolve the issue by manually adding the missing files from the GitHub version, but it might be worth investigating to prevent similar issues for others.

Thanks again for your work on this!

@logicwar
Copy link
Author

logicwar commented Dec 23, 2024

Hi,

I’ve experimented a bit more with paper trading using the new connection method to IB, and I’ve noticed several issues that I’d like to highlight. Could you maybe clarify how "mature" this implementation is?

Here’s what I observed (3.8.24):

  1. Portfolio Value: The Portfolio_value appears to be incorrect with the new connection method, even though it is returned correctly using the legacy login.
  2. Bracket Orders: It seems "bracket" orders are not implemented. Additionally, all those orders get canceled, and the following error is returned: ERROR: Failed to execute order: java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0

Below the json generated and passed for the bracket orders to IBeam (obvioulsy only the limit main order for each stock and the stock itself don't seems to be included which probably exlplains why the orders are cancelled):

[{'conid': 4721, 'quantity': 9.0, 'orderType': 'LMT', 'side': 'BUY', 'tif': 'GTC', 'price': 297.16}, {'conid': 8894, 'quantity': 37.0, 'orderType': 'LMT', 'side': 'BUY', 'tif': 'GTC', 'price': 61.97}, {'conid': 5684, 'quantity': 24.0, 'orderType': 'LMT', 'side': 'BUY', 'tif': 'GTC', 'price': 142.16}]

The positions data returned (number of shares per owned stock) seems to be accurate, but the issues above are quite significant for practical use.

Let me know if there’s any workaround or if these issues are already being addressed. I’d be happy to provide further details if needed.

@logicwar
Copy link
Author

I’ve noticed that the Legacy IB connection is no longer working with version 3.8.24 (and potentially earlier versions—I’m not sure exactly since which version this started). From what I can observe, the create_order() method in interactive_brokers.py is no longer being called. Instead, only the method in order.py seems to be invoked.

I haven’t made any changes to my implementation when calling Lumibot, so this behavior appears to be related to recent updates.

@mkopani
Copy link

mkopani commented Jan 2, 2025

I'm also having issues with all of my limit orders being cancelled.

I'm noticing that on_new_order isn't firing, which means that submitted orders aren't reaching IBKR. When submitted Lumibot orders aren't found in the broker, they are cancelled. (See snippet below)

Cancellation message:
Cannot find order limit order of ... (id=...) in broker (bkr cnt=...), canceling.

for order_lumi in orders_lumi:
# Remove lumibot orders if not in broker.
if order_lumi.identifier not in [order.identifier for order in orders_broker]:
# Filled or canceled orders can be dropped by the broker as they no longer have any effect.
# However, active orders should not be dropped as they are still in effect and if they can't
# be found in the broker, they should be canceled because something went wrong.
if order_lumi.is_active():
self.strategy.logger.info(
f"Cannot find order {order_lumi} (id={order_lumi.identifier}) in broker "
f"(bkr cnt={len(orders_broker)}), canceling."
)
self.broker._process_trade_event(order_lumi, "canceled")

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

3 participants