brndnmtthws / thetagang

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

Requested market data is not subscribed #175

Closed sirugh closed 3 years ago

sirugh commented 3 years ago

First, thanks for making this tool!

I finally got docker/wsl running along with the mounted paths and config, but I'm hitting an issue and I'm a bit stuck. Unsure if I need to modify part of the config or if there is a bug. I've basically copied the sample config files - the only changes are the addition of my username/password to both, although it wasn't clear to me if I needed to add it to both or just thetagang.toml.

See the stack below:

2021-02-11 16:55:41,008 ib_insync.Watchdog INFO Starting
2021-02-11 16:55:41,008 ib_insync.IBC INFO Starting
2021-02-11 16:56:11,035 ib_insync.client INFO Connecting to 127.0.0.1:4002 with clientId 1...
2021-02-11 16:56:11,036 ib_insync.client INFO Connected
2021-02-11 16:56:11,058 ib_insync.client INFO Logged on to server version 152
2021-02-11 16:56:11,061 ib_insync.client INFO API connection ready
2021-02-11 16:56:11,067 ib_insync.wrapper INFO Warning 2158, reqId -1: Sec-def data farm connection is OK:secdefil
2021-02-11 16:56:12,075 ib_insync.ib INFO Synchronization complete
2021-02-11 16:56:12,271 ib_insync.wrapper INFO Warning 2119, reqId -1: Market data farm is connecting:usfarm
2021-02-11 16:56:12,318 ib_insync.wrapper INFO Warning 2104, reqId -1: Market data farm connection is OK:usfarm
2021-02-11 16:56:12,383 ib_insync.wrapper ERROR Error 354, reqId 4: Requested market data is not subscribed.Delayed market data is available.Error&BEST/STK/Top&BEST/STK/Top, contract: Stock(symbol='SPY', exchange='SMART', currency='USD')
2021-02-11 16:56:42,415 eventkit.event ERROR Value () caused exception for event Event<connectedEvent, [[None, None, <function start.<locals>.onConnected at 0x7fa844652c80>]]>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/eventkit/event.py", line 181, in emit
    func(*args)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/thetagang.py", line 113, in onConnected
    portfolio_manager.manage()
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 271, in manage
    self.check_if_can_write_puts(account_summary, portfolio_positions)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 443, in check_if_can_write_puts
    self.wait_for_market_price(ticker)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 60, in wait_for_market_price
    10,
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  [Previous line repeated 7 more times]
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 57, in while_n_times
    raise "Exhausted retries waiting on predicate. This shouldn't happen. "
TypeError: exceptions must derive from BaseException
sirugh commented 3 years ago

Ah, saw on another issue that the problem might be related to subscriptions in the IB Account. I added

NASDAQ (Network C/UTP)
NYSE (Network A/CTA)
NYSE American, BATS, ARCA, IEX, and Regional Exchanges (Network B)
OPRA (US Options Exchanges)

and the error is a bit different now.

2021-02-11 17:18:33,811 ib_insync.wrapper ERROR Error 10197, reqId 4: No market data during competing live session, contract: Stock(symbol='SPY', exchange='SMART', currency='USD')
2021-02-11 17:19:03,810 eventkit.event ERROR Value () caused exception for event Event<connectedEvent, [[None, None, <function start.<locals>.onConnected at 0x7f536587bc80>]]>
Traceback (most recent call last):
  File "/usr/local/lib/python3.7/dist-packages/eventkit/event.py", line 181, in emit
    func(*args)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/thetagang.py", line 113, in onConnected
    portfolio_manager.manage()
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 271, in manage
    self.check_if_can_write_puts(account_summary, portfolio_positions)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 443, in check_if_can_write_puts
    self.wait_for_market_price(ticker)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/portfolio_manager.py", line 60, in wait_for_market_price
    10,
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 60, in while_n_times
    while_n_times(pred, body, remaining - 1)
  [Previous line repeated 7 more times]
  File "/usr/local/lib/python3.7/dist-packages/thetagang/util.py", line 57, in while_n_times
    raise "Exhausted retries waiting on predicate. This shouldn't happen. "
