Browse Source

tests: testing yoyo

Signed-off-by: QuentinN42 <quentin@lieumont.fr>
pull/24/head
QuentinN42 7 months ago
parent
commit
207efc8445
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 39
      auto_trading/strat/yoyo.py
  2. 32
      tests/strat/test_yoyo.py

39
auto_trading/strat/yoyo.py

@ -11,11 +11,6 @@ def is_positive(number: float | None) -> bool:
return number is not None and number > 0
def as_stocks(stocks: Dict[str, float]) -> bool:
"""Check if we currently have some stocks to sell."""
return any(map(is_positive, stocks.values()))
class Yoyo(Strategy):
"""A strat that buy a stock one time then sell it."""
@ -23,7 +18,6 @@ class Yoyo(Strategy):
def __init__(self, stock_name: str):
super().__init__()
self.stock_name = stock_name
self.buy = True
def execute(
self,
@ -31,26 +25,35 @@ class Yoyo(Strategy):
indicators_results: pd.DataFrame,
ptf_state: PTFState,
) -> List[Order]:
market_price = data.loc[data.index[-1][0]].close.to_dict().get(self.stock_name)
try:
market_price = (
data.loc[data.index[-1][0]].close.to_dict().get(self.stock_name)
)
except:
return []
if not market_price:
return []
todo: Callable[[PTFState, float], Order]
if as_stocks(ptf_state.stocks):
todo = self.create_buy_order
todo: Order
if self.as_stocks(ptf_state.stocks):
self.logger.info("sell")
todo = self.create_sell_order(ptf_state, market_price)
else:
todo = self.create_sell_order
return [todo(ptf_state, market_price)]
self.logger.info("buy")
todo = self.create_buy_order(ptf_state, market_price)
return [todo]
def as_stocks(self, stocks: Dict[str, float]) -> bool:
"""Check if we currently have some stocks to sell."""
return is_positive(stocks.get(self.stock_name))
def create_sell_order(self, ptf: PTFState, current_price: float) -> Order:
"""Create a sell order."""
return cast(
Order, Long(self.stock_name, ptf.balance / current_price, current_price)
)
return Short(self.stock_name, ptf.stocks[self.stock_name], current_price)
def create_buy_order(self, ptf: PTFState, current_price: float) -> Order:
"""Create a buy order."""
return cast(
Order, Short(self.stock_name, ptf.stocks[self.stock_name], current_price)
)
return Long(self.stock_name, ptf.balance / current_price, current_price)

32
tests/strat/test_yoyo.py

@ -0,0 +1,32 @@
import pytest
from datetime import datetime
from pandas import DataFrame # type: ignore
from auto_trading.strat.yoyo import Yoyo
from auto_trading.interfaces import PTFState as State
from auto_trading.orders import Long, Short
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"): 0}}), State(balance=0, stocks={}), None),
(
DataFrame({"close": {(date, "AAPL"): 10}}),
State(balance=0, stocks={"AAPL": 1}),
Short,
),
(DataFrame({"close": {(date, "AAPL"): 10}}), State(balance=10, stocks={}), Long),
],
)
def test_yoyo(data, state, output):
strat = Yoyo("AAPL")
res = strat.run(data, state)
if output is None:
assert len(res) == 0
else:
assert len(res) == 1
assert isinstance(res[0], output)
Loading…
Cancel
Save