GitHub - riazarbi/iblncr: Automated Interactive Brokers Portfolio Rebalancing cli

2 min read Original article ↗

iblncr

A command line tool for rebalancing a portfolio of stocks and ETFs via Interactive Brokers.

Basic usage

To use iblncr, install it with pipx:

pipx install git+https://github.com/riazarbi/iblncr.git

The Interactive Brokers API only works with a locally installed, working Trader Workstatation or IB Gateway application. If you don't have one, you can use a headless docker image to run one.

You can either manually start the Docker container:

# in a separate terminal
docker run -it --rm --name broker  -p 4003:4003 ghcr.io/riazarbi/ib-headless:10.30.1t

Or use the built-in launch command:

This will start the IB Gateway Docker container, and prompt you for your account type, username and password. You can use CTRL-C to stop it gracefully.

Once installed, you can run the iblncr in a separate terminal window with:

iblncr rebalance --account <account_number> --model <model_file> --port <port_number>

Argument defaults are as follows:

  • account: None
  • model: None, but there is a sample at iblncr/data/sample_model.yaml
  • port: 4003

If you don't specify an account, the application will list the available accounts given by the API and prompt you to select one.

Model file construction

You can create a basic 'starter model' file with the command

iblncr rebalance --account [ACCOUNT_ID]

The 'starter model' will be populated with your current portfolio holdings, but with np target percentages set. You need to manually set these by opening the file, entering the values, and saving the file.

Adding new tickers

You can add or remove position entries from the file to alter your target portfolio weights. If you cannot figure out the symbol, exchange or currency for a holding you can use this tool to find the correct details.

Development

To run the tests:

poetry install --with dev
poetry run pytest

To run the application:

poetry run iblncr rebalance --account <account_number> --model <model_file> --port <port_number>

AI Code Generation

The code in this repo is based on an earlier R package I wrote called rblncr. I used Cursor to refactor the R code into Python.

The unit tests were written by Cursor.