feat(slot): dcy-021 포지션 사이징 — 보유 슬롯 증액·부분 매도 (BAT-031)
자산 영구 슬롯(dcy-020)의 수량 가변 전제 위에 보유 포지션 수량 조정 구현. 명령형 계약(전략이 cash+quantity 로 qty+price 계산), core 는 실행 substrate, 수량 정본 = 브로커 잔고 재동기화로 "주문 수량 vs 보유 수량" 충돌 해소(별도 필드 없음). - state.py: holding→pending_buy 전이 추가(증액) → 13 transitions (REQ-057) - service.py: PENDING_SELL 진입 qty=caller as-is(None=전량 sentinel, 값=부분 주문) (REQ-062) - intents.py: 증액 admission(holding 슬롯 매수=increase, M 캡 미적용), SellIntent.qty 부분 매도 (REQ-061) - execution.py: _balance_map; 매도 dispatch=브로커잔고(전량)/min(주문,잔고)(부분); 매수 체결 avg_price 가중평균+브로커 정본 resync; 매도 체결 잔고>0→holding/0→inactive; 증액 롤백 holding 복귀 (REQ-066/062) - messaging_handlers.py: sell payload qty 파싱(부분 매도) - baseline 4문서(data-model/runtime-flows/interface-contract/project-structure) 정합 (REQ-067) 검증: 단위 378 / 통합(PG+RabbitMQ) 422 passed, ruff clean. run-the-app 사이징 e2e: 매수 10@1000 → 증액 +5@1200 → 15@1066.67(가중평균) → 부분 매도 6 → 9@1066.67(cost basis) → 전량→inactive. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -192,12 +192,22 @@ class SlotService:
|
||||
else: # HOLDING / ERROR — pending order limit no longer applies.
|
||||
new_limit_price = None
|
||||
|
||||
# qty semantics during a pending state = the ORDER quantity (dcy-021 D1/D4).
|
||||
# Entering PENDING_SELL takes the caller qty AS-IS so a full sell
|
||||
# (qty=None) records a "sell all" sentinel rather than the stale held
|
||||
# mirror; a partial-sell intent records its order qty. Other states keep
|
||||
# the caller-override-else-preserve rule.
|
||||
if to_state is SlotState.PENDING_SELL:
|
||||
new_qty = qty
|
||||
else:
|
||||
new_qty = qty if qty is not None else slot.qty
|
||||
|
||||
# Generic update: caller-supplied fields override; None means "keep".
|
||||
return replace(
|
||||
slot,
|
||||
status=to_state,
|
||||
symbol=symbol if symbol is not None else slot.symbol,
|
||||
qty=qty if qty is not None else slot.qty,
|
||||
qty=new_qty,
|
||||
avg_price=avg_price if avg_price is not None else slot.avg_price,
|
||||
limit_price=new_limit_price,
|
||||
allocated_capital=(
|
||||
|
||||
@@ -37,17 +37,20 @@ class SlotState(str, Enum):
|
||||
ERROR = "error"
|
||||
|
||||
|
||||
# Canonical transitions (dcy-020/REQ-057). There is no inactive self-loop and
|
||||
# no closed->idle recovery family — a permanent slot simply returns to
|
||||
# `inactive` and may activate again via inactive->pending_buy.
|
||||
# Canonical transitions (dcy-020/REQ-057; dcy-021/REQ-057 CHG-03). There is no
|
||||
# inactive self-loop and no closed->idle recovery family — a permanent slot
|
||||
# simply returns to `inactive` and may activate again via inactive->pending_buy.
|
||||
# dcy-021 adds holding->pending_buy for position-sizing increase (the slot stays
|
||||
# a held position; the buy adds to it, returning to holding on fill).
|
||||
_BASE_TRANSITIONS: frozenset[tuple[SlotState, SlotState]] = frozenset(
|
||||
{
|
||||
(SlotState.INACTIVE, SlotState.PENDING_BUY), # 1. buy decision (activate)
|
||||
(SlotState.PENDING_BUY, SlotState.HOLDING), # 2. buy filled
|
||||
(SlotState.PENDING_BUY, SlotState.INACTIVE), # 3. buy rejected/rollback
|
||||
(SlotState.HOLDING, SlotState.PENDING_BUY), # 7. position-sizing increase (dcy-021)
|
||||
(SlotState.HOLDING, SlotState.PENDING_SELL), # 4. sell decision
|
||||
(SlotState.PENDING_SELL, SlotState.INACTIVE), # 5. sell filled (deactivate)
|
||||
(SlotState.PENDING_SELL, SlotState.HOLDING), # 6. sell rejected/rollback
|
||||
(SlotState.PENDING_SELL, SlotState.HOLDING), # 6. sell rejected/rollback (or partial-sell remainder)
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user