You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

85 lines
2.1 KiB

import pytest
from pandas import DataFrame # type: ignore
from auto_trading.errors import UnknowOrder, PTFException
from auto_trading.interfaces import PTF, PTFState
from auto_trading.orders import Short, Long
long_order = Long("BTC", 1, 1)
short_order = Short("GOLD", 1, 1)
class _TestPTFLong(PTF):
@property
def state(self):
return PTFState(0, {})
def execute_long(self, order: Long) -> None:
assert order == long_order
executors = {Long: execute_long}
class _TestPTFLongShort(PTF):
@property
def state(self):
return PTFState(0, {})
def execute_long(self, order: Long) -> None:
assert order == long_order
def execute_short(self, order: Long) -> None:
assert order == short_order
executors = {Long: execute_long, Short: execute_short}
class _TestPTFShort(PTF):
@property
def state(self):
return PTFState(0, {})
def execute_short(self, order: Long) -> None:
assert order == short_order
executors = {Short: execute_short}
class _TestPTFNone(PTF):
@property
def state(self):
return PTFState(0, {})
@pytest.mark.parametrize(
"test_class, must_match",
[
(_TestPTFLong, [long_order]),
(_TestPTFShort, [short_order]),
(_TestPTFLongShort, [long_order, short_order]),
(_TestPTFNone, []),
],
)
def test_ptf_execution(test_class, must_match):
inst = test_class()
for o in [short_order, long_order]:
if o in must_match:
inst._execute(o)
else:
with pytest.raises(UnknowOrder):
inst._execute(o)
@pytest.mark.parametrize("test_class", [_TestPTFLong, _TestPTFShort, _TestPTFNone])
def test_ptf_executions(test_class):
inst = test_class(skip_errors=False)
with pytest.raises(PTFException):
inst.execute_multiples([short_order, long_order])
@pytest.mark.parametrize("test_class", [_TestPTFLong, _TestPTFShort, _TestPTFNone])
def test_ptf_warn_only(test_class):
inst = test_class()
# Do not raise, only print a warning
inst.execute_multiples([short_order, long_order])