def run_pipline_stg(filter_kc=True): logging.info( "************************ process start ***************************************" ) pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME")) strategies = { '海龟交易法则': turtle_trade.check_enter, '潜伏慢阳3d': turtle_trade.continue_increase3, '潜伏慢阳5d': turtle_trade.continue_increase5, '潜伏慢阳7d': turtle_trade.continue_increase7, 'BigInc': turtle_trade.big_inc, 'BreakInc': turtle_trade.break_inc, 'BackInc': turtle_trade.feedback_inc, 'Demon': turtle_trade.demon_inc, '连续含1y': turtle_trade.inc_cyin1, '连续含2y': turtle_trade.inc_cyin2, '放量上涨_1.5': enter.check_volume, '大涨': enter.check_continuous_inc, 'ATR': enter.check_breakthrough, '突破平台': breakthrough_platform.check, '均线多头': keep_increasing.check, '停机坪': parking_apron.check, '回踩年线': backtrace_ma250.check, '低ATR成长策略': low_atr.check_low_increase, # 'pe': pe.check, } stg_result_dict = collections.defaultdict(list) for index, row in pd_names.iterrows(): code = row['code'] name = row['code_name'] if filter_kc and stock_utils.is_jiucaiban(code): continue if "ST" in name: continue code_name = (code, name) print(code_name) data = stock_utils.read_data(code_name) if data is None: continue for strategy, strategy_func in strategies.items(): r = strategy_func(code_name, data) if r and r > 0.0: stg_result_dict[strategy].append((code, name, r)) for strategy, results in stg_result_dict.items(): outs = sorted(results, key=lambda e: e[2], reverse=True) stock_utils.persist(strategy, outs) logging.info( "************************ process end ***************************************" )
def scan_stocks(): pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME")) # 数据库获取沪、深、中小板 zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')] hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')] ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')] candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks print("total stock size:", len(candidate_stocks)) records = get_records(dt='2020-12-15', direction='limit', period='d1') print("涨停数量:", len(records)) for index, row in tqdm(pd_names.iterrows()): code = row['code'] name = row['code_name'] if stock_utils.is_jiucaiban(code): continue if code[3:] not in candidate_stocks: # print(name, code) continue code_name = (code, name) df = stock_utils.read_data(code_name) df.reset_index(drop=True, inplace=True) if len(df) < 60: continue df = df.tail(n=5) df.reset_index(drop=True, inplace=True) # 涨停分析 close = df.iloc[-1]['close'] chg = df.iloc[-1]['pctChg'] volume = df.iloc[-1]['volume'] volume_rate = (volume - df.iloc[-2]['volume']) / df.iloc[-2]['volume'] chgs = [c for c in df['pctChg']] # 涨幅序列 vols = [v / df.iloc[0]['volume'] for v in df['volume']] # 量比序列 feat = standardization(chgs[:-1]) + standardization(vols[:-1]) feat = standardization(chgs[:-1]) # print(feat) tmp_count = 0 for record in records: tmp_chg = json.loads(record.extra)['chgs'][:-1] tmp_vol = json.loads(record.extra)['vols'][:-1] tmp_feat = standardization(tmp_chg) + standardization(tmp_vol) tmp_feat = standardization(tmp_chg) dist = cosine_dist(feat, tmp_feat) if dist > 0.9: # print(name, record.name, chg, "sim:", dist) tmp_count += 1 if tmp_count > 5: print(code, name, chg)
def gen_records(day_ago=0): records = list() pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME")) # 数据库获取沪、深、中小板 zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')] hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')] ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')] candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks print("total stock size:", len(candidate_stocks)) for index, row in tqdm(pd_names.iterrows()): code = row['code'] name = row['code_name'] if stock_utils.is_jiucaiban(code): continue if code[3:] not in candidate_stocks: continue code_name = (code, name) df = stock_utils.read_data(code_name) df = df.head(n=len(df) - day_ago) # n天前 新高/低入口 df.reset_index(drop=True, inplace=True) if len(df) < 60: continue for period in [500, 120, 60, 20, 10, 5]: df = df.tail(n=period) df.reset_index(drop=True, inplace=True) idxMax = df['close'].idxmax(axis=0) idxMin = df['close'].idxmin(axis=0) # print(idxMax, idxMin) dateMax = df.iloc[idxMax]['date'] dateMin = df.iloc[idxMin]['date'] date = df.iloc[-1]['date'] volume = df.iloc[-1]['volume'] record_date = datetime.datetime.strptime(date, '%Y-%m-%d').date() d_period_inc = (df.iloc[-1]['close'] - df.iloc[0]['close']) / df.iloc[0]['close'] volume_avg = df['volume'].sum() / period amount_avg = df['amount'].sum() / period period_inc_avg = d_period_inc / period extraJson = { 'volume_avg': volume_avg, 'amount_avg': amount_avg, 'inc_avg': period_inc_avg } flag, direction = False, None r = Record(name, code, record_date, df.iloc[-1]['close'], "d{}".format(period), volume, d_period_inc) r.set_extra(json.dumps(extraJson)) if df.iloc[-1]['date'] == dateMax: flag, direction = True, 'up' if df.iloc[-1]['date'] == dateMin and 'ST' not in name: flag, direction = True, 'down' if flag: r.set_direction(direction) records.append(r) # 涨停分析 close = df.iloc[-1]['close'] chg = df.iloc[-1]['pctChg'] volume = df.iloc[-1]['volume'] volume_rate = (volume - df.iloc[-2]['volume']) / df.iloc[-2]['volume'] if float(chg) >= 9: chgs = [c for c in df['pctChg']] # 涨幅序列 vols = [v / df.iloc[0]['volume'] for v in df['volume']] # 量比序列 extraJson = { 'volume_rate': volume_rate, 'chgs': chgs, 'vols': vols } r = Record(name, code, record_date, close, "d1", volume, chg) r.set_direction(direction='limit') r.set_extra(json.dumps(extraJson)) records.append(r) return records
def filter_stock(df, code, name): if len(df) < 60 or is_jiucaiban(code) or is_index( code, name): # 去除一些上市不久的企业 688 300 399 return True else: return False
def get_m_candidates(): pd_names = pd.read_csv(ConfigUtils.get_stock("STOCK_NAME")) # 数据库获取沪、深、中小板 zxb_stocks = [s.code for s in get_mark_stocks(mark='zxb')] hsb_stocks = [s.code for s in get_mark_stocks(mark='hsb')] ssb_stocks = [s.code for s in get_mark_stocks(mark='ssb')] candidate_stocks = zxb_stocks + hsb_stocks + ssb_stocks print("total stock size:", len(candidate_stocks)) t_date = datetime.date.today().strftime('%Y-%m') break_stocks = list() high_stocks = list() for index, row in tqdm(pd_names.iterrows()): code = row['code'] name = row['code_name'] if stock_utils.is_jiucaiban(code): continue if code[3:] not in candidate_stocks: continue code_name = (code, name) df = stock_utils.read_data(code_name, root=ConfigUtils.get_stock("DATA_M_DIR")) if df is None or len(df) < 2: continue df = df.tail(n=12) # n天前 新高/低入口 df.reset_index(drop=True, inplace=True) close_df = df.sort_values(by='close', ascending=False) volume_df = df.sort_values(by='volume', ascending=False) amount_df = df.sort_values(by='amount', ascending=False) pctChg_df = df.sort_values(by='pctChg', ascending=False) idxCloseMax0, idxCloseMax1 = close_df.index[0], close_df.index[1] idxVolMax0, idxVolMax1 = volume_df.index[0], volume_df.index[1] idxAmountMax0, idxAmountMax1 = amount_df.index[0], amount_df.index[1] idxChgMax0, idxChgMax1 = pctChg_df.index[0], pctChg_df.index[1] volMax0, volMax1 = df.iloc[idxVolMax0]['volume'], df.iloc[idxVolMax1][ 'volume'] pctMax0, pctMax1 = df.iloc[idxChgMax0]['pctChg'], df.iloc[idxChgMax1][ 'pctChg'] closeMax0, closeMax1 = df.iloc[idxCloseMax0]['close'], df.iloc[ idxCloseMax1]['close'] date0, date1 = df.iloc[idxCloseMax0]['date'], df.iloc[idxCloseMax1][ 'date'] pch0, pch1 = df.iloc[idxCloseMax0]['pctChg'], df.iloc[idxCloseMax1][ 'pctChg'] amount0, amount1 = df.iloc[idxCloseMax0]['amount'], df.iloc[ idxCloseMax1]['amount'] # 经过3个月洗盘, 最近股价将赶超新高 if t_date == date1 and (idxCloseMax1 - idxCloseMax0) > 2: # print(code_name, volMax1, date0, date1, idxVolMax0, idxChgMax0, idxCloseMax0, closeMax0) r = Record(name, code, t_date, closeMax1, 'm', amount1, pch1) break_stocks.append(r) # 持续新高 if t_date == date0: # print(code_name, dateMax0, dateMax1, idxVolMax0, idxChgMax0, idxCloseMax0, closeMax0) r = Record(name, code, t_date, closeMax0, 'm', amount0, pch0) high_stocks.append(r) break_stocks = sorted(break_stocks, key=lambda x: x.volume, reverse=True) high_stocks = sorted(high_stocks, key=lambda x: x.volume, reverse=True) # print(break_stocks[:30]) # print(high_stocks[:30]) break_stocks = [r for r in break_stocks if r.volume < 5e11] high_stocks = [r for r in high_stocks if r.volume < 90081936793] return break_stocks, high_stocks