stock_new更新

This commit is contained in:
muliangshu
2026-03-26 16:00:33 +08:00
parent 9faf6baa72
commit 8f5b35679b
+11 -6
View File
@@ -50,10 +50,12 @@ def stock_gmm(last_day_str,last_month_str):
for i, row in ts_detail.iterrows(): for i, row in ts_detail.iterrows():
res.append(row) res.append(row)
stock_res = pd.DataFrame(res) stock_res = pd.DataFrame(res)
stock_res.to_excel('E:\\工作文件\\avm_output\\airflow_scaled_df.xlsx', encoding='utf-8', index=False)
stock_all_newest = stock_all[stock_all.trade_date == stock_all.trade_date.max()] stock_all_newest = stock_all[stock_all.trade_date == stock_all.trade_date.max()]
stock_all_newest = stock_all_newest[['ts_code', 'turnover_rate', 'close', 'amount', 'pe', 'pe_ttm', 'total_mv', 'pct_chg']] stock_all_newest = stock_all_newest[['ts_code', 'turnover_rate', 'close', 'amount', 'pe', 'pe_ttm', 'total_mv', 'pct_chg']]
X = stock_res[['0', '1', '2', '3', '4', '5', '6']] X = stock_res[['0', '1', '2', '3', '4', '5', '6']]
gmm_df = gmm_trade(stock_res, X) gmm_df = gmm_trade(stock_res, X)
gmm_df.to_excel('E:\\工作文件\\avm_output\\airflow_gmm_df.xlsx', encoding='utf-8', index=False)
gmm_group_df = gmm_df.groupby(['ts_code', 'rating'])['stock_name'].count().reset_index() gmm_group_df = gmm_df.groupby(['ts_code', 'rating'])['stock_name'].count().reset_index()
gmm_group_df.rename(columns={'stock_name': 'count'}, inplace=True) gmm_group_df.rename(columns={'stock_name': 'count'}, inplace=True)
gmm_df = gmm_df.merge(gmm_group_df, how='left', on=['ts_code', 'rating']) gmm_df = gmm_df.merge(gmm_group_df, how='left', on=['ts_code', 'rating'])
@@ -75,21 +77,24 @@ def stock_gmm(last_day_str,last_month_str):
def stock_select(i): def stock_select(i):
last_day = today+relativedelta(days=-i) last_day = today+relativedelta(days=-i)
last_month = last_day+relativedelta(months=-2) last_month = last_day+relativedelta(days=-15)
last_day_str = last_day.strftime('%Y%m%d') last_day_str = last_day.strftime('%Y%m%d')
if last_day_str in trade_dates: last_day_str = '20260325'
return last_day_str # if last_day_str in trade_dates:
if int(last_day_str)<20250101: # return last_day_str
return last_day_str # if int(last_day_str)<20200101:
# return last_day_str
last_month_str = last_month.strftime('%Y%m%d') last_month_str = last_month.strftime('%Y%m%d')
last_month_str = '20260304'
stock_all_newest,select_block,slecct_df=stock_gmm(last_day_str,last_month_str) stock_all_newest,select_block,slecct_df=stock_gmm(last_day_str,last_month_str)
db_stock = slecct_df[['ts_code', 'stock_name', 'open', 'close', 'high', 'low', db_stock = slecct_df[['ts_code', 'stock_name', 'open', 'close', 'high', 'low',
'vol', 'amount', 'pre_close', 'change', 'pct_chg', 'turnover_rate', 'vol', 'amount', 'pre_close', 'change', 'pct_chg', 'turnover_rate',
'turnover_rate_f', 'volume_ratio', 'pe', 'pe_ttm', 'pb', 'ps_ttm', 'ps', 'turnover_rate_f', 'volume_ratio', 'pe', 'pe_ttm', 'pb', 'ps_ttm', 'ps',
'total_share', 'float_share', 'free_share', 'total_mv', 'circ_mv']].copy() 'total_share', 'float_share', 'free_share', 'total_mv', 'circ_mv']].copy()
db_stock['trade_date'] = last_day_str db_stock['trade_date'] = last_day_str
print(select_block)
# db_stock['ts_code'] = db_stock['ts_code'].apply(lambda x: x[0:6]) # db_stock['ts_code'] = db_stock['ts_code'].apply(lambda x: x[0:6])
db_stock.to_sql("mu_stocks_new",con=engine, index=False, if_exists="append") # db_stock.to_sql("mu_stocks_new",con=engine, index=False, if_exists="append")
return last_day_str return last_day_str
if __name__ == '__main__': if __name__ == '__main__':
stock_select(0) stock_select(0)