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.
74 lines
2.4 KiB
74 lines
2.4 KiB
from typing import Dict, List
|
|
|
|
import pandas as pd # type: ignore
|
|
|
|
from ..orders import Long, Short
|
|
from ..interfaces import Indicator, Strategy, Order, PTFState
|
|
|
|
|
|
class BuyUpSellDown(Strategy):
|
|
"""Buy when indicators are green, sell when red."""
|
|
|
|
def __init__(self, indicators: Dict[str, Indicator]):
|
|
"""Init the class"""
|
|
super().__init__(indicators=indicators)
|
|
|
|
def execute(
|
|
self, data: pd.DataFrame, indicators_results: pd.DataFrame, state: PTFState
|
|
) -> List[Order]:
|
|
"""Just hold the value [to_hold].
|
|
|
|
Args:
|
|
data (DataFrame): The Data broker output.
|
|
For each time and each stock give (high, low, open, close).
|
|
|
|
indicators_results (DataFrame): Indicator-Stock valuated float.
|
|
For each indicator and each stock give -1 if realy bad and +1 if realy good.
|
|
|
|
Returns:
|
|
List[Order]: A list of orders to execute.
|
|
"""
|
|
|
|
nb_stock_type = len(indicators_results.columns)
|
|
|
|
orders = []
|
|
# if I have some money
|
|
for stock_name in indicators_results.columns:
|
|
# for each stock
|
|
# I calculate the value of the stock
|
|
market_price = data.loc[data.index[-1][0]].close.to_dict().get(stock_name)
|
|
|
|
### compute trust
|
|
trust = 0
|
|
for (index, row) in indicators_results.iterrows():
|
|
if row[stock_name] > 0:
|
|
trust += 1
|
|
elif row[stock_name] < 0:
|
|
trust -= 1
|
|
print(trust)
|
|
|
|
if market_price is None:
|
|
# retry later
|
|
continue
|
|
|
|
if trust > 0:
|
|
if (balance := state.balance) > 0:
|
|
amount = balance / (market_price * nb_stock_type)
|
|
|
|
# and I buy it all at market price
|
|
orders.append(
|
|
Long(stock=stock_name, amount=amount, price=market_price)
|
|
)
|
|
else:
|
|
print("A PU DE THUNE")
|
|
elif trust < 0:
|
|
# and I sell it all at market price
|
|
orders.append(
|
|
Short(
|
|
stock=stock_name,
|
|
amount=state.stocks[stock_name],
|
|
price=market_price,
|
|
)
|
|
)
|
|
print(state.stocks)
|
|
return orders
|
|
|