def get_recent_dividend_share_stocks(diff_days: int = 5) -> []: """ 获取最近进行分红除权的股票代码列表 Parameters ------ diff_days: int x天内是否个股进行过分红除权 Return ------- array ts_code ts股票代码 """ param = {'list_status': 'L', 'exchange': '', 'fields': 'ts_code,sumbol'} hs_df = get_hs_stock_list(param) ret_jsons = None stock_list = [] for index, row in hs_df.iterrows(): code = row['symbol'] try: is_dividend = is_stock_recent_dividend(code, diff_days) if is_dividend: stock_list.append(row['ts_code']) time.sleep(1) except Exception as err: logger.critical(err) continue logger.info('dividend stock list:') logger.info(stock_list) return stock_list
def test_stock_list_hs300(): df = basic.get_hs_stock_list({ 'list_status': 'L', 'exchange': '', 'fields': 'ts_code,symbol,list_date' }) assert df[df.ts_code == '399300.SZ']['symbol'].iloc[0] == '399300'
def run_his_stock_adj_price_task(past_diff_days, exchange=''): """ 更新沪深股市上市公司前复权股票历史价格 Parameters ------ past_diff_days: 查询x天前的股票复权数据 exchange: 交易所 SSE上交所 SZSE深交所 Return ------- result 是否正常结束 """ param = { 'list_status': 'L', 'exchange': exchange, 'fields': 'ts_code,symbol,list_date' } hs_df = basic.get_hs_stock_list(param) delta = datetime.timedelta(days=past_diff_days) past_str = (datetime.date.today() - delta).strftime('%Y%m%d') today_str = datetime.date.today().strftime('%Y%m%d') for index, row in hs_df.iterrows(): ts_code = row['ts_code'].split('.')[0] logger.info(f'{ts_code} start fqprice job {today_str}') adj_price_df = price.get_adj_price({ 'ts_code': row['ts_code'], 'start_date': past_str, 'end_date': today_str, 'adj': 'qfq', 'freq': 'D', 'asset': 'I' if str(row['ts_code']).startswith('399') else 'E' }) if adj_price_df is None: continue for index, row in adj_price_df.iterrows(): trade_date = row['trade_date'] _id = f'{ts_code}-{trade_date}' data = { '_id': _id, 'code': ts_code, 'date': int(row['trade_date']), 'open': float('%.2f' % row['open']), 'close': float('%.2f' % row['close']), 'high': float('%.2f' % row['high']), 'low': float('%.2f' % row['low']), 'pre_close': float('%.2f' % row['pre_close']), 'change': float('%.2f' % row['change']), 'pct_chg': float('%.2f' % row['pct_chg']), 'vol': float('%.2f' % row['vol']), 'amout': float('%.3f' % row['amount']) } res = dal.updateOne({'_id': _id}, 'hisprice', data, True) time.sleep(1) return True
def calc_user_stock_cover_index(): """ 统计更新用户的持仓,计算持仓跟各大指数的重合度 Parameters ------ Return ------- result 是否正常结束 """ try: # 用户持仓个股增加股票名称 param = {'list_status': 'L', 'exchange': '', 'fields': 'symbol,name'} hs_df = basic.get_hs_stock_list(param) globalStock = {} for index, row in hs_df.iterrows(): globalStock[row['symbol']] = row['name'] _array = dal.queryMany(None, { '_id': 1, 'samples': 1 }, 0, None, 'indexSample') array = list(_array) if len(array) == 0: return None _opt_stock_array = dal.queryMany(None, { '_id': 1, 'stock': 1 }, 0, None, 'optStock') opt_stock_array = list(_opt_stock_array) if len(opt_stock_array) == 0: return None for opt_stock in opt_stock_array: user_id = opt_stock['_id'] user_stock = opt_stock['stock'] user_quant = {} user_quant['user'] = user_id for index_sample in array: index_name = index_sample['_id'] user_quant['index'] = index_name user_quant['_id'] = f'{user_id}_{index_name}' calc_opt_stock_in_index_sample(user_stock, index_sample['samples'], user_quant, globalStock) dal.updateOne({'_id': user_quant['_id']}, 'optQuant', user_quant, True) logger.info(user_quant) except Exception as exp: logger.error(exp) return None return None
def run_stock_alpha_beta_task(): """ 更新沪深股市上市公司相对沪深300的alpha beta Parameters ------ Return ------- result 是否正常结束 """ param = {'list_status': 'L', 'exchange': '', 'fields': 'symbol'} hs_df = basic.get_hs_stock_list(param) for index, row in hs_df.iterrows(): code = row['symbol'] quant.calc_alpha_beta(code, 460) return True
def run_stock_52week_lowprice_task(): """ 更新沪深股市上市公司52周最低价 Parameters ------ Return ------- result 是否正常结束 """ param = {'list_status': 'L', 'exchange': '', 'fields': 'symbol'} hs_df = basic.get_hs_stock_list(param) for index, row in hs_df.iterrows(): code = row['symbol'] quant.manage52WeekLowestPrice({'code': code}) return True
def run_stock_insert_es_task(): """ 构建沪深上市公司代码和名称的es存储 Parameters ------ Return ------- result 是否正常结束 """ param = {'list_status': 'L', 'exchange': '', 'fields': 'symbol'} hs_df = basic.get_hs_stock_list(param) for index, row in hs_df.iterrows(): code = row['symbol'] name = row['name'] es_res = es.insert_document( 'stock', json.dumps({ 'stockcode': row['symbol'], 'stockname': row['name'] }), code) logger.info(f'fetch stock es result {es_res} end') return True
def run_stock_day_week_month_ma_task(): """ 构建沪深上市公司代码和名称的es存储 Parameters ------ Return ------- result 是否正常结束 """ param = {'list_status': 'L', 'exchange': '', 'fields': 'symbol'} hs_df = basic.get_hs_stock_list(param) for index, row in hs_df.iterrows(): try: code = row['symbol'] name = row['name'] today = datetime.datetime.now().strftime("%Y%m%d") two_year = int(time.strftime('%Y', time.localtime( time.time()))) - 2 five_year = int(time.strftime('%Y', time.localtime( time.time()))) - 5 eight_year = int(time.strftime('%Y', time.localtime( time.time()))) - 11 month_day = time.strftime('%m%d', time.localtime(time.time())) two_year_ago = '{}{}'.format(two_year, month_day) five_year_ago = '{}{}'.format(five_year, month_day) eight_year_ago = '{}{}'.format(eight_year, month_day) param_day = { 'ts_code': row['ts_code'], 'start_date': two_year_ago, 'end_date': today, 'adj': 'qfq', 'freq': 'D', 'asset': 'E', 'ma': [20, 30, 60, 90, 120, 250] } day_df = price.get_adj_ma_price(param_day) ma_data = day_df.fillna('').iloc[0] ma_day = { 'trade_date': ma_data['trade_date'], 'ma20': ma_data['ma20'], 'ma30': ma_data['ma30'], 'ma60': ma_data['ma60'], 'ma90': ma_data['ma90'], 'ma120': ma_data['ma120'], 'ma250': ma_data['ma250'] } param_week = { 'ts_code': row['ts_code'], 'start_date': five_year_ago, 'end_date': today, 'adj': 'qfq', 'freq': 'W', 'asset': 'E', 'ma': [20, 30, 60, 90, 120, 250] } week_df = price.get_adj_ma_price(param_week) ma_week_data = week_df.fillna('').iloc[0] ma_week = { 'trade_date': ma_week_data['trade_date'], 'ma20': ma_week_data['ma20'], 'ma30': ma_week_data['ma30'], 'ma60': ma_week_data['ma60'], 'ma90': ma_week_data['ma90'], 'ma120': ma_week_data['ma120'], 'ma250': ma_week_data['ma250'] } param_month = { 'ts_code': row['ts_code'], 'start_date': eight_year_ago, 'end_date': today, 'adj': 'qfq', 'freq': 'M', 'asset': 'E', 'ma': [20, 30, 60, 90, 120] } month_df = price.get_adj_ma_price(param_month) ma_month_data = month_df.fillna('').iloc[0] ma_month = { 'trade_date': ma_month_data['trade_date'], 'ma20': ma_month_data['ma20'], 'ma30': ma_month_data['ma30'], 'ma60': ma_month_data['ma60'], 'ma90': ma_month_data['ma90'], 'ma120': ma_month_data['ma120'] } redisObj = { 'code': code, 'ma_day': ma_day, 'ma_week': ma_week, 'ma_month': ma_month } # save redisRes = redisDal.redisSet(f'ma:{code}', json.dumps(redisObj)) logger.info(f'{code} ma result:' + str(redisRes)) except Exception as exp: logger.error(exp) return True