Test IB fill exchange lookup during ledger conversion
Exercise the exchange fallback from dd32746c through
`update_ledger_from_api_trades()`. An exchange-only fill formerly
requested its contract again. Cover a populated primary exchange
and the two-empty-fields lookup as controls.
Check the stored execution venue and normalized transaction ID,
price and size with a synthetic fill and an offline broker lookup.
(this patch was generated in some part by `Codex` using
`GPT-6` (`OpenAI`))
wkt/land_ib_ledger_exchange
parent
dd32746caf
commit
a031a9fa8b
|
|
@ -4,6 +4,7 @@ Execution identity across IB fill and commission callbacks.
|
||||||
'''
|
'''
|
||||||
import asyncio
|
import asyncio
|
||||||
from types import SimpleNamespace
|
from types import SimpleNamespace
|
||||||
|
from unittest.mock import AsyncMock
|
||||||
|
|
||||||
from bidict import bidict
|
from bidict import bidict
|
||||||
from eventkit import Event
|
from eventkit import Event
|
||||||
|
|
@ -22,7 +23,10 @@ import trio
|
||||||
|
|
||||||
from piker.brokers.ib import broker
|
from piker.brokers.ib import broker
|
||||||
from piker.brokers.ib.api import NonShittyIB
|
from piker.brokers.ib.api import NonShittyIB
|
||||||
from piker.brokers.ib.ledger import api_trades_to_ledger_entries
|
from piker.brokers.ib.ledger import (
|
||||||
|
api_trades_to_ledger_entries,
|
||||||
|
update_ledger_from_api_trades,
|
||||||
|
)
|
||||||
from piker.clearing import OrderDialogs
|
from piker.clearing import OrderDialogs
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -39,6 +43,60 @@ def make_fill(tid: str) -> Fill:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
('primary', 'exchange', 'lookup'),
|
||||||
|
[
|
||||||
|
('CME', 'GLOBEX', False),
|
||||||
|
('', 'CME', False),
|
||||||
|
('', '', True),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_fill_contract_exchange_lookup(
|
||||||
|
primary: str,
|
||||||
|
exchange: str,
|
||||||
|
lookup: bool,
|
||||||
|
) -> None:
|
||||||
|
'''
|
||||||
|
Qualify venue lookup in `update_ledger_from_api_trades()`.
|
||||||
|
|
||||||
|
- Either contract exchange field avoids a broker request.
|
||||||
|
- Two empty fields retain the contract lookup.
|
||||||
|
- Ledger conversion preserves the execution and its venue.
|
||||||
|
|
||||||
|
'''
|
||||||
|
fill = make_fill('fill-1')
|
||||||
|
fill.contract.primaryExchange = primary
|
||||||
|
fill.contract.exchange = exchange
|
||||||
|
fill.contract.currency = 'USD'
|
||||||
|
fill.execution.exchange = 'GLOBEX'
|
||||||
|
fill.execution.side = 'BOT'
|
||||||
|
fill.execution.price = 100
|
||||||
|
fill.execution.shares = 1
|
||||||
|
client = AsyncMock()
|
||||||
|
client.get_con.return_value = []
|
||||||
|
|
||||||
|
transactions, entries = trio.run(
|
||||||
|
update_ledger_from_api_trades,
|
||||||
|
[fill],
|
||||||
|
client,
|
||||||
|
bidict({'DU_TEST': 'paper'}),
|
||||||
|
)
|
||||||
|
if lookup:
|
||||||
|
client.get_con.assert_awaited_once_with(
|
||||||
|
conid=fill.contract.conId,
|
||||||
|
)
|
||||||
|
|
||||||
|
else:
|
||||||
|
client.get_con.assert_not_awaited()
|
||||||
|
|
||||||
|
entry = entries['paper']['fill-1']
|
||||||
|
assert entry['exchange'] == 'GLOBEX'
|
||||||
|
assert entry['primaryExchange'] == primary
|
||||||
|
txn = transactions['paper']['fill-1']
|
||||||
|
assert txn.tid == 'fill-1'
|
||||||
|
assert (txn.price, txn.size) == (100, 1)
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('code', [2181, 10349, 202])
|
@pytest.mark.parametrize('code', [2181, 10349, 202])
|
||||||
def test_order_notice_does_not_synthesize_cancellation(
|
def test_order_notice_does_not_synthesize_cancellation(
|
||||||
code: int,
|
code: int,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue