feat: 添加手动修改持仓功能
- 新增 manual_add_position: 手动添加持仓(不扣现金)
- 新增 manual_update_position: 手动更新持仓信息
- 新增 manual_delete_position: 手动删除持仓
- 新增 manual_set_cash: 手动设置现金金额
- 新增 clear_all_positions: 清空所有持仓
- 更新持仓管理菜单,添加选项9「手动修改持仓」
- 修复止损止盈计算中使用 avg_price 替代 cost_price
| | |
| | | print("6. 买入操作") |
| | | print("7. 卖出操作") |
| | | print("8. 持仓调整建议") |
| | | print("9. 手动修改持仓 ⭐新增") |
| | | print("0. 返回") |
| | | |
| | | choice = input("\n请选择 (0-8): ").strip() |
| | | choice = input("\n请选择 (0-9): ").strip() |
| | | |
| | | if choice == '1': |
| | | print_info("凯利公式仓位计算...") |
| | |
| | | print(f"\n{Color.BOLD}持仓止损止盈状态:{Color.ENDC}") |
| | | for code, pos in tracker.positions.items(): |
| | | profit_pct = pos.profit_loss_pct |
| | | stop_loss_price = pos.cost_price * 0.85 |
| | | take_profit_price = pos.cost_price * 1.30 |
| | | stop_loss_price = pos.avg_price * 0.85 |
| | | take_profit_price = pos.avg_price * 1.30 |
| | | |
| | | status = "正常" |
| | | if pos.current_price <= stop_loss_price: |
| | |
| | | print_info("生成持仓调整建议...") |
| | | tracker = PortfolioTracker() |
| | | |
| | | elif choice == '9': |
| | | print_info("手动修改持仓...") |
| | | tracker = PortfolioTracker() |
| | | |
| | | print(f"\n{Color.BOLD}手动持仓管理{Color.ENDC}") |
| | | print("1. 添加持仓") |
| | | print("2. 修改持仓") |
| | | print("3. 删除持仓") |
| | | print("4. 设置现金") |
| | | print("5. 清空所有持仓") |
| | | print("6. 查看当前持仓") |
| | | print("0. 返回") |
| | | |
| | | sub_choice = input("\n请选择 (0-6): ").strip() |
| | | |
| | | if sub_choice == '1': |
| | | print(f"\n{Color.BOLD}添加新持仓{Color.ENDC}") |
| | | stock_code = input("股票代码 (如 sh600519): ").strip() |
| | | stock_name = input("股票名称: ").strip() |
| | | quantity = int(input("持仓数量 (股): ")) |
| | | avg_price = float(input("成本价: ")) |
| | | current_price_input = input("当前价格 (回车=成本价): ").strip() |
| | | current_price = float(current_price_input) if current_price_input else None |
| | | sector = input("所属行业 (可选): ").strip() |
| | | |
| | | try: |
| | | tracker.manual_add_position(stock_code, stock_name, quantity, avg_price, current_price, sector) |
| | | print_success("持仓添加成功!") |
| | | except Exception as e: |
| | | print_error(f"添加失败: {e}") |
| | | |
| | | elif sub_choice == '2': |
| | | print(f"\n{Color.BOLD}修改持仓{Color.ENDC}") |
| | | if not tracker.positions: |
| | | print_warning("当前无持仓") |
| | | else: |
| | | print("当前持仓:") |
| | | for i, (code, pos) in enumerate(tracker.positions.items(), 1): |
| | | print(f" {i}. {pos.stock_name}({code}): {pos.quantity}股 @ {pos.avg_price:.2f}元") |
| | | |
| | | idx = input("\n选择要修改的持仓序号 (或输入股票代码): ").strip() |
| | | |
| | | if idx.isdigit(): |
| | | idx = int(idx) - 1 |
| | | stock_code = list(tracker.positions.keys())[idx] if 0 <= idx < len(tracker.positions) else None |
| | | else: |
| | | stock_code = idx |
| | | |
| | | if stock_code and stock_code in tracker.positions: |
| | | pos = tracker.positions[stock_code] |
| | | print(f"\n当前: {pos.stock_name} {pos.quantity}股 @ {pos.avg_price:.2f}元") |
| | | |
| | | print("\n输入新值 (回车保持不变):") |
| | | new_quantity = input(f"持仓数量 [{pos.quantity}]: ").strip() |
| | | new_avg_price = input(f"成本价 [{pos.avg_price:.2f}]: ").strip() |
| | | new_current_price = input(f"当前价 [{pos.current_price:.2f}]: ").strip() |
| | | |
| | | updates = {} |
| | | if new_quantity: |
| | | updates['quantity'] = int(new_quantity) |
| | | if new_avg_price: |
| | | updates['avg_price'] = float(new_avg_price) |
| | | if new_current_price: |
| | | updates['current_price'] = float(new_current_price) |
| | | |
| | | if updates: |
| | | try: |
| | | tracker.manual_update_position(stock_code, **updates) |
| | | print_success("持仓更新成功!") |
| | | except Exception as e: |
| | | print_error(f"更新失败: {e}") |
| | | else: |
| | | print_info("未做任何修改") |
| | | else: |
| | | print_error("无效的选择") |
| | | |
| | | elif sub_choice == '3': |
| | | print(f"\n{Color.BOLD}删除持仓{Color.ENDC}") |
| | | if not tracker.positions: |
| | | print_warning("当前无持仓") |
| | | else: |
| | | print("当前持仓:") |
| | | for i, (code, pos) in enumerate(tracker.positions.items(), 1): |
| | | print(f" {i}. {pos.stock_name}({code}): {pos.quantity}股") |
| | | |
| | | idx = input("\n选择要删除的持仓序号 (或输入股票代码): ").strip() |
| | | |
| | | if idx.isdigit(): |
| | | idx = int(idx) - 1 |
| | | stock_code = list(tracker.positions.keys())[idx] if 0 <= idx < len(tracker.positions) else None |
| | | else: |
| | | stock_code = idx |
| | | |
| | | if stock_code and stock_code in tracker.positions: |
| | | confirm = input(f"确认删除 {tracker.positions[stock_code].stock_name}? (y/n): ").strip().lower() |
| | | if confirm == 'y': |
| | | try: |
| | | tracker.manual_delete_position(stock_code) |
| | | print_success("持仓已删除!") |
| | | except Exception as e: |
| | | print_error(f"删除失败: {e}") |
| | | else: |
| | | print_error("无效的选择") |
| | | |
| | | elif sub_choice == '4': |
| | | print(f"\n{Color.BOLD}设置现金{Color.ENDC}") |
| | | print(f"当前现金: ¥{tracker.cash:,.2f}") |
| | | new_cash = input("输入新的现金金额: ").strip() |
| | | if new_cash: |
| | | try: |
| | | tracker.manual_set_cash(float(new_cash)) |
| | | print_success("现金已更新!") |
| | | except Exception as e: |
| | | print_error(f"设置失败: {e}") |
| | | |
| | | elif sub_choice == '5': |
| | | print(f"\n{Color.BOLD}清空所有持仓{Color.ENDC}") |
| | | print_warning(f"当前有 {len(tracker.positions)} 只持仓") |
| | | confirm = input("确认清空所有持仓? (y/n): ").strip().lower() |
| | | if confirm == 'y': |
| | | tracker.clear_all_positions() |
| | | print_success("所有持仓已清空!") |
| | | |
| | | elif sub_choice == '6': |
| | | print(f"\n{Color.BOLD}当前持仓{Color.ENDC}") |
| | | if not tracker.positions: |
| | | print_warning("当前无持仓") |
| | | else: |
| | | print(f"现金: ¥{tracker.cash:,.2f}") |
| | | print(f"持仓市值: ¥{tracker.portfolio_value:,.2f}") |
| | | print(f"总资产: ¥{tracker.total_assets:,.2f}") |
| | | print() |
| | | for code, pos in tracker.positions.items(): |
| | | profit_color = Color.OKGREEN if pos.profit_loss_pct > 0 else Color.FAIL |
| | | print(f" {pos.stock_name}({code}): {pos.quantity}股") |
| | | print(f" 成本: {pos.avg_price:.2f} → 当前: {pos.current_price:.2f}") |
| | | print(f" 盈亏: {profit_color}{pos.profit_loss_pct:.2f}%{Color.ENDC}") |
| | | except Exception as e: |
| | | print_error(f"持仓管理启动失败: {e}") |
| | | import traceback |
| | |
| | | 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("✓ 已清空所有持仓") |
| | | |
| | | |
| | | # 测试代码 |
| | |
| | | { |
| | | "last_update": "2026-03-05 18:30:31", |
| | | "last_update": "2026-03-06 00:47:38", |
| | | "total_assets": 1079934.8364929906, |
| | | "cash": 407857.0, |
| | | "portfolio_value": 672077.8364929907, |
| | |
| | | "profit_loss": 3384.755387851357, |
| | | "profit_loss_pct": 2.889002550231613, |
| | | "entry_date": "2026-03-05", |
| | | "holding_days": 0 |
| | | "holding_days": 0, |
| | | "sector": "", |
| | | "alpha_score": 0.0 |
| | | }, |
| | | { |
| | | "stock_code": "sh600938", |
| | |
| | | "profit_loss": -12354.085526428564, |
| | | "profit_loss_pct": -10.435956687302385, |
| | | "entry_date": "2026-03-05", |
| | | "holding_days": 0 |
| | | "holding_days": 0, |
| | | "sector": "", |
| | | "alpha_score": 0.0 |
| | | }, |
| | | { |
| | | "stock_code": "sh600410", |
| | |
| | | "profit_loss": 19601.399605008395, |
| | | "profit_loss_pct": 16.467889576409245, |
| | | "entry_date": "2026-03-05", |
| | | "holding_days": 0 |
| | | "holding_days": 0, |
| | | "sector": "", |
| | | "alpha_score": 0.0 |
| | | }, |
| | | { |
| | | "stock_code": "sh601857", |
| | |
| | | "profit_loss": 102719.66087625426, |
| | | "profit_loss_pct": 86.18867333130916, |
| | | "entry_date": "2026-03-05", |
| | | "holding_days": 0 |
| | | "holding_days": 0, |
| | | "sector": "", |
| | | "alpha_score": 0.0 |
| | | }, |
| | | { |
| | | "stock_code": "sh600989", |
| | |
| | | "profit_loss": -33416.893849694694, |
| | | "profit_loss_pct": -28.22491984433016, |
| | | "entry_date": "2026-03-05", |
| | | "holding_days": 0 |
| | | } |
| | | ], |
| | | "trade_history": [ |
| | | { |
| | | "date": "2026-03-05 02:35:09", |
| | | "action": "buy", |
| | | "stock_code": "sh601899", |
| | | "stock_name": "紫金矿业", |
| | | "price": 40.4, |
| | | "quantity": 2900, |
| | | "amount": 117160.0 |
| | | }, |
| | | { |
| | | "date": "2026-03-05 02:35:09", |
| | | "action": "buy", |
| | | "stock_code": "sh600938", |
| | | "stock_name": "中国海油", |
| | | "price": 39.46, |
| | | "quantity": 3000, |
| | | "amount": 118380.0 |
| | | }, |
| | | { |
| | | "date": "2026-03-05 02:35:09", |
| | | "action": "buy", |
| | | "stock_code": "sh600410", |
| | | "stock_name": "华胜天成", |
| | | "price": 28.34, |
| | | "quantity": 4200, |
| | | "amount": 119028.0 |
| | | }, |
| | | { |
| | | "date": "2026-03-05 02:35:09", |
| | | "action": "buy", |
| | | "stock_code": "sh601857", |
| | | "stock_name": "中国石油", |
| | | "price": 11.8, |
| | | "quantity": 10100, |
| | | "amount": 119180.0 |
| | | }, |
| | | { |
| | | "date": "2026-03-05 02:35:09", |
| | | "action": "buy", |
| | | "stock_code": "sh600989", |
| | | "stock_name": "宝丰能源", |
| | | "price": 26.31, |
| | | "quantity": 4500, |
| | | "amount": 118395.0 |
| | | "holding_days": 0, |
| | | "sector": "", |
| | | "alpha_score": 0.0 |
| | | } |
| | | ] |
| | | } |