def QA_util_make_min_index(day, type_='1min'): """ explanation: 创建股票分钟线的index params: * day -> 含义:天数 类型:int 参数支持: [] * type_ -> 含义: 周期索引 类型: str 参数支持: ["1min"] """ if QA_util_if_trade(day) is True: return pd.date_range( str(day) + ' 09:30:00', str(day) + ' 11:30:00', freq=type_, closed='right' ).append( pd.date_range( str(day) + ' 13:00:00', str(day) + ' 15:00:00', freq=type_, closed='right' ) ) else: return pd.DataFrame(['No trade'])
def job111(trading_date): if datetime.strptime(trading_date, "%Y-%m-%d").weekday() == 4: train(trading_date, working_dir=working_dir) else: pass if QA_util_if_trade(trading_date): predict(trading_date) else: predict(QA_util_get_real_date(trading_date))
def QA_util_make_hour_index(day, type_='1h'): if QA_util_if_trade(day) is True: return pd.date_range(str(day) + ' 09:30:00', str(day) + ' 11:30:00', freq=type_, closed='right').append( pd.date_range(str(day) + ' 13:00:00', str(day) + ' 15:00:00', freq=type_, closed='right')) else: return pd.DataFrame(['No trade'])
def QA_util_make_hour_index(day, type_='1h'): """创建股票的小时线的index Arguments: day {[type]} -- [description] Returns: [type] -- [description] """ if QA_util_if_trade(day) is True: return pd.date_range(str(day) + ' 09:30:00', str(day) + ' 11:30:00', freq=type_, closed='right').append( pd.date_range(str(day) + ' 13:00:00', str(day) + ' 15:00:00', freq=type_, closed='right')) else: return pd.DataFrame(['No trade'])
def QA_util_make_future_min_index(day, type_='1min'): """创建期货分钟线的index Arguments: day {[type]} -- [description] Returns: [type] -- [description] """ if QA_util_if_trade(day) is True: return pd.date_range(str(day) + '21:00:00', str(day) + '', freq=type_, closed='right').append( pd.date_range(str(day) + ' 13:00:00', str(day) + ' 15:00:00', freq=type_, closed='right')) else: return pd.DataFrame(['No trade'])
def QA_fetch_stock_target(codes, start_date, end_date, type='close'): if QA_util_if_trade(end_date): pass else: end_date = QA_util_get_real_date(end_date) end = QA_util_get_next_datetime(end_date, 5) rng1 = pd.Series(pd.date_range(start_date, end_date, freq='D')).apply(lambda x: str(x)[0:10]) data = QA.QA_fetch_stock_day_adv(codes, start_date, end) market = QA.QA_fetch_index_day(['000001'], start_date, end, format='pd')['close'].reset_index() market = index_pct(market)[[ 'date', 'INDEX_TARGET', 'INDEX_TARGET3', 'INDEX_TARGET4', 'INDEX_TARGET5', 'INDEX_TARGET10' ]] res1 = data.to_qfq().data res1.columns = [x + '_qfq' for x in res1.columns] data = data.data.join(res1).fillna(0).reset_index() res = data.groupby('code').apply(pct, type=type)[[ 'date', 'code', 'PRE_DATE', 'OPEN_MARK', 'PASS_MARK', 'TARGET', 'TARGET3', 'TARGET4', 'TARGET5', 'TARGET10', 'AVG_TARGET' ]] res = pd.merge(res, market, on='date') res['date'] = res['date'].apply(lambda x: str(x)[0:10]) res['next_date'] = res['date'].apply( lambda x: QA_util_get_pre_trade_date(x, -2)) res['PRE_DATE'] = res['PRE_DATE'].apply(lambda x: str(x)[0:10]) res = res.set_index(['date', 'code']).loc[rng1] res['INDEX_TARGET'] = res['TARGET'] - res['INDEX_TARGET'] res['INDEX_TARGET3'] = res['TARGET3'] - res['INDEX_TARGET3'] res['INDEX_TARGET4'] = res['TARGET4'] - res['INDEX_TARGET4'] res['INDEX_TARGET5'] = res['TARGET5'] - res['INDEX_TARGET5'] res['INDEX_TARGET10'] = res['TARGET10'] - res['INDEX_TARGET10'] for columnname in res.columns: if res[columnname].dtype == 'float64': res[columnname] = res[columnname].astype('float16') if res[columnname].dtype == 'int64': res[columnname] = res[columnname].astype('int8') return (res)
def QA_fetch_index_target(codes, start_date, end_date): if QA_util_if_trade(end_date): pass else: end_date = QA_util_get_real_date(end_date) end = QA_util_get_next_datetime(end_date, 5) rng1 = pd.Series(pd.date_range(start_date, end_date, freq='D')).apply(lambda x: str(x)[0:10]) data = QA.QA_fetch_index_day_adv(codes, start_date, end).data.fillna(0).reset_index() res = data.groupby('code').apply(index_pct)[[ 'date', 'code', 'INDEX_TARGET', 'INDEX_TARGET3', 'INDEX_TARGET4', 'INDEX_TARGET5', 'INDEX_TARGET10' ]] res['date'] = res['date'].apply(lambda x: str(x)[0:10]) res = res.set_index(['date', 'code']).loc[rng1] for columnname in res.columns: if res[columnname].dtype == 'float64': res[columnname] = res[columnname].astype('float16') if res[columnname].dtype == 'int64': res[columnname] = res[columnname].astype('int8') return (res)
def util_is_trade_time( _time=datetime.datetime.now(), market=MARKET_TYPE.STOCK_CN, code=None): """判断当前是否为交易时间""" date_today = _time.isoformat()[0:10] if market is MARKET_TYPE.STOCK_CN: if QA_util_if_trade(date_today): if _time.hour in [10, 13, 14]: return True elif _time.hour == 9 and _time.minute >= 15: # 修改成9:15 加入 9:15-9:30的盘前竞价时间 return True elif _time.hour == 11 and _time.minute <= 32: # 11:30 -> 11:31 也刷新数据 return True # elif _time.hour == 12 and _time.minute >= 58: # 12:58 - 13:00 也刷新数据 # return True elif _time.hour == 15 and _time.minute <= 2: # 15:00 - 15:02 也刷新数据 return True else: return False else: return False elif market is MARKET_TYPE.FUTURE_CN: date_yesterday = str((_time - datetime.timedelta(days=1)).date()) is_today_open = QA_util_if_trade(date_today) is_yesterday_open = QA_util_if_trade(date_yesterday) # 考虑周六日的期货夜盘情况 if is_today_open == False: # 可能是周六或者周日 if is_yesterday_open == False or (_time.hour > 2 or _time.hour == 2 and _time.minute > 30): return False shortName = "" # i , p for i in range(len(code)): ch = code[i] if ch.isdigit(): # ch >= 48 and ch <= 57: break shortName += code[i].upper() period = [[9, 0, 10, 15], [10, 30, 11, 30], [13, 30, 15, 0]] if (shortName in ["IH", 'IF', 'IC']): period = [[9, 30, 11, 30], [13, 0, 15, 0]] elif (shortName in ["T", "TF"]): period = [[9, 15, 11, 30], [13, 0, 15, 15]] if 0 <= _time.weekday <= 4: for i in range(len(period)): p = period[i] if ((_time.hour > p[0] or (_time.hour == p[0] and _time.minute >= p[1])) and (_time.hour < p[2] or (_time.hour == p[2] and _time.minute < p[3]))): return True # 最新夜盘时间表_2019.03.29 nperiod = [ [['AU', 'AG', 'SC'], [21, 0, 2, 30]], [['CU', 'AL', 'ZN', 'PB', 'SN', 'NI'], [21, 0, 1, 0]], [['RU', 'RB', 'HC', 'BU', 'FU', 'SP'], [21, 0, 23, 0]], [[ 'A', 'B', 'Y', 'M', 'JM', 'J', 'P', 'I', 'L', 'V', 'PP', 'EG', 'C', 'CS' ], [21, 0, 23, 0]], [['SR', 'CF', 'RM', 'MA', 'TA', 'ZC', 'FG', 'IO', 'CY'], [21, 0, 23, 30]], ] for i in range(len(nperiod)): for j in range(len(nperiod[i][0])): if nperiod[i][0][j] == shortName: p = nperiod[i][1] condA = _time.hour > p[0] or (_time.hour == p[0] and _time.minute >= p[1]) condB = _time.hour < p[2] or (_time.hour == p[2] and _time.minute < p[3]) # in one day if p[2] >= p[0]: if ((_time.weekday >= 0 and _time.weekday <= 4) and condA and condB): return True else: if (((_time.weekday >= 0 and _time.weekday <= 4) and condA) or ((_time.weekday >= 1 and _time.weekday <= 5) and condB)): return True return False return False