예제 #1
0
def timeline():
    now = datetime.now()
    offset = timedelta.days(-15)
    # the offset for new date
    limit_date = (now + offset)
    date_lookup = Booking.query("date_added").filter(Booking.date_added.between(limit_date, now)).all()
    #  sort data using pandas
    date_data = bookings_schema.dump(date_lookup)
    return date_data
예제 #2
0
    def _run_seven(self):
        '''7*24'''
        while True:
            day = datetime.now().strftime('%Y%m%d')
            left_days = list(filter(lambda x: x > day, self.trading_days))
            if len(left_days) == 0:
                self.cfg.log.info('读取交易日历...')
                self.get_actionday()
                self.cfg.log.info('读取交易日历完成')
                left_days = list(filter(lambda x: x > day, self.trading_days))
            next_trading_day = left_days[0]
            has_hight = (datetime.strptime(next_trading_day, '%Y%m%d') -
                         datetime.strptime(day, '%Y%m%d')).days in [1, 3]

            now_time = datetime.now().strftime('%H%M%S')
            if not self.t.logined:
                # 当前非交易日
                sleep_seconds = 0
                if day not in self.trading_days:
                    # 昨天有夜盘:今天凌晨有数据
                    if now_time <= '020000' and (
                            datetime.today() + timedelta.days(-1)
                    ).strftime('%Y%m%d') in self.trading_days:
                        time.sleep(1)
                    else:
                        self.cfg.log.info('{} is not tradingday.'.format(day))
                        self.cfg.log.info(
                            'continue after {}'.format(next_trading_day +
                                                       ' 08:30:00'))
                        sleep_seconds = (int)((datetime.strptime(
                            next_trading_day + '08:31:00', '%Y%m%d%H:%M:%S') -
                                               datetime.now()).total_seconds())
                elif now_time <= '083000':
                    self.cfg.log.info('continue after {}'.format(day +
                                                                 ' 08:30:00'))
                    sleep_seconds = (int)((
                        datetime.strptime(day + '08:31:00', '%Y%m%d%H:%M:%S') -
                        datetime.now()).total_seconds())
                elif now_time >= '150000':
                    if has_hight:
                        if datetime.now().strftime('%H%M%S') < '203000':
                            self.cfg.log.info(
                                'continue after {}'.format(day + ' 20:30:00'))
                            sleep_seconds = (int)(
                                (datetime.strptime(day + '20:31:00',
                                                   '%Y%m%d%H:%M:%S') -
                                 datetime.now()).total_seconds())
                    else:
                        self.cfg.log.info(
                            'continue after {}'.format(next_trading_day +
                                                       ' 08:30:00'))
                        sleep_seconds = (int)((datetime.strptime(
                            next_trading_day + '08:31:00', '%Y%m%d%H:%M:%S') -
                                               datetime.now()).total_seconds())
                if sleep_seconds > 0:
                    time.sleep(sleep_seconds)
                    sleep_seconds = 0
                # 启动接口
                self.start_api()
                time.sleep(10)
                # 已收盘
            # elif sum([1 if x != InstrumentStatus.Closed else 0 for x in self.t.instrument_status.values()]) == 0:
            #     self.t.ReqUserLogout()
            #     self.q.ReqUserLogout()
            #     if has_hight:
            #         self.cfg.log.info('continue after {}'.format(day + ' 20:30:00'))
            #         time.sleep((datetime.strptime(day + '20:31:00', '%Y%m%d%H:%M:%S') - datetime.now()).total_seconds())
            #     else:
            #         self.cfg.log.info('continue after {}'.format(next_trading_day + ' 08:30:00'))
            #         time.sleep((datetime.strptime(next_trading_day + '08:31:00', '%Y%m%d%H:%M:%S') - datetime.now()).total_seconds())
            # # 夜盘全部非交易
            # elif now_time < '030000' and sum([1 if x == InstrumentStatus.Continous else 0 for x in self.t.instrument_status.values()]) == 0:
            #     cur_trading_day = self.t.tradingday
            #     self.t.ReqUserLogout()
            #     self.q.ReqUserLogout()
            #     # cur_trading_day = self.trading_days[self.trading_days.index(next_trading_day) - 1] 周末时取值不对
            #     self.cfg.log.info('continue after {}'.format(cur_trading_day + ' 08:30:00'))
            #     time.sleep((datetime.strptime(cur_trading_day + '08:31:00', '%Y%m%d%H:%M:%S') - datetime.now()).total_seconds())
            else:
                time.sleep(60 * 10)
