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.
 
 

54 lines
1.5 KiB

import pytest
from datetime import datetime
from pandas import DataFrame # type: ignore
from auto_trading.strat.hold import Hold
from auto_trading.interfaces import PTFState as State
from auto_trading.orders import Long
date = datetime.strptime("2015-03-31", "%Y-%m-%d")
@pytest.mark.parametrize(
"data, state, output",
[
(DataFrame(), State(balance=0, stocks={}), None),
(
DataFrame({"close": {(date, "AAPL"): None}}),
State(balance=10, stocks={}),
None,
),
(
DataFrame({"close": {(date, "AAPL"): 10}}),
State(balance=10, stocks={}),
Long(stock="AAPL", amount=1, price=10),
),
(
DataFrame({"close": {(date, "AAPL"): 10}}),
State(
balance=10,
stocks={"AAPL": 5},
),
Long(stock="AAPL", amount=1, price=10),
),
(
DataFrame({"close": {(date, "AAPL"): 10, (date, "TSLA"): 10}}),
State(
balance=10,
stocks={"AAPL": 5, "TSLA": 5},
),
Long(stock="AAPL", amount=1, price=10),
),
],
)
def test_hold(data, state, output):
strat = Hold(to_hold="AAPL")
res = strat.run(data, state)
if output is None:
assert len(res) == 0
else:
assert len(res) == 1
assert res[0].stock == output.stock
assert res[0].amount == output.amount
assert res[0].price == output.price