#!/usr/bin/env python3
import json
import sys

# 读取实时数据
with open('data/temp_realtime.json', 'r') as f:
    data = json.load(f)

prices = data['prices']
holdings = data['holdings']

print("=== 关键价位检查 ===")

# 检查每个持仓的关键价位
for holding in holdings:
    code = holding['code']
    name = holding['name']
    current_price = holding['price']
    analysis = holding.get('analysis', {})
    
    stop_loss = analysis.get('stop_loss')
    take_profit = analysis.get('take_profit')
    
    if stop_loss and stop_loss != '':
        try:
            stop_loss_val = float(stop_loss)
            distance_pct = (current_price - stop_loss_val) / stop_loss_val * 100
            if distance_pct < 5:  # 距离止损不到5%
                print(f"⚠️ {name}({code}) 现价{current_price} 距止损{stop_loss_val}仅{abs(distance_pct):.1f}%")
        except:
            pass
    
    if take_profit and take_profit != '':
        try:
            take_profit_val = float(take_profit)
            distance_pct = (take_profit_val - current_price) / current_price * 100
            if distance_pct < 5:  # 距离止盈不到5%
                print(f"🎯 {name}({code}) 现价{current_price} 距止盈{take_profit_val}仅{abs(distance_pct):.1f}%")
        except:
            pass

# 检查涨跌幅超过5%的股票
print("\n=== 异动股票检查（涨跌幅>5%） ===")
for code, price_data in prices.items():
    change_pct = price_data['change_pct']
    if abs(change_pct) >= 5:
        name = price_data['name']
        price = price_data['price']
        print(f"{'📈' if change_pct > 0 else '📉'} {name}({code}) 现价{price} {change_pct:+.2f}%")

# 检查决策库中的操作区间
print("\n=== 决策库操作区间检查 ===")
# 这里需要读取决策库，但数据太大，我们只检查几个关键股票
key_stocks = ['06160', '600110', '688411', '01478']

for code in key_stocks:
    if code in prices:
        price_data = prices[code]
        name = price_data['name']
        price = price_data['price']
        
        # 根据历史回顾判断
        if code == '06160':
            print(f"🔵 {name}({code}) 现价{price} → 两批试仓已完成，止损160安全，目标175/185")
        elif code == '600110':
            print(f"🔵 {name}({code}) 现价{price} → 6月4日已按11.5~11.8加仓，现价仍在区间内")
        elif code == '688411':
            print(f"⚠️ {name}({code}) 现价{price} → 大涨10.87%，追踪止盈290接近，注意风险")
        elif code == '01478':
            print(f"⚠️ {name}({code}) 现价{price} → 反弹7.44%，深套股反弹至13~14可减仓")