示例#1
0
 def _get_exchange_current_time(self, exchange_name, matrix_id):
     try:
         import octobot_trading.api as exchange_api
         exchange_manager = exchange_api.get_exchange_manager_from_exchange_name_and_id(
             exchange_name,
             exchange_api.get_exchange_id_from_matrix_id(exchange_name, matrix_id)
         )
         return exchange_api.get_exchange_current_time(exchange_manager)
     except ImportError:
         self.logger.error("Strategy requires OctoBot-Trading package installed")
示例#2
0
 def get_current_exchange_time(self):
     try:
         import octobot_trading.api as exchange_api
         if self.exchange_id is not None:
             return exchange_api.get_exchange_current_time(
                 exchange_api.get_exchange_manager_from_exchange_name_and_id(
                     self.exchange_name,
                     self.exchange_id
                 )
             )
     except ImportError:
         self.logger.error(f"Can't get current exchange time: requires OctoBot-Trading package installed")
     return None
    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}")