fix: compute SELL decision outcome using sell quantity (#322)
This commit is contained in:
@@ -1658,8 +1658,8 @@ async def trading_cycle(
|
|||||||
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
||||||
if buy_trade and buy_trade.get("price") is not None:
|
if buy_trade and buy_trade.get("price") is not None:
|
||||||
buy_price = float(buy_trade["price"])
|
buy_price = float(buy_trade["price"])
|
||||||
buy_qty = int(buy_trade.get("quantity") or 1)
|
sell_qty = int(quantity or 0)
|
||||||
trade_pnl = (trade_price - buy_price) * buy_qty
|
trade_pnl = (trade_price - buy_price) * sell_qty
|
||||||
decision_logger.update_outcome(
|
decision_logger.update_outcome(
|
||||||
decision_id=buy_trade["decision_id"],
|
decision_id=buy_trade["decision_id"],
|
||||||
pnl=trade_pnl,
|
pnl=trade_pnl,
|
||||||
@@ -2755,8 +2755,8 @@ async def run_daily_session(
|
|||||||
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
buy_trade = get_latest_buy_trade(db_conn, stock_code, market.code)
|
||||||
if buy_trade and buy_trade.get("price") is not None:
|
if buy_trade and buy_trade.get("price") is not None:
|
||||||
buy_price = float(buy_trade["price"])
|
buy_price = float(buy_trade["price"])
|
||||||
buy_qty = int(buy_trade.get("quantity") or 1)
|
sell_qty = int(quantity or 0)
|
||||||
trade_pnl = (trade_price - buy_price) * buy_qty
|
trade_pnl = (trade_price - buy_price) * sell_qty
|
||||||
decision_logger.update_outcome(
|
decision_logger.update_outcome(
|
||||||
decision_id=buy_trade["decision_id"],
|
decision_id=buy_trade["decision_id"],
|
||||||
pnl=trade_pnl,
|
pnl=trade_pnl,
|
||||||
|
|||||||
@@ -2750,6 +2750,9 @@ async def test_sell_order_uses_broker_balance_qty_not_db() -> None:
|
|||||||
assert call_kwargs["order_type"] == "SELL"
|
assert call_kwargs["order_type"] == "SELL"
|
||||||
# Must use broker-confirmed qty (5), NOT DB-recorded ordered qty (10)
|
# Must use broker-confirmed qty (5), NOT DB-recorded ordered qty (10)
|
||||||
assert call_kwargs["quantity"] == 5
|
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
|
@pytest.mark.asyncio
|
||||||
|
|||||||
Reference in New Issue
Block a user