2023-02-26 20:59:55 +00:00
|
|
|
'''
|
2023-02-23 20:21:10 +00:00
|
|
|
Paper-mode testing
|
2023-02-26 20:59:55 +00:00
|
|
|
'''
|
2023-02-23 20:21:10 +00:00
|
|
|
|
2023-02-09 19:53:57 +00:00
|
|
|
import trio
|
2023-02-23 20:21:10 +00:00
|
|
|
from exceptiongroup import BaseExceptionGroup
|
2023-02-09 23:14:41 +00:00
|
|
|
from typing import (
|
|
|
|
AsyncContextManager,
|
2023-02-12 22:04:49 +00:00
|
|
|
Literal,
|
2023-02-09 23:14:41 +00:00
|
|
|
)
|
2023-02-23 20:21:10 +00:00
|
|
|
|
|
|
|
import pytest
|
|
|
|
from tractor._exceptions import ContextCancelled
|
|
|
|
from uuid import uuid4
|
2023-02-09 23:14:41 +00:00
|
|
|
from functools import partial
|
2023-02-23 20:21:10 +00:00
|
|
|
|
|
|
|
from piker.log import get_logger
|
|
|
|
from piker.clearing._messages import Order
|
2023-03-10 21:42:37 +00:00
|
|
|
from piker.accounting import (
|
2023-02-12 22:04:49 +00:00
|
|
|
open_pps,
|
2023-02-09 19:53:57 +00:00
|
|
|
)
|
2023-02-14 22:06:48 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
log = get_logger(__name__)
|
2023-02-09 19:53:57 +00:00
|
|
|
|
2023-02-24 18:42:44 +00:00
|
|
|
|
2023-02-23 20:21:10 +00:00
|
|
|
def get_fqsn(broker, symbol):
|
2023-02-26 20:59:55 +00:00
|
|
|
fqsn = f'{symbol}.{broker}'
|
2023-02-23 20:21:10 +00:00
|
|
|
return (fqsn, symbol, broker)
|
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
oid = ''
|
|
|
|
test_exec_mode = 'live'
|
|
|
|
(fqsn, symbol, broker) = get_fqsn('kraken', 'xbtusdt')
|
|
|
|
brokers = [broker]
|
|
|
|
account = 'paper'
|
|
|
|
|
|
|
|
|
|
|
|
async def _async_main(
|
|
|
|
open_test_pikerd_and_ems: AsyncContextManager,
|
|
|
|
action: Literal['buy', 'sell'] | None = None,
|
|
|
|
price: int = 30000,
|
2023-02-26 21:11:28 +00:00
|
|
|
executions: int = 1,
|
|
|
|
size: float = 0.01,
|
2023-03-09 19:33:12 +00:00
|
|
|
|
2023-02-26 21:11:28 +00:00
|
|
|
# Assert options
|
2023-02-26 20:59:55 +00:00
|
|
|
assert_entries: bool = False,
|
|
|
|
assert_pps: bool = False,
|
|
|
|
assert_zeroed_pps: bool = False,
|
|
|
|
assert_msg: bool = False,
|
2023-03-09 19:33:12 +00:00
|
|
|
|
2023-02-28 18:16:26 +00:00
|
|
|
) -> None:
|
2023-02-26 20:59:55 +00:00
|
|
|
'''
|
2023-02-28 18:16:26 +00:00
|
|
|
Start piker, place a trade and assert data in
|
|
|
|
pps stream, ledger and position table.
|
2023-02-28 18:05:57 +00:00
|
|
|
|
2023-02-26 21:11:28 +00:00
|
|
|
'''
|
2023-02-26 20:59:55 +00:00
|
|
|
oid: str = ''
|
|
|
|
last_msg = {}
|
2023-02-26 21:11:28 +00:00
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
async with open_test_pikerd_and_ems() as (
|
|
|
|
services,
|
|
|
|
(book, trades_stream, pps, accounts, dialogs),
|
|
|
|
):
|
|
|
|
if action:
|
|
|
|
for x in range(executions):
|
|
|
|
oid = str(uuid4())
|
|
|
|
order = Order(
|
|
|
|
exec_mode=test_exec_mode,
|
|
|
|
action=action,
|
|
|
|
oid=oid,
|
|
|
|
account=account,
|
|
|
|
size=size,
|
|
|
|
symbol=fqsn,
|
|
|
|
price=price,
|
|
|
|
brokers=brokers,
|
2023-02-25 20:59:14 +00:00
|
|
|
)
|
2023-02-26 20:59:55 +00:00
|
|
|
# This is actually a syncronous call to push a message
|
|
|
|
book.send(order)
|
|
|
|
|
|
|
|
async for msg in trades_stream:
|
|
|
|
last_msg = msg
|
|
|
|
match msg:
|
2023-02-26 21:11:28 +00:00
|
|
|
# Wait for position message before moving on
|
2023-02-26 20:59:55 +00:00
|
|
|
case {'name': 'position'}:
|
|
|
|
break
|
|
|
|
|
|
|
|
# Teardown piker like a user would
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
2023-02-28 04:51:03 +00:00
|
|
|
if assert_entries or assert_pps or assert_zeroed_pps or assert_msg:
|
|
|
|
_assert(
|
|
|
|
assert_entries,
|
|
|
|
assert_pps,
|
|
|
|
assert_zeroed_pps,
|
|
|
|
pps,
|
|
|
|
last_msg,
|
|
|
|
size,
|
|
|
|
executions,
|
|
|
|
)
|
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
|
|
|
|
def _assert(
|
|
|
|
assert_entries,
|
|
|
|
assert_pps,
|
|
|
|
assert_zerod_pps,
|
|
|
|
pps,
|
|
|
|
last_msg,
|
|
|
|
size,
|
|
|
|
executions,
|
|
|
|
):
|
|
|
|
with (
|
2023-02-28 19:14:05 +00:00
|
|
|
open_pps(broker, account, write_on_exit=False) as table,
|
2023-02-25 20:59:14 +00:00
|
|
|
):
|
2023-02-26 20:59:55 +00:00
|
|
|
'''
|
2023-02-28 18:26:37 +00:00
|
|
|
Assert multiple cases including pps,
|
|
|
|
ledger and final position message state
|
2023-02-28 18:05:57 +00:00
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
'''
|
|
|
|
if assert_entries:
|
2023-02-28 18:31:17 +00:00
|
|
|
for key, val in [
|
|
|
|
('broker', broker),
|
|
|
|
('account', account),
|
|
|
|
('symbol', fqsn),
|
|
|
|
('size', size * executions),
|
|
|
|
('currency', symbol),
|
|
|
|
('avg_price', table.pps[symbol].ppu)
|
|
|
|
]:
|
|
|
|
assert last_msg[key] == val
|
2023-02-26 20:59:55 +00:00
|
|
|
|
|
|
|
if assert_pps:
|
|
|
|
last_ppu = pps[(broker, account)][-1]
|
|
|
|
assert last_ppu['avg_price'] == table.pps[symbol].ppu
|
|
|
|
|
|
|
|
if assert_zerod_pps:
|
|
|
|
assert not bool(table.pps)
|
|
|
|
|
|
|
|
|
|
|
|
def _run_test_and_check(fn):
|
2023-03-09 19:33:12 +00:00
|
|
|
'''
|
2023-02-28 18:39:13 +00:00
|
|
|
Close position and assert empty position in pps
|
|
|
|
|
|
|
|
'''
|
2023-02-26 20:59:55 +00:00
|
|
|
with pytest.raises(BaseExceptionGroup) as exc_info:
|
|
|
|
trio.run(fn)
|
2023-02-25 00:09:36 +00:00
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
for exception in exc_info.value.exceptions:
|
|
|
|
assert isinstance(exception, KeyboardInterrupt) or isinstance(
|
|
|
|
exception, ContextCancelled
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2023-02-28 18:39:13 +00:00
|
|
|
def test_buy(
|
2023-03-09 19:33:12 +00:00
|
|
|
open_test_pikerd_and_ems: AsyncContextManager,
|
2023-02-28 18:39:13 +00:00
|
|
|
):
|
|
|
|
'''
|
|
|
|
Enter a trade and assert entries are made in pps and ledger files.
|
|
|
|
|
|
|
|
'''
|
2023-02-23 20:21:10 +00:00
|
|
|
_run_test_and_check(
|
2023-02-26 20:59:55 +00:00
|
|
|
partial(
|
|
|
|
_async_main,
|
|
|
|
open_test_pikerd_and_ems=open_test_pikerd_and_ems,
|
|
|
|
action='buy',
|
|
|
|
assert_entries=True,
|
|
|
|
),
|
2023-02-25 00:09:36 +00:00
|
|
|
)
|
|
|
|
|
2023-02-25 20:59:14 +00:00
|
|
|
# Open ems and assert existence of pps entries
|
2023-02-25 00:09:36 +00:00
|
|
|
_run_test_and_check(
|
2023-02-26 20:59:55 +00:00
|
|
|
partial(
|
|
|
|
_async_main,
|
|
|
|
open_test_pikerd_and_ems=open_test_pikerd_and_ems,
|
|
|
|
assert_pps=True,
|
|
|
|
),
|
2023-02-23 20:21:10 +00:00
|
|
|
)
|
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
|
2023-02-28 18:39:13 +00:00
|
|
|
def test_sell(
|
2023-03-09 19:33:12 +00:00
|
|
|
open_test_pikerd_and_ems: AsyncContextManager,
|
2023-02-28 18:39:13 +00:00
|
|
|
):
|
|
|
|
'''
|
2023-02-28 19:22:24 +00:00
|
|
|
Sell position and ensure pps are zeroed.
|
2023-02-28 18:39:13 +00:00
|
|
|
|
|
|
|
'''
|
2023-02-24 18:42:44 +00:00
|
|
|
_run_test_and_check(
|
2023-02-26 20:59:55 +00:00
|
|
|
partial(
|
|
|
|
_async_main,
|
|
|
|
open_test_pikerd_and_ems=open_test_pikerd_and_ems,
|
|
|
|
action='sell',
|
|
|
|
price=1,
|
|
|
|
),
|
2023-02-23 20:21:10 +00:00
|
|
|
)
|
2023-02-24 18:42:44 +00:00
|
|
|
|
|
|
|
_run_test_and_check(
|
2023-02-26 20:59:55 +00:00
|
|
|
partial(
|
|
|
|
_async_main,
|
|
|
|
open_test_pikerd_and_ems=open_test_pikerd_and_ems,
|
|
|
|
assert_zeroed_pps=True,
|
|
|
|
),
|
2023-02-24 18:42:44 +00:00
|
|
|
)
|
|
|
|
|
2023-03-09 19:33:12 +00:00
|
|
|
|
2023-02-28 18:39:13 +00:00
|
|
|
def test_multi_sell(
|
2023-03-09 19:33:12 +00:00
|
|
|
open_test_pikerd_and_ems: AsyncContextManager,
|
2023-02-28 18:39:13 +00:00
|
|
|
):
|
|
|
|
'''
|
2023-03-09 19:33:12 +00:00
|
|
|
Make 5 market limit buy orders and
|
|
|
|
then sell 5 slots at the same price.
|
2023-02-28 18:39:13 +00:00
|
|
|
Finally, assert cleared positions.
|
|
|
|
|
|
|
|
'''
|
2023-02-25 20:59:14 +00:00
|
|
|
_run_test_and_check(
|
2023-02-26 20:59:55 +00:00
|
|
|
partial(
|
|
|
|
_async_main,
|
|
|
|
open_test_pikerd_and_ems=open_test_pikerd_and_ems,
|
|
|
|
action='buy',
|
|
|
|
executions=5,
|
|
|
|
),
|
2023-02-25 20:59:14 +00:00
|
|
|
)
|
2023-02-24 18:42:44 +00:00
|
|
|
|
2023-02-25 20:59:14 +00:00
|
|
|
_run_test_and_check(
|
|
|
|
partial(
|
2023-02-26 20:59:55 +00:00
|
|
|
_async_main,
|
|
|
|
open_test_pikerd_and_ems=open_test_pikerd_and_ems,
|
|
|
|
action='sell',
|
|
|
|
executions=5,
|
|
|
|
price=1,
|
|
|
|
assert_zeroed_pps=True,
|
2023-02-25 20:59:14 +00:00
|
|
|
),
|
|
|
|
)
|