AssetManager.API/sql/add_total_cost_column.sql
OpenClaw Agent c411caea17 feat: Position表增加TotalCost字段 + 金融计算单元测试
1. Position实体增加TotalCost字段
   - 精确追踪卖出后的剩余成本
   - 避免用Shares*AvgPrice计算成本的精度问题

2. PortfolioService逻辑更新
   - 买入时更新TotalCost
   - 卖出时按比例减少TotalCost
   - 所有成本计算改用TotalCost字段

3. 增加关键计算步骤日志
   - 创建/更新持仓时记录成本变化
   - 持仓计算时记录关键数值

4. 新增金融计算单元测试
   - 卖出成本计算测试
   - 汇率变化影响测试
   - 夏普比率计算测试
   - 最大回撤计算测试
   - 边界情况测试

5. 提供数据库迁移SQL脚本
2026-03-25 04:27:40 +00:00

18 lines
635 B
SQL
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 为 positions 表添加 total_cost 字段
-- 执行前请备份数据
-- 1. 添加字段
ALTER TABLE positions ADD COLUMN total_cost DECIMAL(18,4) DEFAULT 0;
-- 2. 根据现有数据初始化 total_costshares * avg_price
UPDATE positions SET total_cost = shares * avg_price WHERE total_cost = 0 OR total_cost IS NULL;
-- 3. 验证更新结果
SELECT id, stock_code, shares, avg_price, total_cost, shares * avg_price as calculated_cost
FROM positions
LIMIT 10;
-- SQLite 版本(如果使用 SQLite
-- ALTER TABLE positions ADD COLUMN total_cost REAL DEFAULT 0;
-- UPDATE positions SET total_cost = shares * avg_price;