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.

27 lines
996 B

from ..interfaces import Strategy, Portfolio
import numpy as np
class AllIn(Strategy):
"""Just all in the greatest result"""
def __init__(self):
"""Initialise the anti strat"""
def run(self, ptf: Portfolio, result: dict, current_conversion_rate: dict) -> None:
"""Run the strategy"""
# first sell all the stocks
money = 0
for stock, amount in ptf.content().items():
#print(f"{stock}: {amount}")
if amount > 0:
money += current_conversion_rate[stock]*amount
ptf.widraw(amount, stock)
# after get the greatest result
result = {k:-1 if np.isnan(v) else v for k,v in result.items() }
epsilon = 1e-6
greatest = max(result, key=lambda k: result[k]-(np.inf if current_conversion_rate[k]<epsilon else 0))
# then buy all the greatest result
to_add = money/current_conversion_rate[greatest]
ptf.deposit(to_add, greatest)