|
|
@ -7,7 +7,7 @@ from abc import ABC, abstractmethod, abstractproperty |
|
|
|
from dataclasses import dataclass, field |
|
|
|
from datetime import timedelta, datetime |
|
|
|
from pandas import DataFrame |
|
|
|
from typing import Dict, List, Optional, Type, Callable |
|
|
|
from typing import Dict, List, Optional, Any, Callable |
|
|
|
|
|
|
|
from .errors import OrderException, UnknowOrder, PTFException |
|
|
|
|
|
|
@ -26,15 +26,15 @@ class CandlesProperties: |
|
|
|
|
|
|
|
class DataBroker(ABC): |
|
|
|
""" Somethink that give you data. """ |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
""" Init the class. """ |
|
|
|
self.logger = logging.getLogger(self.__class__.__name__) |
|
|
|
|
|
|
|
|
|
|
|
@abstractproperty |
|
|
|
def properties(self) -> CandlesProperties: |
|
|
|
""" Return the properties of the candles for this broker. """ |
|
|
|
|
|
|
|
|
|
|
|
@abstractproperty |
|
|
|
def current_change(self) -> DataFrame: |
|
|
|
""" Return the current change for each money. """ |
|
|
@ -57,11 +57,11 @@ class DataBroker(ABC): |
|
|
|
|
|
|
|
class Indicator(ABC): |
|
|
|
""" Somethink that give you an insight of the market. """ |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self): |
|
|
|
""" Init the class. """ |
|
|
|
self.logger = logging.getLogger(self.__class__.__name__) |
|
|
|
|
|
|
|
|
|
|
|
@abstractmethod |
|
|
|
def __call__(self, data: DataFrame) -> DataFrame: |
|
|
|
"""Return a dataframe of valuation of each stock from the input data. |
|
|
@ -99,7 +99,8 @@ class Strategy(ABC): |
|
|
|
return self.execute(data, indicators_results) |
|
|
|
|
|
|
|
@abstractmethod |
|
|
|
def execute(self, data: DataFrame, indicators_results: DataFrame) -> List[Order]: |
|
|
|
def execute(self, data: DataFrame, |
|
|
|
indicators_results: DataFrame) -> List[Order]: |
|
|
|
"""Execute the strategy with the indicators insights. |
|
|
|
|
|
|
|
Args: |
|
|
@ -116,10 +117,10 @@ class Strategy(ABC): |
|
|
|
|
|
|
|
class PTF(ABC): |
|
|
|
""" Somethink that buy or sell stocks.""" |
|
|
|
|
|
|
|
executors: Dict[object, Callable[["PTF", Order], None]] = {} |
|
|
|
|
|
|
|
executors: Dict[Any, Callable[[Any, Any], None]] = {} |
|
|
|
history: List[Order] |
|
|
|
|
|
|
|
|
|
|
|
def __init__(self, skip_errors: bool = True, save_errors: bool = True): |
|
|
|
""" Init the class. |
|
|
|
|
|
|
@ -131,7 +132,7 @@ class PTF(ABC): |
|
|
|
self.history = [] |
|
|
|
self.skip_errors = skip_errors |
|
|
|
self.save_errors = save_errors |
|
|
|
|
|
|
|
|
|
|
|
@abstractproperty |
|
|
|
def balance(self) -> float: |
|
|
|
""" Return the current total balance. """ |
|
|
@ -149,7 +150,8 @@ class PTF(ABC): |
|
|
|
order.successfull = True |
|
|
|
except OrderException as e: |
|
|
|
if not self.skip_errors: |
|
|
|
raise PTFException(f"Got and order exception : {e.message}") from e |
|
|
|
raise PTFException( |
|
|
|
f"Got and order exception : {e.message}") from e |
|
|
|
self.logger.warn("Got an order exception : %s", e.message) |
|
|
|
order.successfull = False |
|
|
|
if self.save_errors or order.successfull: |
|
|
|