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-02-09 19:53:57 +00:00
|
|
|
from piker.pp import (
|
|
|
|
open_trade_ledger,
|
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,
|
|
|
|
# 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-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
|
|
|
|
|
2023-02-28 18:19:21 +00:00
|
|
|
if (
|
|
|
|
assert_entries
|
|
|
|
or assert_pps
|
|
|
|
or assert_zeroed_pps
|
|
|
|
or assert_msg
|
|
|
|
):
|
2023-02-26 20:59:55 +00:00
|
|
|
_assert(
|
|
|
|
assert_entries,
|
|
|
|
assert_pps,
|
|
|
|
assert_zeroed_pps,
|
|
|
|
pps,
|
|
|
|
last_msg,
|
|
|
|
size,
|
|
|
|
executions,
|
|
|
|
)
|
2023-02-12 22:04:49 +00:00
|
|
|
|
2023-02-26 20:59:55 +00:00
|
|
|
# Teardown piker like a user would
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
|
|
|
|
def _assert(
|
|
|
|
assert_entries,
|
|
|
|
assert_pps,
|
|
|
|
assert_zerod_pps,
|
|
|
|
pps,
|
|
|
|
last_msg,
|
|
|
|
size,
|
|
|
|
executions,
|
|
|
|
):
|
|
|
|
with (
|
|
|
|
open_trade_ledger(broker, account) as ledger,
|
|
|
|
open_pps(broker, account) as table,
|
2023-02-25 20:59:14 +00:00
|
|
|
):
|
2023-02-26 20:59:55 +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:
|
|
|
|
assert last_msg['broker'] == broker
|
|
|
|
assert last_msg['account'] == account
|
|
|
|
assert last_msg['symbol'] == fqsn
|
|
|
|
assert last_msg['size'] == size * executions
|
|
|
|
assert last_msg['currency'] == symbol
|
|
|
|
assert last_msg['avg_price'] == table.pps[symbol].ppu
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
|
|
|
|
# Close position and assert empty position in pps
|
|
|
|
def _run_test_and_check(fn):
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def test_buy(open_test_pikerd_and_ems: AsyncContextManager, delete_testing_dir):
|
2023-02-25 20:59:14 +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
|
|
|
|
|
|
|
def test_sell(open_test_pikerd_and_ems: AsyncContextManager, delete_testing_dir):
|
2023-02-25 20:59:14 +00:00
|
|
|
# Sell position
|
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
|
|
|
|
2023-02-25 20:59:14 +00:00
|
|
|
# Ensure pps are zeroed
|
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-02-28 18:16:26 +00:00
|
|
|
|
|
|
|
@pytest.mark.skip(reason='Due to precision issues, this test will currently fail')
|
2023-02-26 20:59:55 +00:00
|
|
|
def test_multi_sell(open_test_pikerd_and_ems: AsyncContextManager, delete_testing_dir):
|
2023-02-25 20:59:14 +00:00
|
|
|
# Make 5 market limit buy orders
|
|
|
|
_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
|
|
|
# Sell 5 slots at the same price, assert cleared positions
|
|
|
|
_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
|
|
|
),
|
|
|
|
)
|