Better pp loading at startup

- directly lookup the position data for the current symbol
- let `mk_alloc()` create the allocator
- load and set account name for pp in sidepane
fsp_feeds
Tyler Goodlet 2021-09-07 09:23:18 -04:00
parent 2bc07ae05b
commit 5d25a0d370
1 changed files with 24 additions and 26 deletions

View File

@ -39,7 +39,6 @@ from ..clearing._allocate import (
Allocator, Allocator,
mk_allocator, mk_allocator,
Position, Position,
_size_units,
) )
from ..data._source import Symbol from ..data._source import Symbol
from ..data._normalize import iterticks from ..data._normalize import iterticks
@ -509,7 +508,7 @@ async def open_order_mode(
trades_stream, trades_stream,
positions positions
), ),
trio.open_nursery() as n, trio.open_nursery() as tn,
): ):
log.info(f'Opening order mode for {brokername}.{symbol.key}') log.info(f'Opening order mode for {brokername}.{symbol.key}')
@ -526,37 +525,34 @@ async def open_order_mode(
symbol = chart.linked.symbol symbol = chart.linked.symbol
symkey = chart.linked._symbol.key symkey = chart.linked._symbol.key
pp_msg = None # NOTE: requires that the backend exactly specifies
for sym, msg in positions.items(): # the expected symbol key in it's positions msg.
if sym.lower() in symkey: pp_msg = positions.get(symkey)
pp_msg = msg
break
# net-zero pp # net-zero pp
startup_pp = Position( startup_pp = Position(
symbol=chart.linked.symbol, symbol=symbol,
size=0, size=0,
avg_price=0, avg_price=0,
) )
if pp_msg:
startup_pp.update_from_msg(msg)
# load account names from ``brokers.toml`` # load account names from ``brokers.toml``
accounts = bidict(config.load_accounts()) accounts = bidict(config.load_accounts())
pp_account = None
if pp_msg:
log.info(f'Loading pp for {symkey}:\n{pformat(pp_msg)}')
startup_pp.update_from_msg(pp_msg)
pp_account = accounts.inverse.get(pp_msg.get('account'))
# lookup account for this pp or load the user default
# for this backend
# allocator # allocator
limit_value, alloc = mk_allocator( alloc = mk_allocator(
symbol=symbol,
alloc=Allocator( accounts=accounts,
symbol=symbol, account=pp_account,
account=None, # select paper by default
_accounts=accounts,
size_unit=_size_units['currency'],
units_limit=400,
currency_limit=5e3,
slots=4,
),
startup_pp=startup_pp, startup_pp=startup_pp,
) )
form.model = alloc form.model = alloc
@ -587,7 +583,9 @@ async def open_order_mode(
) )
# set startup limit value read during alloc init # set startup limit value read during alloc init
order_pane.on_ui_settings_change('limit', limit_value) order_pane.on_ui_settings_change('limit', alloc.limit())
order_pane.on_ui_settings_change('account', pp_account)
# make fill bar and positioning snapshot # make fill bar and positioning snapshot
order_pane.update_status_ui(size=startup_pp.size) order_pane.update_status_ui(size=startup_pp.size)
@ -640,7 +638,7 @@ async def open_order_mode(
) )
# spawn updater task # spawn updater task
n.start_soon( tn.start_soon(
display_pnl, display_pnl,
feed, feed,
mode, mode,
@ -672,9 +670,9 @@ async def open_order_mode(
# to handle input since the ems connection is ready # to handle input since the ems connection is ready
started.set() started.set()
n.start_soon( tn.start_soon(
process_trades_and_update_ui, process_trades_and_update_ui,
n, tn,
feed, feed,
mode, mode,
trades_stream, trades_stream,