async def set_final_eval(self, matrix_id: str, cryptocurrency: str,
                             symbol: str, time_frame):
        strategies_analysis_note_counter = 0
        evaluation = commons_constants.INIT_EVAL_NOTE
        # Strategies analysis
        for evaluated_strategy_node in matrix.get_tentacles_value_nodes(
                matrix_id,
                matrix.get_tentacle_nodes(
                    matrix_id,
                    exchange_name=self.exchange_name,
                    tentacle_type=evaluators_enums.EvaluatorMatrixTypes.
                    STRATEGIES.value),
                cryptocurrency=cryptocurrency,
                symbol=symbol):

            if evaluators_util.check_valid_eval_note(
                    evaluators_api.get_value(evaluated_strategy_node),
                    evaluators_api.get_type(evaluated_strategy_node),
                    evaluators_constants.EVALUATOR_EVAL_DEFAULT_TYPE):
                evaluation += evaluators_api.get_value(
                    evaluated_strategy_node
                )  # TODO * evaluated_strategies.get_pertinence()
                strategies_analysis_note_counter += 1  # TODO evaluated_strategies.get_pertinence()

        if strategies_analysis_note_counter > 0:
            self.final_eval = evaluation / strategies_analysis_note_counter
            await self.create_state(cryptocurrency=cryptocurrency,
                                    symbol=symbol)
