How do I set up a Flex Query at Interactive Brokers?
You need two things: an Activity Flex Query — a saved report with eight sections in XML — and a Flex Web Service token that lets a program download it. Both live on the same page of the IB portal and take about ten minutes. Below: the menu path, the steps and a map of what each section is for.
Updated
What you end up with
A Flex Query is a report template in the IB portal: which sections, which fields, which period, which format. Each query has a number — the Query ID. The Flex Web Service lets a program fetch that report without logging in; according to IBKR Guides a request needs the current token and the Query ID (Configure Flex Web Service).
Both are set up in one place: Performance & Reports → Flex Queries (or Menu → Reporting → Flex Queries). The diagram shows where to click and what you get.
- Activity Flex Query, “+”Sections + Delivery Configuration8 sections, Models Optional, XML, Last 365 Calendar Days
- Continue → CreateQuery IDthe report number
- Flex Web Service ConfigurationFlex Web Service Status → SaveShould Expire After; Valid For IP Address — empty
- Generate New TokenTokenthe connection key
Item names as in IBKR Guides (Client Portal). The Query ID and the token go into PnlTrack.
Step by step
The steps follow IBKR Guides Create an Activity Flex Query and Configure Flex Web Service; the sections and delivery settings are the ones the PnlTrack import expects.
- Open Flex Queries. Log in to the Client Portal and open Performance & Reports → Flex Queries (or Menu → Reporting → Flex Queries).
- Create an Activity Flex Query. In the Activity Flex Query panel click “+” and give the query any name.
- Tick the eight sections. Under Sections select: Cash Transactions, Corporate Actions, Financial Instrument Information, Open Positions, Option Exercises, Assignments and Expirations, Realized and Unrealized Performance Summary in Base, Trades, Transfers.
- Turn on every field in each section. Each section opens a window with its fields: select them all and click Save. For Open Positions turn on both levels of detail — Summary and Lot.
- Set the Delivery Configuration. Models — Optional, Format — XML, Period — Last 365 Calendar Days. Leave the General Configuration as it is.
- Create it and copy the Query ID. Click Continue, review the settings and click Create. Copy the Query ID of the new query.
- Enable the Flex Web Service. On the same page open Flex Web Service Configuration, tick Flex Web Service Status and click Save.
- Generate a token. In Should Expire After pick the longest period, leave Valid For IP Address empty, click Generate New Token and copy the token.
- Paste them into PnlTrack. In PnlTrack add an Interactive Brokers connection with the token, then the daily report with its Query ID — the import starts right away.
What each section is for
Flex delivers exactly the sections ticked in the query. In our exports a ticked section with no data arrives empty, while an unticked one is missing from the file altogether — and there is nothing to rebuild it from later. So the list is short and exact: everything the import reads, nothing more. An extra section does no harm, it just isn’t used. What each section holds is described in the Activity Flex Query Reference.
- 1Cash Transactions
- PnlTrack takes:
- dividends, withholding tax, interest, broker fees, deposits and withdrawals
- without it:
- no dividends and no tax on them
- 2Corporate Actions
- PnlTrack takes:
- splits, spin-offs, mergers
- without it:
- splits and mergers are not applied
- 3Financial Instrument Information
- PnlTrack takes:
- ISIN, conid, FIGI — identifying the security
- without it:
- the security cannot be identified reliably; a ticker does not pin it down
- 4Open Positions
- PnlTrack takes:
- positions and cost basis as the broker sees them
- without it:
- nothing to reconcile positions against; the return-of-capital correction is not applied
- 5Option Exercises, Assignments and Expirations
- PnlTrack takes:
- option exercises, assignments and expirations
- without it:
- exercises and expirations are not visible
- 6Realized and Unrealized Performance Summary in Base
- PnlTrack takes:
- the broker’s flag for a cost adjustment
- without it:
- cost adjustments go unseen
- 7Trades
- PnlTrack takes:
- buys and sells, prices, commissions
- without it:
- nothing is imported
- 8Transfers
- PnlTrack takes:
- securities moving in and out between accounts and brokers
- without it:
- a sale of transferred shares looks like a sale without a purchase
Exactly what the PnlTrack import reads; the app’s onboarding shows the same list.
Why Models is Optional, not All
Models in the Delivery Configuration takes two values, Optional and All (per IBKR Guides). With All the IB portal warns in a single line that realized P/L sections and fields are disabled when models are included. IBKR Guides does not mention this — you only see it in the portal itself.
We checked what it means in practice with two exports of the same account: with All, Open Positions had a zero cost basis (Cost Basis Price, Cost Basis Money) and the Realized and Unrealized Performance Summary in Base was empty. A zero here means “field switched off”, not “the shares were free”, and PnlTrack does not record it as a fact. With Optional the same fields are filled in and match the CSV statement of that account.
- OptionalP/L and cost fields filled inpositions with cost basis, per-security summary
- AllP/L fields switched offOpen Positions with zero cost, summary empty
Based on two of our exports of the same account, half an hour apart.
The period, and history older than a year
Last 365 Calendar Days is one of the Period options listed in IBKR Guides. With it every daily run brings the last year: new trades appear, and PnlTrack does not duplicate the ones already imported.
History older than a year is not pulled through Flex. In PnlTrack you upload it as Activity Statement CSV files, one per year — the app has its own step-by-step guide for that. A Flex XML file cannot be uploaded by hand; it only comes in through the token.
Same-day trades — optional
The Activity Flex Query is refreshed once a day. If you want trades to show up sooner, add a second report in PnlTrack — a Trade Confirmation Flex Query, created on the same Flex Queries page (Trade Confirmation Flex Query panel, “+”) with a single section and XML format. It uses the same token; only its Query ID is needed. PnlTrack fetches it about every 30 minutes.
If something is off
- The token stopped working. Tokens expire (per IBKR Guides the default is 6 hours, which is why you pick a longer period). Generate a new one and replace the key in PnlTrack. Note that a new token invalidates the old one — if another program uses the same IB login, it needs the new token too.
- An address is set in Valid For IP Address. The token then works only from that address, and PnlTrack’s servers cannot use it. Leave the field empty.
- No positions, transfers or security codes. Most likely a section is not ticked. PnlTrack tells you after the import when Open Positions, Realized and Unrealized Performance Summary in Base, Transfers or Financial Instrument Information is missing — open the query at IB, tick the section and save.
- Positions show a zero cost. Check Models: it must be Optional.
What to do with the imported trades for your tax return is covered in Interactive Brokers and the Finnish tax return.
FAQ
- Is it safe to hand over a Flex Web Service token?
- The token is used together with the Query ID to request Flex reports — that is how IBKR Guides describes it. You do not share your IB password. You can replace the token at any time: per IBKR Guides a new token invalidates the old one.
- How long does a token last?
- Per IBKR Guides you set it in Should Expire After, and the default is 6 hours. For a daily import pick the longest period; when it runs out, generate a new token and replace the key in PnlTrack.
- Why Models = Optional?
- With All the IB portal switches off the realized P/L fields, and in our exports positions arrived with a zero cost basis. With Optional those fields are filled in.
- Can I import more than a year of history?
- The Flex query for PnlTrack is set to Last 365 Calendar Days. Older history is uploaded as Activity Statement CSV files, one per year; overlapping periods are fine — PnlTrack removes duplicates.
Sources
Statements about tax rules and exchange rates in this guide are based on these sources.
- IBKR Guides: Create an Activity Flex Query (Client Portal) — menu path, sections, Delivery Configuration: Models, Format, Period
- IBKR Guides: Configure Flex Web Service (Client Portal) — enabling the service, token expiry and IP, a new token invalidates the old
- IBKR Reporting Reference: Activity Flex Query Reference — what each section holds
- IBKR Reporting Reference: Open Positions — Summary and Lot levels, cost basis
- IBKR Reporting Reference: Trades — levels of detail for trades
- IBKR Reporting Reference: Transfers (ACATS, Internal) — position transfers between brokers
This is general information, not tax or investment advice. Check your own situation against the Tax Administration’s guidance or with a professional.