qnloft-stock/fp/涨跌停数据.py

99 lines
3.2 KiB
Python
Raw Permalink Normal View History

2023-11-24 06:42:22 +00:00
from pyecharts.charts import Line
from pyecharts import options as opts
from utils.comm import *
class LimitList:
def __init__(self, trade_date):
self.trade_date = trade_date.strftime('%Y%m%d')
self.start_date = return_trading_day(trade_date, -7)
self.end_date = self.trade_date
self.limit_df = pd.DataFrame()
for _ in range(5):
try:
# 涨跌停列表
self.limit_df = xcsc_pro.limit_list_d(start_date=self.start_date, end_date=self.end_date)
break
except Exception as e:
print("涨跌停列表数据获取失败,", e)
time.sleep(1)
continue
else:
print("涨跌停列表数据获取失败次数过多,请关注!!!")
def limit_list_chart(self):
# 按照 trade_date 和 limit 进行分组统计
grouped = self.limit_df.groupby(['trade_date', 'limit']).size().reset_index(name='count')
# 使用 pivot_table 将 limit 类型作为列,统计数量
pivot_table = grouped.pivot_table(index='trade_date', columns='limit', values='count', fill_value=0)
# print(pivot_table)
# D跌停U涨停Z炸板
# 获取不同 limit 类型的数据
u_list = pivot_table['U'].tolist()
d_list = pivot_table['D'].tolist()
z_list = pivot_table['Z'].tolist()
trade_dates = pivot_table.index.tolist()
c = (
Line(init_opts=opts.InitOpts(width="100%", height="380px"))
.add_xaxis(trade_dates)
.add_yaxis("跌停数", d_list)
.add_yaxis("涨停数", u_list)
.add_yaxis("炸板数", z_list)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger="axis"),
title_opts=opts.TitleOpts(title="涨跌停 —— 复盘", pos_left="center"),
yaxis_opts=opts.AxisOpts(offset=5),
xaxis_opts=opts.AxisOpts(offset=5, axislabel_opts=opts.LabelOpts(rotate=45, interval=0)),
legend_opts=opts.LegendOpts(pos_top="8%")
)
2023-11-24 15:29:22 +00:00
.render(f"html/{self.trade_date}/limit_list_chart.html")
2023-11-24 06:42:22 +00:00
)
def html_page_data(self):
"""
生成HTML文件
:return:
"""
print(self.limit_df)
df = self.limit_df[self.limit_df['trade_date'] == self.end_date].fillna(0).sort_values(
by=["industry", "limit_times"],
ascending=False).reset_index(drop=True)
table_content = f'<table class="table table-hover"><thead>' \
f'<tr>' \
f'<th scope="col">股票代码</th>' \
f'<th scope="col">所属行业</th>' \
f'<th scope="col">股票名称</th>' \
f'<th scope="col">换手率</th>' \
f'<th scope="col">连板数</th>' \
f'<th scope="col">状态</th>' \
f'</tr>' \
f'</thead>' \
f'<tbody>'
for index, row in df.iterrows():
table_content += f'<tr><th scope="row">{row["ts_code"]}</th>' \
f'<td>{row["industry"]}</td>' \
f'<td>{row["name"]}</td>' \
f'<td>{row["turnover_ratio"]}</td>' \
f'<td>{row["limit_times"]}</td>'
if row['limit'] == 'D':
table_content += f'<td class="table-success">跌停</td>'
elif row['limit'] == 'U':
table_content += f'<td class="table-danger">涨停</td>'
elif row['limit'] == 'Z':
table_content += f'<td class="table-warning">炸板</td>'
table_content += '</tr>'
table_content += f'</tbody></table>'
return table_content
if __name__ == '__main__':
limit = LimitList(datetime.strptime('20231108', "%Y%m%d"))
# limit.limit_list_chart()
limit.html_page_data()