When collecting historical data you might see warnings that you don't have sufficient market data permissions with IB:
quantrocket.history: WARNING [demo-stocks-1d] IB reports no market data subscription for BA. STK (conid 908964), please enable subscription in IB Account Management (error code 162: Historical Market Data Service error message:No market data permissions for BATEUK, CHIXUK, TRQXUK STK)
Below are several steps to help you resolve this permission error.
Did you subscribe to the market data in Account Management?
First, the basics: make sure you've subscribed to the relevant market data in IB account management.
Did you share the market data with your paper account?
If you're using a paper account, make sure you've shared the market data with your paper account.
Does the stock trade on multiple exchanges?
For stocks, a common issue is having a market data subscription for the primary exchange but not for the ECNs where the stock also trades. Since the historical data IB returns is consolidated from multiple exchanges, IB won't return the data unless you have all of the needed subscriptions. There are several techniques to help pinpoint which market data subscriptions are needed.
Market Data Assistant Tool
You can use IB's Market Data Assistant Tool to pinpoint the missing subscriptions. Log in to Account Management and click on Support > Market Data Assistant:
Enter the security parameters, and the search results will display a list of related market data subscriptions:
Not all of the displayed subscriptions are necessarily required. For example, some subscriptions may be for level 2 quotes, which usually aren't required for top-of-book data (unless there is no separate level 1-only subscription). If you're not sure which subscriptions are necessary, open a support ticket with IB.
Market Data Subscription Manager on TWS chart
Another technique to determine market data permissions is to open the symbol in a chart in Trader Workstation (TWS) then click the Market Data Subscription Manager link. This will take you to a page in Account Management which will display the required market data subscription. The permissions required to retrieve historical market data via the API are the same as those required to view a chart in Trader Workstation.
ValidExchanges field in master file
Another helpful technique is to look at the ValidExchanges
field in the master file, which lists all the exchanges where the stock trades. You can use the following code to see all exchanges in a given master file:
import pandas as pd
import itertools
from quantrocket.master import download_master_file
download_master_file(exchanges=["EBS", "VIRTX"], sec_types="STK", filepath_or_buffer="swiss_stk.csv")
securities = pd.read_csv("swiss_stk.csv")
all_exchanges = set(itertools.chain(*securities.ValidExchanges.str.split(",").tolist()))
print(all_exchanges)
{'BATECH', 'CHIXCH', 'EBS', 'SMART', 'TRQXCH', 'VIRTX'}
Primary exchange filter
If you're subscribed to the primary exchange and don't want to subscribe to the ECNs, you can use a primary exchange filter to tell QuantRocket to request IB data from the primary exchange only, which should render the additional subscriptions unnecessary.
Level 1 vs Level 2
Usually, level 2 (deep book) data is not required to collect top-of-book historical data. However, for some exchanges, there is no separate level 1 subscription, in which case a level 2 subscription may be required.
Data unavailable via API
Occasionally IB's licensing terms from the exchange do not permit them to provide market data via API. This can affect entire exchanges: for example, mainland China stocks (SEHKNTL and SEHKSZSE) are unavailable via API. Or it can affect particular symbols only, for example certain Austrian stocks (VSE) and certain Japanese stocks. If you get a permission error even though you subscribed to the appropriate market data, check the IB Gateway GUI for a message like the following: