Пример #1
0
    def evaluate_start_end_size_timestamps(self, security_item):
        """
        evaluate the size for recording data
        :param security_item:
        :type security_item: str
        :return:the start,end,size need to recording,size=0 means finish recording
        :rtype:(pd.Timestamp,pd.Timestamp,int)
        """

        # get latest record
        latest_record = get_data(security_id=security_item.id,
                                 provider=self.provider,
                                 data_schema=self.data_schema, level=self.level.value,
                                 order=self.data_schema.timestamp.desc(), limit=1,
                                 return_type='domain',
                                 session=self.session)

        if latest_record:
            latest_timestamp = latest_record[0].timestamp
        else:
            latest_timestamp = security_item.timestamp

        if not latest_timestamp:
            return latest_timestamp, None, self.default_size, None

        current_time = pd.Timestamp.now()
        time_delta = current_time - latest_timestamp

        if self.level == TradingLevel.LEVEL_1DAY:
            if is_same_date(current_time, latest_timestamp):
                return latest_timestamp, None, 0, None
            return latest_timestamp, None, time_delta.days + 1, None

        close_hour, close_minute = get_close_time(security_item.id)

        # to today,check closing time
        if time_delta.days == 0:
            if latest_timestamp.hour == close_hour and latest_timestamp.minute == close_minute:
                return latest_timestamp, None, 0, None

        if self.level == TradingLevel.LEVEL_5MIN:
            if time_delta.days > 0:
                minutes = (time_delta.days + 1) * get_one_day_trading_minutes(security_item.id)
                return latest_timestamp, None, int(math.ceil(minutes / 5)) + 1, None
            else:
                return latest_timestamp, None, int(math.ceil(time_delta.total_seconds() / (5 * 60))) + 1, None
        if self.level == TradingLevel.LEVEL_1HOUR:
            if time_delta.days > 0:
                minutes = (time_delta.days + 1) * get_one_day_trading_minutes(security_item.id)
                return latest_timestamp, None, int(math.ceil(minutes / 60)) + 1, None
            else:
                return latest_timestamp, None, int(math.ceil(time_delta.total_seconds() / (60 * 60))) + 1, None
Пример #2
0
    def evaluate_start_end_size_timestamps(self, security_item):
        """
        evaluate the size for recording data
        :param security_item:
        :type security_item: str
        :return:the start,end,size need to recording,size=0 means finish recording
        :rtype:(pd.Timestamp,pd.Timestamp,int)
        """

        # get latest record
        latest_record = get_data(security_id=security_item.id,
                                 provider=self.provider,
                                 data_schema=self.data_schema,
                                 level=self.level.value,
                                 order=self.data_schema.timestamp.desc(),
                                 limit=1,
                                 return_type='domain',
                                 session=self.session)

        if latest_record:
            latest_timestamp = latest_record[0].timestamp
        else:
            latest_timestamp = security_item.timestamp

        if not latest_timestamp:
            return latest_timestamp, None, self.default_size, None

        current_time = pd.Timestamp.now()
        time_delta = current_time - latest_timestamp

        if self.level == TradingLevel.LEVEL_1DAY:
            if is_same_date(current_time, latest_timestamp):
                return latest_timestamp, None, 0, None
            return latest_timestamp, None, time_delta.days + 1, None

        close_hour, close_minute = get_close_time(security_item.id)

        # to today,check closing time
        # 0,0 means never stop,e.g,coin
        if (close_hour != 0 and close_minute != 0) and time_delta.days == 0:
            if latest_timestamp.hour == close_hour and latest_timestamp.minute == close_minute:
                return latest_timestamp, None, 0, None

        if self.kdata_use_begin_time:
            touching_timestamp = latest_timestamp + pd.Timedelta(
                seconds=self.level.to_second())
        else:
            touching_timestamp = latest_timestamp

        waiting_seconds, size = self.level.count_from_timestamp(
            touching_timestamp,
            one_day_trading_minutes=get_one_day_trading_minutes(
                security_item.id))
        if not self.one_shot and waiting_seconds and (waiting_seconds > 30):
            t = waiting_seconds / 2
            self.logger.info(
                'level:{},recorded_time:{},touching_timestamp:{},current_time:{},next_ok_time:{},just sleep:{} seconds'
                .format(
                    self.level.value, latest_timestamp, touching_timestamp,
                    current_time, touching_timestamp +
                    pd.Timedelta(seconds=self.level.to_second()), t))
            time.sleep(t)

        return latest_timestamp, None, size, None
