当前位置: 首页 > news >正文

市场宽度实时定时版

from jqdata import *
import datetime as dt
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import json, textwrap# 获取股票和价格数据
stocks = get_index_stocks("000985.XSHG")
now = dt.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
yesterday = (dt.datetime.now() - dt.timedelta(days=1)).strftime("%Y-%m-%d")count = 90  #1年的交易日
price_hist = get_price(stocks,end_date=yesterday,frequency="1d",fields=["close"],count=count + 19,panel=False)# 获取行业信息
def getStockIndustry(stocks):industry = get_industry(stocks)dict = {stock: info["sw_l1"]["industry_name"]for stock, info in industry.items()if "sw_l1" in info}industry_2_stock = {}for stock, info in industry.items():if "sw_l1" in info:industry_name = info["sw_l1"]["industry_name"]ls = industry_2_stock.get(industry_name, [])ls.append(stock)industry_2_stock[industry_name] = lsreturn pd.Series(dict), industry_2_stockdef update_price():cur_price = get_price(stocks,end_date=now,frequency="1m",fields=["close"],count=1,panel=False)cur_price['time'] = pd.to_datetime(cur_price['time']).dt.floor('d')new_price = pd.concat([price_hist, cur_price], ignore_index=True)new_price= new_price.sort_values(['code', 'time']).reset_index(drop=True)return new_pricedef update_df():# 处理数据h = update_price()h["date"] = pd.DatetimeIndex(h.time).datedf_close = h.pivot(index="code", columns="date", values="close").dropna(axis=0)df_ma20 = df_close.rolling(window=20, axis=1).mean().iloc[:, -count:]df_bias = df_close.iloc[:, -count:] > df_ma20df_bias["industry_name"], industry_2_stock = getStockIndustry(stocks)df_ratio = ((df_bias.groupby("industry_name").sum() * 100.0)/ df_bias.groupby("industry_name").count()).round()df_ratio.loc['合计'] = df_ratio.sum().astype("int32")DF = df_ratio.Treturn DF#======================================================================def update_html(DF):industry_cols = DF.columns[:-1]df = DF.iloc[:, :-1].copy()  # 创建数据框副本,避免修改原始数据df.insert(len(industry_cols), '市场平均', df[industry_cols].mean(axis=1).round().astype(int))df.index = pd.to_datetime(df.index)use_cols = [c for c in df.columns if c not in {'合计'}]# 1. 只保留 [x, y, value] 三元组给 EChartsdata_js = [[j, i, int(df.iloc[i, j])]for i in range(len(df))for j in range(len(df.columns))]
#     print("data_js:", data_js)# 2. 行业名、日期单独传两份数组,前端用索引去取cols   = df.columns.tolist()
#     print("cols:", cols)dates  = [str(d) for d in df.index.date]
#     print("dates:", dates)
cell_w  = 40          # 每列宽 80 pxcell_h  = 40          # 每行高 26 px(足够看清文字)width   = len(use_cols) * cell_w    # 留边给 visualMapheight  = len(df)        * cell_h    # 随行情自动增高# 写进单文件 HTML
html = textwrap.dedent(f'''\<!doctype html><html><head><meta charset="utf-8"><title>行业热力图(可点击)</title><script src="https://cdn.jsdelivr.net/npm/echarts@5/dist/echarts.min.js"></script><style>#wrapper{{width:100%;max-height:70vh;overflow:auto;border:1px solid #ddd}}#chart{{width:{width}px;height:{height}px;margin:auto}}</style></head><body><div id="chart" style="width:{{width}}px;height:{{height}}px;margin:auto"></div><script>const raw      = {json.dumps(data_js)};const cols     = {json.dumps(cols)};const dates    = {json.dumps(dates)};const myChart = echarts.init(document.getElementById('chart'));const option = {{tooltip: {{position: 'top',formatter: p => {{const [x, y, val] = p.data;return `${{dates[y]}}<br/>${{cols[x]}}: <b>${{val}}</b>`;}}}},animation: true,grid: {{left: '10%', right: '5%', bottom: '5%', top: '2%'}},xAxis: {{type: 'category',data: cols,position: 'top', axisLabel: {{interval: 0,rotate: 45,fontSize: 11}},splitArea: {{show: true}}}},yAxis: {{type: 'category',data: dates,splitArea: {{show: true}}}},visualMap: {{min: 0, max: 100, calculable: true, type: 'continuous',orient: 'horizontal', left: 'center', bottom: '2%',inRange: {{color: ['#005824','#238b45','#5cb85c','#90ee90','#ffff99','#ffaa99','#ff7d66','#f54437','#b00000']}}}},series: [{{name: '热力图',type: 'heatmap',data: raw,label: {{show: true, fontSize: 11, color: '#000'}},itemStyle: {{borderWidth: 1, borderColor: '#fff'}},emphasis: {{itemStyle: {{shadowBlur: 12, shadowColor: 'rgba(0,0,0,.4'}}}}}}]}};myChart.setOption(option);// 点击事件myChart.on('click', params => {{const [,,val,ind,dt] = params.data;alert(`${{dt}}\\n${{ind}}: ${{val}}`);}});</script></body></html>''')open('行业宽度-定时刷新版.html','w',encoding='utf-8').write(html)print("可以打开-行业宽度-定时刷新版.html")# 定时刷新 10分钟一次
while True:DF = update_df()update_html(DF)time.sleep(60 * 10) 

 