예제 #3
0
 def test_datetime(self):
     print(datetime.now().timetuple())
     print(timedelta.days(1))
     pass
예제 #4
0
    def _run_seven(self):
        print_time = ''
        while True:
            day = datetime.now().strftime('%Y%m%d')
            left_days = list(filter(lambda x: x > day, self.trading_days))
            if len(left_days) == 0:
                self.get_actionday()
                self.get_trading_time()
                left_days = list(filter(lambda x: x > day, self.trading_days))
            next_trading_day = left_days[0]
            has_hight = (datetime.strptime(next_trading_day, '%Y%m%d') -
                         datetime.strptime(day, '%Y%m%d')).days in [1, 3]

            now_time = datetime.now().strftime('%H%M%S')
            if not self.t.logined:
                # 当前非交易日
                if day not in self.trading_days:
                    # 昨天有夜盘:今天凌晨有数据
                    if now_time <= '020000' and (
                            datetime.today() + timedelta.days(-1)
                    ).strftime('%Y%m%d') in self.trading_days:
                        sleep(1)
                    else:
                        cfg.log.info('{} is not tradingday.'.format(day))
                        cfg.log.info(
                            'continue after {}'.format(next_trading_day +
                                                       ' 08:30:00'))
                        sleep((datetime.strptime(next_trading_day + '08:31:00',
                                                 '%Y%m%d%H:%M:%S') -
                               datetime.now()).total_seconds())
                elif now_time <= '083000':
                    cfg.log.info('continue after {}'.format(day + ' 08:30:00'))
                    sleep((
                        datetime.strptime(day + '08:31:00', '%Y%m%d%H:%M:%S') -
                        datetime.now()).total_seconds())
                elif now_time >= '153000':
                    if has_hight:
                        if datetime.now().strftime('%H%M%S') < '203000':
                            cfg.log.info(
                                'continue after {}'.format(day + ' 20:30:00'))
                            sleep((datetime.strptime(day + '20:31:00',
                                                     '%Y%m%d%H:%M:%S') -
                                   datetime.now()).total_seconds())
                    else:
                        cfg.log.info(
                            'continue after {}'.format(next_trading_day +
                                                       ' 08:30:00'))
                        sleep((datetime.strptime(next_trading_day + '08:31:00',
                                                 '%Y%m%d%H:%M:%S') -
                               datetime.now()).total_seconds())
                self.run()
                sleep(10)
            # 已收盘
            elif sum([
                    1 if x != InstrumentStatus.Closed else 0
                    for x in self.t.instrument_status.values()
            ]) == 0:
                self.t.ReqUserLogout()
                self.q.ReqUserLogout()
                if has_hight:
                    cfg.log.info('continue after {}'.format(day + ' 20:30:00'))
                    sleep((
                        datetime.strptime(day + '20:31:00', '%Y%m%d%H:%M:%S') -
                        datetime.now()).total_seconds())
                else:
                    cfg.log.info('continue after {}'.format(next_trading_day +
                                                            ' 08:30:00'))
                    sleep((datetime.strptime(next_trading_day + '08:31:00',
                                             '%Y%m%d%H:%M:%S') -
                           datetime.now()).total_seconds())
                # if cfg.mongo:
                #     cfg.mongo.conn.drop_database('future_real_min')
                if cfg.rds:
                    cfg.rds.flushdb()

            # 夜盘全部非交易
            elif now_time < '030000' and sum([
                    1 if x == InstrumentStatus.Continous else 0
                    for x in self.t.instrument_status.values()
            ]) == 0:
                cur_trading_day = self.t.tradingday
                self.t.ReqUserLogout()
                self.q.ReqUserLogout()
                # cur_trading_day = self.trading_days[self.trading_days.index(next_trading_day) - 1] 周末时取值不对
                cfg.log.info('continue after {}'.format(cur_trading_day +
                                                        ' 08:30:00'))
                sleep((datetime.strptime(cur_trading_day + '08:31:00',
                                         '%Y%m%d%H:%M:%S') -
                       datetime.now()).total_seconds())
            else:
                # 没有行情时不会显示
                if print_time != self.tick_time:
                    actionday = self.TradingDay
                    if self.tick_time[0:2] > '20':
                        actionday = self.Actionday
                    elif self.tick_time[0:2] < '04':
                        actionday = self.Actionday1
                    print_time = self.tick_time
                    cfg.log.info('tick time:{} [diff]{}s'.format(
                        print_time, (datetime.strptime(actionday + print_time,
                                                       '%Y%m%d%H:%M:%S') -
                                     datetime.now()).total_seconds()))
                sleep(60)