Remove breaking call to load pps from ledger

explicit_write_pps_on_exit
jaredgoldman 2023-02-25 17:45:21 -05:00
parent 3e83764b5b
commit fcd8b8eb78
3 changed files with 22 additions and 12 deletions

View File

@ -43,7 +43,6 @@ from ..pp import (
Transaction, Transaction,
open_trade_ledger, open_trade_ledger,
open_pps, open_pps,
load_pps_from_ledger
) )
from ..data._normalize import iterticks from ..data._normalize import iterticks
from ..data._source import unpack_fqsn from ..data._source import unpack_fqsn
@ -250,6 +249,7 @@ class PaperBoi(Struct):
) )
await self.ems_trades_stream.send(msg) await self.ems_trades_stream.send(msg)
# lookup any existing position
key = fqsn.rstrip(f'.{self.broker}') key = fqsn.rstrip(f'.{self.broker}')
t = Transaction( t = Transaction(
fqsn=fqsn, fqsn=fqsn,
@ -263,12 +263,11 @@ class PaperBoi(Struct):
with ( with (
open_trade_ledger(self.broker, 'paper') as ledger, open_trade_ledger(self.broker, 'paper') as ledger,
open_pps(self.broker, 'piker-paper') as table open_pps(self.broker, 'paper') as table
): ):
ledger.update({oid: t.to_dict()}) ledger.update({oid: t.to_dict()})
# Write to pps toml right now # Write to pps toml right now
table.update_from_trans({oid: t}) table.update_from_trans({oid: t})
load_pps_from_ledger(self.broker, 'piker-paper')
pp = table.pps[key] pp = table.pps[key]
pp_msg = BrokerdPosition( pp_msg = BrokerdPosition(

View File

@ -730,7 +730,7 @@ def load_pps_from_ledger(
return {} return {}
mod = get_brokermod(brokername) mod = get_brokermod(brokername)
src_records: dict[str, Transaction] = mod.norm_trade_records(ledger) src_records: dict[str, Transaction] = mod.norm_tr_records(ledger)
if filter_by: if filter_by:
records = {} records = {}

View File

@ -63,7 +63,7 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
test_account = "paper" test_account = "paper"
(fqsn, symbol, broker) = get_fqsn("kraken", "xbtusdt") (fqsn, symbol, broker) = get_fqsn("kraken", "xbtusdt")
brokers = [broker] brokers = [broker]
test_pp_account = "piker-paper" account = "paper"
positions: dict[ positions: dict[
# brokername, acctid # brokername, acctid
tuple[str, str], tuple[str, str],
@ -76,6 +76,7 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
assert_entries: bool = False, assert_entries: bool = False,
assert_pps: bool = False, assert_pps: bool = False,
assert_zeroed_pps: bool = False, assert_zeroed_pps: bool = False,
assert_msg: bool = True,
executions: int = 1, executions: int = 1,
size: float = 0.01, size: float = 0.01,
) -> None: ) -> None:
@ -87,7 +88,7 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
nonlocal oid nonlocal oid
nonlocal positions nonlocal positions
book: OrderBook book: OrderBook
msg = () msg = {}
# Set up piker and EMS # Set up piker and EMS
async with ( async with (
open_test_pikerd() as (_, _, _, services), open_test_pikerd() as (_, _, _, services),
@ -129,24 +130,26 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
await trio.sleep(1) await trio.sleep(1)
# Assert entries are made in both ledger and PPS # Assert entries are made in both ledger and PPS
if assert_entries or assert_pps or assert_zeroed_pps: if assert_entries or assert_pps or assert_zeroed_pps or assert_msg:
_assert( _assert(
assert_entries, assert_entries,
assert_pps, assert_pps,
assert_zeroed_pps, assert_zeroed_pps,
assert_msg,
pps, pps,
msg, msg,
size,
) )
# Close piker like a user would # Close piker like a user would
raise KeyboardInterrupt raise KeyboardInterrupt
def _assert( def _assert(
assert_entries, assert_pps, assert_zerod_pps, pps, msg assert_entries, assert_pps, assert_zerod_pps, assert_msg, pps, msg, size
): ):
with ( with (
open_trade_ledger(broker, test_account) as ledger, open_trade_ledger(broker, test_account) as ledger,
open_pps(broker, test_pp_account) as table, open_pps(broker, test_account) as table,
): ):
# TODO: Assert between msg and pp, ledger and pp, ledger and message # TODO: Assert between msg and pp, ledger and pp, ledger and message
# for proper values # for proper values
@ -155,7 +158,7 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
if assert_entries: if assert_entries:
latest_ledger_entry = ledger[oid] latest_ledger_entry = ledger[oid]
latest_position = pps[(broker, test_account)][-1] latest_position = pps[(broker, test_account)][-1]
pp_price = table.conf[broker][test_pp_account][fqsn]["ppu"] pp_price = table.conf[broker][account][fqsn]["ppu"]
# assert most # assert most
assert list(ledger.keys())[-1] == oid assert list(ledger.keys())[-1] == oid
assert latest_ledger_entry["size"] == test_size assert latest_ledger_entry["size"] == test_size
@ -164,7 +167,7 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
# Ensure the price-per-unit (breakeven) price is close to our clearing price # Ensure the price-per-unit (breakeven) price is close to our clearing price
assert math.isclose(pp_price, latest_ledger_entry["size"], rel_tol=1) assert math.isclose(pp_price, latest_ledger_entry["size"], rel_tol=1)
assert table.brokername == broker assert table.brokername == broker
assert table.acctid == test_pp_account assert table.acctid == account
# assert that the last pps price is the same as the ledger price # assert that the last pps price is the same as the ledger price
if assert_pps: if assert_pps:
@ -173,9 +176,17 @@ def test_paper_trade(open_test_pikerd: AsyncContextManager, delete_testing_dir):
assert latest_position["avg_price"] == latest_ledger_entry["price"] assert latest_position["avg_price"] == latest_ledger_entry["price"]
if assert_zerod_pps: if assert_zerod_pps:
# assert that positions are present # assert that positions are not present
assert not bool(table.pps) assert not bool(table.pps)
if assert_msg and msg["name"] == "position":
latest_position = pps[(broker, test_account)][-1]
breakpoint()
assert msg["broker"] == broker
assert msg["account"]== test_account
assert msg["symbol"] == fqsn
assert msg["avg_price"]== latest_position["avg_price"]
# Close position and assert empty position in pps # Close position and assert empty position in pps
def _run_test_and_check(exception, fn): def _run_test_and_check(exception, fn):
with pytest.raises(exception) as exc_info: with pytest.raises(exception) as exc_info: