Add fill handler to order mode

basic_orders
Tyler Goodlet 2021-01-15 19:40:40 -05:00
parent bdc02010cf
commit 3e959ec260
1 changed files with 35 additions and 14 deletions

View File

@ -21,9 +21,10 @@ import time
from contextlib import asynccontextmanager from contextlib import asynccontextmanager
from dataclasses import dataclass, field from dataclasses import dataclass, field
from pprint import pformat from pprint import pformat
from typing import Optional, Dict, Callable from typing import Optional, Dict, Callable, Any
import uuid import uuid
import trio
import pyqtgraph as pg import pyqtgraph as pg
from pyqtgraph import ViewBox, Point, QtCore, QtGui from pyqtgraph import ViewBox, Point, QtCore, QtGui
from pyqtgraph import functions as fn from pyqtgraph import functions as fn
@ -436,25 +437,42 @@ class OrderMode:
# self.book._confirmed_orders[uuid] = req_msg # self.book._confirmed_orders[uuid] = req_msg
return req_msg return req_msg
async def on_exec( def on_fill(
self, self,
uuid: str, uuid: str,
msg: Dict[str, str], msg: Dict[str, Any],
) -> None: ) -> None:
log.info(f'New fill\n{pformat(msg)}')
line = self.lines._order_lines[uuid]
line = self.lines.remove_line(uuid=uuid) # XXX: not sure why the time is so off here
log.debug(f'deleting line with oid: {uuid}') # looks like we're gonna have to do some fixing..
ohlc = self.chart._shm.array
for fill in msg['fills']: indexes = ohlc['time'] >= msg['broker_time']
if any(indexes):
arrow_index = ohlc['index'][indexes[-1]]
else:
arrow_index = ohlc['index'][-1]
self.arrows.add( self.arrows.add(
uuid, uuid,
msg['index'], arrow_index,
msg['price'], msg['price'],
pointing='up' if msg['action'] == 'buy' else 'down', pointing='up' if msg['action'] == 'buy' else 'down',
color=line.color color=line.color
) )
async def on_exec(
self,
uuid: str,
msg: Dict[str, Any],
) -> None:
# only once all fills have cleared and the execution
# is complet do we remove our "order line"
line = self.lines.remove_line(uuid=uuid)
log.debug(f'deleting {line} with oid: {uuid}')
# DESKTOP NOTIFICATIONS # DESKTOP NOTIFICATIONS
# #
# TODO: this in another task? # TODO: this in another task?
@ -478,7 +496,9 @@ class OrderMode:
if msg is not None: if msg is not None:
self.lines.remove_line(uuid=uuid) self.lines.remove_line(uuid=uuid)
else: else:
log.warning(f'Received cancel for unsubmitted order {pformat(msg)}') log.warning(
f'Received cancel for unsubmitted order {pformat(msg)}'
)
def submit_exec(self) -> None: def submit_exec(self) -> None:
"""Send execution order to EMS. """Send execution order to EMS.
@ -492,6 +512,7 @@ class OrderMode:
# make line graphic # make line graphic
line, y = self.lines.create_line(uid) line, y = self.lines.create_line(uid)
line.oid = uid
# send order cmd to ems # send order cmd to ems
self.book.send( self.book.send(