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_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_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_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_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_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, "无每日涨跌停价格数据")