def KScheduler(cls, action, tz=None, deep_sleep=list()): """ 适用于A股的信号采集实时任务,适用于跨时区任务 :param tz: :param deep_sleep: :param action: 模型的运行管理对象 :return: """ if not isinstance(action, type(ModelAction)): raise TypeError('Object action must be a subclass of ModelAction') if tz is not None: if tz not in pytz.all_timezones: raise ValueError('this tz: %s not in pytz time zones' % tz) else: tz = pytz.timezone(tz) profit_probe_period = dt.timedelta(minutes=1) profit_probe_next = dt.datetime.now( ) if tz is None else dt.datetime.now(tz=tz).replace(tzinfo=None) _f = False # 记录当天是否收盘 klines = action.klines # 需要跟踪的bar周期 _id = cls.open_kline_update_log() # 记录bar更新 from Calf.data import KlineData as kd while 1: try: crt = dt.datetime.now() if tz is None else dt.datetime.now( tz=tz).replace(tzinfo=None) if not action.is_trade_day( dt.datetime(crt.year, crt.month, crt.day)): print('{0} today is not in business'.format(crt)) time.sleep(60 * 60 * 2) # sleep two hours continue if action.trade_day_end(crt) and _f: print(fontcolor.F_GREEN + '-' * 80) print('# %s Today Overview #' % action.name) print('Signal:') action.signals_summary() print('Order:') action.orders_summary() print(fontcolor.F_GREEN + '-' * 80 + fontcolor.END) sound_notice('close.wav').start() _f = False # open_am <= crt <= close_am or open_pm <= crt < close_pm if action.trade_date(crt): # 交易日盘中 s_d = crt - dt.timedelta(minutes=3) e_d = crt + dt.timedelta(minutes=3) log = kd.read_log(start_date=s_d, end_date=e_d, status=200) if len(log): log['_id'] = log['_id'].astype('str') for i, r in log.iterrows(): if r.kline in klines and r['_id'] != _id[r.kline]: print('this kline %s find update' % r.kline) action.real(r.kline) print(r.kline + ' id update:' + _id[r.kline] + '-->' + r['_id']) _id[r.kline] = r['_id'] cls.set_kline_update_log(_id) if profit_probe_next <= crt: print('profit probing date:{0}'.format(crt)) action.probing() profit_probe_next += profit_probe_period _f = True time.sleep(5) else: print('{0} this datetime is not in business'.format(crt)) profit_probe_next = crt _f = False sleep = 60 * 5 if crt.hour in deep_sleep else 60 * 30 time.sleep(sleep) except Exception as e: ExceptionInfo(e)
def rerun(cls, action, deep_sleep, offset=None): """ 复盘演示 :param action: :param deep_sleep: :param offset: :return: """ from Calf.data import KlineData as kd profit_probe_period = dt.timedelta(minutes=1) profit_probe_next = CalfDateTime.now(offset=offset) _f = False # 记录当天是否收盘 klines = action.klines # 需要跟踪的bar周期 _id = cls.open_kline_update_log() # 记录bar更新 while 1: try: crt = CalfDateTime.now(offset=offset) if not trading.is_trade_day( dt.datetime(crt.year, crt.month, crt.day)): print('{0} today is not in business'.format(crt)) time.sleep(60 * 60 * 2) # sleep two hours continue if action.trade_day_end(crt) and _f: print(fontcolor.F_GREEN + '-' * 80) print('# %s Today Overview #' % action.name) print('Signal:') action.signals_summary() print('Order:') action.orders_summary() print(fontcolor.F_GREEN + '-' * 80 + fontcolor.END) sound_notice('close.wav').start() _f = False # open_am <= crt <= close_am or open_pm <= crt < close_pm if action.trade_date(crt): # 交易日盘中 s_d = crt - dt.timedelta(minutes=3) e_d = crt + dt.timedelta(minutes=3) log = kd.read_log(start_date=s_d, end_date=e_d, status=200) if len(log): log['_id'] = log['_id'].astype('str') for i, r in log.iterrows(): if r.kline in klines and r['_id'] != _id[r.kline]: print('this kline %s find update' % r.kline) action.real(r.kline, start_time=crt) print(r.kline + ' id update:' + _id[r.kline] + '-->' + r['_id']) _id[r.kline] = r['_id'] cls.set_kline_update_log(_id) if profit_probe_next <= crt: print('profit probing date:{0}'.format(crt)) # action.probing() profit_probe_next += profit_probe_period _f = True time.sleep(5) else: print('{0} this datetime is not in business'.format(crt)) profit_probe_next = crt _f = False # sleep = 60 * 30 # if crt.hour == 9 or crt.hour == 12: # sleep = 60 * 5 sleep = 60 * 5 if crt.hour in deep_sleep else 60 * 30 time.sleep(sleep) except Exception as e: ExceptionInfo(e)