From 6d7e6557d239f15fa99cb95a5df618e40e595fd6 Mon Sep 17 00:00:00 2001 From: agentson Date: Sat, 28 Feb 2026 14:38:10 +0900 Subject: [PATCH] fix: compute SELL decision outcome using sell quantity (#322) --- src/main.py | 8 ++++---- tests/test_main.py | 3 +++ 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/src/main.py b/src/main.py index cc158a2..6f8272c 100644 --- a/src/main.py +++ b/src/main.py @@ -1658,8 +1658,8 @@ async def trading_cycle( buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code) if buy_trade and buy_trade.get("price") is not None: buy_price = float(buy_trade["price"]) - buy_qty = int(buy_trade.get("quantity") or 1) - trade_pnl = (trade_price - buy_price) * buy_qty + sell_qty = int(quantity or 0) + trade_pnl = (trade_price - buy_price) * sell_qty decision_logger.update_outcome( decision_id=buy_trade["decision_id"], pnl=trade_pnl, @@ -2755,8 +2755,8 @@ async def run_daily_session( buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code) if buy_trade and buy_trade.get("price") is not None: buy_price = float(buy_trade["price"]) - buy_qty = int(buy_trade.get("quantity") or 1) - trade_pnl = (trade_price - buy_price) * buy_qty + sell_qty = int(quantity or 0) + trade_pnl = (trade_price - buy_price) * sell_qty decision_logger.update_outcome( decision_id=buy_trade["decision_id"], pnl=trade_pnl, diff --git a/tests/test_main.py b/tests/test_main.py index 63ee0da..cdb2651 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -2750,6 +2750,9 @@ async def test_sell_order_uses_broker_balance_qty_not_db() -> None: assert call_kwargs["order_type"] == "SELL" # Must use broker-confirmed qty (5), NOT DB-recorded ordered qty (10) assert call_kwargs["quantity"] == 5 + updated_buy = decision_logger.get_decision_by_id(buy_decision_id) + assert updated_buy is not None + assert updated_buy.outcome_pnl == -25.0 @pytest.mark.asyncio