ib: more trade record edge case handling
- timestamps came as `'date'`-keyed from 2022 and before but now are `'datetime'`.. - some symbols seem to have no commission field, so handle that.. - when no `'price'` field found return `None` from `norm_trade()`. - add a warn log on mid-fill commission updates.go_httpx
parent
ab1463d942
commit
97dd7e766a
|
@ -1183,7 +1183,14 @@ async def deliver_trade_events(
|
||||||
pos
|
pos
|
||||||
and fill
|
and fill
|
||||||
):
|
):
|
||||||
assert fill.commissionReport == cr
|
now_cr: CommissionReport = fill.commissionReport
|
||||||
|
if (now_cr != cr):
|
||||||
|
log.warning(
|
||||||
|
'UhhHh ib updated the commission report mid-fill..?\n'
|
||||||
|
f'was: {pformat(cr)}\n'
|
||||||
|
f'now: {pformat(now_cr)}\n'
|
||||||
|
)
|
||||||
|
|
||||||
await emit_pp_update(
|
await emit_pp_update(
|
||||||
ems_stream,
|
ems_stream,
|
||||||
accounts_def,
|
accounts_def,
|
||||||
|
|
|
@ -31,7 +31,11 @@ from typing import (
|
||||||
)
|
)
|
||||||
|
|
||||||
from bidict import bidict
|
from bidict import bidict
|
||||||
import pendulum
|
from pendulum import (
|
||||||
|
DateTime,
|
||||||
|
parse,
|
||||||
|
from_timestamp,
|
||||||
|
)
|
||||||
from ib_insync import (
|
from ib_insync import (
|
||||||
Contract,
|
Contract,
|
||||||
Commodity,
|
Commodity,
|
||||||
|
@ -66,10 +70,11 @@ tx_sort: Callable = partial(
|
||||||
iter_by_dt,
|
iter_by_dt,
|
||||||
parsers={
|
parsers={
|
||||||
'dateTime': parse_flex_dt,
|
'dateTime': parse_flex_dt,
|
||||||
'datetime': pendulum.parse,
|
'datetime': parse,
|
||||||
# for some some fucking 2022 and
|
|
||||||
# back options records...fuck me.
|
# XXX: for some some fucking 2022 and
|
||||||
'date': pendulum.parse,
|
# back options records.. f@#$ me..
|
||||||
|
'date': parse,
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -89,15 +94,38 @@ def norm_trade(
|
||||||
|
|
||||||
conid: int = str(record.get('conId') or record['conid'])
|
conid: int = str(record.get('conId') or record['conid'])
|
||||||
bs_mktid: str = str(conid)
|
bs_mktid: str = str(conid)
|
||||||
comms = record.get('commission')
|
|
||||||
if comms is None:
|
|
||||||
comms = -1*record['ibCommission']
|
|
||||||
|
|
||||||
price = record.get('price') or record['tradePrice']
|
# NOTE: sometimes weird records (like BTTX?)
|
||||||
|
# have no field for this?
|
||||||
|
comms: float = -1 * (
|
||||||
|
record.get('commission')
|
||||||
|
or record.get('ibCommission')
|
||||||
|
or 0
|
||||||
|
)
|
||||||
|
if not comms:
|
||||||
|
log.warning(
|
||||||
|
'No commissions found for record?\n'
|
||||||
|
f'{pformat(record)}\n'
|
||||||
|
)
|
||||||
|
|
||||||
|
price: float = (
|
||||||
|
record.get('price')
|
||||||
|
or record.get('tradePrice')
|
||||||
|
)
|
||||||
|
if price is None:
|
||||||
|
log.warning(
|
||||||
|
'No `price` field found in record?\n'
|
||||||
|
'Skipping normalization..\n'
|
||||||
|
f'{pformat(record)}\n'
|
||||||
|
)
|
||||||
|
return None
|
||||||
|
|
||||||
# the api doesn't do the -/+ on the quantity for you but flex
|
# the api doesn't do the -/+ on the quantity for you but flex
|
||||||
# records do.. are you fucking serious ib...!?
|
# records do.. are you fucking serious ib...!?
|
||||||
size = record.get('quantity') or record['shares'] * {
|
size: float|int = (
|
||||||
|
record.get('quantity')
|
||||||
|
or record['shares']
|
||||||
|
) * {
|
||||||
'BOT': 1,
|
'BOT': 1,
|
||||||
'SLD': -1,
|
'SLD': -1,
|
||||||
}[record['side']]
|
}[record['side']]
|
||||||
|
@ -128,26 +156,31 @@ def norm_trade(
|
||||||
# otype = tail[6]
|
# otype = tail[6]
|
||||||
# strike = tail[7:]
|
# strike = tail[7:]
|
||||||
|
|
||||||
print(f'skipping opts contract {symbol}')
|
log.warning(
|
||||||
|
f'Skipping option contract -> NO SUPPORT YET!\n'
|
||||||
|
f'{symbol}\n'
|
||||||
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
# timestamping is way different in API records
|
# timestamping is way different in API records
|
||||||
dtstr = record.get('datetime')
|
dtstr: str = record.get('datetime')
|
||||||
date = record.get('date')
|
date: str = record.get('date')
|
||||||
flex_dtstr = record.get('dateTime')
|
flex_dtstr: str = record.get('dateTime')
|
||||||
|
|
||||||
if dtstr or date:
|
if dtstr or date:
|
||||||
dt = pendulum.parse(dtstr or date)
|
dt: DateTime = parse(dtstr or date)
|
||||||
|
|
||||||
elif flex_dtstr:
|
elif flex_dtstr:
|
||||||
# probably a flex record with a wonky non-std timestamp..
|
# probably a flex record with a wonky non-std timestamp..
|
||||||
dt = parse_flex_dt(record['dateTime'])
|
dt: DateTime = parse_flex_dt(record['dateTime'])
|
||||||
|
|
||||||
# special handling of symbol extraction from
|
# special handling of symbol extraction from
|
||||||
# flex records using some ad-hoc schema parsing.
|
# flex records using some ad-hoc schema parsing.
|
||||||
asset_type: str = record.get(
|
asset_type: str = (
|
||||||
'assetCategory'
|
record.get('assetCategory')
|
||||||
) or record.get('secType', 'STK')
|
or record.get('secType')
|
||||||
|
or 'STK'
|
||||||
|
)
|
||||||
|
|
||||||
if (expiry := (
|
if (expiry := (
|
||||||
record.get('lastTradeDateOrContractMonth')
|
record.get('lastTradeDateOrContractMonth')
|
||||||
|
@ -357,6 +390,7 @@ def norm_trade_records(
|
||||||
if txn is None:
|
if txn is None:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
|
# inject txns sorted by datetime
|
||||||
insort(
|
insort(
|
||||||
records,
|
records,
|
||||||
txn,
|
txn,
|
||||||
|
@ -405,7 +439,7 @@ def api_trades_to_ledger_entries(
|
||||||
txn_dict[attr_name] = val
|
txn_dict[attr_name] = val
|
||||||
|
|
||||||
tid = str(txn_dict['execId'])
|
tid = str(txn_dict['execId'])
|
||||||
dt = pendulum.from_timestamp(txn_dict['time'])
|
dt = from_timestamp(txn_dict['time'])
|
||||||
txn_dict['datetime'] = str(dt)
|
txn_dict['datetime'] = str(dt)
|
||||||
acctid = accounts[txn_dict['acctNumber']]
|
acctid = accounts[txn_dict['acctNumber']]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue