Update entry count on position msgs, draft a composite position info type
parent
ff6ac6ba4f
commit
45d6682ae0
|
@ -113,28 +113,36 @@ class OrderMode:
|
||||||
|
|
||||||
def on_position_update(
|
def on_position_update(
|
||||||
self,
|
self,
|
||||||
msg: dict,
|
|
||||||
|
size: float,
|
||||||
|
price: float,
|
||||||
|
|
||||||
) -> None:
|
) -> None:
|
||||||
print(f'Position update {msg}')
|
|
||||||
|
|
||||||
sym = self.chart._lc._symbol
|
line = self._position_line
|
||||||
if msg['symbol'].lower() not in sym.key:
|
|
||||||
return
|
|
||||||
|
|
||||||
size = msg['size']
|
if line is None and size:
|
||||||
|
|
||||||
self._position.update(msg)
|
# create and show a pp line
|
||||||
if self._position_line:
|
|
||||||
self._position_line.delete()
|
|
||||||
|
|
||||||
if size != 0.0:
|
|
||||||
line = self._position_line = position_line(
|
line = self._position_line = position_line(
|
||||||
self.chart,
|
self.chart,
|
||||||
level=msg['avg_price'],
|
level=price,
|
||||||
size=size,
|
size=size,
|
||||||
)
|
)
|
||||||
line.show()
|
line.show()
|
||||||
|
|
||||||
|
elif line:
|
||||||
|
|
||||||
|
if size != 0.0:
|
||||||
|
line.set_level(price)
|
||||||
|
line.update_labels({'size': size})
|
||||||
|
line.show()
|
||||||
|
|
||||||
|
else:
|
||||||
|
# remove pp line from view
|
||||||
|
line.delete()
|
||||||
|
self._position_line = None
|
||||||
|
|
||||||
def uuid(self) -> str:
|
def uuid(self) -> str:
|
||||||
return str(uuid.uuid4())
|
return str(uuid.uuid4())
|
||||||
|
|
||||||
|
@ -386,6 +394,35 @@ class OrderMode:
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class PositionInfo:
|
||||||
|
|
||||||
|
line: LevelLine
|
||||||
|
pp_label: Label
|
||||||
|
size_label: Label
|
||||||
|
info_label: Label
|
||||||
|
info: dict
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self,
|
||||||
|
avg_price,
|
||||||
|
size,
|
||||||
|
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
self.info['avg_price'] = avg_price
|
||||||
|
self.size_label.fields['size'] = size
|
||||||
|
self.info_label.fields['size'] = size
|
||||||
|
|
||||||
|
|
||||||
|
def position_info(
|
||||||
|
|
||||||
|
price: float,
|
||||||
|
size: float
|
||||||
|
|
||||||
|
) -> PositionInfo:
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
async def run_order_mode(
|
async def run_order_mode(
|
||||||
|
|
||||||
chart: 'ChartPlotWidget', # noqa
|
chart: 'ChartPlotWidget', # noqa
|
||||||
|
@ -434,30 +471,38 @@ async def run_order_mode(
|
||||||
# update_on_range_change=False,
|
# update_on_range_change=False,
|
||||||
|
|
||||||
fmt_str='\n'.join((
|
fmt_str='\n'.join((
|
||||||
'size: {entry_count}',
|
'{entry_size} @ {percent_pnl}% PnL',
|
||||||
'% port: {percent_of_port}',
|
'{percent_of_port}% of port = ${base_unit_value}',
|
||||||
# '$val: {base_unit_value}',
|
|
||||||
)),
|
)),
|
||||||
|
|
||||||
fields={
|
fields={
|
||||||
'entry_count': 0,
|
'entry_size': 0,
|
||||||
'percent_of_port': 0,
|
'percent_pnl': 0,
|
||||||
'base_unit_value': 0,
|
'percent_of_port': 2,
|
||||||
|
'base_unit_value': '1k',
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
pp_label.render()
|
pp_label.render()
|
||||||
|
|
||||||
from PyQt5.QtCore import QPointF
|
from PyQt5.QtCore import QPointF
|
||||||
|
from . import _lines
|
||||||
|
|
||||||
# order line endpoint anchor
|
# order line endpoint anchor
|
||||||
def bottom_marker_right() -> QPointF:
|
def align_to_pp_label() -> QPointF:
|
||||||
|
# pp = _lines._pp_label.txt
|
||||||
|
# scene_rect = pp.mapToScene(pp.boundingRect()).boundingRect()
|
||||||
|
# br = scene_rect.bottomRight()
|
||||||
|
|
||||||
return QPointF(
|
return QPointF(
|
||||||
marker_right_points(chart)[0] - pp_label.w,
|
|
||||||
|
marker_right_points(chart)[2] - pp_label.w ,
|
||||||
view.height() - pp_label.h,
|
view.height() - pp_label.h,
|
||||||
|
# br.x() - pp_label.w,
|
||||||
|
# br.y(),
|
||||||
)
|
)
|
||||||
|
|
||||||
# TODO: position on botto if l1/book is on top side
|
# TODO: position on botto if l1/book is on top side
|
||||||
pp_label.scene_anchor = bottom_marker_right
|
pp_label.scene_anchor = align_to_pp_label
|
||||||
pp_label.hide()
|
pp_label.hide()
|
||||||
|
|
||||||
mode = OrderMode(
|
mode = OrderMode(
|
||||||
|
@ -483,9 +528,18 @@ async def run_order_mode(
|
||||||
else: # to be safe
|
else: # to be safe
|
||||||
mode._size = 1.0
|
mode._size = 1.0
|
||||||
|
|
||||||
# update any exising positions
|
# update any exising position
|
||||||
for sym, msg in positions.items():
|
for sym, msg in positions.items():
|
||||||
mode.on_position_update(msg)
|
|
||||||
|
our_sym = mode.chart._lc._symbol.key
|
||||||
|
if sym.lower() in our_sym:
|
||||||
|
|
||||||
|
mode._position.update(msg)
|
||||||
|
size = msg['size']
|
||||||
|
price = msg['avg_price']
|
||||||
|
mode.on_position_update(size, price)
|
||||||
|
pp_label.fields['entry_size'] = size
|
||||||
|
pp_label.render()
|
||||||
|
|
||||||
def get_index(time: float):
|
def get_index(time: float):
|
||||||
|
|
||||||
|
@ -522,7 +576,19 @@ async def run_order_mode(
|
||||||
'position',
|
'position',
|
||||||
):
|
):
|
||||||
# show line label once order is live
|
# show line label once order is live
|
||||||
mode.on_position_update(msg)
|
|
||||||
|
sym = mode.chart._lc._symbol
|
||||||
|
if msg['symbol'].lower() in sym.key:
|
||||||
|
|
||||||
|
mode._position.update(msg)
|
||||||
|
size = msg['size']
|
||||||
|
price = msg['avg_price']
|
||||||
|
mode.on_position_update(size, price)
|
||||||
|
pp_label.fields['entry_size'] = size
|
||||||
|
pp_label.render()
|
||||||
|
|
||||||
|
# short circuit to next msg to avoid
|
||||||
|
# uncessary msg content lookups
|
||||||
continue
|
continue
|
||||||
|
|
||||||
resp = msg['resp']
|
resp = msg['resp']
|
||||||
|
|
Loading…
Reference in New Issue