Assert that trades persist in ems after teardown and startup
parent
68a196218b
commit
df868cec35
|
@ -8,8 +8,12 @@ from piker.log import get_logger
|
||||||
from piker.clearing._messages import (
|
from piker.clearing._messages import (
|
||||||
Order
|
Order
|
||||||
)
|
)
|
||||||
from typing import AsyncContextManager
|
from typing import (
|
||||||
|
AsyncContextManager,
|
||||||
|
Any,
|
||||||
|
)
|
||||||
|
|
||||||
|
from functools import partial
|
||||||
from piker.pp import (
|
from piker.pp import (
|
||||||
Position,
|
Position,
|
||||||
Transaction,
|
Transaction,
|
||||||
|
@ -38,21 +42,38 @@ from piker.clearing._client import (
|
||||||
|
|
||||||
log = get_logger(__name__)
|
log = get_logger(__name__)
|
||||||
|
|
||||||
|
_clearing_price: float
|
||||||
|
|
||||||
|
|
||||||
def test_paper_trade(
|
def test_paper_trade(
|
||||||
open_test_pikerd: AsyncContextManager
|
open_test_pikerd: AsyncContextManager
|
||||||
):
|
):
|
||||||
|
_cleared_price: float
|
||||||
|
test_exec_mode='live'
|
||||||
|
test_action = 'buy'
|
||||||
|
test_oid = '560beac8-b1b1-4dee-bd1e-6604a704c9ea'
|
||||||
|
test_account = 'paper'
|
||||||
|
test_size = 1
|
||||||
|
test_price = 30000
|
||||||
|
test_broker = 'kraken'
|
||||||
|
test_brokers = [test_broker]
|
||||||
|
test_symbol = 'xbtusdt'
|
||||||
|
test_fqsn = f'{test_symbol}.{test_broker}'
|
||||||
|
test_pp_account = 'piker-paper'
|
||||||
|
|
||||||
async def main():
|
|
||||||
|
async def open(
|
||||||
|
open_pikerd: AsyncContextManager,
|
||||||
|
send_order: bool = False,
|
||||||
|
assert_entries: bool = False,
|
||||||
|
teardown: bool = True,
|
||||||
|
) -> Any:
|
||||||
# type declares
|
# type declares
|
||||||
book: OrderBook
|
book: OrderBook
|
||||||
trades_stream: tractor.MsgStream
|
global _cleared_price
|
||||||
pps: dict[str, list[BrokerdPosition]]
|
|
||||||
accounts: list[str]
|
|
||||||
dialogs: dict[str, Status]
|
|
||||||
|
|
||||||
async with (
|
async with (
|
||||||
open_test_pikerd() as (_, _, _, services),
|
open_pikerd() as (_, _, _, services),
|
||||||
|
|
||||||
open_ems(
|
open_ems(
|
||||||
'xbtusdt.kraken',
|
'xbtusdt.kraken',
|
||||||
|
@ -66,78 +87,67 @@ def test_paper_trade(
|
||||||
),
|
),
|
||||||
):
|
):
|
||||||
|
|
||||||
test_exec_mode='live'
|
|
||||||
test_action = 'buy'
|
|
||||||
test_oid = '560beac8-b1b1-4dee-bd1e-6604a704c9ea'
|
|
||||||
test_account = 'paper'
|
|
||||||
test_size = 1
|
|
||||||
test_price = 30000
|
|
||||||
test_broker = 'kraken'
|
|
||||||
test_brokers = [test_broker]
|
|
||||||
test_symbol = 'xbtusdt'
|
|
||||||
test_fqsn = f'{test_symbol}.{test_broker}'
|
|
||||||
test_pp_account = 'piker-paper'
|
|
||||||
|
|
||||||
order = Order(
|
if send_order:
|
||||||
exec_mode=test_exec_mode,
|
order = Order(
|
||||||
action=test_action,
|
exec_mode=test_exec_mode,
|
||||||
oid=test_oid,
|
action=test_action,
|
||||||
account=test_account,
|
oid=test_oid,
|
||||||
size=test_size,
|
account=test_account,
|
||||||
symbol=test_fqsn,
|
size=test_size,
|
||||||
price=test_price,
|
symbol=test_fqsn,
|
||||||
brokers=test_brokers
|
price=test_price,
|
||||||
)
|
brokers=test_brokers
|
||||||
|
)
|
||||||
|
|
||||||
book.send(order)
|
book.send(order)
|
||||||
|
|
||||||
await trio.sleep(1)
|
await trio.sleep(1)
|
||||||
|
|
||||||
cleared_ledger_entry = {}
|
if assert_entries:
|
||||||
# check if trades have been updated in in ledge and pp
|
|
||||||
with open_trade_ledger(test_broker, test_account) as ledger:
|
|
||||||
log.warning(f'ledger: {ledger}')
|
|
||||||
cleared_ledger_entry = ledger[test_oid]
|
|
||||||
assert list(ledger.keys())[0] == test_oid
|
|
||||||
assert cleared_ledger_entry['size'] == test_size
|
|
||||||
assert cleared_ledger_entry['fqsn'] == test_fqsn
|
|
||||||
|
|
||||||
with open_pps(test_broker, test_pp_account) as table:
|
cleared_ledger_entry = {}
|
||||||
# save pps in local state
|
# check if trades have been updated in in ledge and pp
|
||||||
assert table.brokername == test_broker
|
with open_trade_ledger(test_broker, test_account) as ledger:
|
||||||
assert table.acctid == test_pp_account
|
cleared_ledger_entry = ledger[test_oid]
|
||||||
# assert cleared_ledger_entry['price'] == table.conf.clears[0].price
|
_cleared_price = cleared_ledger_entry["price"]
|
||||||
pp_price = table.conf[test_broker][test_pp_account][test_fqsn]["ppu"]
|
assert list(ledger.keys())[0] == test_oid
|
||||||
assert math.isclose(pp_price, cleared_ledger_entry['size'], rel_tol=1)
|
assert cleared_ledger_entry['size'] == test_size
|
||||||
|
assert cleared_ledger_entry['fqsn'] == test_fqsn
|
||||||
|
|
||||||
raise KeyboardInterrupt
|
|
||||||
|
|
||||||
|
with open_pps(test_broker, test_pp_account) as table:
|
||||||
|
# save pps in local state
|
||||||
|
assert table.brokername == test_broker
|
||||||
|
assert table.acctid == test_pp_account
|
||||||
|
# assert cleared_ledger_entry['price'] == table.conf.clears[0].price
|
||||||
|
pp_price = table.conf[test_broker][test_pp_account][test_fqsn]["ppu"]
|
||||||
|
assert math.isclose(pp_price, cleared_ledger_entry['size'], rel_tol=1)
|
||||||
|
|
||||||
|
if teardown:
|
||||||
|
raise KeyboardInterrupt
|
||||||
|
return pps
|
||||||
|
|
||||||
|
async def open_and_assert_pps():
|
||||||
|
pps = await open(open_test_pikerd)
|
||||||
|
assert pps(test_broker, test_account)[0] == _cleared_price
|
||||||
|
|
||||||
with pytest.raises(
|
with pytest.raises(
|
||||||
trio.MultiError
|
trio.MultiError
|
||||||
) as exc_info:
|
) as exc_info:
|
||||||
trio.run(main)
|
# run initial time and send sent and assert trade
|
||||||
|
trio.run(partial(open,
|
||||||
|
open_pikerd=open_test_pikerd,
|
||||||
|
send_order=True,
|
||||||
|
assert_entries=True,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
# Run again just to boot piker
|
||||||
|
trio.run(partial(open,
|
||||||
|
open_pikerd=open_test_pikerd,
|
||||||
|
)
|
||||||
|
)
|
||||||
|
trio.run(open_and_assert_pps)
|
||||||
|
|
||||||
|
|
||||||
def test_trades_persist(
|
|
||||||
open_test_pikerd: AsyncContextManager
|
|
||||||
):
|
|
||||||
|
|
||||||
async def main():
|
|
||||||
async with (
|
|
||||||
open_test_pikerd() as (_, _, _, services),
|
|
||||||
|
|
||||||
open_ems(
|
|
||||||
'xbtusdt.kraken',
|
|
||||||
mode='paper',
|
|
||||||
) as (
|
|
||||||
book,
|
|
||||||
trades_stream,
|
|
||||||
pps,
|
|
||||||
accounts,
|
|
||||||
dialogs,
|
|
||||||
),
|
|
||||||
):
|
|
||||||
|
|
||||||
assert pps.len()
|
|
||||||
trio.run(main)
|
|
||||||
|
|
Loading…
Reference in New Issue