modify stat files.
This commit is contained in:
@ -152,14 +152,31 @@ def calculate_yield():
|
||||
row1['close'] = row1['last_price']
|
||||
row1['time_key'] = row1['update_time']
|
||||
|
||||
# 2. 获取 2024-01-01 之前的历史数据
|
||||
# 2. 获取 2024-01-01 的年初数据
|
||||
cursor.execute("SELECT * FROM hs300_his_kline_none WHERE code=%s AND time_key<'2024-01-01' ORDER BY time_key DESC LIMIT 1", (code,))
|
||||
row2 = cursor.fetchone()
|
||||
|
||||
# 3. 获取 2024-09-24 之前的历史数据
|
||||
# 3. 获取 2024-09-24 的初始数据
|
||||
cursor.execute("SELECT * FROM hs300_his_kline_none WHERE code=%s AND time_key<'2024-09-24' ORDER BY time_key DESC LIMIT 1", (code,))
|
||||
row3 = cursor.fetchone()
|
||||
|
||||
# 3.1 获取 2021-01-01 之后的历史数据中最大值(前复权)
|
||||
cursor.execute("SELECT * FROM hs300_qfq_his WHERE code=%s AND time_key>='2021-01-01' ORDER BY close DESC LIMIT 1", (code,))
|
||||
row4 = cursor.fetchone()
|
||||
|
||||
# 3.2 获取 2021-01-01 之后的历史数据中最小值(前复权)
|
||||
cursor.execute("SELECT * FROM hs300_qfq_his WHERE code=%s AND time_key>='2021-01-01' ORDER BY close ASC LIMIT 1", (code,))
|
||||
row5 = cursor.fetchone()
|
||||
|
||||
# 3.4 获取 2021-01-01 之后的历史数据中pe最大值
|
||||
cursor.execute("SELECT * FROM hs300_qfq_his WHERE code=%s AND time_key>='2021-01-01' ORDER BY pe_ratio DESC LIMIT 1", (code,))
|
||||
row6 = cursor.fetchone()
|
||||
|
||||
# 3.5 获取 2021-01-01 之后的历史数据中pe最小值
|
||||
cursor.execute("SELECT * FROM hs300_qfq_his WHERE code=%s AND time_key>='2021-01-01' ORDER BY pe_ratio ASC LIMIT 1", (code,))
|
||||
row7 = cursor.fetchone()
|
||||
|
||||
|
||||
# 4. 读取复权数据
|
||||
cursor.execute("SELECT * FROM futu_rehab WHERE code=%s ORDER BY ex_div_date ASC", (code,))
|
||||
rehab_res = cursor.fetchall()
|
||||
@ -177,22 +194,47 @@ def calculate_yield():
|
||||
year_yield = row1['qfq_close'] / row2['qfq_close'] - 1 if row1 and row2 else None
|
||||
yield_0924 = row1['qfq_close'] / row3['qfq_close'] - 1 if row3 and row1 else None
|
||||
|
||||
# 6.1 计算当前股价是 2021 年之后最高点及最低点的百分比
|
||||
max_price_pct = row1['qfq_close'] / row4['close'] if row1 and row4 and row4['close'] !=0 else None
|
||||
max_price_pe_pct = row1['pe_ttm_ratio'] / row4['pe_ratio'] if row1 and row4 and row4['pe_ratio'] !=0 else None
|
||||
min_price_pct = row1['qfq_close'] / row5['close'] if row1 and row5 and row5['close'] !=0 else None
|
||||
min_price_pe_pct = row1['pe_ttm_ratio'] / row5['pe_ratio'] if row1 and row5 and row5['pe_ratio'] !=0 else None
|
||||
max_pe_pct = row1['pe_ttm_ratio'] / row6['pe_ratio'] if row1 and row6 and row6['pe_ratio'] !=0 else None
|
||||
min_pe_pct = row1['pe_ttm_ratio'] / row7['pe_ratio'] if row1 and row7 and row7['pe_ratio'] !=0 else None
|
||||
|
||||
|
||||
# 7. 收集结果
|
||||
result = {
|
||||
'code': code,
|
||||
'name': name,
|
||||
'year_begin_date': row2['time_key'].date(),
|
||||
'year_begin_close': row2['close'],
|
||||
'year_begin_close': row2['qfq_close'],
|
||||
'0924_date': row3['time_key'].date(),
|
||||
'0924_close': row3['close'],
|
||||
'0924_close': row3['qfq_close'],
|
||||
'max_price_date': row4['time_key'].date(),
|
||||
'max_price': row4['close'],
|
||||
'max_price_pe': row4['pe_ratio'],
|
||||
'max_pe_date': row6['time_key'].date(),
|
||||
'max_pe': row6['pe_ratio'],
|
||||
'min_price_date': row5['time_key'].date(),
|
||||
'min_price': row5['close'],
|
||||
'min_price_pe': row5['pe_ratio'],
|
||||
'min_pe_date': row7['time_key'].date(),
|
||||
'min_pe': row7['pe_ratio'],
|
||||
'latest_date': row1['time_key'].date(),
|
||||
'latest_close': row1['close'],
|
||||
'latest_close': row1['qfq_close'],
|
||||
'year_yield': year_yield,
|
||||
'yield_0924': yield_0924,
|
||||
'total_market_val': row1.get('total_market_val', None),
|
||||
'pe_ttm_ratio': row1.get('pe_ttm_ratio', None),
|
||||
'dividend_ratio_ttm': row1.get('dividend_ratio_ttm', None),
|
||||
'dividend_lfy_ratio': row1.get('dividend_lfy_ratio', None),
|
||||
'max_price_pct': max_price_pct,
|
||||
'max_price_pe_pct': max_price_pe_pct,
|
||||
'min_price_pct': min_price_pct,
|
||||
'min_price_pe_pct': min_price_pe_pct,
|
||||
'max_pe_pct': max_pe_pct,
|
||||
'min_pe_pct': min_pe_pct,
|
||||
}
|
||||
results.append(result)
|
||||
logging.info(f"{result}")
|
||||
@ -203,7 +245,11 @@ def calculate_yield():
|
||||
# 获取当前日期 格式化为 yyyymmdd
|
||||
current_date = datetime.now()
|
||||
date_string = current_date.strftime('%Y%m%d')
|
||||
fieldnames = ['code', 'name', 'year_begin_date', 'year_begin_close', '0924_date', '0924_close', 'latest_date', 'latest_close', 'year_yield', 'yield_0924', 'total_market_val', 'pe_ttm_ratio', 'dividend_ratio_ttm', 'dividend_lfy_ratio']
|
||||
fieldnames = ['code', 'name', 'year_begin_date', 'year_begin_close', '0924_date', '0924_close',
|
||||
'max_price_date', 'max_price', 'max_price_pe', 'max_pe_date', 'max_pe', 'min_price_date', 'min_price', 'min_price_pe', 'min_pe_date', 'min_pe',
|
||||
'latest_date', 'latest_close', 'year_yield', 'yield_0924', 'total_market_val', 'pe_ttm_ratio', 'dividend_ratio_ttm', 'dividend_lfy_ratio',
|
||||
'max_price_pct', 'max_price_pe_pct', 'min_price_pct', 'min_price_pe_pct', 'max_pe_pct', 'min_pe_pct'
|
||||
]
|
||||
write_to_csv(f'./result/stat_growth{date_string}.csv', fieldnames, results)
|
||||
|
||||
except Exception as e:
|
||||
|
||||
Reference in New Issue
Block a user