extrange / ibkr-docker

Run IBKR Gateway/TWS in a Docker container
159 stars 34 forks source link

Cannot connect to TWS API #16

Closed katanisk8 closed 1 year ago

katanisk8 commented 1 year ago

Hi

My docker-compose setup of TWS:

image

Container is successfully created and I can open TWS and see trades etc:

image

I'm trying connect to TWS using provided EClientSocket from TWS API. Using host: ikbr, port: 8888 (tried use host as localhost, 127.0.0.1, host.docker.internal etc. nothing helped):

image

I am not getting any error but IsConnected() method at line 42 all time returning me false.

In ikbr container logs I can see thet connection to 127.0.0.1:7496 is refused:

image

What I'm doing wrong?

extrange commented 1 year ago

Try adding IBC_AcceptIncomingConnectionAction: accept to the environment key:

  ibkr:
    image: ghcr.io/extrange/ibkr:stable
    ports:
      - 6080:6080
      - 8888:8888
    restart: always
    ulimits:
      nofile: 10000
    environment:
      IBC_AcceptIncomingConnectionAction: accept
katanisk8 commented 1 year ago

Still same:

image

image

image

katanisk8 commented 1 year ago

@extrange Trusted APIs must be provided in global configuration. How to do this via env in docker?

extrange commented 1 year ago

That's strange, I'm using ib_insync and I have no problems connecting to the API, I'm not sure if this could be an issue with the C# library you're using.

You didn't specify IBC_TradingMode - by default, live is used - is your username and password for a live account?

What do you mean by trusted APIs - do you mean trusted incoming connections?

katanisk8 commented 1 year ago

To local trader workstation I'm login to live account:

image

But if app is launched I can see that this is paper account:

image

Trusted APIs I mean options in global configuration -> API -> Settings:

image

extrange commented 1 year ago

Ok. By default, the paper account is used (even if a live username is provided). Can you add the following to your config and try again?

GATEWAY_OR_TWS: tws
IBC_TradingMode: live

socat in the container will forward the API connection automatically, so external connections will appear as localhost to TWS. There is therefore no need to specify trusted IPs.

katanisk8 commented 1 year ago

Still connection refused:

image

extrange commented 1 year ago

Can you run the following and paste the output?

docker exec $(docker ps | awk '/ibkr/ {print $1}') bash -c 'apt-get install -qy iproute2 && ss -antp'
katanisk8 commented 1 year ago

State Recv-Q Send-Q Local Address:Port Peer Address:PortProcess LISTEN 0 5 0.0.0.0:5900 0.0.0.0: users:(("Xvnc",pid=7,fd=8)) LISTEN 0 5 0.0.0.0:8888 0.0.0.0: users:(("socat",pid=10,fd=5)) LISTEN 0 4096 127.0.0.11:33375 0.0.0.0: LISTEN 0 100 0.0.0.0:6080 0.0.0.0: users:(("python3",pid=19,fd=3)) CLOSE-WAIT 271 0 172.19.0.3:57548 96.16.54.170:443 users:(("java",pid=45,fd=65)) TIME-WAIT 0 0 172.19.0.3:57612 8.17.22.31:4000 TIME-WAIT 0 0 172.19.0.3:48766 146.75.118.132:80 ESTAB 0 0 172.19.0.3:59032 8.17.22.31:4001 users:(("java",pid=45,fd=61)) ESTAB 0 0 172.19.0.3:57640 8.17.22.31:4000 users:(("java",pid=45,fd=60)) ESTAB 0 0 172.19.0.3:59722 96.16.54.168:443 users:(("java",pid=45,fd=30)) TIME-WAIT 0 0 172.19.0.3:45700 64.190.197.40:4001 FIN-WAIT-2 0 0 172.19.0.3:55570 103.38.91.3:4001 CLOSE-WAIT 271 0 172.19.0.3:57558 96.16.54.170:443 users:(("java",pid=45,fd=66)) TIME-WAIT 0 0 172.19.0.3:45726 64.190.197.40:4001 LISTEN 0 5 [::]:5900 [::]:* users:(("Xvnc",pid=7,fd=9))

katanisk8 commented 1 year ago

@extrange any idea?

peter-hartmann commented 1 year ago

When using tws (default) then the '''Enable Active and Socket Clients''' option needs to be checked under '''File>Global Configuration...>API'''. Then it works. Not sure if this can be set as environment variable....

extrange commented 1 year ago

Yup, that's correct. Specifically this needs to be ticked (Enable ActiveX and Socket Clients) Screenshot from 2023-04-19 22-20-31

Unfortunately, IBCAlpha does not do this (issue). At this point, I'm afraid you'll have to manually set it, or otherwise use IBGateway (it automatically has the API enabled).

Closing this issue, as it is out of scope for this repository.

AliMoeen commented 1 month ago

Unfortunately, IBCAlpha does not do this (https://github.com/IbcAlpha/IBC/issues/120). At this point, I'm afraid you'll have to manually set it, or otherwise use IBGateway (it automatically has the API enabled).

How should I manually set ? Using VNC UI?

extrange commented 1 month ago

Yup, on the VNC web client at localhost:6080. To persist it, you can volume mount the resulting ibg.xml in /root/Jts/<long random string>.