fix: SELL outcome PnL uses sell quantity (#322) #337

Merged
jihoson merged 2 commits from feature/issue-322-sell-pnl-sell-qty into feature/v3-session-policy-stream 2026-02-28 18:21:34 +09:00
2 changed files with 7 additions and 4 deletions
Showing only changes of commit 6d7e6557d2 - Show all commits

View File

@@ -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,

View File

@@ -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