Browse Source

feat: change rate history

quents
QuentinN42 8 months ago
parent
commit
782b0b427c
Signed by: number42 GPG Key ID: 2CD7D563712B3A50
  1. 4
      auto_trading/broker/backtest.py
  2. 11
      auto_trading/interfaces.py
  3. 4
      auto_trading/ptf/in_memory.py
  4. 2
      main.py

4
auto_trading/broker/backtest.py

@ -29,6 +29,10 @@ class Backtest(DataBroker):
def cursor(self) -> datetime.datetime:
return self.dates[self._cursor]
@property
def change_rate_history(self) -> pd.DataFrame:
return self.data.close.unstack(level=1)
@staticmethod
def _prepare_data(f_name: str, **kwargs) -> pd.DataFrame:
data = pd.read_csv(f_name, index_col=[0, 1], parse_dates=True, **kwargs)

11
auto_trading/interfaces.py

@ -138,17 +138,19 @@ class PTF(ABC):
"""Somethink that buy or sell stocks."""
executors: Dict[Any, Callable[[Any, Any], None]] = {}
history: List[Order]
orders_history: List[Order]
states_history: List[PTFState]
def __init__(self, skip_errors: bool = True, save_errors: bool = True):
"""Init the class.
Args:
skip_errors (bool, optional): Do we skip orders in failure ? Defaults to True.
save_errors (bool, optional): Do we save orders in failure in the history ? Defaults to True.
save_errors (bool, optional): Do we save orders in failure in the orders_history ? Defaults to True.
"""
self.logger = logging.getLogger(self.__class__.__name__)
self.history = []
self.orders_history = []
self.states_history = []
self.skip_errors = skip_errors
self.save_errors = save_errors
@ -182,7 +184,8 @@ class PTF(ABC):
self.logger.warning("Got an order exception : %s", e.message)
order.successfull = False
if self.save_errors or order.successfull:
self.history.append(order)
self.orders_history.append(order)
self.states_history.append(self.state)
def _execute(self, order: Order) -> None:
"""Execute one order.

4
auto_trading/ptf/in_memory.py

@ -1,4 +1,4 @@
from typing import Dict, Callable
from typing import Dict, Callable, List
from ..interfaces import PTF, PTFState
from ..orders import Long, Short
@ -28,7 +28,7 @@ class InMemoryPortfolio(PTF):
return PTFState(self._balance, self._stocks.copy())
@property
def change_rate(self):
def change_rate(self) -> Dict[str, float]:
return self.change_rate_getter()
def execute_short(self, order: Short) -> None:

2
main.py

@ -15,7 +15,7 @@ if __name__ == "__main__":
bot.run()
for order in ptf.history:
for order in ptf.orders_history:
print(order)
print(bot.ptf.total_balance(bot.broker.current_change))

Loading…
Cancel
Save