log
2026-07-23 c9a0711438d32f33b65ffa29d8efd7aa37d61bdc
code/portfolio/portfolio_tracker.py
@@ -17,15 +17,16 @@
    """持仓数据类"""
    stock_code: str
    stock_name: str
    cost_price: float  # 成本价
    current_price: float  # 当前价
    shares: float  # 持仓股数
    market_value: float  # 市值
    profit_loss: float  # 盈亏金额
    profit_loss_pct: float  # 盈亏百分比
    holding_days: int  # 持仓天数
    quantity: int = 0  # 持仓股数
    avg_price: float = 0.0  # 成本价
    cost_basis: float = 0.0  # 成本总额
    current_price: float = 0.0  # 当前价
    current_value: float = 0.0  # 市值
    profit_loss: float = 0.0  # 盈亏金额
    profit_loss_pct: float = 0.0  # 盈亏百分比
    entry_date: str = ""  # 建仓日期
    holding_days: int = 0  # 持仓天数
    sector: str = ""  # 所属行业
    buy_date: str = ""  # 建仓日期
    alpha_score: float = 0.0  # α因子得分
@dataclass
@@ -84,6 +85,17 @@
                print(f"✓ 加载持仓状态: {len(self.positions)}只股票")
            except Exception as e:
                print(f"⚠️ 加载持仓状态失败: {e}")
    @property
    def state(self) -> Dict:
        """获取持仓状态"""
        return {
            'last_update': datetime.now().strftime('%Y-%m-%d %H:%M:%S'),
            'total_assets': self.total_assets,
            'cash': self.cash,
            'portfolio_value': self.portfolio_value,
            'positions': [asdict(pos) for pos in self.positions.values()]
        }
    
    def _save_state(self):
        """保存持仓状态"""
@@ -155,20 +167,19 @@
        
        # 判断是建仓还是加仓
        if stock_code in self.positions:
            # 加仓
            position = self.positions[stock_code]
            old_shares = position.shares
            old_amount = position.cost_price * old_shares
            old_shares = position.quantity
            old_amount = position.avg_price * old_shares
            
            # 重新计算成本价
            total_shares = old_shares + shares
            total_amount = old_amount + amount
            new_cost_price = total_amount / total_shares
            
            position.shares = total_shares
            position.cost_price = new_cost_price
            position.quantity = total_shares
            position.avg_price = new_cost_price
            position.cost_basis = total_amount
            position.current_price = price
            position.market_value = position.shares * price
            position.current_value = position.quantity * price
            position.alpha_score = alpha_score
            if sector:
                position.sector = sector
@@ -176,19 +187,19 @@
            action = 'add'
            description = f"加仓: 从{old_shares:.0f}股增至{total_shares:.0f}股"
        else:
            # 建仓
            position = Position(
                stock_code=stock_code,
                stock_name=stock_name,
                cost_price=price,
                quantity=shares,
                avg_price=price,
                cost_basis=price * shares,
                current_price=price,
                shares=shares,
                market_value=price * shares,
                current_value=price * shares,
                profit_loss=0.0,
                profit_loss_pct=0.0,
                entry_date=today,
                holding_days=0,
                sector=sector,
                buy_date=today,
                alpha_score=alpha_score
            )
            self.positions[stock_code] = position
@@ -241,17 +252,15 @@
        amount = price * shares
        
        # 判断是减仓还是清仓
        if shares >= position.shares:
            # 清仓
        if shares >= position.quantity:
            action = 'sell'
            description = f"清仓: 卖出{position.shares:.0f}股"
            description = f"清仓: 卖出{position.quantity:.0f}股"
            del self.positions[stock_code]
        else:
            # 减仓
            action = 'reduce'
            description = f"减仓: 从{position.shares:.0f}股减至{position.shares - shares:.0f}股"
            position.shares -= shares
            position.market_value = position.shares * price
            description = f"减仓: 从{position.quantity:.0f}股减至{position.quantity - shares:.0f}股"
            position.quantity -= shares
            position.current_value = position.quantity * price
        
        # 更新现金
        self.cash += amount
@@ -279,7 +288,7 @@
    
    def _update_portfolio_value(self):
        """更新持仓市值"""
        self.portfolio_value = sum(pos.market_value for pos in self.positions.values())
        self.portfolio_value = sum(pos.current_value for pos in self.positions.values())
        self.total_assets = self.cash + self.portfolio_value
    
    def update_prices(self, stock_prices: Dict[str, float]):
@@ -293,9 +302,9 @@
            if stock_code in self.positions:
                position = self.positions[stock_code]
                position.current_price = price
                position.market_value = position.shares * price
                position.profit_loss = (price - position.cost_price) * position.shares
                position.profit_loss_pct = (price - position.cost_price) / position.cost_price * 100
                position.current_value = position.quantity * price
                position.profit_loss = (price - position.avg_price) * position.quantity
                position.profit_loss_pct = (price - position.avg_price) / position.avg_price * 100 if position.avg_price > 0 else 0
        
        self._update_portfolio_value()
        self._save_state()
