Factor and simplify paper clearing logic

basic_orders
Tyler Goodlet 2021-03-07 16:41:14 -05:00
parent 5deea50963
commit d58a82bd38
1 changed files with 32 additions and 48 deletions

View File

@ -279,63 +279,47 @@ async def simulate_fills(
tick.get('size', client.last_ask[1]), tick.get('size', client.last_ask[1]),
) )
buys = client._buys.get(sym, {}) orders = client._buys.get(sym, {})
book_sequence = reversed(
sorted(orders.keys(), key=itemgetter(1)))
# iterate book prices descending def pred(our_price):
for oid, our_bid in reversed( return tick_price < our_price
sorted(buys.keys(), key=itemgetter(1))
):
if tick_price < our_bid:
# retreive order info elif ttype in ('bid',):
(size, reqid, action) = buys.pop((oid, our_bid))
# clearing price would have filled entirely
await client.fake_fill(
# todo slippage to determine fill price
price=tick_price,
size=size,
action=action,
reqid=reqid,
oid=oid,
)
else:
# prices are interated in sorted order so
# we're done
break
if ttype in ('bid',):
client.last_bid = ( client.last_bid = (
tick_price, tick_price,
tick.get('size', client.last_bid[1]), tick.get('size', client.last_bid[1]),
) )
sells = client._sells.get(sym, {}) orders = client._sells.get(sym, {})
book_sequence = sorted(orders.keys(), key=itemgetter(1))
# iterate book prices ascending def pred(our_price):
for oid, our_ask in sorted( return tick_price > our_price
sells.keys(), key=itemgetter(1)
):
if tick_price > our_ask:
# retreive order info elif ttype in ('trade', 'last'):
(size, reqid, action) = sells.pop((oid, our_ask))
# clearing price would have filled entirely
await client.fake_fill(
price=tick_price,
size=size,
action=action,
reqid=reqid,
oid=oid,
)
else:
# prices are interated in sorted order so
# we're done
break
if ttype in ('trade', 'last'):
# TODO: simulate actual book queues and our orders # TODO: simulate actual book queues and our orders
# place in it, might require full L2 data? # place in it, might require full L2 data?
pass continue
# iterate book prices descending
for oid, our_price in book_sequence:
if pred(our_price):
# retreive order info
(size, reqid, action) = orders.pop((oid, our_price))
# clearing price would have filled entirely
await client.fake_fill(
# todo slippage to determine fill price
price=tick_price,
size=size,
action=action,
reqid=reqid,
oid=oid,
)
else:
# prices are iterated in sorted order so we're done
break