编辑代码

import random
import matplotlib.pyplot as plt

# 初始化参数
num_stocks = 50  # 成分股数量
time_steps = 1000  # 时间步长
initial_index_value = 3000  # 初始大盘指数

# 随机生成初始股票价格和财务指标
initial_prices = [random.uniform(50, 150) for _ in range(num_stocks)]
profit_margins = [random.uniform(0.05, 0.20) for _ in range(num_stocks)]
roe_values = [random.uniform(0.1, 0.3) for _ in range(num_stocks)]
asset_values = [random.uniform(50000, 200000) for _ in range(num_stocks)]
liability_values = [random.uniform(10000, 80000) for _ in range(num_stocks)]

# 设置涨跌停板限制
price_limit_up = 0.1  # 10%涨停
price_limit_down = -0.1  # 10%跌停

# 模拟宏观经济因素
macro_factors = {
    'interest_rate': 0.02,  # 初始利率
    'economic_growth': 0.03  # 经济增长率
}

# 模拟大盘指数
index_values = [initial_index_value for _ in range(time_steps)]
daily_returns = [0 for _ in range(time_steps)]

# 模拟Fed的加降息对大盘的影响
fed_actions = [0.02, 0.03, 0.02, 0.01, 0.015]

# 模拟媒体新闻的正负反馈
media_news_influence = [1.05, 0.95] * 500  # 重复序列模拟正负反馈

# 模拟债券市场
treasury_yield = 2.0

# 模拟期货市场
futures_price = 100

# 模拟外资流入流出
foreign_capital_inflow_outflow = [random.uniform(-0.01, 0.01) for _ in range(time_steps)]

# 牛市效应参数
bull_market_confidence_boost = 0.02  # 投资者信心增强

# 大盘熔断阈值
circuit_breaker_threshold = 0.2  # 20%的跌幅触发熔断

# 黑天鹅事件
black_swan_event = [bool(random.getrandbits(1)) for _ in range(time_steps)]  # 随机触发黑天鹅事件

# 模拟股票价格变化
for i in range(1, time_steps):
    # 模拟公司盈利变化和其他财务指标变化
    pe_changes = [random.uniform(-0.1, 0.1) for _ in range(num_stocks)]
    ttm_changes = [random.uniform(-0.05, 0.05) for _ in range(num_stocks)]
    
    # 计算财务指标变化对股票价格的影响
    profit_margin_effect = [pm * random.uniform(0.01, 0.03) for pm in profit_margins]
    roe_effect = [r * random.uniform(0.01, 0.03) for r in roe_values]
    asset_effect = [a * random.uniform(0.001, 0.005) for a in asset_values]
    liability_effect = [l * random.uniform(-0.001, -0.005) for l in liability_values]
    
    # 计算股票的价格变化
    price_changes = [(pc + tc + pme + re + ae + le) for pc, tc, pme, re, ae, le in zip(pe_changes, ttm_changes, profit_margin_effect, roe_effect, asset_effect, liability_effect)]
    
    # 应用涨跌停板限制
    price_changes = [min(max(change, -price_limit_down), price_limit_up) for change in price_changes]
    
    # 更新股票价格
    for j, price_change in enumerate(price_changes):
        initial_prices[j] += price_change
    
    # 计算当日大盘指数变化
    daily_returns[i] = sum(price_changes) / num_stocks + macro_factors['economic_growth']
    
    # 更新大盘指数
    index_values[i] = index_values[i-1] * (1 + daily_returns[i])
    
    # 模拟宏观经济因素变化
    if i % len(fed_actions) == 0 and i != 0:
        current_fed_action = fed_actions[i // len(fed_actions) - 1]
        macro_factors['interest_rate'] = current_fed_action
        treasury_yield += current_fed_action
    
    # 熔断机制检查
    if daily_returns[i-1] < -circuit_breaker_threshold:
        index_values[i] = index_values[i-1]  # 维持前一时间步的指数值,触发熔断
    
    # 模拟期货价格变化
    futures_price += random.uniform(-2, 2)

# 可视化大盘指数
plt.figure(figsize=(15, 7))
plt.plot(range(time_steps), index_values, label='Index Value')
plt.axhline(y=3000, color='r', linestyle='--', label='Initial Benchmark')
plt.title('Simulated Stock Market Index with Various Factors')
plt.xlabel('Time Step')
plt.ylabel('Index Value')
plt.legend()
plt.show()