When collecting IBKR historical data, you might see warnings that you don't have sufficient market data permissions with IBKR:
quantrocket.history: WARNING [demo-stocks-1d] IBKR reports no market data subscription for BA. STK (conid 908964), please enable subscription in IBKR Client Portal (error code 162: Historical Market Data Service error message:No market data permissions for BATEUK, CHIXUK, TRQXUK STK)Below are several steps to help resolve this permission error.
First, make sure you've subscribed to the relevant market data in IBKR Client Portal.
If you're using a paper account, make sure you've shared the market data with your paper account.
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 IBKR returns is consolidated from multiple exchanges, IBKR won't return the data unless you have all the needed subscriptions. There are several techniques to pinpoint which market data subscriptions are needed.
You can use IBKR's Market Data Assistant Tool to pinpoint the missing subscriptions. Log in to Client Portal and click Support > Market Data Assistant:

Enter the security parameters, and the search results will display a list of related market data subscriptions:

Not all 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 IBKR.
Another technique is to open the symbol in a chart in Trader Workstation (TWS), then click the Market Data Subscription Manager link. This opens a page in Client Portal that displays 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.

The ValidExchanges field in the master file lists all exchanges where a stock trades. Use the following code to see all exchanges in a 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'}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 IBKR data from the primary exchange only. This should make the additional subscriptions unnecessary.
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.
Occasionally, IBKR's licensing terms from the exchange do not permit IBKR to provide market data via API. This can affect entire exchanges, such as mainland China stocks (SEHKNTL and SEHKSZSE), which are unavailable via API. It can also affect particular symbols, such as certain Austrian stocks (VSE) and Japanese stocks. If you get a permission error despite subscribing to the appropriate market data, check the IB Gateway GUI for a message like the following:
