brndnmtthws / thetagang

ThetaGang is an IBKR bot for collecting money
GNU Affero General Public License v3.0
1.99k stars 266 forks source link

ERROR:ib_insync.wrapper:Error 10197, reqId..... No market data during competing live session #167

Closed jbieberly closed 3 years ago

jbieberly commented 3 years ago

I have this set up in a VM and I'm trying to run the Docker container with TWS 978 on my paper trading account.

The Bot hangs on "Searching option chain for symbol=SPY right=P, this can take a while..."

The ib_insync.loglogs show an error:

ERROR:ib_insync.wrapper:Error 10197, reqId..... No market data during competing live session.

I've verified that all my other IBKR sessions are closed, also it is getting market data, current prices for SPY show up in the console and in the error message. I can launch TWS by itself and it gets market data successfully on the same paper account.

I found these two threads regarding the same error message, but none has a resolution:

https://groups.io/g/insync/topic/19741887 https://groups.io/g/twsapi/topic/24929715

brndnmtthws commented 3 years ago

So I've run into this a few times unfortunately, and I'll share what has worked for me. I can't necessarily guarantee it'll help. Worst case, if you can't get it resolved, you can try opening a support ticket with IBKR, but they are pretty useless.

  1. Make sure you're running the latest version of ThetaGang. I actually just published a new release before writing this comment.
  2. Check your config.ini and make sure that ExistingSessionDetectedAction=primaryoverride (I actually just realized this is wrong in the sample config, so I will update that).
  3. Double check that your WAN IP does not change. In some cases, such as if your ISP uses a CGNAT or you have dual-WAN, your public IP may change. You can verify this with checking that curl -4 icanhazip.com when run repeatedly always returns the same IP. IBKR, for whatever reason, has decided to tie API sessions to your IP address which is a mistake on their part. Presumably they do this for "security" reasons, but this is security theatre at best, and at worst is wrong and an incorrect implementation of RESTful API best practices.
  4. If you have an IBKR session open and don't log off cleanly, I have found there's an unspecified logoff period. When shutting down TWS or after using the website/app, be sure to explicitly log out or use the File -> Exit option from TWS. This usually helps.

In my case, problem 3 (above) was an issue for me. I worked around it by using a WireGuard VPN which I start and stop before running ThetaGang. My IP is not consistent so IBKR has a fit any time I try to use their service. Without a VPN it does not work for me.

I hope that helps. Let me know how it goes.

jbieberly commented 3 years ago

I am using the docker image with the command brndnmtthws/thetagang:latest, assuming you updated the docker image when you publish a new release, I should be running the newest one.

I tried changing ExistingSessionDetectedAction=primaryoverride

Now I get several different Error 10197 reqId numbers (310,413,332,etc) then a single Error 10197 reqId 471. Whereas the original manual setting I would get a lot of "Info" level logging and a single Error 10197 reqId 471

I don't believe I have any other IBKR sessions open at the moment, I tried this after a reboot, which you would think would kill all the sessions. It's possible I could get a different result next week if I try running this without any other logins at all, assuming they should timeout. -Update: I tried running the docker command after not making an IBKR login for 4 days, no change in behavior.

I will keep an eye on my IP and see if that changes. _-_Update: My IP remains the same over the long weekend and the issue persists.__

Thanks for your help!

Ianfinn1994 commented 3 years ago

@jbieberly are you using a standalone virtual machine, or are you using a WSL? Could you elaborate an easy way for a novice code to implement this bot easily to run all day on my windows tower?

jbieberly commented 3 years ago

I'm using a Centos machine with KVM running another Centos VM. Since it is available as a Docker image it might be easiest to run it through the Windows Docker application for you.

Ianfinn1994 commented 3 years ago

I've never really ran any code before like this. It gives me an error about not being able to find the ibc file and when I clone the repo from GitHub and edit it on vscode, I can't find the file system. Maybe a bit more tinkering. Also I see an ip setting and a few things that I'm not sure if I have to add their location to the bot config file too.

brndnmtthws commented 3 years ago

I've never really ran any code before like this. It gives me an error about not being able to find the ibc file and when I clone the repo from GitHub and edit it on vscode, I can't find the file system. Maybe a bit more tinkering. Also I see an ip setting and a few things that I'm not sure if I have to add their location to the bot config file too.

It's a big leap to go from never using Docker to suddenly running something like this with Docker. My suggestion is to take some time to familiarize yourself with the tools. Specifically, this tool (when used with Docker) requires the use of volumes, as your configuration files need to be passed into the container in some way. Perhaps do a search on YouTube for "docker introduction", and re-read the official docs.

