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)
    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 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()
Example #4
0
def test_check_valid_eval_note():
    assert not check_valid_eval_note(START_PENDING_EVAL_NOTE)
    assert check_valid_eval_note(True)
    assert check_valid_eval_note({"a": 1})
    assert check_valid_eval_note({"a": 1},
                                 eval_time=1,
                                 expiry_delay=2,
                                 current_time=1)
    assert check_valid_eval_note({"a": 1},
                                 eval_time=1,
                                 expiry_delay=2,
                                 current_time=2)
    assert not check_valid_eval_note(
        {"a": 1}, eval_time=1, expiry_delay=2, current_time=3)

    assert check_valid_eval_note(INIT_EVAL_NOTE)
Example #5
0
def get_evaluations_by_evaluator(matrix_id,
                                 exchange_name=None,
                                 tentacle_type=None,
                                 cryptocurrency=None,
                                 symbol=None,
                                 time_frame=None,
                                 allow_missing=True,
                                 allowed_values=None) -> dict:
    """
    Return a dict of evaluation nodes by evaluator name
    :param matrix_id: the matrix id
    :param exchange_name: the exchange name
    :param tentacle_type: the tentacle type
    :param cryptocurrency: the currency ticker
    :param symbol: the traded pair
    :param time_frame: the evaluation time frame
    :param allow_missing: if False will raise UnsetTentacleEvaluation on missing or invalid evaluation
    :param allowed_values: a white list of allowed values not to be taken as invalid
    :return: the dict of evaluation nodes by evaluator name
    """
    evaluator_nodes = get_node_children_by_names_at_path(
        matrix_id,
        get_tentacle_path(exchange_name=exchange_name,
                          tentacle_type=tentacle_type))
    evaluations_by_evaluator = {}
    for evaluator_name, node in evaluator_nodes.items():
        evaluation = get_tentacles_value_nodes(matrix_id, [node],
                                               cryptocurrency=cryptocurrency,
                                               symbol=symbol,
                                               time_frame=time_frame)
        if len(evaluation) > 1:
            logging.get_logger("matrix_manager").warning(
                "More than one evaluation corresponding to the given tentacle filter, "
                "this means there is an issue in this methods given arguments")
        elif evaluation:
            eval_value = evaluation[0].node_value
            if (allowed_values is not None and eval_value in allowed_values) or \
                    evaluators_util.check_valid_eval_note(eval_value):
                evaluations_by_evaluator[evaluator_name] = evaluation[0]
            elif not allow_missing:
                raise errors.UnsetTentacleEvaluation(
                    f"Missing {time_frame if time_frame else 'evaluation'} "
                    f"for {evaluator_name} on {symbol}, evaluation is "
                    f"{repr(eval_value)}).")
    return evaluations_by_evaluator
    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}")
Example #7
0
def test_check_valid_eval_note():
    assert not check_valid_eval_note(START_PENDING_EVAL_NOTE)
    assert not check_valid_eval_note(math.nan)
    assert not check_valid_eval_note(np.nan)

    assert check_valid_eval_note(INIT_EVAL_NOTE)