http://www.hskmm.com/?act=detail&tid=27304

相关文章:

  • 2025 年光伏展会预定,上海伏勒密科技有限公司打造覆盖全产业链的国际化新能源会展服务平台
  • ant-design-vue 4.x版本在谷歌浏览器80版本中样式不显示的问题
  • 实验结论
  • 2025 年喷雾干燥机厂家最新推荐:国内实力企业排行榜,含离心式 / 压力式 / 实验型设备品牌深度解析
  • oracle修改监听端口
  • 2025 年干燥机厂家最新推荐排行榜:聚焦国内优质干燥机厂家,精选实力品牌助力企业精准选购
  • Wireshark 4.6.0 for macOS, Windows - 网络协议分析器
  • 精密弹簧厂家最新推荐榜:高精度与耐用性能的工业优选
  • pyLDAPGui开发历程:跨平台LDAP图形化工具诞生记
  • 3D计算机视觉与图形学研究成果获奖
  • 2025 年最新推荐!等离子清洗机源头厂家权威榜单发布,涵盖大气 / 真空 / 宽幅 / 微波 / 自动化等多类型设备厂家推荐
  • JeecgBoot低代码 v3.8.3 大版本发布,组织架构革新+全面迈向 Spring Boot 3 时代
  • 2025年9月文章一览
  • DevOps技术演进:混合云时代下的本土化突围与智能化未来
  • 2025 年模块电源厂家最新推荐排行榜:dc/dc、ac/dc 及工业级模块电源优质企业全面解析与选购参考
  • 【AI生成】小模型微调技术浅析
  • [iOS] YYModel 初步学习 - 教程
  • 吴恩达深度学习课程一:神经网络和深度学习 第二周:神经网络基础(三)
  • 分治
  • qwen3:0.6b模型的基本参数存在的价值应用场景分析
  • Gitee Insight领航研发效能工具市场:国产化与智能化双轮驱动下的技术突围
  • 【大数据】AI就业影响数据可视化分析系统 计算机毕业设计项目 Anaconda+Django+Spark+Hadoop环境调整 附源码+文档+讲解
  • 2026 航空航天、智能感知与控制国际学术会议
  • Trae 或 vscode无法在vue文件中自动跳转解决
  • 实用指南:小白也能学会的大模型构建:DeepSeek实战教程+代码解析
  • 安徽京准:NTP网络时间服务器技术应用方案
  • 2025工矿灯厂家TOP5推荐:高光效与耐用性深度评测
  • ​​无源探头与高压差分探头的技术比较与应用选择​​
  • PKDV5351高压差分探头在氢燃料电池堆电压均衡监测中的实战应用案例
  • 2025秋_8