I can tell you what commands to type, but you will keep hitting walls until you have a little more experience with the tools. I hope that helps.

Ianfinn1994 commented 3 years ago

Well I know that docker is built with containers, I've watched a few videos but I don't really learn like that because people really don't explain things. I need this to work though, because I have 10 k and want to backtest this with 3 etfs I regularly sell premium on, but because my family kept me away from technology, i really have a hard time initializing things, i can navigate around the shell and cd into things, but like you said, walls. I pretty much have toe code either on my desktop in a folder and i try to open in a debian environment like that, and I've git cloned into vscode. Also used docker desktop and tried to use the cli to build like that but it says docker isn't recognized. Ill keep scanning youtube, or if you could get me started i could figure out the rest im sure. I know once i build the dockerfile, that it will install everything i need and then i can run the bot.

jbieberly commented 3 years ago

I was able to get the app to work by installing it on my same VM I had configured to run the Docker image. So the issue I am having is only present while running with Docker. Probably this is what I should be doing anyway if I want to modify it or add something to the tool.

Ianfinn1994 commented 3 years ago

https://www.docker.com/blog/docker-desktop-wsl-2-best-practices/

https://marketplace.visualstudio.com/items?itemName=ms-vscode-remote.vscode-remote-extensionpack

I found this, and apparently vscode isn't really the best option.

brndnmtthws commented 3 years ago

I'm not super familiar with running Docker on windows, but I think the best place to start is to follow the official Docker guide.

It sounds like you already have Docker installed and working, which is a good start. Here's a guide to using volumes on windows. You'll need to create a directory somewhere on your machine with the necessary configuration files (thetagang.toml and config.ini). Start with the example configuration files from the repo (you shouldn't need to modify the config.ini file much, but you should go through thetagang.toml).

brndnmtthws commented 3 years ago

I've updated the README with more detailed instructions here: https://github.com/brndnmtthws/thetagang#up-and-running-with-docker. Hopefully it's a little more clear how to use it.

I'm going to close this issue now, please re-open a new issue if you continue to have problems.

Ianfinn1994 commented 3 years ago

I promise you i will understand it after this part. I am in a WSL:ubuntu distro. Ive edited the file a little bit with my basic information, and have been using my ide and the terminal mostly for this. As far as all these paths? Im getting a build error as well about /etc/thetagang. should thetagang dir be made inside the /etc of my vm? Or one here and one in /etc?

delos@DESKTOP-087HUSE:~/thetagang $ curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/thetagang.toml -o ~/thetagang/thetagang.toml % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 3619 100 3619 0 0 18848 0 --:--:-- --:--:-- --:--:-- 18848

delos@DESKTOP-087HUSE:~/thetagang$ $ curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/ibc-config.ini - ~/thetagang/config.ini $: command not found

delos@DESKTOP-087HUSE:~/thetagang$ curl -Lq https://raw.githubusercontent.com/brndnmtthws/thetagang/main/ibc-config.ini -o ~/thetagang/config.ini % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 20417 100 20417 0 0 106k 0 --:--:-- --:--:-- --:--:-- 106k

delos@DESKTOP-087HUSE:~/thetagang$ docker run --rm -it \

-v ~/ibc:/etc/thetagang \
brndnmtthws/thetagang:latest \
--config /etc/thetagang/thetagang.toml

Unable to find image 'brndnmtthws/thetagang:latest' locally latest: Pulling from brndnmtthws/thetagang b802c2c29a77: Already exists 714bb000e3da: Pull complete 26104bd1066d: Pull complete 4f4fb700ef54: Pull complete 52d927c1b1b7: Pull complete Digest: sha256:2df6eaf2a815e9dcace870ffc615cc0b591deae69ccd51c9b0f909f996075e83 Status: Downloaded newer image for brndnmtthws/thetagang:latest

  • export DISPLAY=:1
  • DISPLAY=:1
  • Xvfb :1 -ac -screen 0 1024x768x24
  • exec poetry run thetagang --config /etc/thetagang/thetagang.toml Usage: thetagang [OPTIONS] Try 'thetagang -h' for help.

Error: Invalid value for '-c' / '--config': Path '/etc/thetagang/thetagang.toml' does not exist.

delos@DESKTOP-087HUSE:~/thetagang$

brndnmtthws commented 3 years ago

Change the command to:

$ docker run --rm -it \
    -v ~/thetagang:/etc/thetagang \
    brndnmtthws/thetagang:latest \
    --config /etc/thetagang/thetagang.toml

And you should be good. I'll fix the README, that's an error.