def job_new_share(start, end): df_col_list = dbs["stock_basic"]["new_share"]["query_cols"].split(",") day_interval = (end - start).days if day_interval > 3000: gap = day_interval for i in range(3000, 0, -1): if gap % i == 0: day_interval = i break new_share = pd.DataFrame(columns=df_col_list) num = (end - start).days pro = ts.pro_api() for i in range(0, num, day_interval): new_share = new_share.append( pro.new_share( start_date=(start + datetime.timedelta(days=i)).strftime('%Y%m%d'), end_date=(start + datetime.timedelta(days=i + day_interval) ).strftime('%Y%m%d'), fields=dbs["stock_basic"]["new_share"]["query_cols"])) float_to_str_list = [ 'issue_date', 'amount', 'market_amount', 'price', 'pe', 'limit_amount', 'funds', 'ballot' ] new_share = basicFunctions.floatToStr(new_share, float_to_str_list, False) sqlFunctions.insertSql( "stock_basic", new_share, "new_share", tuple(dbs["stock_basic"]["new_share"]["query_cols"].split(",")))
def get_namechange(first): pro = ts.pro_api() if first: start_dt = '19901219' else: start_dt = open(basicFunctions.startDt, 'r').read() namechange = pro.namechange( start_date=start_dt, end_date=end_dt, fields=dbs["stock_basic"]["namechange"]["query_cols"]) sqlFunctions.insertSql( "stock_basic", namechange, "namechange", tuple(dbs["stock_basic"]["namechange"]["query_cols"].split(","))) print("已获取股票曾用名")
def get_stock_basic_d(first): """ 无area、industry。 :param first: :return: """ pro = ts.pro_api() stock_basic_d = pro.stock_basic( list_status='D', fields=dbs["stock_basic"]["stock_basic_d"]["query_cols"]) sqlFunctions.insertSql( "stock_basic", stock_basic_d, "stock_basic_d", tuple(dbs["stock_basic"]["stock_basic_d"]["query_cols"].split(","))) print("已获取退市股票列表")
def job_stk_managers(ts_code_listStr): df_col_list = dbs["stock_basic"]["stk_managers"]["query_cols"].split(",") interval = 150 stk_managers = pd.DataFrame(columns=df_col_list) pro = ts.pro_api() print("正在获取", ts_code_listStr[:9], "等上市公司管理层") for i in range(0, len(ts_code_listStr), interval): stk_managers = stk_managers.append( pro.stk_managers( ts_code=ts_code_listStr[i:i + interval], fields=dbs["stock_basic"]["stk_managers"]["query_cols"])) time.sleep(0.6) print("stock_basic.stk_managers 开始插入:", len(stk_managers)) sqlFunctions.insertSql( "stock_basic", stk_managers, "stk_managers", tuple(dbs["stock_basic"]["stk_managers"]["query_cols"].split(",")))
def get_moneyflow(start_dt, ts_code): df_col_list = dbs["stock_daily"]["moneyflow"]["query_cols"].split(",") moneyflow = pd.DataFrame(columns=df_col_list) day_interval = 2000 start_dt_query = start_dt if start_dt == "19901219": time_temp = datetime.datetime( 1990, 12, 19) + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') else: time_temp = datetime.datetime.now() end_dt_query = end_dt pro = ts.pro_api() while start_dt_query <= end_dt: moneyflow = moneyflow.append(pro.moneyflow( ts_code=ts_code.replace("_", "."), start_date=start_dt_query, end_date=end_dt_query, fields=dbs["stock_daily"]["moneyflow"]["query_cols"]), sort=False) time.sleep(0.6) time_temp = time_temp + datetime.timedelta(days=1) start_dt_query = time_temp.strftime('%Y%m%d') time_temp = time_temp + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') if moneyflow is not None: if len(moneyflow) > 0: float_to_str_list = [ "buy_sm_vol", "buy_sm_amount", "sell_sm_vol", "sell_sm_amount", "buy_md_vol", "buy_md_amount", "sell_md_vol", "sell_md_amount", "buy_lg_vol", "buy_lg_amount", "sell_lg_vol", "sell_lg_amount", "buy_elg_vol", "buy_elg_amount", "sell_elg_vol", "sell_elg_amount", "net_mf_vol", "net_mf_amount" ] moneyflow = basicFunctions.floatToStr(moneyflow, float_to_str_list, False) print("开始插入", ts_code, "个股资金流向数据:", len(moneyflow)) sqlFunctions.insertSql( "stock_daily", moneyflow, ts_code, tuple( dbs["stock_daily"]["moneyflow"]["query_cols"].split(","))) else: print(ts_code, "无个股资金流向数据") else: print(ts_code, "无个股资金流向数据")
def get_stock_basic_l_or_p(first): """ 无delist_date :param first: :return: """ pro = ts.pro_api() stock_basic_L = pro.stock_basic( list_status='L', fields=dbs["stock_basic"]["stock_basic_l_or_p"]["query_cols"]) stock_basic_P = pro.stock_basic( list_status='P', fields=dbs["stock_basic"]["stock_basic_l_or_p"]["query_cols"]) stock_basic_l_or_p = stock_basic_L.append(stock_basic_P) sqlFunctions.insertSql( "stock_basic", stock_basic_l_or_p, "stock_basic_l_or_p", tuple( dbs["stock_basic"]["stock_basic_l_or_p"]["query_cols"].split(","))) print("已获取上市股票列表")
def get_daily_basic(start_dt, ts_code): df_col_list = dbs["stock_daily"]["daily_basic"]["query_cols"].split(",") daily_basic = pd.DataFrame(columns=df_col_list) day_interval = 2000 start_dt_query = start_dt if start_dt == "19901219": time_temp = datetime.datetime( 1990, 12, 19) + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') else: time_temp = datetime.datetime.now() end_dt_query = end_dt pro = ts.pro_api() while start_dt_query <= end_dt: daily_basic = daily_basic.append(pro.daily_basic( ts_code=ts_code.replace("_", "."), start_date=start_dt_query, end_date=end_dt_query, fields=dbs["stock_daily"]["daily_basic"]["query_cols"]), sort=False) time.sleep(0.6) time_temp = time_temp + datetime.timedelta(days=1) start_dt_query = time_temp.strftime('%Y%m%d') time_temp = time_temp + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') if daily_basic is not None: if len(daily_basic) > 0: float_to_str_list = [ "turnover_rate", "turnover_rate_f", "volume_ratio", "pe", "pe_ttm", "pb", "ps", "ps_ttm", "total_share", "float_share", "free_share", "total_mv", "circ_mv" ] daily_basic = basicFunctions.floatToStr(daily_basic, float_to_str_list, False) print("开始插入", ts_code, "每日指标数据:", len(daily_basic)) sqlFunctions.insertSql( "stock_daily", daily_basic, ts_code, tuple(dbs["stock_daily"]["daily_basic"]["query_cols"].split( ","))) else: print(ts_code, "无每日指标数据") else: print(ts_code, "无每日指标数据")
def get_hs_const(first): pro = ts.pro_api() sh_hs_const = pro.hs_const( hs_type='SH', is_new=0, fields=dbs["stock_basic"]["hs_const"]["query_cols"]).append( pro.hs_const(hs_type='SH', fields=dbs["stock_basic"]["hs_const"]["query_cols"])) sz_hs_const = pro.hs_const( hs_type='SZ', is_new=0, fields=dbs["stock_basic"]["hs_const"]["query_cols"]).append( pro.hs_const(hs_type='SZ', fields=dbs["stock_basic"]["hs_const"]["query_cols"])) hs_const = sh_hs_const.append(sz_hs_const) sqlFunctions.insertSql( "stock_basic", hs_const, "hs_const", tuple(dbs["stock_basic"]["hs_const"]["query_cols"].split(","))) print("已获取沪深股通成份股")
def get_daily(start_dt, ts_code): df_col_list = dbs["stock_daily"]["daily"]["query_cols"].replace( "change_", "change").split(",") daily = pd.DataFrame(columns=df_col_list) day_interval = 2000 start_dt_query = start_dt if start_dt == "19901219": time_temp = datetime.datetime( 1990, 12, 19) + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') else: time_temp = datetime.datetime.now() end_dt_query = end_dt pro = ts.pro_api() while start_dt_query <= end_dt: daily = daily.append(pro.daily( ts_code=ts_code.replace("_", "."), start_date=start_dt_query, end_date=end_dt_query, fields=dbs["stock_daily"]["daily"]["query_cols"].replace( "change_", "change")), sort=False) time.sleep(0.6) time_temp = time_temp + datetime.timedelta(days=1) start_dt_query = time_temp.strftime('%Y%m%d') time_temp = time_temp + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') if daily is not None: if len(daily) > 0: float_to_str_list = [ "open", "high", "low", "close", "pre_close", "change", "pct_chg", "vol", "amount" ] daily = basicFunctions.floatToStr(daily, float_to_str_list, False) print("开始插入", ts_code, "日线行情数据:", len(daily)) sqlFunctions.insertSql( "stock_daily", daily, ts_code, tuple(dbs["stock_daily"]["daily"]["query_cols"].split(","))) else: print(ts_code, "无日线行情数据") else: print(ts_code, "无日线行情数据")
def get_stock_company(first): pro = ts.pro_api() sse_stock_company = pro.stock_company( fields=dbs["stock_basic"]["stock_company"]["query_cols"]) szse_stock_company = pro.stock_company( exchange='SZSE', fields=dbs["stock_basic"]["stock_company"]["query_cols"]) stock_company = sse_stock_company.append(szse_stock_company) float_to_str_list = ['reg_capital'] stock_company = basicFunctions.floatToStr(stock_company, float_to_str_list, False) float_to_int_to_str_list = ['employees'] stock_company = basicFunctions.floatToStr(stock_company, float_to_int_to_str_list, True) sqlFunctions.insertSql( "stock_basic", stock_company, "stock_company", tuple(dbs["stock_basic"]["stock_company"]["query_cols"].split(","))) with open(basicFunctions.lockF, 'w') as f: f.write("1") print("已获取上市公司基本信息")
def job_stk_rewards(ts_code_listStr): df_col_list = dbs["stock_basic"]["stk_rewards"]["query_cols"].split(",") interval = 50 stk_rewards = pd.DataFrame(columns=df_col_list) pro = ts.pro_api() print("正在获取", ts_code_listStr[:9], "等管理层薪酬和持股") for i in range(0, len(ts_code_listStr), interval): stk_rewards = stk_rewards.append( pro.stk_rewards( ts_code=ts_code_listStr[i:i + interval], fields=dbs["stock_basic"]["stk_rewards"]["query_cols"])) time.sleep(0.6) float_to_str_list = ['reward'] stk_rewards = basicFunctions.floatToStr(stk_rewards, float_to_str_list, False) float_to_int_to_str_list = ['hold_vol'] stk_rewards = basicFunctions.floatToStr(stk_rewards, float_to_int_to_str_list, True) print("stock_basic.stk_rewards 开始插入:", len(stk_rewards)) sqlFunctions.insertSql( "stock_basic", stk_rewards, "stk_rewards", tuple(dbs["stock_basic"]["stk_rewards"]["query_cols"].split(",")))
def get_stk_limit(start_dt, ts_code): df_col_list = dbs["stock_daily"]["stk_limit"]["query_cols"].split(",") stk_limit = pd.DataFrame(columns=df_col_list) day_interval = 2000 start_dt_query = start_dt if start_dt == "19901219": time_temp = datetime.datetime( 1990, 12, 19) + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') else: time_temp = datetime.datetime.now() end_dt_query = end_dt pro = ts.pro_api() while start_dt_query <= end_dt: stk_limit = stk_limit.append(pro.stk_limit( ts_code=ts_code.replace("_", "."), start_date=start_dt_query, end_date=end_dt_query, fields=dbs["stock_daily"]["stk_limit"]["query_cols"]), sort=False) time.sleep(0.6) time_temp = time_temp + datetime.timedelta(days=1) start_dt_query = time_temp.strftime('%Y%m%d') time_temp = time_temp + datetime.timedelta(days=day_interval) end_dt_query = time_temp.strftime('%Y%m%d') if stk_limit is not None: if len(stk_limit) > 0: float_to_str_list = ["up_limit", "down_limit"] stk_limit = basicFunctions.floatToStr(stk_limit, float_to_str_list, False) print("开始插入", ts_code, "每日涨跌停价格数据:", len(stk_limit)) sqlFunctions.insertSql( "stock_daily", stk_limit, ts_code, tuple( dbs["stock_daily"]["stk_limit"]["query_cols"].split(","))) else: print(ts_code, "无每日涨跌停价格数据") else: print(ts_code, "无每日涨跌停价格数据")
def get_trade_cal(first): pro = ts.pro_api() if first: start_dt = '19901219' else: start_dt = open(basicFunctions.startDt, 'r').read() sse_trade_cal = pro.trade_cal( exchange='SSE', start_date=start_dt, end_date=end_dt, fields=dbs["stock_basic"]["trade_cal"]["query_cols"]) szse_trade_cal = pro.trade_cal( exchange='SZSE', start_date=start_dt, end_date=end_dt, fields=dbs["stock_basic"]["trade_cal"]["query_cols"]) trade_cal = sse_trade_cal.append(szse_trade_cal) trade_cal['is_open'] = trade_cal['is_open'].astype(str) # 将一整列由int转为str sqlFunctions.insertSql( "stock_basic", trade_cal, "trade_cal", tuple(dbs["stock_basic"]["trade_cal"]["query_cols"].split(","))) print("已获取交易日历")