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