Esempio n. 2
0
def _get_tentacles_values(evaluations, tentacle_type_node, exchange):
    try:
        import octobot_evaluators.api as evaluators_api
    except ImportError:
        logging.get_logger("InterfaceUtil").error(
            "_get_tentacles_values requires OctoBot-Evaluators package installed"
        )
        return {}
    for tentacle_name, tentacle_name_node in evaluators_api.get_children_list(
            tentacle_type_node).items():
        evaluations[exchange][tentacle_name] = {}
        for cryptocurrency, cc_node in evaluators_api.get_children_list(
                tentacle_name_node).items():
            evaluations[exchange][tentacle_name][cryptocurrency] = {}
            if evaluators_api.has_children(cc_node):
                for symbol, symbol_node in evaluators_api.get_children_list(
                        cc_node).items():
                    if evaluators_api.has_children(symbol_node):
                        evaluations[exchange][tentacle_name][symbol] = {}
                        for time_frame, time_frame_node in evaluators_api.get_children_list(
                                symbol_node).items():
                            evaluations[exchange][tentacle_name][symbol][time_frame] = \
                                evaluators_api.get_value(time_frame_node)
                    else:
                        evaluations[exchange][tentacle_name][
                            symbol] = evaluators_api.get_value(symbol_node)
            else:
                evaluations[exchange][tentacle_name][
                    cryptocurrency] = evaluators_api.get_value(cc_node)
 async def set_final_eval(self, matrix_id: str, cryptocurrency: str, symbol: str, time_frame):
     # Strategies analysis
     for evaluated_strategy_node in matrix.get_tentacles_value_nodes(
             matrix_id,
             matrix.get_tentacle_nodes(matrix_id,
                                       exchange_name=self.exchange_name,
                                       tentacle_type=evaluators_enums.EvaluatorMatrixTypes.STRATEGIES.value,
                                       tentacle_name=Strategies.DipAnalyserStrategyEvaluator.get_name()),
             symbol=symbol):
         if evaluators_util.check_valid_eval_note(evaluators_api.get_value(evaluated_strategy_node),
                                                  evaluators_api.get_type(evaluated_strategy_node),
                                                  Strategies.DipAnalyserStrategyEvaluator.get_eval_type()):
             self.final_eval = evaluators_api.get_value(evaluated_strategy_node)
             await self.create_state()
    async def matrix_callback(self, matrix_id, evaluator_name, evaluator_type,
                              eval_note, eval_note_type, exchange_name,
                              cryptocurrency, symbol, time_frame):
        if evaluator_type not in self.allowed_evaluator_types:
            # only wake up on relevant callbacks
            return

        try:
            TA_by_timeframe = {
                available_time_frame: matrix.get_evaluations_by_evaluator(
                    matrix_id,
                    exchange_name,
                    evaluators_enums.EvaluatorMatrixTypes.TA.value,
                    cryptocurrency,
                    symbol,
                    available_time_frame.value,
                    allow_missing=False,
                    allowed_values=[commons_constants.START_PENDING_EVAL_NOTE])
                for available_time_frame in self.strategy_time_frames
            }

            if evaluator_type == evaluators_enums.EvaluatorMatrixTypes.REAL_TIME.value:
                # trigger re-evaluation
                exchange_id = trading_api.get_exchange_id_from_matrix_id(
                    exchange_name, matrix_id)
                await evaluators_channel.trigger_technical_evaluators_re_evaluation_with_updated_data(
                    matrix_id, evaluator_name, evaluator_type, exchange_name,
                    cryptocurrency, symbol, exchange_id,
                    self.strategy_time_frames)
                # do not continue this evaluation
                return

            total_evaluation = 0
            total_weights = 0

            for time_frame, eval_by_ta in TA_by_timeframe.items():
                for evaluation in eval_by_ta.values():
                    eval_value = evaluators_api.get_value(evaluation)
                    if evaluators_util.check_valid_eval_note(
                            eval_value,
                            eval_type=evaluators_api.get_type(evaluation),
                            expected_eval_type=evaluators_constants.
                            EVALUATOR_EVAL_DEFAULT_TYPE):
                        weight = self.weight_by_time_frames.get(
                            time_frame.value, self.DEFAULT_WEIGHT)
                        total_evaluation += eval_value * weight
                        total_weights += weight

            if total_weights > 0:
                self.eval_note = total_evaluation / total_weights
                await self.strategy_completed(cryptocurrency, symbol)

        except errors.UnsetTentacleEvaluation as e:
            self.logger.error(f"Missing technical evaluator data for ({e})")
    async def _trigger_evaluation(self, matrix_id, evaluator_name,
                                  evaluator_type, eval_note, eval_note_type,
                                  exchange_name, cryptocurrency, symbol):
        # ensure only start evaluations when technical evaluators have been initialized
        try:
            TA_by_timeframe = {
                available_time_frame: matrix.get_evaluations_by_evaluator(
                    matrix_id,
                    exchange_name,
                    evaluators_enums.EvaluatorMatrixTypes.TA.value,
                    cryptocurrency,
                    symbol,
                    available_time_frame.value,
                    allow_missing=False,
                    allowed_values=[commons_constants.START_PENDING_EVAL_NOTE])
                for available_time_frame in self.strategy_time_frames
            }
            # social evaluators by symbol
            social_evaluations_by_evaluator = matrix.get_evaluations_by_evaluator(
                matrix_id, exchange_name,
                evaluators_enums.EvaluatorMatrixTypes.SOCIAL.value,
                cryptocurrency, symbol)
            # social evaluators by crypto currency
            social_evaluations_by_evaluator.update(
                matrix.get_evaluations_by_evaluator(
                    matrix_id, exchange_name,
                    evaluators_enums.EvaluatorMatrixTypes.SOCIAL.value,
                    cryptocurrency))
            available_rt_time_frames = self.get_available_time_frames(
                matrix_id, exchange_name,
                evaluators_enums.EvaluatorMatrixTypes.REAL_TIME.value,
                cryptocurrency, symbol)
            RT_evaluations_by_time_frame = {
                available_time_frame: matrix.get_evaluations_by_evaluator(
                    matrix_id, exchange_name,
                    evaluators_enums.EvaluatorMatrixTypes.REAL_TIME.value,
                    cryptocurrency, symbol, available_time_frame)
                for available_time_frame in available_rt_time_frames
            }
            if self.re_evaluate_TA_when_social_or_realtime_notification \
                    and any(value for value in TA_by_timeframe.values()) \
                    and evaluator_type != evaluators_enums.EvaluatorMatrixTypes.TA.value \
                    and evaluator_type in self.re_evaluation_triggering_eval_types \
                    and evaluator_name not in self.background_social_evaluators:
                if evaluators_util.check_valid_eval_note(
                        eval_note,
                        eval_type=eval_note_type,
                        expected_eval_type=evaluators_constants.
                        EVALUATOR_EVAL_DEFAULT_TYPE):
                    # trigger re-evaluation
                    exchange_id = trading_api.get_exchange_id_from_matrix_id(
                        exchange_name, matrix_id)
                    await evaluators_channel.trigger_technical_evaluators_re_evaluation_with_updated_data(
                        matrix_id, evaluator_name, evaluator_type,
                        exchange_name, cryptocurrency, symbol, exchange_id,
                        self.strategy_time_frames)
                    # do not continue this evaluation
                    return
            counter = 0
            total_evaluation = 0

            for eval_by_rt in RT_evaluations_by_time_frame.values():
                for evaluation in eval_by_rt.values():
                    eval_value = evaluators_api.get_value(evaluation)
                    if evaluators_util.check_valid_eval_note(
                            eval_value,
                            eval_type=evaluators_api.get_type(evaluation),
                            expected_eval_type=evaluators_constants.
                            EVALUATOR_EVAL_DEFAULT_TYPE):
                        total_evaluation += eval_value
                        counter += 1

            for eval_by_ta in TA_by_timeframe.values():
                for evaluation in eval_by_ta.values():
                    eval_value = evaluators_api.get_value(evaluation)
                    if evaluators_util.check_valid_eval_note(
                            eval_value,
                            eval_type=evaluators_api.get_type(evaluation),
                            expected_eval_type=evaluators_constants.
                            EVALUATOR_EVAL_DEFAULT_TYPE):
                        total_evaluation += eval_value
                        counter += 1

            if social_evaluations_by_evaluator:
                exchange_manager = trading_api.get_exchange_manager_from_exchange_name_and_id(
                    exchange_name,
                    trading_api.get_exchange_id_from_matrix_id(
                        exchange_name, self.matrix_id))
                current_time = trading_api.get_exchange_current_time(
                    exchange_manager)
                for evaluation in social_evaluations_by_evaluator.values():
                    eval_value = evaluators_api.get_value(evaluation)
                    if evaluators_util.check_valid_eval_note(
                            eval_value,
                            eval_type=evaluators_api.get_type(evaluation),
                            expected_eval_type=evaluators_constants.
                            EVALUATOR_EVAL_DEFAULT_TYPE,
                            eval_time=evaluators_api.get_time(evaluation),
                            expiry_delay=self.
                            social_evaluators_default_timeout,
                            current_time=current_time):
                        total_evaluation += eval_value
                        counter += 1

            if counter > 0:
                self.eval_note = total_evaluation / counter
                await self.strategy_completed(cryptocurrency, symbol)

        except errors.UnsetTentacleEvaluation as e:
            if evaluator_type == evaluators_enums.EvaluatorMatrixTypes.TA.value:
                self.logger.error(
                    f"Missing technical evaluator data for ({e})")
            # otherwise it's a social or real-time evaluator, it will shortly be taken into account by TA update cycle
        except Exception as e:
            self.logger.exception(
                e, True, f"Error when computing strategy evaluation: {e}")