Skip to content

Commit 5b7c393

Browse files
author
DUYN
committed
Setup States
1 parent 9724b80 commit 5b7c393

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed

bot/context/analyzing_state.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,26 @@
1+
import logging
2+
13
from bot.context.bot_state import BotState
4+
from bot.context.bot_context import BotContext
5+
6+
logger = logging.getLogger(__name__)
27

38

49
class AnalyzingState(BotState):
510

6-
def run(self):
7-
pass
11+
def __init__(self):
12+
logger.info("Initializing analyzing state ...")
13+
14+
def run(self) -> None:
15+
logger.info("Analyzing state started ...")
16+
17+
from bot.context.data_providing_state import DataProvidingState
18+
context = BotContext()
19+
context.transition_to(DataProvidingState)
20+
context.run()
821

9-
def stop(self):
22+
def stop(self) -> None:
1023
pass
1124

12-
def reconfigure(self):
25+
def reconfigure(self) -> None:
1326
pass

bot/context/bot_context.py

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import logging
2-
from typing import Dict, Type, Any
32
from pandas import DataFrame
3+
from typing import Dict, Type, Any, List
44

5-
from bot.utils import Singleton
65
from bot import OperationalException
76
from bot.data import DataProviderExecutor
87
from bot.context.bot_state import BotState
98
from bot.strategies import StrategyExecutor
9+
from bot.utils import Singleton, DataSource
10+
1011

1112
logger = logging.getLogger(__name__)
1213

@@ -23,8 +24,17 @@ class BotContext(metaclass=Singleton):
2324
"""
2425
_state: BotState = None
2526

27+
"""
28+
List of all the buying and selling orders.
29+
"""
30+
_buy_orders: DataFrame = None
31+
_sell_orders: DataFrame = None
32+
33+
"""
34+
The data sources for the bot
35+
"""
2636
_analyzed_data: DataFrame = None
27-
_raw_data_sources: Dict[str, DataFrame] = None
37+
_data_sources: List[DataSource] = None
2838

2939
_strategy_executor = None
3040
_data_provider_executor: DataProviderExecutor = None
@@ -68,12 +78,12 @@ def analyzed_data(self, data_frame: DataFrame) -> None:
6878
self._analyzed_data = data_frame
6979

7080
@property
71-
def raw_data(self) -> Dict[str, DataFrame]:
72-
return self._raw_data_sources
81+
def data_sources(self) -> List[DataSource]:
82+
return self._data_sources
7383

74-
@raw_data.setter
75-
def raw_data(self, raw_data_sources: Dict[str, DataFrame]) -> None:
76-
self._raw_data_sources = raw_data_sources
84+
@data_sources.setter
85+
def data_sources(self, data_sources: List[DataSource]) -> None:
86+
self._data_sources = data_sources
7787

7888
@property
7989
def data_provider_executor(self) -> DataProviderExecutor:

bot/context/data_providing_state.py

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,74 @@
11
import logging
22
from time import sleep
3+
from typing import List
34
from wrapt import synchronized
45

6+
from bot.utils import DataSource
57
from bot import OperationalException
68
from bot.events.observer import Observer
79
from bot.context.bot_state import BotState
810
from bot.context.bot_context import BotContext
9-
from bot.context.analyzing_state import AnalyzingState
1011
from bot.data.data_provider_executor import DataProviderExecutor
1112

1213
logger = logging.getLogger(__name__)
1314

1415

1516
class DataProvidingState(BotState, Observer):
1617

17-
def __init__(self):
18+
def __init__(self) -> None:
1819
super(DataProvidingState, self).__init__()
1920

2021
logger.info("Initializing data providing state ...")
2122

2223
# Initialize the manager
2324
context = BotContext()
2425

25-
self._executor: DataProviderExecutor = context.data_provider_executor
26-
self._executor.add_observer(self)
26+
self._data_provider_executor: DataProviderExecutor = context.data_provider_executor
27+
self._data_provider_executor.add_observer(self)
2728

2829
self._updated = False
2930
self._started = False
3031

31-
def _clean_up(self):
32+
def _clean_up(self) -> None:
3233

33-
if self._executor.processing:
34-
self._executor.stop()
34+
if self._data_provider_executor.processing:
35+
self._data_provider_executor.stop()
3536

3637
self._updated = False
3738
self._started = False
3839

39-
def run(self):
40+
def run(self) -> None:
4041
logger.info("Data providing state started ...")
4142

42-
# if not self._started:
43-
# self._executor.start()
44-
#
45-
# # Sleep till updated
46-
# while not self._updated:
47-
# sleep(1)
48-
#
49-
# if self._updated:
50-
#
51-
# # Collect all data from the data providers
52-
# data_entries = {}
53-
#
54-
# for data_provider in self._executor.registered_data_providers:
55-
# data_entries[data_provider.get_id()] = data_provider.data
56-
#
57-
# context = BotContext()
58-
# context.raw_data = data_entries
59-
# context.transition_to(AnalyzingState)
60-
# else:
61-
# raise OperationalException("Abruptly ended out of run state")
62-
63-
def stop(self):
43+
data_sources: List[DataSource] = []
44+
45+
if not self._started:
46+
self._data_provider_executor.start()
47+
48+
# Sleep till updated
49+
while not self._updated:
50+
sleep(1)
51+
52+
if self._updated:
53+
54+
# Collect all data from the data providers
55+
for data_provider in self._data_provider_executor.registered_data_providers:
56+
data_sources.append(DataSource(data_provider.get_id(), data_provider.data))
57+
58+
context = BotContext()
59+
context.data_sources = data_sources
60+
61+
# Transitioning to another state
62+
from bot.context.analyzing_state import AnalyzingState
63+
context.transition_to(AnalyzingState)
64+
context.run()
65+
else:
66+
raise OperationalException("Abruptly ended out of run state")
67+
68+
def stop(self) -> None:
6469
pass
6570

66-
def reconfigure(self):
71+
def reconfigure(self) -> None:
6772
self._clean_up()
6873

6974
@synchronized

0 commit comments

Comments
 (0)