def create_bond_dataframe(periods_range=10): data = pd.read_csv('./data/kezhuanzhai.csv', index_col=0) result_data = data.copy() calender = pd.read_csv('./data/calender.csv', converters={'cal_date': str}) forecast_strategy.end_date = '20201231' for index, item in data.iterrows(): _, ndate = forecast_strategy.find_buy_day(item.CBSTOCKCODE, item.CBIPOANNCDATE, 0, calender) df_ndate = db.get_k_data(item.CBSTOCKCODE, ndate.replace('-', '', 2), ndate.replace('-', '', 2)) if len(df_ndate) > 0: result_data.loc[index, 'ndate_open'] = df_ndate.open.values[0] result_data.loc[index, 'ndate_close'] = df_ndate.close.values[0] result_data.loc[index, 'ndate_pct_chg'] = df_ndate.pct_chg.values[0] result_data.loc[index, 'ndate_date'] = df_ndate.trade_date.values[0] for i in range(1, periods_range + 1): is_exist, trade_date = forecast_strategy.find_buy_day( item.CBSTOCKCODE, item.CBIPOANNCDATE, i, calender) if is_exist: dataframe = db.get_k_data(item.CBSTOCKCODE, trade_date.replace('-', '', 2), trade_date.replace('-', '', 2)) if len(df_ndate) > 0: result_data.loc[ index, f'date{str(i)}_open'] = dataframe.open.values[0] result_data.loc[ index, f'date{str(i)}_close'] = dataframe.close.values[0] result_data.loc[ index, f'date{str(i)}_pct_chg'] = dataframe.pct_chg.values[0] result_data.loc[ index, f'date{str(i)}_date'] = dataframe.trade_date.values[0] else: if trade_date is not None: result_data.loc[index, f'date{str(i)}_date'] = trade_date.replace( '-', '', 2) if pd.isnull(item.CBLISTDATE): continue try: dflist = db.get_k_data(item.CBSTOCKCODE, item.CBLISTDATE.replace('-', '', 2), item.CBLISTDATE.replace('-', '', 2)) except: continue if len(dflist) == 0: continue result_data.loc[index, 'list_open'] = dflist.open.values[0] result_data.loc[index, 'list_close'] = dflist.close.values[0] result_data.loc[index, 'list_pct_chg'] = dflist.pct_chg.values[0] result_data.loc[index, 'list_date'] = dflist.trade_date.values[0] result_data.to_csv('./data/convertible-bond.csv') return result_data
def get_dt_data(code, start, end): global dt_data global ts_count start = tran_dateformat(start) end = tran_dateformat(end) dt = dt_data[(dt_data['ts_code'] == code) & (dt_data['start_to_end'] == (start + end))] if len(dt) == 0: try: dt = get_k_data(code, start.replace('-', '', 3), end.replace('-', '', 3)) except: time.sleep(40) dt = get_dt_data(code, start, end) pass if dt is None: return dt if len(dt) == 0: return dt dt['start_to_end'] = start + end ts_count += 1 dt_data = dt_data.append(dt) return dt
def getDatas(ts_code, begindate, enddate, *args, **kwargs): begindate = begindate.strftime('%Y%m%d') enddate = enddate.strftime('%Y%m%d') ds_df = db2df.get_k_data(ts_code, begindate, enddate) ds_df.index = pd.to_datetime(ds_df.trade_date) ds_df = ds_df[['open', 'high', 'low', 'close', 'vol']] return ds_df
def get_dt_data(code, start, end): dt = get_k_data(code, start.replace('-', '', 3), end.replace('-', '', 3)) if dt is None: return dt if len(dt) == 0: return dt return dt
def get_dt_data(code, start, end): global dt_data start = tran_dateformat(start.replace('-', '', 3)) end = tran_dateformat(end.replace('-', '', 3)) dt = dt_data[(dt_data['ts_code'] == code) & (dt_data['start_to_end'] == (start + end))] if len(dt) == 0: dt = get_k_data(code, start.replace('-', '', 3), end.replace('-', '', 3)) if dt is None: return dt if len(dt) == 0: return dt dt['start_to_end'] = start + end dt_data = dt_data.append(dt) return dt
def get_basic_info(code, start, end): global basic_info try: start = tran_dateformat(start).replace('-', '', 3) except: print('aaaa', code, start, end) pass end = tran_dateformat(end).replace('-', '', 3) df = basic_info[(basic_info['ts_code'] == code) & (basic_info['start_to_end'] == pd.to_numeric(start + end) )].drop_duplicates('trade_date') if df is None or len(df) == 0: # df = pro.daily_basic(ts_code=code, start_date=start, end_date=end, # fields='ts_code,close,trade_date,turnover_rate_f,volume_ratio,pe_ttm,circ_mv') df = get_k_data(code, start, end) if df is None or len(df) == 0: return None df['start_to_end'] = start + end basic_info = basic_info.append(df) df = df.reset_index().drop(columns='index') return df
dt = get_k_data(code, start.replace('-', '', 3), end.replace('-', '', 3)) if dt is None: return dt if len(dt) == 0: return dt return dt st_path = './data/st_stock.csv' stock_info_path = './data/stock_basic_info.csv' loan_path = './data/rongquanall.csv' dt_data = pd.read_csv('./data/dt_data.csv') check_trade_condition = CheckTradeCondition(st_path, stock_info_path, loan_path, buy='open', sell='close', long_short='long') high_limit_open = get_k_data('002684.SZ', '20201103', '20201103') # st股票,一字涨停 5% high_limit_close = get_k_data('300670.SZ', '20201103', '20201103') # 收盘涨停 low_limit_open = get_k_data('002496.SZ', '20201103', '20201103') # st 股票,一字跌停 -5% low_limit_close = get_k_data('600225.SH', '20201103', '20201103') # st 股票,收盘跌停 -5% engine = create_engine('mysql+pymysql://root:myh123@localhost:3306/quant?charset=utf8',pool_recycle=1) def get_k_data(ts_code, start, end) -> pd.DataFrame: global engine sql = "SELECT * FROM quant.stock_daily where ts_code ='" + ts_code + "' and trade_date between '" + start + "' and '" + end + "'" return pd.read_sql(sql, engine) class TestCheckTradeCondition(TestCase):