piker/piker/brokers/ib
Tyler Goodlet 87385a4e2d ib: never relay "Warning:" errors to EMS..
You'd think they could be bothered to make either a "log" or "warning"
msg type instead of a `type='error'`.. but alas, this attempts to detect
all such "warning"-errors and never proxy them to the clearing engine
thus avoiding the cancellation of any associated (by `reqid`)
pre-existing orders (control dialogs).

Also update all surrounding log messages to a more multiline style.
2026-01-02 16:59:09 -05:00
..
README.rst Handle read and write of `pps.toml` using `MktPair` 2023-05-09 14:49:25 -04:00
__init__.py ib: more fixes to try and get positioning correct.. 2023-07-19 16:46:36 -04:00
_flex_reports.py Pass sync code flag in flex report processor 2023-08-01 09:12:52 -04:00
_util.py ib: jig `.data_reset_hack()` with vnc-client failover 2026-01-02 16:59:09 -05:00
api.py ib: add connect status info emit 2025-02-11 14:56:17 -05:00
broker.py ib: never relay "Warning:" errors to EMS.. 2026-01-02 16:59:09 -05:00
feed.py `.brokers.ib.feed`: better `tractor.to_asyncio` typing and var naming throughout! 2026-01-02 16:59:09 -05:00
ledger.py ib: more trade record edge case handling 2025-02-11 14:56:17 -05:00
symbols.py ib: `.api` mod and log-fmt cleaning 2025-02-11 14:56:17 -05:00

README.rst

ib backend

more or less the "everything broker" for traditional and international markets. they are the "go to" provider for automatic retail trading and we interface to their APIs using the ib_insync project.

status

current support is production grade and both real-time data and order management should be correct and fast. this backend is used by core devs for live trading.

currently there is not yet full support for: - options charting and trading - paxos based crypto rt feeds and trading

config

In order to get order mode support your brokers.toml needs to have something like the following:

[ib]
hosts = [
 "127.0.0.1",
]
# TODO: when we eventually spawn gateways in our
# container, we can just dynamically allocate these
# using IBC.
ports = [
    4002,
    4003,
    4006,
    4001,
    7497,
]

# XXX: for a paper account the flex web query service
# is not supported so you have to manually download
# and XML report and put it in a location that can be
# accessed by the ``brokerd.ib`` backend code for parsing.
flex_token = '1111111111111111'
flex_trades_query_id = '6969696'  # live accounts only?

# 3rd party web-api token
# (XXX: not sure if this works yet)
trade_log_token = '111111111111111'

# when clients are being scanned this determines
# which clients are preferred to be used for data feeds
# based on account names which are detected as active
# on each client.
prefer_data_account = [
    # this has to be first in order to make data work with dual paper + live
    'main',
    'algopaper',
]

[ib.accounts]
main = 'U69696969'
algopaper = 'DU9696969'

If everything works correctly you should see any current positions loaded in the pps pane on chart load and you should also be able to check your trade records in the file:

<pikerk_conf_dir>/ledgers/trades_ib_algopaper.toml

An example ledger file will have entries written verbatim from the trade events schema:

["0000e1a7.630f5e5a.01.01"]
secType = "FUT"
conId = 515416577
symbol = "MNQ"
lastTradeDateOrContractMonth = "20221216"
strike = 0.0
right = ""
multiplier = "2"
exchange = "GLOBEX"
primaryExchange = ""
currency = "USD"
localSymbol = "MNQZ2"
tradingClass = "MNQ"
includeExpired = false
secIdType = ""
secId = ""
comboLegsDescrip = ""
comboLegs = []
execId = "0000e1a7.630f5e5a.01.01"
time = 1661972086.0
acctNumber = "DU69696969"
side = "BOT"
shares = 1.0
price = 12372.75
permId = 441472655
clientId = 6116
orderId = 985
liquidation = 0
cumQty = 1.0
avgPrice = 12372.75
orderRef = ""
evRule = ""
evMultiplier = 0.0
modelCode = ""
lastLiquidity = 1
broker_time = 1661972086.0
name = "ib"
commission = 0.57
realizedPNL = 243.41
yield_ = 0.0
yieldRedemptionDate = 0
listingExchange = "GLOBEX"
date = "2022-08-31T18:54:46+00:00"

your pps.toml file will have position entries like,

[ib.algopaper."mnq.globex.20221216"]
size = -1.0
ppu = 12423.630576923071
bs_mktid = 515416577
expiry = "2022-12-16T00:00:00+00:00"
clears = [
 { dt = "2022-08-31T18:54:46+00:00", ppu = 12423.630576923071, accum_size = -19.0, price = 12372.75, size = 1.0, cost = 0.57, tid = "0000e1a7.630f5e5a.01.01" },
]