Browse Source

feat: tests for in memory

pull/14/head
QuentinN42 1 year ago
parent
commit
16f8774f34
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 1
      auto_trading/ptf/in_memory.py
  2. 28
      tests/tpf/test_in_memory.py

1
auto_trading/ptf/in_memory.py

@ -13,6 +13,7 @@ class InMemoryPortfolio(PTF):
def __init__(self, change_rate_getter: Callable[[], Dict[str, float]]):
"""Init the class with a pointer to the bot to retrieve the change rate."""
super().__init__()
self.change_rate_getter = change_rate_getter
@property

28
tests/tpf/test_in_memory.py

@ -0,0 +1,28 @@
import pytest
from pandas import DataFrame # type: ignore
from auto_trading.ptf.in_memory import InMemoryPortfolio
from auto_trading.interfaces import PTFState as State
from auto_trading.orders import Long, Short
@pytest.mark.parametrize(
"change_rate, start_stocks, start_balance, order, stop_stocks, stop_balance",
[
({"AAPL": 10}, {"AAPL": 0}, 10, Long("AAPL", 1, 10), {"AAPL": 1}, 0),
({"AAPL": 10}, {"AAPL": 0}, 10, Long("AAPL", 0.5, 10), {"AAPL": 0.5}, 5),
({"AAPL": 10}, {"AAPL": 0}, 10, Long("AAPL", 0.5, 20), {"AAPL": 0.5}, 0),
({"AAPL": 10}, {"AAPL": 1}, 0, Short("AAPL", 1, 10), {"AAPL": 0}, 10),
({"AAPL": 10}, {"AAPL": 1}, 0, Short("AAPL", 0.5, 10), {"AAPL": 0.5}, 5),
({"AAPL": 10}, {"AAPL": 1}, 0, Short("AAPL", 0.5, 5), {"AAPL": 0.5}, 2.5),
],
)
def test_execute(
change_rate, start_stocks, start_balance, order, stop_stocks, stop_balance
):
ptf = InMemoryPortfolio(lambda: change_rate)
ptf._stocks = start_stocks
ptf._balance = start_balance
ptf.execute_multiples([order])
assert ptf._stocks == stop_stocks
assert ptf._balance == stop_balance
Loading…
Cancel
Save