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%") ) .render(f"html/{self.trade_date}/limit_list_chart.html") ) 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'
股票代码 | ' \ f'所属行业 | ' \ f'股票名称 | ' \ f'换手率 | ' \ f'连板数 | ' \ f'状态 | ' \ f'||
---|---|---|---|---|---|---|---|
{row["ts_code"]} | ' \ f'{row["industry"]} | ' \ f'{row["name"]} | ' \ f'{row["turnover_ratio"]} | ' \ f'{row["limit_times"]} | ' if row['limit'] == 'D': table_content += f'跌停 | ' elif row['limit'] == 'U': table_content += f'涨停 | ' elif row['limit'] == 'Z': table_content += f'炸板 | ' table_content += '