Skip to content

Commit

Permalink
[Tentacles] add historical config
Browse files Browse the repository at this point in the history
GuillaumeDSM committed Nov 23, 2024
1 parent fd51466 commit 4ad526d
Showing 3 changed files with 60 additions and 0 deletions.
7 changes: 7 additions & 0 deletions octobot_commons/configuration/__init__.py
Original file line number Diff line number Diff line change
@@ -19,6 +19,7 @@
from octobot_commons.configuration import fields_utils
from octobot_commons.configuration import user_inputs
from octobot_commons.configuration import user_input_configuration
from octobot_commons.configuration import historical_configuration


from octobot_commons.configuration.configuration import (
@@ -59,6 +60,10 @@
get_raw_config_and_user_inputs,
load_and_save_user_inputs,
)
from octobot_commons.configuration.historical_configuration import (
add_historical_tentacle_config,
get_historical_tentacle_config,
)


__all__ = [
@@ -88,4 +93,6 @@
"get_raw_config_and_user_inputs_from_class",
"get_raw_config_and_user_inputs",
"load_and_save_user_inputs",
"add_historical_tentacle_config",
"get_historical_tentacle_config",
]
52 changes: 52 additions & 0 deletions octobot_commons/configuration/historical_configuration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Drakkar-Software OctoBot-Commons
# Copyright (c) Drakkar-Software, All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3.0 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library.
import octobot_commons.constants as constants


def add_historical_tentacle_config(
master_config: dict, config_start_time: float, historical_config: dict
):
"""
Adds the given historical_config to the master_config historical configurations
"""
if constants.CONFIG_HISTORICAL_CONFIGURATION not in master_config:
master_config[constants.CONFIG_HISTORICAL_CONFIGURATION] = []
# use list to ensure it still can be serialized
master_config[constants.CONFIG_HISTORICAL_CONFIGURATION].append(
[config_start_time, historical_config]
)
# always keep the most recent first to be able to find the most up to date first when iterating
master_config[constants.CONFIG_HISTORICAL_CONFIGURATION].sort(
key=lambda x: x[0], reverse=True
)


def get_historical_tentacle_config(master_config: dict, current_time: float) -> dict:
"""
:return: the historical configuration associated to the given time
"""
try:
for config_start_time_and_config in master_config[
constants.CONFIG_HISTORICAL_CONFIGURATION
]:
if config_start_time_and_config[0] <= current_time:
return config_start_time_and_config[1]
# no suitable config found: fallback to the oldest config
return master_config[constants.CONFIG_HISTORICAL_CONFIGURATION][-1][1]
except KeyError:
raise KeyError(
f"{constants.CONFIG_HISTORICAL_CONFIGURATION} not found in master_config."
)
1 change: 1 addition & 0 deletions octobot_commons/constants.py
Original file line number Diff line number Diff line change
@@ -154,6 +154,7 @@
EVALUATOR_PRIORITY = "priority"
DEFAULT_EVALUATOR_PRIORITY = 0
CONFIG_TENTACLES_REQUIRED_CANDLES_COUNT = "required_candles_count"
CONFIG_HISTORICAL_CONFIGURATION = "_historical_configuration"
NESTED_TENTACLE_CONFIG = "nested_tentacle_configuration"
CONFIG_ACTIVATION_TOPICS = "activation method"
CONFIG_TRIGGER_TIMEFRAMES = "Trigger_timeframes"

0 comments on commit 4ad526d

Please sign in to comment.