TypeError: exceptions must derive from BaseException
sirugh commented 3 years ago

Ok, red herring - the No market data during competing live session error was from me being logged in to the online web portal. Guess they don't allow you to do that while trying to access the API. So back to the original "not subscribed" error, even after adding the subscriptions. Is there a specific subscription we need to enable? What about asset class/permissions to trade? I see this hardcoded:

https://github.com/brndnmtthws/thetagang/blob/2360c6b8b8ccb488982c247811821df1e417dfc4/thetagang/portfolio_manager.py#L64

brndnmtthws commented 3 years ago

So a couple things first:

  1. Have you enabled market data subscriptions? It sounds like you have, but you may want to double check you have the right subscriptions. At the very least, I think you want Cboe One and US equities & options streaming bundle. See below for the subscriptions I have enabled.
  2. Check the ExistingSessionDetectedAction setting here. I recommend using primaryoverride which will kick out any other sessions. IBKR doesn't let you have more than one active session at a time.

It sounds like you may have already somewhat figured it out. Let me know how it goes. It's easier to start by making sure you can get basic things working with TWS first before making about with the automated tools.

My subscriptions:

Screen Shot 2021-02-09 at 10 59 22 AM
sirugh commented 3 years ago

I checked and my config is set to primaryoverride, but maybe it can't override if I'm authed to the web portal. It does show a popup if I'm on TWS and run the docker container, so that's good. At this point though, I feel like I might just be hitting some issue with IBKR/paper permissions. Maybe it's because I haven't actually funded my live account? I might need to call them.

Anyways, after mirroring your settings as closely as possible, I'm still getting the "Requested market data is not subscribed" error. Is there something I can try in the TWS app to replicate the contract: Stock(symbol='SPY', exchange='SMART', currency='USD') command?

FWIW I am trying this on paper first. Based on this article and my current settings, I should be getting data as I'm in that fifth row of case 1, yet when I log in to paper-trading on TWS and hover over "SPY" I see this:

Image from Gyazo

These are my current subscriptions:

Image from Gyazo

And these are my trading permissions:

Image from Gyazo

brndnmtthws commented 3 years ago

Hmm, I can only assume IB isn't giving you data because it's not actually funded yet. Maybe they want to see a balance that can cover the data fees before they enable it.

You could try setting market_data_type = 4, which will use frozen data, and that's usually sufficient for testing a paper account.

Ianfinn1994 commented 3 years ago

@sirugh , I had to make a demo account. They said i didnt have the ability to access data because my account doesnt pay fees or something like that. Also @brndnmtthws, im trying to run the tool on chromebook virtual debian machine, it was working a few days ago, and now im getting this error.

2021-02-15 02:12:02,176 ib_insync.client INFO Connecting to 127.0.0.1:4002 with clientId 1... 2021-02-15 02:12:02,179 ib_insync.client INFO Disconnecting 2021-02-15 02:12:02,179 ib_insync.client ERROR API connection failed: ConnectionRefusedError(111, "Connect call failed ('127.0.0.1', 4002)") 2021-02-15 02:12:02,180 ib_insync.client ERROR Make sure API port on TWS/IBG is open 2021-02-15 02:12:02,180 ib_insync.IBC INFO Terminating 2021-02-15 02:12:05,450 ib_insync.IBC INFO Starting

brndnmtthws commented 3 years ago

@Ianfinn1994 there isn't enough context from that log message for me to help. You're going to have to do some debugging. Please open a new issue in the future.

sirugh commented 3 years ago

They said i didnt have the ability to access data because my account doesnt pay fees or something like that.

Yea this is my assertion as well. I am in the process of funding the account and will update this when I see if that makes a difference.

sirugh commented 3 years ago

To follow up here, I got past the initial errors by funding the account first which then allowed me to subscribe to market data. Might be worth dropping a line in a FAQ related to this? If you have one, I'd be happy to open a PR.

brndnmtthws commented 3 years ago

To follow up here, I got past the initial errors by funding the account first which then allowed me to subscribe to market data. Might be worth dropping a line in a FAQ related to this? If you have one, I'd be happy to open a PR.

That'd be great