|
1 | | -from typing import Type |
| 1 | +import logging |
| 2 | +import time |
| 3 | +from typing import List |
| 4 | + |
2 | 5 | from investing_bot_framework.core.context.states import BotState |
| 6 | +from investing_bot_framework.core.executors import ExecutionScheduler |
| 7 | +from investing_bot_framework.core.exceptions import OperationalException |
| 8 | +from investing_bot_framework.core.workers import Worker |
| 9 | +from investing_bot_framework.core.executors import Executor |
| 10 | +from investing_bot_framework.core.events import Observer |
| 11 | +from investing_bot_framework.core.configuration.config_constants import DEFAULT_MAX_WORKERS, SETTINGS_MAX_WORKERS |
| 12 | + |
| 13 | +logger = logging.getLogger(__name__) |
| 14 | + |
| 15 | + |
| 16 | +class StrategyExecutor(Executor): |
| 17 | + """ |
| 18 | + Class StrategyExecutor: is an executor for Strategy instances. |
| 19 | + """ |
| 20 | + |
| 21 | + def __init__(self, strategies: List = None, max_workers: int = DEFAULT_MAX_WORKERS): |
| 22 | + super(StrategyExecutor, self).__init__(max_workers=max_workers) |
| 23 | + |
| 24 | + self._registered_strategies: List = [] |
| 25 | + |
| 26 | + if strategies is not None and len(strategies) > 0: |
| 27 | + self._registered_strategies = strategies |
| 28 | + |
| 29 | + def create_workers(self) -> List[Worker]: |
| 30 | + return self._registered_strategies |
| 31 | + |
| 32 | + @property |
| 33 | + def registered_strategies(self) -> List: |
| 34 | + return self._registered_strategies |
| 35 | + |
| 36 | + @property |
| 37 | + def configured(self): |
| 38 | + return self._registered_strategies is not None and len(self._registered_strategies) > 0 |
| 39 | + |
| 40 | + |
| 41 | +class StrategyScheduler(ExecutionScheduler): |
| 42 | + """ |
| 43 | + Strategy scheduler that will function as a scheduler. |
| 44 | + """ |
| 45 | + |
| 46 | + def __init__(self): |
| 47 | + self._configured = False |
| 48 | + super(StrategyScheduler, self).__init__() |
| 49 | + |
| 50 | + def configure(self, strategies: List) -> None: |
| 51 | + self._planning = {} |
| 52 | + |
| 53 | + for strategy in strategies: |
| 54 | + self.add_execution_task( |
| 55 | + execution_id=strategy.get_id(), |
| 56 | + time_unit=strategy.get_time_unit(), |
| 57 | + interval=strategy.get_time_interval() |
| 58 | + ) |
| 59 | + |
| 60 | + self._configured = True |
| 61 | + |
| 62 | + @property |
| 63 | + def configured(self) -> bool: |
| 64 | + return self._configured |
3 | 65 |
|
4 | 66 |
|
5 | | -class StrategyState(BotState): |
| 67 | +class StrategyState(BotState, Observer): |
| 68 | + |
| 69 | + from investing_bot_framework.core.context.states.data_providing_state import DataProvidingState |
| 70 | + transition_state_class = DataProvidingState |
| 71 | + |
| 72 | + registered_strategies: List = None |
| 73 | + strategy_scheduler: StrategyScheduler = None |
| 74 | + |
| 75 | + def __init__(self, context): |
| 76 | + super(StrategyState, self).__init__(context) |
| 77 | + self._updated = False |
| 78 | + self.strategy_executor = None |
| 79 | + |
| 80 | + def _schedule_strategies(self) -> List: |
| 81 | + |
| 82 | + if not StrategyState.strategy_scheduler: |
| 83 | + StrategyState.strategy_scheduler = StrategyScheduler() |
| 84 | + |
| 85 | + if not StrategyState.strategy_scheduler.configured: |
| 86 | + StrategyState.strategy_scheduler.configure(self.registered_strategies) |
| 87 | + |
| 88 | + planning = StrategyState.strategy_scheduler.schedule_executions() |
| 89 | + planned_strategies = [] |
| 90 | + |
| 91 | + for strategy in self.registered_strategies: |
| 92 | + |
| 93 | + if strategy.get_id() in planning: |
| 94 | + planned_strategies.append(strategy) |
| 95 | + |
| 96 | + return planned_strategies |
| 97 | + |
| 98 | + def _start_strategies(self, strategies: List) -> None: |
| 99 | + |
| 100 | + self.strategy_executor = StrategyExecutor( |
| 101 | + strategies=strategies, |
| 102 | + max_workers=self.context.settings.get(SETTINGS_MAX_WORKERS, DEFAULT_MAX_WORKERS) |
| 103 | + ) |
| 104 | + |
| 105 | + if self.strategy_executor.configured: |
| 106 | + self.strategy_executor.add_observer(self) |
| 107 | + self.strategy_executor.start() |
| 108 | + else: |
| 109 | + # Skip the execution |
| 110 | + self._updated = True |
6 | 111 |
|
7 | 112 | def run(self) -> None: |
8 | | - pass |
9 | 113 |
|
10 | | - def stop(self) -> None: |
11 | | - pass |
| 114 | + if self.registered_strategies is None: |
| 115 | + raise OperationalException("Data providing state has not any data providers configured") |
| 116 | + |
| 117 | + # Schedule the strategies providers |
| 118 | + planned_strategies = self._schedule_strategies() |
| 119 | + |
| 120 | + # Execute all the strategies providers |
| 121 | + self._start_strategies(planned_strategies) |
| 122 | + |
| 123 | + # Sleep till updated |
| 124 | + while not self._updated: |
| 125 | + time.sleep(1) |
12 | 126 |
|
13 | | - def reconfigure(self) -> None: |
14 | | - pass |
| 127 | + # Collect all strategies from the strategies providers |
| 128 | + for strategies in self.strategy_executor.registered_strategies: |
| 129 | + logger.info("Data provider: {} finished running".format(strategies.get_id())) |
15 | 130 |
|
16 | 131 | def update(self, observable, **kwargs) -> None: |
17 | | - pass |
| 132 | + self._updated = True |
18 | 133 |
|
19 | | - def get_transition_state_class(self) -> Type: |
20 | | - from investing_bot_framework.core.context.states.data_provider_state import DataProviderState |
21 | | - return DataProviderState |
| 134 | + @staticmethod |
| 135 | + def register_strategies(strategies: List) -> None: |
| 136 | + StrategyState.registered_strategies = strategies |
0 commit comments