5 changed files with 127 additions and 45 deletions
@ -1,27 +0,0 @@ |
|||
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.withdraw(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) |
@ -0,0 +1,41 @@ |
|||
from typing import List |
|||
|
|||
import pandas as pd # type: ignore |
|||
|
|||
from ..orders import Long |
|||
from ..interfaces import Strategy, Order, PTFState |
|||
|
|||
|
|||
class Hold(Strategy): |
|||
"""Just hold some stock.""" |
|||
|
|||
def __init__(self, to_hold: str): |
|||
"""Init the class""" |
|||
super().__init__() |
|||
self.to_hold = to_hold |
|||
|
|||
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. |
|||
""" |
|||
orders = [] |
|||
# if I have some money |
|||
if (balance := state.balance) > 0: |
|||
# I calculate the value of the stock |
|||
market_price = state.conversion_rate.iloc[-1].to_dict()[self.to_hold] |
|||
amount = balance / market_price |
|||
# and I buy it all at market price |
|||
orders.append(Long(stock=self.to_hold, amount=amount, price=market_price)) |
|||
|
|||
return orders # type: ignore |
@ -0,0 +1,44 @@ |
|||
import pytest |
|||
from pandas import DataFrame # type: ignore |
|||
|
|||
from auto_trading.strat.hold import Hold |
|||
from auto_trading.interfaces import PTFState as State |
|||
from auto_trading.orders import Long |
|||
|
|||
|
|||
@pytest.mark.parametrize( |
|||
"state, output", |
|||
[ |
|||
(State(balance=0, stocks={}, conversion_rate=DataFrame()), None), |
|||
( |
|||
State(balance=10, stocks={}, conversion_rate=DataFrame({"AAPL": [10]})), |
|||
Long(stock="AAPL", amount=1, price=10), |
|||
), |
|||
( |
|||
State( |
|||
balance=10, |
|||
stocks={"AAPL": 5}, |
|||
conversion_rate=DataFrame({"AAPL": [10]}), |
|||
), |
|||
Long(stock="AAPL", amount=1, price=10), |
|||
), |
|||
( |
|||
State( |
|||
balance=10, |
|||
stocks={"AAPL": 5, "TSLA": 5}, |
|||
conversion_rate=DataFrame({"AAPL": [10], "TSLA": [20]}), |
|||
), |
|||
Long(stock="AAPL", amount=1, price=10), |
|||
), |
|||
], |
|||
) |
|||
def test_hold(state, output): |
|||
strat = Hold(to_hold="AAPL") |
|||
res = strat.execute(DataFrame(), DataFrame(), state) |
|||
if output is None: |
|||
assert len(res) == 0 |
|||
else: |
|||
assert len(res) == 1 |
|||
assert res[0].stock == output.stock |
|||
assert res[0].amount == output.amount |
|||
assert res[0].price == output.price |
Loading…
Reference in new issue