@@ -315,10 +324,10 @@
            data.append({
                '股票代码': position.stock_code,
                '股票名称': position.stock_name,
                '成本价': position.cost_price,
                '成本价': position.avg_price,
                '当前价': position.current_price,
                '持仓数': position.shares,
                '市值': position.market_value,
                '持仓数': position.quantity,
                '市值': position.current_value,
                '盈亏': position.profit_loss,
                '盈亏%': position.profit_loss_pct,
                '持仓天数': position.holding_days,
@@ -385,16 +394,14 @@
            target_market_value = target_value * target_weight
            
            if stock_code in self.positions:
                # 已持仓,计算需要调整的金额
                position = self.positions[stock_code]
                current_market_value = position.market_value
                current_market_value = position.current_value
                diff_value = target_market_value - current_market_value
                
                price = current_prices.get(stock_code, position.current_price)
                
                if abs(diff_value) > 100:  # 忽略微小调整
                if abs(diff_value) > 100:
                    if diff_value > 0:
                        # 需要加仓
                        shares = diff_value / price
                        rebalance_actions.append({
                            'stock_code': stock_code,
@@ -408,9 +415,8 @@
                            )
                        })
                    else:
                        # 需要减仓
                        shares = abs(diff_value) / price
                        shares = min(shares, position.shares)  # 不能超过持仓
                        shares = min(shares, position.quantity)
                        rebalance_actions.append({
                            'stock_code': stock_code,
                            'stock_name': position.stock_name,
@@ -538,6 +544,119 @@
        report.append("=" * 60)
        
        return "\n".join(report)
    def manual_add_position(self, stock_code: str, stock_name: str,
                            quantity: int, avg_price: float,
                            current_price: float = None,
                            sector: str = "", entry_date: str = None):
        """
        手动添加持仓(不扣现金,直接设置持仓状态)
        Args:
            stock_code: 股票代码
            stock_name: 股票名称
            quantity: 持仓数量
            avg_price: 成本价
            current_price: 当前价格(默认等于成本价)
            sector: 所属行业
            entry_date: 建仓日期
        """
        if current_price is None:
            current_price = avg_price
        if entry_date is None:
            entry_date = datetime.now().strftime('%Y-%m-%d')
        profit_loss = (current_price - avg_price) * quantity
        profit_loss_pct = (current_price - avg_price) / avg_price * 100 if avg_price > 0 else 0
        position = Position(
            stock_code=stock_code,
            stock_name=stock_name,
            quantity=quantity,
            avg_price=avg_price,
            cost_basis=avg_price * quantity,
            current_price=current_price,
            current_value=current_price * quantity,
            profit_loss=profit_loss,
            profit_loss_pct=profit_loss_pct,
            entry_date=entry_date,
            holding_days=0,
            sector=sector,
            alpha_score=0.0
        )
        self.positions[stock_code] = position
        self._update_portfolio_value()
        self._save_state()
        print(f"✓ 手动添加持仓: {stock_name}({stock_code}) {quantity}股 @ {avg_price:.2f}元")
        return position
    def manual_update_position(self, stock_code: str, **kwargs):
        """
        手动更新持仓信息
        Args:
            stock_code: 股票代码
            **kwargs: 要更新的字段 (quantity, avg_price, current_price, sector 等)
        """
        if stock_code not in self.positions:
            raise ValueError(f"股票{stock_code}不在持仓中")
        position = self.positions[stock_code]
        for key, value in kwargs.items():
            if hasattr(position, key):
                setattr(position, key, value)
        position.cost_basis = position.avg_price * position.quantity
        position.current_value = position.current_price * position.quantity
        position.profit_loss = (position.current_price - position.avg_price) * position.quantity
        position.profit_loss_pct = (position.current_price - position.avg_price) / position.avg_price * 100 if position.avg_price > 0 else 0
        self._update_portfolio_value()
        self._save_state()
        print(f"✓ 更新持仓: {position.stock_name}({stock_code})")
        return position
    def manual_delete_position(self, stock_code: str):
        """
        手动删除持仓
        Args:
            stock_code: 股票代码
        """
        if stock_code not in self.positions:
            raise ValueError(f"股票{stock_code}不在持仓中")
        position = self.positions[stock_code]
        del self.positions[stock_code]
        self._update_portfolio_value()
        self._save_state()
        print(f"✓ 删除持仓: {position.stock_name}({stock_code})")
    def manual_set_cash(self, cash: float):
        """
        手动设置现金
        Args:
            cash: 现金金额
        """
        self.cash = cash
        self.total_assets = self.cash + self.portfolio_value
        self._save_state()
        print(f"✓ 设置现金: ¥{cash:,.2f}")
    def clear_all_positions(self):
        """清空所有持仓"""
        self.positions.clear()
        self._update_portfolio_value()
        self._save_state()
        print("✓ 已清空所有持仓")
# 测试代码