2021-07-21 23:42:15 +00:00
|
|
|
# piker: trading gear for hackers
|
|
|
|
# Copyright (C) Tyler Goodlet (in stewardship for piker0)
|
|
|
|
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
"""
|
|
|
|
Position info and display
|
|
|
|
|
|
|
|
"""
|
2021-08-04 17:37:51 +00:00
|
|
|
from __future__ import annotations
|
2021-08-23 18:21:26 +00:00
|
|
|
from dataclasses import dataclass
|
2021-07-21 23:42:15 +00:00
|
|
|
from functools import partial
|
2021-09-11 22:41:49 +00:00
|
|
|
from math import floor, copysign
|
2021-08-04 17:37:51 +00:00
|
|
|
from typing import Optional
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-08-15 17:37:28 +00:00
|
|
|
|
2021-09-13 22:40:12 +00:00
|
|
|
# from PyQt5.QtWidgets import QStyle
|
|
|
|
# from PyQt5.QtGui import (
|
|
|
|
# QIcon, QPixmap, QColor
|
|
|
|
# )
|
2021-07-22 15:23:04 +00:00
|
|
|
from pyqtgraph import functions as fn
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-07-22 16:05:53 +00:00
|
|
|
from ._annotate import LevelMarker
|
2021-07-21 23:42:15 +00:00
|
|
|
from ._anchors import (
|
2021-07-23 18:19:22 +00:00
|
|
|
pp_tight_and_right, # wanna keep it straight in the long run
|
2021-07-21 23:42:15 +00:00
|
|
|
gpath_pin,
|
|
|
|
)
|
2021-09-16 15:49:22 +00:00
|
|
|
from ..calc import humanize, pnl, puterize
|
2021-09-07 02:05:42 +00:00
|
|
|
from ..clearing._allocate import Allocator, Position
|
2021-09-11 22:41:49 +00:00
|
|
|
from ..data._normalize import iterticks
|
|
|
|
from ..data.feed import Feed
|
2021-07-21 23:42:15 +00:00
|
|
|
from ._label import Label
|
2021-09-04 18:27:10 +00:00
|
|
|
from ._lines import LevelLine, order_line
|
2021-09-13 22:40:12 +00:00
|
|
|
from ._style import _font
|
2021-08-20 14:30:36 +00:00
|
|
|
from ._forms import FieldsForm, FillStatusBar, QLabel
|
2021-08-18 12:55:42 +00:00
|
|
|
from ..log import get_logger
|
|
|
|
|
|
|
|
log = get_logger(__name__)
|
2021-09-11 22:41:49 +00:00
|
|
|
_pnl_tasks: dict[str, bool] = {}
|
|
|
|
|
|
|
|
|
2021-09-14 22:31:49 +00:00
|
|
|
async def update_pnl_from_feed(
|
2021-09-11 22:41:49 +00:00
|
|
|
|
|
|
|
feed: Feed,
|
|
|
|
order_mode: OrderMode, # noqa
|
2021-10-22 16:58:12 +00:00
|
|
|
tracker: PositionTracker,
|
2021-09-11 22:41:49 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
'''Real-time display the current pp's PnL in the appropriate label.
|
|
|
|
|
|
|
|
``ValueError`` if this task is spawned where there is a net-zero pp.
|
|
|
|
|
|
|
|
'''
|
|
|
|
global _pnl_tasks
|
|
|
|
|
|
|
|
pp = order_mode.current_pp
|
|
|
|
live = pp.live_pp
|
|
|
|
key = live.symbol.key
|
|
|
|
|
2021-09-14 22:31:49 +00:00
|
|
|
log.info(f'Starting pnl display for {pp.alloc.account}')
|
|
|
|
|
2021-09-11 22:41:49 +00:00
|
|
|
if live.size < 0:
|
2022-03-02 14:20:17 +00:00
|
|
|
types = ('ask', 'last', 'last', 'dark_trade')
|
2021-09-11 22:41:49 +00:00
|
|
|
|
|
|
|
elif live.size > 0:
|
2022-03-02 14:20:17 +00:00
|
|
|
types = ('bid', 'last', 'last', 'dark_trade')
|
2021-09-11 22:41:49 +00:00
|
|
|
|
|
|
|
else:
|
2021-10-22 16:58:12 +00:00
|
|
|
log.info(f'No position (yet) for {tracker.alloc.account}@{key}')
|
|
|
|
return
|
2021-09-11 22:41:49 +00:00
|
|
|
|
|
|
|
# real-time update pnl on the status pane
|
|
|
|
try:
|
|
|
|
async with feed.stream.subscribe() as bstream:
|
|
|
|
# last_tick = time.time()
|
|
|
|
async for quotes in bstream:
|
|
|
|
|
|
|
|
# now = time.time()
|
|
|
|
# period = now - last_tick
|
|
|
|
|
|
|
|
for sym, quote in quotes.items():
|
|
|
|
|
|
|
|
for tick in iterticks(quote, types):
|
|
|
|
# print(f'{1/period} Hz')
|
|
|
|
|
|
|
|
size = order_mode.current_pp.live_pp.size
|
|
|
|
if size == 0:
|
|
|
|
# terminate this update task since we're
|
|
|
|
# no longer in a pp
|
|
|
|
order_mode.pane.pnl_label.format(pnl=0)
|
|
|
|
return
|
|
|
|
|
|
|
|
else:
|
|
|
|
# compute and display pnl status
|
|
|
|
order_mode.pane.pnl_label.format(
|
|
|
|
pnl=copysign(1, size) * pnl(
|
|
|
|
# live.avg_price,
|
|
|
|
order_mode.current_pp.live_pp.avg_price,
|
|
|
|
tick['price'],
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
# last_tick = time.time()
|
|
|
|
finally:
|
|
|
|
assert _pnl_tasks[key]
|
|
|
|
assert _pnl_tasks.pop(key)
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
|
2021-08-23 18:21:26 +00:00
|
|
|
@dataclass
|
2021-08-27 18:48:55 +00:00
|
|
|
class SettingsPane:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Composite set of widgets plus an allocator model for configuring
|
2021-08-23 18:21:26 +00:00
|
|
|
order entry sizes and position limits per tradable instrument.
|
2021-08-20 14:30:36 +00:00
|
|
|
|
|
|
|
'''
|
2021-08-23 18:21:26 +00:00
|
|
|
# input fields
|
2021-08-20 14:30:36 +00:00
|
|
|
form: FieldsForm
|
|
|
|
|
2021-08-23 18:21:26 +00:00
|
|
|
# output fill status and labels
|
|
|
|
fill_bar: FillStatusBar
|
|
|
|
|
2021-08-20 14:30:36 +00:00
|
|
|
step_label: QLabel
|
|
|
|
pnl_label: QLabel
|
|
|
|
limit_label: QLabel
|
|
|
|
|
2021-09-10 15:50:24 +00:00
|
|
|
# encompasing high level namespace
|
|
|
|
order_mode: Optional['OrderMode'] = None # typing: ignore # noqa
|
2021-08-25 14:27:58 +00:00
|
|
|
|
2021-09-14 14:36:44 +00:00
|
|
|
def set_accounts(
|
|
|
|
self,
|
|
|
|
names: list[str],
|
|
|
|
sizes: Optional[list[float]] = None,
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
combo = self.form.fields['account']
|
|
|
|
return combo.set_items(names)
|
|
|
|
|
2021-09-11 17:20:44 +00:00
|
|
|
def on_selection_change(
|
|
|
|
self,
|
|
|
|
text: str,
|
|
|
|
key: str,
|
|
|
|
|
|
|
|
) -> None:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Called on any order pane drop down selection change.
|
2021-09-11 17:20:44 +00:00
|
|
|
|
|
|
|
'''
|
2021-10-12 14:33:51 +00:00
|
|
|
log.info(f'selection input {key}:{text}')
|
2021-09-11 17:20:44 +00:00
|
|
|
self.on_ui_settings_change(key, text)
|
|
|
|
|
2021-08-26 12:40:14 +00:00
|
|
|
def on_ui_settings_change(
|
2021-08-25 14:27:58 +00:00
|
|
|
self,
|
|
|
|
|
|
|
|
key: str,
|
|
|
|
value: str,
|
|
|
|
|
|
|
|
) -> bool:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Called on any order pane edit field value change.
|
2021-08-25 14:27:58 +00:00
|
|
|
|
|
|
|
'''
|
2021-09-10 15:50:24 +00:00
|
|
|
mode = self.order_mode
|
|
|
|
|
2021-09-11 17:10:20 +00:00
|
|
|
# an account switch request
|
2021-09-10 15:50:24 +00:00
|
|
|
if key == 'account':
|
|
|
|
|
2021-09-11 17:10:20 +00:00
|
|
|
# hide details on the old selection
|
2021-09-10 15:50:24 +00:00
|
|
|
old_tracker = mode.current_pp
|
|
|
|
old_tracker.hide_info()
|
|
|
|
|
|
|
|
# re-assign the order mode tracker
|
|
|
|
account_name = value
|
2021-09-11 14:41:52 +00:00
|
|
|
tracker = mode.trackers.get(account_name)
|
|
|
|
|
2021-09-11 17:10:20 +00:00
|
|
|
# if selection can't be found (likely never discovered with
|
|
|
|
# a ``brokerd`) then error and switch back to the last
|
|
|
|
# selection.
|
2021-09-11 14:41:52 +00:00
|
|
|
if tracker is None:
|
|
|
|
sym = old_tracker.chart.linked.symbol.key
|
|
|
|
log.error(
|
|
|
|
f'Account `{account_name}` can not be set for {sym}'
|
|
|
|
)
|
|
|
|
self.form.fields['account'].setCurrentText(
|
2021-09-14 17:10:39 +00:00
|
|
|
old_tracker.alloc.account)
|
2021-09-11 14:41:52 +00:00
|
|
|
return
|
|
|
|
|
2021-09-10 15:50:24 +00:00
|
|
|
self.order_mode.current_pp = tracker
|
2021-09-14 17:10:39 +00:00
|
|
|
assert tracker.alloc.account == account_name
|
2021-09-10 15:50:24 +00:00
|
|
|
self.form.fields['account'].setCurrentText(account_name)
|
|
|
|
tracker.show()
|
|
|
|
tracker.hide_info()
|
|
|
|
|
2021-09-11 22:41:49 +00:00
|
|
|
self.display_pnl(tracker)
|
2021-09-10 18:59:42 +00:00
|
|
|
|
2021-09-10 15:50:24 +00:00
|
|
|
# load the new account's allocator
|
|
|
|
alloc = tracker.alloc
|
|
|
|
|
|
|
|
else:
|
|
|
|
tracker = mode.current_pp
|
|
|
|
alloc = tracker.alloc
|
|
|
|
|
2021-08-26 12:40:14 +00:00
|
|
|
size_unit = alloc.size_unit
|
|
|
|
|
2021-09-10 15:50:24 +00:00
|
|
|
# WRITE any settings to current pp's allocator
|
2021-09-16 15:26:11 +00:00
|
|
|
try:
|
2021-10-12 14:33:51 +00:00
|
|
|
if key == 'size_unit':
|
|
|
|
# implicit re-write of value if input
|
|
|
|
# is the "text name" of the units.
|
|
|
|
# yah yah, i know this is badd..
|
2021-09-16 15:26:11 +00:00
|
|
|
alloc.size_unit = value
|
2021-09-16 15:49:22 +00:00
|
|
|
else:
|
2021-10-12 14:33:51 +00:00
|
|
|
value = puterize(value)
|
|
|
|
if key == 'limit':
|
2022-03-02 14:20:17 +00:00
|
|
|
pp = mode.current_pp.live_pp
|
|
|
|
|
2021-10-12 14:33:51 +00:00
|
|
|
if size_unit == 'currency':
|
2022-03-02 14:20:17 +00:00
|
|
|
dsize = pp.dsize
|
|
|
|
if dsize > value:
|
|
|
|
log.error(
|
|
|
|
f'limit must > then current pp: {dsize}'
|
|
|
|
)
|
|
|
|
raise ValueError
|
|
|
|
|
2021-10-12 14:33:51 +00:00
|
|
|
alloc.currency_limit = value
|
2022-03-02 14:20:17 +00:00
|
|
|
|
2021-10-12 14:33:51 +00:00
|
|
|
else:
|
2022-03-02 14:20:17 +00:00
|
|
|
size = pp.size
|
|
|
|
if size > value:
|
|
|
|
log.error(
|
|
|
|
f'limit must > then current pp: {size}'
|
|
|
|
)
|
|
|
|
raise ValueError
|
|
|
|
|
2021-10-12 14:33:51 +00:00
|
|
|
alloc.units_limit = value
|
|
|
|
|
|
|
|
elif key == 'slots':
|
2022-03-02 14:20:17 +00:00
|
|
|
if value <= 0:
|
|
|
|
raise ValueError('slots must be > 0')
|
2021-10-12 14:33:51 +00:00
|
|
|
alloc.slots = int(value)
|
|
|
|
|
|
|
|
else:
|
2022-03-02 14:20:17 +00:00
|
|
|
log.error(f'Unknown setting {key}')
|
|
|
|
raise ValueError
|
2021-08-26 12:40:14 +00:00
|
|
|
|
2021-09-16 15:26:11 +00:00
|
|
|
log.info(f'settings change: {key}: {value}')
|
|
|
|
|
|
|
|
except ValueError:
|
|
|
|
log.error(f'Invalid value for `{key}`: {value}')
|
2021-08-26 12:40:14 +00:00
|
|
|
|
2021-09-23 14:06:47 +00:00
|
|
|
# READ out settings and update the status UI / settings widgets
|
2021-08-26 12:40:14 +00:00
|
|
|
suffix = {'currency': ' $', 'units': ' u'}[size_unit]
|
|
|
|
limit = alloc.limit()
|
|
|
|
|
|
|
|
# TODO: a reverse look up from the position to the equivalent
|
|
|
|
# account(s), if none then look to user config for default?
|
2021-09-10 15:50:24 +00:00
|
|
|
self.update_status_ui(pp=tracker)
|
2021-08-26 12:40:14 +00:00
|
|
|
|
2021-08-26 19:08:29 +00:00
|
|
|
step_size, currency_per_slot = alloc.step_sizes()
|
|
|
|
|
|
|
|
if size_unit == 'currency':
|
|
|
|
step_size = currency_per_slot
|
2021-08-26 12:40:14 +00:00
|
|
|
|
|
|
|
self.step_label.format(
|
|
|
|
step_size=str(humanize(step_size)) + suffix
|
|
|
|
)
|
|
|
|
self.limit_label.format(
|
|
|
|
limit=str(humanize(limit)) + suffix
|
|
|
|
)
|
|
|
|
|
|
|
|
# update size unit in UI
|
|
|
|
self.form.fields['size_unit'].setCurrentText(
|
|
|
|
alloc._size_units[alloc.size_unit]
|
|
|
|
)
|
|
|
|
self.form.fields['slots'].setText(str(alloc.slots))
|
|
|
|
self.form.fields['limit'].setText(str(limit))
|
|
|
|
|
2021-09-23 14:06:47 +00:00
|
|
|
# update of level marker size label based on any new settings
|
|
|
|
tracker.update_from_pp()
|
|
|
|
|
2021-08-25 14:27:58 +00:00
|
|
|
# TODO: maybe return a diff of settings so if we can an error we
|
|
|
|
# can have general input handling code to report it through the
|
|
|
|
# UI in some way?
|
|
|
|
return True
|
2021-08-20 14:30:36 +00:00
|
|
|
|
2021-08-23 18:21:26 +00:00
|
|
|
def update_status_ui(
|
|
|
|
self,
|
2021-09-10 15:50:24 +00:00
|
|
|
|
|
|
|
pp: PositionTracker,
|
2021-08-23 18:21:26 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
2021-09-10 15:50:24 +00:00
|
|
|
alloc = pp.alloc
|
2021-08-23 18:21:26 +00:00
|
|
|
slots = alloc.slots
|
2021-09-10 15:50:24 +00:00
|
|
|
used = alloc.slots_used(pp.live_pp)
|
2021-08-23 18:21:26 +00:00
|
|
|
|
2021-08-25 14:27:58 +00:00
|
|
|
# calculate proportion of position size limit
|
|
|
|
# that exists and display in fill bar
|
|
|
|
# TODO: what should we do for fractional slot pps?
|
2021-08-23 18:21:26 +00:00
|
|
|
self.fill_bar.set_slots(
|
|
|
|
slots,
|
2021-08-28 16:58:08 +00:00
|
|
|
|
|
|
|
# TODO: how to show "partial" slots?
|
2021-08-27 18:48:55 +00:00
|
|
|
# min(round(prop * slots), slots)
|
2021-08-28 18:07:18 +00:00
|
|
|
min(used, slots)
|
2021-08-23 18:21:26 +00:00
|
|
|
)
|
2021-09-14 17:10:39 +00:00
|
|
|
self.update_account_icons({alloc.account: pp.live_pp})
|
2021-09-14 16:26:06 +00:00
|
|
|
|
|
|
|
def update_account_icons(
|
|
|
|
self,
|
|
|
|
pps: dict[str, Position],
|
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
form = self.form
|
|
|
|
accounts = form.fields['account']
|
|
|
|
|
|
|
|
for account_name, pp in pps.items():
|
|
|
|
icon_name = None
|
|
|
|
|
|
|
|
if pp.size > 0:
|
|
|
|
icon_name = 'long_pp'
|
|
|
|
elif pp.size < 0:
|
|
|
|
icon_name = 'short_pp'
|
|
|
|
|
|
|
|
accounts.set_icon(account_name, icon_name)
|
2021-08-23 18:21:26 +00:00
|
|
|
|
2021-09-11 22:41:49 +00:00
|
|
|
def display_pnl(
|
|
|
|
self,
|
|
|
|
tracker: PositionTracker,
|
|
|
|
|
2021-09-14 22:31:49 +00:00
|
|
|
) -> None:
|
2021-09-11 22:41:49 +00:00
|
|
|
'''Display the PnL for the current symbol and personal positioning (pp).
|
|
|
|
|
|
|
|
If a position is open start a background task which will
|
|
|
|
real-time update the pnl label in the settings pane.
|
|
|
|
|
|
|
|
'''
|
|
|
|
mode = self.order_mode
|
|
|
|
sym = mode.chart.linked.symbol
|
|
|
|
size = tracker.live_pp.size
|
|
|
|
feed = mode.quote_feed
|
2021-09-14 22:31:49 +00:00
|
|
|
pnl_value = 0
|
|
|
|
|
|
|
|
if size:
|
|
|
|
# last historical close price
|
|
|
|
last = feed.shm.array[-1][['close']][0]
|
|
|
|
pnl_value = copysign(1, size) * pnl(
|
|
|
|
tracker.live_pp.avg_price,
|
|
|
|
last,
|
2021-09-11 22:41:49 +00:00
|
|
|
)
|
|
|
|
|
2021-09-14 22:31:49 +00:00
|
|
|
# maybe start update task
|
|
|
|
global _pnl_tasks
|
|
|
|
if sym.key not in _pnl_tasks:
|
|
|
|
_pnl_tasks[sym.key] = True
|
|
|
|
self.order_mode.nursery.start_soon(
|
|
|
|
update_pnl_from_feed,
|
|
|
|
feed,
|
|
|
|
mode,
|
2021-10-22 16:58:12 +00:00
|
|
|
tracker,
|
2021-09-14 22:31:49 +00:00
|
|
|
)
|
2021-09-11 22:41:49 +00:00
|
|
|
|
2021-09-14 22:31:49 +00:00
|
|
|
# immediately display in status label
|
|
|
|
self.pnl_label.format(pnl=pnl_value)
|
2021-09-11 22:41:49 +00:00
|
|
|
|
2021-08-20 14:30:36 +00:00
|
|
|
|
2021-08-26 14:33:49 +00:00
|
|
|
def position_line(
|
|
|
|
|
|
|
|
chart: 'ChartPlotWidget', # noqa
|
|
|
|
size: float,
|
|
|
|
level: float,
|
|
|
|
color: str,
|
|
|
|
|
|
|
|
orient_v: str = 'bottom',
|
|
|
|
marker: Optional[LevelMarker] = None,
|
|
|
|
|
|
|
|
) -> LevelLine:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Convenience routine to create a line graphic representing a "pp"
|
2021-08-26 14:33:49 +00:00
|
|
|
aka the acro for a,
|
|
|
|
"{piker, private, personal, puny, <place your p-word here>} position".
|
|
|
|
|
|
|
|
If ``marker`` is provided it will be configured appropriately for
|
|
|
|
the "direction" of the position.
|
|
|
|
|
|
|
|
'''
|
2021-09-04 18:27:10 +00:00
|
|
|
line = order_line(
|
2021-08-26 14:33:49 +00:00
|
|
|
chart,
|
|
|
|
level,
|
2021-09-04 18:27:10 +00:00
|
|
|
|
|
|
|
# TODO: could we maybe add a ``action=None`` which
|
|
|
|
# would be a mechanism to check a marker was passed in?
|
|
|
|
|
2021-08-26 14:33:49 +00:00
|
|
|
color=color,
|
|
|
|
highlight_on_hover=False,
|
|
|
|
movable=False,
|
|
|
|
hide_xhair_on_hover=False,
|
|
|
|
only_show_markers_on_hover=False,
|
2021-09-04 18:27:10 +00:00
|
|
|
always_show_labels=False,
|
|
|
|
|
|
|
|
# explicitly disable ``order_line()`` factory's creation
|
|
|
|
# of a level marker since we do it in this tracer thing.
|
|
|
|
show_markers=False,
|
2021-08-26 14:33:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
if marker:
|
|
|
|
# configure marker to position data
|
|
|
|
|
|
|
|
if size > 0: # long
|
|
|
|
style = '|<' # point "up to" the line
|
|
|
|
elif size < 0: # short
|
|
|
|
style = '>|' # point "down to" the line
|
|
|
|
|
|
|
|
marker.style = style
|
|
|
|
|
|
|
|
# set marker color to same as line
|
|
|
|
marker.setPen(line.currentPen)
|
|
|
|
marker.setBrush(fn.mkBrush(line.currentPen.color()))
|
|
|
|
marker.level = level
|
|
|
|
marker.update()
|
|
|
|
marker.show()
|
|
|
|
|
|
|
|
# show position marker on view "edge" when out of view
|
|
|
|
vb = line.getViewBox()
|
|
|
|
vb.sigRangeChanged.connect(marker.position_in_view)
|
|
|
|
|
|
|
|
line.set_level(level)
|
|
|
|
|
|
|
|
return line
|
|
|
|
|
|
|
|
|
2021-07-22 15:23:04 +00:00
|
|
|
class PositionTracker:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Track and display real-time positions for a single symbol
|
2021-09-10 15:50:24 +00:00
|
|
|
over multiple accounts on a single chart.
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-08-27 18:48:55 +00:00
|
|
|
Graphically composed of a level line and marker as well as labels
|
|
|
|
for indcating current position information. Updates are made to the
|
|
|
|
corresponding "settings pane" for the chart's "order mode" UX.
|
|
|
|
|
2021-07-22 15:23:04 +00:00
|
|
|
'''
|
2021-07-21 23:42:15 +00:00
|
|
|
# inputs
|
|
|
|
chart: 'ChartPlotWidget' # noqa
|
2021-09-10 15:50:24 +00:00
|
|
|
|
2021-08-28 18:07:18 +00:00
|
|
|
alloc: Allocator
|
2021-09-07 01:27:53 +00:00
|
|
|
startup_pp: Position
|
2021-09-10 15:50:24 +00:00
|
|
|
live_pp: Position
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
# allocated
|
|
|
|
pp_label: Label
|
|
|
|
size_label: Label
|
|
|
|
line: Optional[LevelLine] = None
|
|
|
|
|
2021-08-01 22:53:30 +00:00
|
|
|
_color: str = 'default_lightest'
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
chart: 'ChartPlotWidget', # noqa
|
2021-08-28 18:07:18 +00:00
|
|
|
alloc: Allocator,
|
2021-09-07 01:27:53 +00:00
|
|
|
startup_pp: Position,
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
|
|
|
|
self.chart = chart
|
2021-09-10 15:50:24 +00:00
|
|
|
|
2021-08-28 18:07:18 +00:00
|
|
|
self.alloc = alloc
|
2021-09-07 01:27:53 +00:00
|
|
|
self.startup_pp = startup_pp
|
|
|
|
self.live_pp = startup_pp.copy()
|
2021-07-22 15:23:04 +00:00
|
|
|
|
2021-07-21 23:42:15 +00:00
|
|
|
view = chart.getViewBox()
|
|
|
|
|
2021-08-10 20:59:44 +00:00
|
|
|
# literally the 'pp' (pee pee) label that's always in view
|
2021-07-21 23:42:15 +00:00
|
|
|
self.pp_label = pp_label = Label(
|
|
|
|
view=view,
|
|
|
|
fmt_str='pp',
|
|
|
|
color=self._color,
|
|
|
|
update_on_range_change=False,
|
|
|
|
)
|
|
|
|
|
2021-07-23 18:19:22 +00:00
|
|
|
# create placeholder 'up' level arrow
|
|
|
|
self._level_marker = None
|
|
|
|
self._level_marker = self.level_marker(size=1)
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
pp_label.scene_anchor = partial(
|
|
|
|
gpath_pin,
|
|
|
|
gpath=self._level_marker,
|
|
|
|
label=pp_label,
|
|
|
|
)
|
|
|
|
pp_label.render()
|
|
|
|
|
|
|
|
self.size_label = size_label = Label(
|
|
|
|
view=view,
|
|
|
|
color=self._color,
|
|
|
|
|
|
|
|
# this is "static" label
|
|
|
|
# update_on_range_change=False,
|
|
|
|
fmt_str='\n'.join((
|
2021-08-28 18:07:18 +00:00
|
|
|
':{slots_used:.1f}x',
|
2021-07-21 23:42:15 +00:00
|
|
|
)),
|
|
|
|
|
|
|
|
fields={
|
2021-08-28 18:07:18 +00:00
|
|
|
'slots_used': 0,
|
2021-07-21 23:42:15 +00:00
|
|
|
},
|
|
|
|
)
|
|
|
|
size_label.render()
|
2021-07-22 15:23:04 +00:00
|
|
|
|
2021-07-23 18:19:22 +00:00
|
|
|
size_label.scene_anchor = partial(
|
|
|
|
pp_tight_and_right,
|
|
|
|
label=self.pp_label,
|
2021-07-21 23:42:15 +00:00
|
|
|
)
|
2021-07-23 18:19:22 +00:00
|
|
|
|
2021-08-18 12:55:42 +00:00
|
|
|
@property
|
|
|
|
def pane(self) -> FieldsForm:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Return handle to pp side pane form.
|
2021-08-18 12:55:42 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
return self.chart.linked.godwidget.pp_pane
|
|
|
|
|
2021-07-23 18:19:22 +00:00
|
|
|
def update_graphics(
|
|
|
|
self,
|
|
|
|
marker: LevelMarker
|
|
|
|
|
|
|
|
) -> None:
|
2022-03-02 14:20:17 +00:00
|
|
|
'''
|
|
|
|
Update all labels.
|
2021-07-23 18:19:22 +00:00
|
|
|
|
|
|
|
Meant to be called from the maker ``.paint()``
|
|
|
|
for immediate, lag free label draws.
|
|
|
|
|
|
|
|
'''
|
|
|
|
self.pp_label.update()
|
|
|
|
self.size_label.update()
|
|
|
|
|
2021-09-07 01:27:53 +00:00
|
|
|
def update_from_pp(
|
2021-07-22 15:23:04 +00:00
|
|
|
self,
|
2021-08-18 12:55:42 +00:00
|
|
|
position: Optional[Position] = None,
|
2021-07-22 15:23:04 +00:00
|
|
|
|
|
|
|
) -> None:
|
2021-09-05 17:50:53 +00:00
|
|
|
'''Update graphics and data from average price and size passed in our
|
|
|
|
EMS ``BrokerdPosition`` msg.
|
2021-07-22 15:23:04 +00:00
|
|
|
|
|
|
|
'''
|
2021-08-18 12:55:42 +00:00
|
|
|
# live pp updates
|
|
|
|
pp = position or self.live_pp
|
2021-07-22 15:23:04 +00:00
|
|
|
|
2021-09-05 17:50:53 +00:00
|
|
|
self.update_line(
|
2021-09-07 01:27:53 +00:00
|
|
|
pp.avg_price,
|
|
|
|
pp.size,
|
|
|
|
self.chart.linked.symbol.lot_size_digits,
|
2021-09-05 17:50:53 +00:00
|
|
|
)
|
2021-07-22 15:23:04 +00:00
|
|
|
|
|
|
|
# label updates
|
2021-08-28 18:07:18 +00:00
|
|
|
self.size_label.fields['slots_used'] = round(
|
|
|
|
self.alloc.slots_used(pp), ndigits=1)
|
2021-07-22 15:23:04 +00:00
|
|
|
self.size_label.render()
|
|
|
|
|
2021-09-07 01:27:53 +00:00
|
|
|
if pp.size == 0:
|
2021-07-22 15:23:04 +00:00
|
|
|
self.hide()
|
|
|
|
|
|
|
|
else:
|
2021-09-07 01:27:53 +00:00
|
|
|
self._level_marker.level = pp.avg_price
|
2021-07-23 18:19:22 +00:00
|
|
|
|
|
|
|
# these updates are critical to avoid lag on view/scene changes
|
2021-07-22 15:23:04 +00:00
|
|
|
self._level_marker.update() # trigger paint
|
2021-07-23 18:19:22 +00:00
|
|
|
self.pp_label.update()
|
|
|
|
self.size_label.update()
|
|
|
|
|
2021-07-22 15:23:04 +00:00
|
|
|
self.show()
|
|
|
|
|
2021-07-23 18:19:22 +00:00
|
|
|
# don't show side and status widgets unless
|
|
|
|
# order mode is "engaged" (which done via input controls)
|
|
|
|
self.hide_info()
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
def level(self) -> float:
|
|
|
|
if self.line:
|
|
|
|
return self.line.value()
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
def show(self) -> None:
|
2021-08-18 12:55:42 +00:00
|
|
|
if self.live_pp.size:
|
2021-07-21 23:42:15 +00:00
|
|
|
self.line.show()
|
2021-09-04 18:27:10 +00:00
|
|
|
self.line.show_labels()
|
|
|
|
|
2021-07-22 15:23:04 +00:00
|
|
|
self._level_marker.show()
|
|
|
|
self.pp_label.show()
|
|
|
|
self.size_label.show()
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
def hide(self) -> None:
|
2021-07-22 15:23:04 +00:00
|
|
|
self.pp_label.hide()
|
|
|
|
self._level_marker.hide()
|
2021-07-21 23:42:15 +00:00
|
|
|
self.size_label.hide()
|
2021-07-22 15:23:04 +00:00
|
|
|
if self.line:
|
|
|
|
self.line.hide()
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-07-22 15:23:04 +00:00
|
|
|
def hide_info(self) -> None:
|
2021-07-23 18:19:22 +00:00
|
|
|
'''Hide details (right now just size label?) of position.
|
2021-07-22 15:23:04 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
self.size_label.hide()
|
2021-09-04 18:27:10 +00:00
|
|
|
if self.line:
|
|
|
|
self.line.hide_labels()
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-07-23 18:19:22 +00:00
|
|
|
# TODO: move into annoate module
|
2021-07-21 23:42:15 +00:00
|
|
|
def level_marker(
|
|
|
|
self,
|
|
|
|
size: float,
|
|
|
|
|
2021-07-23 18:19:22 +00:00
|
|
|
) -> LevelMarker:
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
if self._level_marker:
|
|
|
|
self._level_marker.delete()
|
|
|
|
|
|
|
|
# arrow marker
|
|
|
|
# scale marker size with dpi-aware font size
|
|
|
|
font_size = _font.font.pixelSize()
|
|
|
|
|
|
|
|
# scale marker size with dpi-aware font size
|
|
|
|
arrow_size = floor(1.375 * font_size)
|
|
|
|
|
|
|
|
if size > 0:
|
|
|
|
style = '|<'
|
|
|
|
|
|
|
|
elif size < 0:
|
|
|
|
style = '>|'
|
|
|
|
|
|
|
|
arrow = LevelMarker(
|
|
|
|
chart=self.chart,
|
|
|
|
style=style,
|
|
|
|
get_level=self.level,
|
|
|
|
size=arrow_size,
|
2021-07-23 18:19:22 +00:00
|
|
|
on_paint=self.update_graphics,
|
2021-07-21 23:42:15 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
self.chart.getViewBox().scene().addItem(arrow)
|
|
|
|
arrow.show()
|
|
|
|
|
|
|
|
return arrow
|
|
|
|
|
|
|
|
def update_line(
|
|
|
|
self,
|
|
|
|
price: float,
|
|
|
|
size: float,
|
2021-09-05 17:50:53 +00:00
|
|
|
size_digits: int,
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
) -> None:
|
|
|
|
'''Update personal position level line.
|
|
|
|
|
|
|
|
'''
|
|
|
|
# do line update
|
|
|
|
line = self.line
|
|
|
|
|
2021-09-05 17:50:53 +00:00
|
|
|
if size:
|
|
|
|
if line is None:
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-09-05 17:50:53 +00:00
|
|
|
# create and show a pp line
|
|
|
|
line = self.line = position_line(
|
|
|
|
chart=self.chart,
|
|
|
|
level=price,
|
|
|
|
size=size,
|
|
|
|
color=self._color,
|
|
|
|
marker=self._level_marker,
|
|
|
|
)
|
2021-07-21 23:42:15 +00:00
|
|
|
|
2021-09-05 17:50:53 +00:00
|
|
|
else:
|
2021-07-21 23:42:15 +00:00
|
|
|
|
|
|
|
line.set_level(price)
|
2021-07-22 15:23:04 +00:00
|
|
|
self._level_marker.level = price
|
2021-07-21 23:42:15 +00:00
|
|
|
self._level_marker.update()
|
|
|
|
|
2021-09-05 17:50:53 +00:00
|
|
|
# update LHS sizing label
|
|
|
|
line.update_labels({
|
|
|
|
'size': size,
|
|
|
|
'size_digits': size_digits,
|
2021-09-09 14:34:48 +00:00
|
|
|
'fiat_size': round(price * size, ndigits=2),
|
|
|
|
|
|
|
|
# TODO: per account lines on a single (or very related) symbol
|
2021-09-14 17:10:39 +00:00
|
|
|
'account': self.alloc.account,
|
2021-09-05 17:50:53 +00:00
|
|
|
})
|
|
|
|
line.show()
|
|
|
|
|
|
|
|
elif line: # remove pp line from view if it exists on a net-zero pp
|
|
|
|
line.delete()
|
|
|
|
self.line = None
|