|
|
@ -9,10 +9,9 @@ from ..interfaces import Indicator, Strategy, Order, PTFState |
|
|
|
class BuyUpSellDown(Strategy): |
|
|
|
"""Just hold some stock.""" |
|
|
|
|
|
|
|
def __init__(self, to_hold: str, indicators: Dict[str, Indicator]): |
|
|
|
def __init__(self, indicators: Dict[str, Indicator]): |
|
|
|
"""Init the class""" |
|
|
|
super().__init__(indicators=indicators) |
|
|
|
self.to_hold = to_hold |
|
|
|
|
|
|
|
def execute( |
|
|
|
self, data: pd.DataFrame, indicators_results: pd.DataFrame, state: PTFState |
|
|
@ -35,10 +34,12 @@ class BuyUpSellDown(Strategy): |
|
|
|
orders = [] |
|
|
|
# if I have some money |
|
|
|
for stock_name in indicators_results.columns: |
|
|
|
#for each stock |
|
|
|
# for each stock |
|
|
|
if (balance := state.balance) > 0: |
|
|
|
# I calculate the value of the stock |
|
|
|
market_price = data.loc[data.index[-1][0]].close.to_dict().get(stock_name) |
|
|
|
market_price = ( |
|
|
|
data.loc[data.index[-1][0]].close.to_dict().get(stock_name) |
|
|
|
) |
|
|
|
|
|
|
|
trust = int(indicators_results[stock_name]) |
|
|
|
print(trust) |
|
|
@ -46,14 +47,18 @@ class BuyUpSellDown(Strategy): |
|
|
|
if market_price is None: |
|
|
|
# retry later |
|
|
|
return [] |
|
|
|
|
|
|
|
|
|
|
|
amount = balance / (market_price * nb_stock_type) |
|
|
|
|
|
|
|
if trust > 0: |
|
|
|
# and I buy it all at market price |
|
|
|
orders.append(Long(stock=stock_name, amount=amount, price=market_price)) |
|
|
|
orders.append( |
|
|
|
Long(stock=stock_name, amount=amount, price=market_price) |
|
|
|
) |
|
|
|
elif trust < 0: |
|
|
|
# and I sell it all at market price |
|
|
|
orders.append(Short(stock=stock_name, amount=amount, price=market_price)) |
|
|
|
orders.append( |
|
|
|
Short(stock=stock_name, amount=amount, price=market_price) |
|
|
|
) |
|
|
|
|
|
|
|
return orders # type: ignore |
|
|
|