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
price_limit_down = -0.1
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_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
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()