100 lines
4.9 KiB
Python
100 lines
4.9 KiB
Python
import datetime
|
|
from sqlalchemy import create_engine
|
|
from dateutil.relativedelta import relativedelta
|
|
import pandas as pd
|
|
from sklearn.preprocessing import StandardScaler
|
|
from sklearn.mixture import GaussianMixture
|
|
import numpy as np
|
|
from collections import defaultdict
|
|
engine = create_engine('mysql+pymysql://root:Goojia123456@192.168.0.93:3306/stock')
|
|
mu_stocks_all = pd.read_sql(f"select * from mu_stocks_new", con = engine)
|
|
stock_basic_infos = pd.read_sql(f"select * from stock_basic", con=engine)
|
|
trade_dates = mu_stocks_all['trade_date'].tolist()
|
|
today = datetime.datetime.now()
|
|
def gmm_trade(get_data,df):
|
|
gmm = GaussianMixture(n_components=100, covariance_type='spherical', random_state=0)
|
|
data = df.values
|
|
data = np.array(data, dtype='float64')
|
|
x = data
|
|
gmm.fit(x)
|
|
y_hat = gmm.predict(x)
|
|
gmm_means_ = [i[0] for i in gmm.means_]
|
|
d = defaultdict(list)
|
|
for k, va in [(v, i) for i, v in enumerate(gmm_means_)]:
|
|
d[va] = k
|
|
gmm_means_list = []
|
|
for label in y_hat:
|
|
mean_value = d[label]
|
|
gmm_means_list.append(mean_value)
|
|
get_data["rating"] = y_hat
|
|
get_data["mean"] = gmm_means_list
|
|
return get_data
|
|
def stock_gmm(last_day_str,last_month_str):
|
|
start = datetime.datetime.now()
|
|
stock_all = pd.read_sql(f"select * from stock_index_daily where trade_date>'{last_month_str}' and trade_date<='{last_day_str}'", con = engine)
|
|
end = datetime.datetime.now()
|
|
print('MySQL Query Running time: %s Seconds' % (end - start))
|
|
stock_all = stock_all.sort_values(by="trade_date", ascending=False)
|
|
stock_select = stock_all[((stock_all['ts_code'].str.startswith('600')) | (stock_all['ts_code'].str.startswith('00')))]
|
|
stock_data = stock_select.dropna(subset=['turnover_rate_f', 'volume_ratio', 'pe', 'pe_ttm', 'pb', 'ps_ttm', 'ps'])
|
|
stock_group = stock_data.groupby(['ts_code'])
|
|
scaler = StandardScaler()
|
|
import_col = ['turnover_rate_f', 'volume_ratio', 'pe', 'pe_ttm', 'pb', 'ps_ttm', 'ps']
|
|
scaler_col = [str(i) for i in range(len(import_col))]
|
|
res = []
|
|
for ts_code, ts_detail in stock_group:
|
|
if len(ts_detail) > 3:
|
|
ts_detail[scaler_col] = ts_detail.apply(lambda x: (0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0), axis=1,
|
|
result_type="expand")
|
|
ts_detail[scaler_col] = scaler.fit_transform(ts_detail[import_col].values)
|
|
for i, row in ts_detail.iterrows():
|
|
res.append(row)
|
|
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_newest[['ts_code', 'turnover_rate', 'close', 'amount', 'pe', 'pe_ttm', 'total_mv', 'pct_chg']]
|
|
X = stock_res[['0', '1', '2', '3', '4', '5', '6']]
|
|
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.rename(columns={'stock_name': 'count'}, inplace=True)
|
|
gmm_df = gmm_df.merge(gmm_group_df, how='left', on=['ts_code', 'rating'])
|
|
min_mean = gmm_df['mean'].min()
|
|
slecct_df = gmm_df[gmm_df['mean'] == min_mean]
|
|
count_max = slecct_df['count'].max()
|
|
slecct_df = slecct_df[slecct_df['count'] == count_max]
|
|
slecct_df.drop_duplicates(subset=["ts_code"], keep="first", inplace=True)
|
|
block_list = list(slecct_df['stock_name'])
|
|
select_block = []
|
|
for name in block_list:
|
|
status = 1
|
|
for i in ['N', 'ST']:
|
|
if i in name:
|
|
status = 0
|
|
if status == 1:
|
|
select_block.append(name)
|
|
return stock_all_newest,select_block,slecct_df
|
|
|
|
def stock_select(i):
|
|
last_day = today+relativedelta(days=-i)
|
|
last_month = last_day+relativedelta(days=-15)
|
|
last_day_str = last_day.strftime('%Y%m%d')
|
|
last_day_str = '20260325'
|
|
# if last_day_str in trade_dates:
|
|
# 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 = '20260304'
|
|
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',
|
|
'vol', 'amount', 'pre_close', 'change', 'pct_chg', 'turnover_rate',
|
|
'turnover_rate_f', 'volume_ratio', 'pe', 'pe_ttm', 'pb', 'ps_ttm', 'ps',
|
|
'total_share', 'float_share', 'free_share', 'total_mv', 'circ_mv']].copy()
|
|
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.to_sql("mu_stocks_new",con=engine, index=False, if_exists="append")
|
|
return last_day_str
|
|
if __name__ == '__main__':
|
|
stock_select(0) |