add stat files.
This commit is contained in:
@ -1,3 +1,18 @@
|
||||
"""
|
||||
Script Name:
|
||||
Description: 统计过去十年来,hs300 和 sp500成分股的投资胜率,年化回报率等数据。
|
||||
|
||||
Author: [Your Name]
|
||||
Created Date: YYYY-MM-DD
|
||||
Last Modified: YYYY-MM-DD
|
||||
Version: 1.0
|
||||
|
||||
Modification History:
|
||||
- YYYY-MM-DD [Your Name]:
|
||||
- YYYY-MM-DD [Your Name]:
|
||||
- YYYY-MM-DD [Your Name]:
|
||||
"""
|
||||
|
||||
import pymysql
|
||||
import logging
|
||||
import sys
|
||||
@ -5,6 +20,7 @@ import time
|
||||
import numpy as np
|
||||
from datetime import datetime
|
||||
import argparse
|
||||
import config
|
||||
|
||||
# 设置默认值
|
||||
default_min_stat_years = 5
|
||||
@ -40,45 +56,25 @@ def parse_arguments():
|
||||
# 获取用户输入的参数
|
||||
min_stat_years, debug, market_key = parse_arguments()
|
||||
|
||||
|
||||
# 配置日志格式
|
||||
formatter = logging.Formatter('%(asctime)s %(levelname)s [%(filename)s:%(lineno)d] - %(message)s')
|
||||
|
||||
# 动态生成日志文件名,基于 min_stat_years 的值
|
||||
log_filename = f'./log/stat_yield_{market_key}_{min_stat_years}years_rate.log'
|
||||
file_handler = logging.FileHandler(log_filename)
|
||||
file_handler.setFormatter(formatter)
|
||||
|
||||
console_handler = logging.StreamHandler(sys.stdout)
|
||||
console_handler.setFormatter(formatter)
|
||||
|
||||
logging.basicConfig(level=logging.INFO, handlers=[file_handler, console_handler])
|
||||
|
||||
|
||||
# MySQL 配置
|
||||
db_config = {
|
||||
'host': '172.18.0.2',
|
||||
'user': 'root',
|
||||
'password': 'mysqlpw',
|
||||
'database': 'stockdb'
|
||||
}
|
||||
# 配置日志
|
||||
config.setup_logging(f'./log/stat_yield_{market_key}_{min_stat_years}years_rate.log')
|
||||
|
||||
# 传入表名的映射
|
||||
table_mapping = {
|
||||
"hs300": {
|
||||
"codes": "hs300",
|
||||
"his_data": "hs300_qfq_his",
|
||||
"his_data": "hs300_ajust_kline_202410",
|
||||
"stat_res": f"hs300_{min_stat_years}years_yield_stats_2410"
|
||||
},
|
||||
"sp500": {
|
||||
"codes": "sp500",
|
||||
"his_data": "sp500_qfq_his_202410",
|
||||
"his_data": "sp500_ajust_kline_202410",
|
||||
"stat_res": f"sp500_{min_stat_years}years_yield_stats_2410"
|
||||
}
|
||||
}
|
||||
|
||||
# 连接 MySQL
|
||||
connection = pymysql.connect(**db_config)
|
||||
connection = pymysql.connect(**config.db_config)
|
||||
|
||||
# 获取股票代码
|
||||
def get_codes(table_mapping, index_name):
|
||||
@ -89,7 +85,7 @@ def get_codes(table_mapping, index_name):
|
||||
sql = f"SELECT code, code_name FROM {table_mapping[index_name]['codes']} LIMIT 1"
|
||||
else:
|
||||
# 否则查询所有数据
|
||||
sql = f"SELECT code, code_name FROM {table_mapping[index_name]['codes']}"
|
||||
sql = f"SELECT code, code_name FROM {table_mapping[index_name]['codes']} "
|
||||
cursor.execute(sql)
|
||||
return cursor.fetchall()
|
||||
except pymysql.MySQLError as e:
|
||||
@ -172,8 +168,9 @@ def calculate_yield_rate(data):
|
||||
if time_diff < min_stat_years:
|
||||
continue
|
||||
|
||||
close_start = data[i]['close']
|
||||
close_end = data[j]['close']
|
||||
# 使用后复权数据,避免前复权中出现负数,0,从而影响统计。
|
||||
close_start = data[i]['hfq_close']
|
||||
close_end = data[j]['hfq_close']
|
||||
yield_rate = (close_end / close_start) - 1
|
||||
annual_yield_rate = yield_rate * 365 / (end_time_key - start_time_key).days
|
||||
|
||||
@ -182,7 +179,7 @@ def calculate_yield_rate(data):
|
||||
max_deficit_start = start_time_key
|
||||
max_deficit_end = end_time_key
|
||||
for k in range(i + 1, j):
|
||||
if data[k]['close'] > close_start:
|
||||
if data[k]['hfq_close'] > close_start:
|
||||
deficit_days = (data[k]['time_key'] - start_time_key).days
|
||||
max_deficit_days = deficit_days
|
||||
max_deficit_end = data[k]['time_key']
|
||||
@ -273,6 +270,7 @@ def main(index_name):
|
||||
compute_statistics(connection, table_mapping, index_name, code, name, results)
|
||||
|
||||
logging.info(f"完成 {code} 的处理")
|
||||
time.sleep(1)
|
||||
except Exception as e:
|
||||
logging.error(f"处理过程中出现错误: {e}", exc_info=True)
|
||||
finally:
|
||||
|
||||
Reference in New Issue
Block a user