综合python案例,市场热度偏向哪一方?

wen python案例 1

本文目录导读:

综合python案例,市场热度偏向哪一方?

  1. 先明确:市场热度可能指哪些“双方”
  2. 综合 Python 分析框架
  3. 典型案例结论示例
  4. 关键提醒

要判断“市场热度偏向哪一方”,需要先明确 “哪一方”具体指什么,不同维度下,结论可能完全不同。

下面我用 Python 综合案例,从几个常见维度来分析市场热度偏向。

先明确:市场热度可能指哪些“双方”

维度 一方 另一方
买卖方向 买方(看多) 卖方(看空)
资金流向 主力/大单流入 主力/大单流出
涨跌情绪 上涨家数 下跌家数
交易活跃度 高换手/高成交 低换手/低成交
板块轮动 成长/科技 价值/防御
期货持仓 多头持仓 空头持仓

综合 Python 分析框架

下面用一个模拟但结构完整的案例,综合判断市场热度偏向。

数据准备(模拟)

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(42)
# 模拟 30 个交易日、5 个板块的数据
dates = pd.date_range("2024-01-01", periods=30)
sectors = ["科技", "金融", "消费", "医药", "能源"]
df = pd.DataFrame({
    "date": np.repeat(dates, len(sectors)),
    "sector": sectors * len(dates),
    "涨跌幅": np.random.normal(0, 2, 30*5),
    "成交额": np.random.uniform(50, 500, 30*5),
    "主力净流入": np.random.normal(0, 10, 30*5),
    "换手率": np.random.uniform(0.5, 8, 30*5),
})
# 模拟市场涨跌家数
breadth = pd.DataFrame({
    "date": dates,
    "上涨家数": np.random.randint(1000, 3000, 30),
    "下跌家数": np.random.randint(1000, 3000, 30),
})

多维度热度计算

# ---------- 维度1:涨跌家数(情绪面) ----------
breadth["情绪差值"] = breadth["上涨家数"] - breadth["下跌家数"]
breadth["情绪偏向"] = np.where(breadth["情绪差值"] > 0, "偏多", "偏空")
emotion_score = breadth["情绪差值"].mean()
print(f"平均涨跌家数差:{emotion_score:.1f}")
# ---------- 维度2:主力资金(资金面) ----------
fund_flow = df.groupby("date")["主力净流入"].sum()
fund_score = fund_flow.mean()
print(f"平均主力净流入:{fund_score:.2f} 亿")
# ---------- 维度3:板块涨跌(结构面) ----------
sector_perf = df.groupby("sector")["涨跌幅"].mean().sort_values(ascending=False)
print("\n各板块平均涨跌幅:")
print(sector_perf)
# ---------- 维度4:成交活跃度 ----------
volume_score = df.groupby("date")["成交额"].sum()
volume_trend = volume_score.pct_change().mean()
print(f"\n成交额平均日变化:{volume_trend:.2%}")
# ---------- 维度5:换手率(投机热度) ----------
turnover_score = df["换手率"].mean()
print(f"平均换手率:{turnover_score:.2f}%")

综合打分模型

def normalize(x, min_val, max_val):
    return (x - min_val) / (max_val - min_val)
# 将各维度映射到 0~1 分数,0=极度偏空,1=极度偏多
score_emotion = normalize(emotion_score, -2000, 2000)
score_fund = normalize(fund_score, -20, 20)
score_volume = normalize(volume_trend, -0.05, 0.05)
score_turnover = normalize(turnover_score, 0.5, 8)
score_sector = normalize(sector_perf.iloc[0] - sector_perf.iloc[-1], 0, 10)
# 加权综合(权重可调)
weights = {
    "情绪面": 0.30,
    "资金面": 0.25,
    "成交活跃": 0.15,
    "投机热度": 0.10,
    "板块分化": 0.20,
}
total_score = (
    score_emotion * weights["情绪面"] +
    score_fund * weights["资金面"] +
    score_volume * weights["成交活跃"] +
    score_turnover * weights["投机热度"] +
    score_sector * weights["板块分化"]
)
print(f"\n综合热度得分:{total_score:.3f}")
if total_score > 0.6:
    print("市场热度明显偏向【多方】")
elif total_score < 0.4:
    print("市场热度明显偏向【空方】")
else:
    print("市场热度【多空均衡/震荡】")

可视化

fig, axes = plt.subplots(2, 2, figsize=(12, 8))
# 涨跌家数
axes[0,0].plot(breadth["date"], breadth["上涨家数"], label="上涨")
axes[0,0].plot(breadth["date"], breadth["下跌家数"], label="下跌")
axes[0,0].set_title("涨跌家数")
axes[0,0].legend()
# 主力资金
axes[0,1].bar(fund_flow.index, fund_flow.values, color=np.where(fund_flow>0, "red", "green"))
axes[0,1].set_title("主力净流入")
# 板块表现
sector_perf.plot(kind="barh", ax=axes[1,0], color="steelblue")
axes[1,0].set_title("板块平均涨跌幅")
# 综合得分
axes[1,1].barh(["综合热度"], [total_score], color="orange")
axes[1,1].axvline(0.5, color="gray", linestyle="--")
axes[1,1].set_xlim(0, 1)
axes[1,1].set_title("综合热度得分(>0.5偏多)")
plt.tight_layout()
plt.show()

典型案例结论示例

假设运行结果如下:

平均涨跌家数差:+350
平均主力净流入:+3.2 亿
成交额平均日变化:+1.5%
平均换手率:4.2%
板块领涨:科技 +3.1%,领跌:能源 -1.2%
综合热度得分:0.68
市场热度明显偏向【多方】

解读:

  • 情绪面:上涨家数多于下跌家数 → 偏多
  • 资金面:主力净流入为正 → 偏多
  • 结构面:科技领涨、能源落后 → 成长风格占优
  • 投机面:换手率中等偏高 → 活跃但未过热
  • 综合:多头占优,但需警惕高位分化

关键提醒

  1. 数据源决定结论:以上为模拟数据,真实分析需接入 akshare、tushare、yfinance 等。
  2. 权重主观性强:不同权重会得出不同结论,建议做敏感性分析。
  3. 热度 ≠ 未来涨跌:热度是同步/滞后指标,不能直接预测。
  4. 分维度看更可靠:不要只看一个总分,要拆开看情绪、资金、结构是否共振。

如果你能告诉我:

  • 你关注的是A股/美股/期货/加密货币?
  • “两方”具体指多空、板块、还是资金?
  • 有没有真实数据源?

我可以给你一套可直接运行的完整代码,输出明确结论。

上一篇综合赛后python案例,新赛季格局有何变化?

下一篇当前分类已是最新一篇

抱歉,评论功能暂时关闭!