Пример #3
0
    def run(self):
        # iterate timestamp of the min level,e.g,9:30,9:35,9.40...for 5min level
        # timestamp represents the timestamp in kdata
        for timestamp in iterate_timestamps(security_type=self.security_type, exchange=self.exchanges[0],
                                            start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp,
                                            level=self.level):

            if not is_trading_date(security_type=self.security_type, exchange=self.exchanges[0], timestamp=timestamp):
                continue
            if self.real_time:
                # all selector move on to handle the coming data
                if self.kdata_use_begin_time:
                    real_end_timestamp = timestamp + pd.Timedelta(seconds=self.level.to_second())
                else:
                    real_end_timestamp = timestamp

                waiting_seconds, _ = self.level.count_from_timestamp(real_end_timestamp,
                                                                     one_day_trading_minutes=get_one_day_trading_minutes(
                                                                         security_type=self.security_type))
                # meaning the future kdata not ready yet,we could move on to check
                if waiting_seconds and (waiting_seconds > 0):
                    # iterate the selector from min to max which in finished timestamp kdata
                    for level in self.trading_level_asc:
                        if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0],
                                                      timestamp=timestamp, level=level)):
                            for selector in self.selectors:
                                if selector.level == level:
                                    selector.move_on(timestamp, self.kdata_use_begin_time, timeout=waiting_seconds + 20)

            # on_trading_open to setup the account
            if self.level == TradingLevel.LEVEL_1DAY or (
                    self.level != TradingLevel.LEVEL_1DAY and is_open_time(security_type=self.security_type,
                                                                           exchange=self.exchanges[0],
                                                                           timestamp=timestamp)):
                self.account_service.on_trading_open(timestamp)

            # the time always move on by min level step and we could check all level targets in the slot
            self.handle_targets_slot(timestamp=timestamp)

            for level in self.trading_level_asc:
                # in every cycle, all level selector do its job in its time
                if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0],
                                              timestamp=timestamp, level=level)):
                    long_targets, short_targets = self.selectors_comparator.make_decision(timestamp=timestamp,
                                                                                          trading_level=level)

                    self.targets_slot.input_targets(level, long_targets, short_targets)

            # on_trading_close to calculate date account
            if self.level == TradingLevel.LEVEL_1DAY or (
                    self.level != TradingLevel.LEVEL_1DAY and is_close_time(security_type=self.security_type,
                                                                            exchange=self.exchanges[0],
                                                                            timestamp=timestamp)):
                self.account_service.on_trading_close(timestamp)

        self.on_finish()
Пример #4
0
    def run(self):
        # iterate timestamp of the min level,e.g,9:30,9:35,9.40...for 5min level
        # timestamp represents the timestamp in kdata
        handled_timestamp = None
        for timestamp in iterate_timestamps(security_type=self.security_type, exchange=self.exchanges[0],
                                            start_timestamp=self.start_timestamp, end_timestamp=self.end_timestamp,
                                            level=self.trading_level):
            if self.real_time and handled_timestamp:
                # all selector move on to handle the coming data
                if self.kdata_use_begin_time:
                    touching_timestamp = handled_timestamp + pd.Timedelta(seconds=self.trading_level.to_second())
                else:
                    touching_timestamp = handled_timestamp

                waiting_seconds, _ = self.trading_level.count_from_timestamp(touching_timestamp,
                                                                             one_day_trading_minutes=get_one_day_trading_minutes(
                                                                                 security_type=self.security_type))
                if waiting_seconds and (waiting_seconds > 10):
                    t = waiting_seconds / 2
                    self.logger.info(
                        'level:{},handled_timestamp:{},touching_timestamp:{},current_time:{},next_ok_time:{},just sleep:{} seconds'.format(
                            self.trading_level.value,
                            handled_timestamp,
                            touching_timestamp,
                            now_pd_timestamp(),
                            touching_timestamp + pd.Timedelta(
                                seconds=self.trading_level.to_second()),
                            t))

                    time.sleep(t)

                    for selector in self.selectors:
                        if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0],
                                                      timestamp=timestamp, level=selector.level)):
                            if self.kdata_use_begin_time:
                                to_touching_timestamp = timestamp + pd.Timedelta(
                                    seconds=selector.level.to_second())
                            else:
                                to_touching_timestamp = timestamp

                            selector.move_on(timestamp, to_touching_timestamp)

            # on_trading_open to setup the account
            if self.trading_level == TradingLevel.LEVEL_1DAY or (
                    self.trading_level != TradingLevel.LEVEL_1DAY and is_open_time(security_type=self.security_type,
                                                                                   exchange=self.exchanges[0],
                                                                                   timestamp=timestamp)):
                self.account_service.on_trading_open(timestamp)

            # the time always move on by min level step and we could check all level targets in the slot
            self.handle_targets_slot(timestamp=timestamp)

            for level in self.trading_level_asc:
                # in every cycle, all level selector do its job in its time
                if (is_in_finished_timestamps(security_type=self.security_type, exchange=self.exchanges[0],
                                              timestamp=timestamp, level=level)):
                    df = self.selectors_comparator.make_decision(timestamp=timestamp,
                                                                 trading_level=level)
                    if not df.empty:
                        selected = set(df['security_id'].to_list())
                    else:
                        selected = {}

                    self.targets_slot.input_targets(level, selected)

            handled_timestamp = timestamp

            # on_trading_close to calculate date account
            if self.trading_level == TradingLevel.LEVEL_1DAY or (
                    self.trading_level != TradingLevel.LEVEL_1DAY and is_close_time(security_type=self.security_type,
                                                                                    exchange=self.exchanges[0],
                                                                                    timestamp=timestamp)):
                self.account_service.on_trading_close(timestamp)

        self.on_finish()