Start free

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.

Where things are in the IB portal
Performance & Reports → Flex Queries
or Menu → Reporting → Flex Queries
  • Activity Flex Query, “+”
    Sections + Delivery Configuration
    8 sections, Models Optional, XML, Last 365 Calendar Days
    • Continue → Create
      Query ID
      the report number
  • Flex Web Service Configuration
    Flex Web Service Status → Save
    Should Expire After; Valid For IP Address — empty
    • Generate New Token
      Token
      the 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.

  1. Open Flex Queries. Log in to the Client Portal and open Performance & Reports → Flex Queries (or Menu → Reporting → Flex Queries).
  2. Create an Activity Flex Query. In the Activity Flex Query panel click “+” and give the query any name.
  3. 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.
  4. 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.
  5. Set the Delivery Configuration. Models — Optional, Format — XML, Period — Last 365 Calendar Days. Leave the General Configuration as it is.
  6. Create it and copy the Query ID. Click Continue, review the settings and click Create. Copy the Query ID of the new query.
  7. Enable the Flex Web Service. On the same page open Flex Web Service Configuration, tick Flex Web Service Status and click Save.
  8. 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.
  9. 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.

The eight sections of the Activity Flex Query
  1. 1Cash Transactions
    PnlTrack takes:
    dividends, withholding tax, interest, broker fees, deposits and withdrawals
    without it:
    no dividends and no tax on them
  2. 2Corporate Actions
    PnlTrack takes:
    splits, spin-offs, mergers
    without it:
    splits and mergers are not applied
  3. 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
  4. 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
  5. 5Option Exercises, Assignments and Expirations
    PnlTrack takes:
    option exercises, assignments and expirations
    without it:
    exercises and expirations are not visible
  6. 6Realized and Unrealized Performance Summary in Base
    PnlTrack takes:
    the broker’s flag for a cost adjustment
    without it:
    cost adjustments go unseen
  7. 7Trades
    PnlTrack takes:
    buys and sells, prices, commissions
    without it:
    nothing is imported
  8. 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.

Models in the Delivery Configuration
Models
  • Optional
    P/L and cost fields filled in
    positions with cost basis, per-security summary
  • All
    P/L fields switched off
    Open 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.

  1. IBKR Guides: Create an Activity Flex Query (Client Portal) — menu path, sections, Delivery Configuration: Models, Format, Period
  2. IBKR Guides: Configure Flex Web Service (Client Portal) — enabling the service, token expiry and IP, a new token invalidates the old
  3. IBKR Reporting Reference: Activity Flex Query Reference — what each section holds
  4. IBKR Reporting Reference: Open Positions — Summary and Lot levels, cost basis
  5. IBKR Reporting Reference: Trades — levels of detail for trades
  6. 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.