2023-02-09 19:53:57 +00:00
|
|
|
import trio
|
|
|
|
import pytest
|
|
|
|
import tractor
|
|
|
|
import math
|
2023-02-14 22:06:48 +00:00
|
|
|
import os
|
|
|
|
from shutil import rmtree
|
|
|
|
from piker.config import get_app_dir
|
2023-02-09 19:53:57 +00:00
|
|
|
from piker.log import get_logger
|
|
|
|
from piker.clearing._messages import (
|
|
|
|
Order
|
|
|
|
)
|
2023-02-12 22:04:49 +00:00
|
|
|
from uuid import uuid4
|
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
|
|
|
)
|
|
|
|
from functools import partial
|
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
|
|
|
)
|
|
|
|
from piker.clearing import (
|
|
|
|
open_ems,
|
|
|
|
)
|
|
|
|
from piker.clearing._client import (
|
|
|
|
OrderBook,
|
|
|
|
)
|
2023-02-12 22:04:49 +00:00
|
|
|
from piker.clearing._messages import (
|
|
|
|
BrokerdPosition
|
|
|
|
)
|
|
|
|
from exceptiongroup import BaseExceptionGroup
|
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-15 18:47:23 +00:00
|
|
|
@pytest.fixture(scope="session")
|
2023-02-14 22:06:48 +00:00
|
|
|
def paper_cleanup():
|
|
|
|
yield
|
|
|
|
app_dir = get_app_dir('piker')
|
2023-02-15 01:06:05 +00:00
|
|
|
if os.path.isfile(app_dir):
|
2023-02-15 01:02:36 +00:00
|
|
|
rmtree(app_dir)
|
|
|
|
assert not os.path.isfile(app_dir)
|
2023-02-14 22:06:48 +00:00
|
|
|
|
2023-02-09 19:53:57 +00:00
|
|
|
def test_paper_trade(
|
2023-02-14 22:06:48 +00:00
|
|
|
open_test_pikerd: AsyncContextManager,
|
|
|
|
paper_cleanup: None
|
2023-02-09 19:53:57 +00:00
|
|
|
):
|
2023-02-12 22:04:49 +00:00
|
|
|
|
2023-02-15 00:00:49 +00:00
|
|
|
cleared_price: float
|
2023-02-09 23:14:41 +00:00
|
|
|
test_exec_mode='live'
|
|
|
|
test_account = 'paper'
|
|
|
|
test_size = 1
|
|
|
|
test_broker = 'kraken'
|
|
|
|
test_brokers = [test_broker]
|
|
|
|
test_symbol = 'xbtusdt'
|
|
|
|
test_fqsn = f'{test_symbol}.{test_broker}'
|
|
|
|
test_pp_account = 'piker-paper'
|
2023-02-12 22:04:49 +00:00
|
|
|
positions: dict[
|
|
|
|
# brokername, acctid
|
|
|
|
tuple[str, str],
|
|
|
|
list[BrokerdPosition],
|
|
|
|
]
|
2023-02-09 23:14:41 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
async def _async_main(
|
2023-02-09 23:14:41 +00:00
|
|
|
open_pikerd: AsyncContextManager,
|
2023-02-12 22:04:49 +00:00
|
|
|
action: Literal['buy', 'sell'] | None = None,
|
|
|
|
price: int = 30000,
|
2023-02-09 23:14:41 +00:00
|
|
|
assert_entries: bool = False,
|
2023-02-14 17:07:42 +00:00
|
|
|
) -> None:
|
|
|
|
"""Spawn a paper piper actor, place a trade and assert entries are present
|
|
|
|
in both trade ledger and pps tomls. Then restart piker and ensure
|
|
|
|
that pps from previous trade exists in the ems pps.
|
|
|
|
Finally close the position and ensure that the position in pps.toml is closed.
|
|
|
|
"""
|
2023-02-09 19:53:57 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
oid: str = str(uuid4())
|
|
|
|
book: OrderBook
|
|
|
|
global cleared_price
|
|
|
|
global positions
|
|
|
|
|
|
|
|
# Set up piker and EMS
|
2023-02-09 19:53:57 +00:00
|
|
|
async with (
|
2023-02-09 23:14:41 +00:00
|
|
|
open_pikerd() as (_, _, _, services),
|
2023-02-09 19:53:57 +00:00
|
|
|
open_ems(
|
2023-02-12 22:04:49 +00:00
|
|
|
test_fqsn,
|
|
|
|
mode=test_account,
|
2023-02-09 19:53:57 +00:00
|
|
|
) as (
|
|
|
|
book,
|
|
|
|
trades_stream,
|
|
|
|
pps,
|
|
|
|
accounts,
|
|
|
|
dialogs,
|
|
|
|
),
|
|
|
|
):
|
2023-02-09 23:14:41 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
# Send order to EMS
|
|
|
|
if action:
|
2023-02-09 23:14:41 +00:00
|
|
|
order = Order(
|
|
|
|
exec_mode=test_exec_mode,
|
2023-02-12 22:04:49 +00:00
|
|
|
action=action,
|
|
|
|
oid=oid,
|
2023-02-09 23:14:41 +00:00
|
|
|
account=test_account,
|
|
|
|
size=test_size,
|
|
|
|
symbol=test_fqsn,
|
2023-02-12 22:04:49 +00:00
|
|
|
price=price,
|
2023-02-09 23:14:41 +00:00
|
|
|
brokers=test_brokers
|
|
|
|
)
|
|
|
|
|
|
|
|
book.send(order)
|
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
await trio.sleep(2)
|
2023-02-09 23:14:41 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
# Assert entries are made in both ledger and PPS
|
|
|
|
if assert_entries:
|
2023-02-09 23:14:41 +00:00
|
|
|
cleared_ledger_entry = {}
|
|
|
|
with open_trade_ledger(test_broker, test_account) as ledger:
|
2023-02-12 22:04:49 +00:00
|
|
|
cleared_ledger_entry = ledger[oid]
|
|
|
|
cleared_price = cleared_ledger_entry["price"]
|
|
|
|
|
|
|
|
assert list(ledger.keys())[-1] == oid
|
2023-02-09 23:14:41 +00:00
|
|
|
assert cleared_ledger_entry['size'] == test_size
|
|
|
|
assert cleared_ledger_entry['fqsn'] == test_fqsn
|
|
|
|
|
|
|
|
with open_pps(test_broker, test_pp_account) as table:
|
|
|
|
pp_price = table.conf[test_broker][test_pp_account][test_fqsn]["ppu"]
|
|
|
|
assert math.isclose(pp_price, cleared_ledger_entry['size'], rel_tol=1)
|
2023-02-12 22:04:49 +00:00
|
|
|
assert table.brokername == test_broker
|
|
|
|
assert table.acctid == test_pp_account
|
2023-02-09 19:53:57 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
positions = pps
|
|
|
|
|
|
|
|
# Close piker like a user would
|
|
|
|
raise KeyboardInterrupt
|
|
|
|
|
|
|
|
# Open piker and ensure last pps price is the same as ledger entry
|
|
|
|
async def _open_and_assert_pps():
|
|
|
|
await _async_main(open_test_pikerd)
|
|
|
|
assert positions(test_broker, test_account)[-1] == cleared_price
|
|
|
|
|
|
|
|
# Close position and assert empty position in pps
|
|
|
|
async def _close_pp_and_assert():
|
|
|
|
await _async_main(open_test_pikerd, 'sell', 1)
|
|
|
|
with open_pps(test_broker, test_pp_account) as table:
|
|
|
|
assert len(table.pps) == 0
|
|
|
|
|
|
|
|
# run initial time and send sent and assert trade
|
2023-02-09 19:53:57 +00:00
|
|
|
with pytest.raises(
|
2023-02-12 22:04:49 +00:00
|
|
|
BaseExceptionGroup
|
2023-02-14 17:07:42 +00:00
|
|
|
):
|
2023-02-12 22:04:49 +00:00
|
|
|
trio.run(partial(_async_main,
|
2023-02-09 23:14:41 +00:00
|
|
|
open_pikerd=open_test_pikerd,
|
2023-02-12 22:04:49 +00:00
|
|
|
action='buy',
|
2023-02-09 23:14:41 +00:00
|
|
|
)
|
2023-02-12 22:04:49 +00:00
|
|
|
)
|
2023-02-09 23:14:41 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
with pytest.raises(
|
|
|
|
BaseExceptionGroup
|
2023-02-14 17:07:42 +00:00
|
|
|
):
|
2023-02-12 22:04:49 +00:00
|
|
|
trio.run(_open_and_assert_pps)
|
2023-02-09 19:53:57 +00:00
|
|
|
|
2023-02-12 22:04:49 +00:00
|
|
|
with pytest.raises(
|
|
|
|
BaseExceptionGroup
|
2023-02-14 17:07:42 +00:00
|
|
|
):
|
2023-02-12 22:04:49 +00:00
|
|
|
trio.run(_close_pp_and_assert)
|
2023-02-09 19:53:57 +00:00
|
|
|
|