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.

60 lines
1.8 KiB

"""Yoyo strategy."""
from auto_trading.interfaces import Strategy, Order, PTFState
from auto_trading.orders import Long, Short
from typing import List, Dict, cast, Callable
import pandas as pd # type: ignore
def is_positive(number: float | None) -> bool:
"""Return True if the price is positive."""
return number is not None and number > 0
class Yoyo(Strategy):
"""A strat that buy a stock one time then sell it."""
def __init__(self, stock_name: str):
self.stock_name = stock_name
def execute(
data: pd.DataFrame,
indicators_results: pd.DataFrame,
ptf_state: PTFState,
) -> List[Order]:
market_price = (
return []
if not market_price:
return []
todo: Order
if self.as_stocks(ptf_state.stocks):"sell")
todo = self.create_sell_order(ptf_state, market_price)
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 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 Long(self.stock_name, ptf.balance / current_price, current_price)