Browse Source

try something

jorickdefraine
jorickdefraine 1 year ago
parent
commit
6abfac8227
  1. 30
      auto_trading/indicators/lastVariation.py
  2. 47
      auto_trading/strat/buyhighselllow.py
  3. 3
      main.py

30
auto_trading/indicators/lastVariation.py

@ -0,0 +1,30 @@
"""Dumb indicator, for testing purposes."""
import pandas as pd # type: ignore
from ..interfaces import Indicator
class LastVariation(Indicator):
"""If last value is greater than current value return 1 otherwise -1."""
def __init__(self, value: pd.Series):
"""Save the value."""
super().__init__()
self.value = value
def __call__(self, data: pd.DataFrame) -> pd.Series:
"""Return a dataframe of valuation of each stock from the input data.
Args:
data (DataFrame): Time-Stock valuated candlestick data.
For each time and each stock give (high, low, open, close).
Returns:
DataFrame: Stock valuated float.
For each stock give -1 if realy bad and +1 if realy good.
"""
if self.value[-1]["close"] >= self.value[-2]["close"]:
return 1
else:
return -1
# return self.value

47
auto_trading/strat/buyhighselllow.py

@ -0,0 +1,47 @@
from typing import List
import pandas as pd # type: ignore
from ..orders import Long
from ..interfaces import Strategy, Order, PTFState
from ..indicators.lastVariation import LastVariation
class BuyHighSellLow(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 = data.loc[data.index[-1][0]].close.to_dict().get(self.to_hold)
if market_price is None:
# retry later
return []
amount = balance / market_price
# and I buy it all at market price
if LastVariation(self.to_hold):
orders.append(Long(stock=self.to_hold, amount=amount, price=market_price))
else:
orders.append(Short(stock=self.to_hold, amount=amount, price=market_price))
return orders # type: ignore

3
main.py

@ -2,6 +2,7 @@ import pandas as pd # type: ignore
from auto_trading.broker.backtest import Backtest
from auto_trading.strat.hold import Hold
from auto_trading.strat.buyhighselllow import BuyHighSellLow
from auto_trading.ptf.in_memory import InMemoryPortfolio
from auto_trading.bot import Bot
@ -14,7 +15,7 @@ if __name__ == "__main__":
ptf = InMemoryPortfolio(
base_balance=100, change_rate_getter=lambda: bt.current_change
)
strategy = Hold("GOOGL")
strategy = BuyHighSellLow("WLTW")
bot = Bot(ptf, strategy, bt)

Loading…
Cancel
Save