A command-line tool for requesting, downloading, and processing Interactive Brokers (IB) Flex queries in XML format. This tool helps filter and display transaction data within a specified date range.
This was written for simplifying the xml import to Portfolio Performance, but can be used for any FlexQuery.
- Sends requests to IB to generate Flex queries based on your credentials.
- Downloads the generated XML report and saves it locally.
- Filters transactions (trades and cash transactions) by date range.
- Displays the filtered transactions in a formatted table on the console.
- Python 3.7+
- Access to an Interactive Brokers FlexQuery token
-
Clone the repository:
git clone https://github.com/cubinet-code/flexquery cd flexquery
-
Install the package:
pip install .
After installing, you can use the flexquery
command directly from the command line.
You can get help by running flexquery --help
.
> flexquery --help
Usage: flexquery [OPTIONS] REPORT_NUMBER
Options:
-s, --start-date [%Y-%m-%d] Start Date [env var: FLEXQUERY_START_DATE;
default: 2024-10-31; required]
-e, --end-date [%Y-%m-%d] End Date [env var: FLEXQUERY_END_DATE;
default: 2024-10-31; required]
-t, --token TEXT IB FlexQuery token for authentication [env
var: FLEXQUERY_TOKEN; required]
--help Show this message and exit.
Example:
flexquery 123456 --start-date 2024-01-01 --end-date 2024-12-31 --token YOUR_IB_FLEX_TOKEN
You can also set the token using the FLEXQUERY_TOKEN
environment variable instead of passing it as a command-line argument:
Also the other parameters can be set via environment variables (see --help).
export FLEXQUERY_TOKEN="YOUR_IB_FLEX_TOKEN"
flexquery 123456 --start-date 2024-01-01 --end-date 2024-12-31
- Request Generation: Sends a request to IB to generate the FlexQuery report.
- Download: Downloads the generated XML report when it becomes available.
- Filtering: Filters transactions based on the specified date range.
- Display: Prints the filtered transactions in a tabular format.
- make_xml_request: Sends a request to IB to generate the FlexQuery report.
- get_xml: Waits and retrieves the report once it’s ready.
- download_xml: Downloads and saves the XML report to the
history
directory. - filter_transactions_by_date_range: Filters transactions within a specified date range.
- print_transactions: Displays the filtered transactions in a table format using
tabulate
. - write_xml: Saves the XML data to a file.
lxml
: For XML parsing and processing.click
: For command-line argument parsing.urllib3
: For HTTP requests.tabulate
: For table formatting in the console.loguru
: For enhanced logging.
Install dependencies using requirements.txt
:
pip install -r requirements.txt
This project is licensed under the MIT License.
Feel free to contribute by opening issues or submitting pull requests.