Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 25 additions & 4 deletions application/execution_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -1128,6 +1128,7 @@ def _limit_buy_price(symbol, price, default_premium, premium_by_symbol=None) ->
DEFAULT_SAFE_HAVEN_CASH_SUBSTITUTE_THRESHOLD_USD = 1000.0
SMALL_ACCOUNT_SAFE_HAVEN_CASH_SUBSTITUTE_LIMIT_USD = 2000.0
SMALL_ACCOUNT_EXISTING_WHOLE_SHARE_RETENTION_SYMBOLS = frozenset({"TQQQ", "SOXL"})
_SMALL_ACCOUNT_RETENTION_MIN_TARGET_SHARE_RATIO_DEFAULT = 0.85
SMALL_ACCOUNT_EXISTING_WHOLE_SHARE_RETENTION_MIN_TARGET_SHARE_RATIO_BY_SYMBOL = {
"SOXX": 0.90,
}
Expand Down Expand Up @@ -1175,20 +1176,40 @@ def _apply_safe_haven_cash_substitution_to_weights(
return adjusted, tuple(dict.fromkeys(substituted))


def _should_retain_existing_whole_share(symbol, *, target_value, price) -> bool:
def _should_retain_existing_whole_share(symbol, *, target_value, price, quantity=0.0) -> bool:
"""Decide whether an existing whole-share position should be retained.

Universal rule: if the account already holds this symbol (>0 shares) and the
strategy wants to keep a meaningful fraction of a share (target >= 85% of 1-share
price), retain the position. This prevents the sell-then-fail-to-rebuy cycle for
small accounts where target < 1 share but still close to it.

Genuine reductions (target << 1 share) are NOT blocked — the sell proceeds.
The hardcoded lists act as overrides for symbols that need a different threshold.
"""
normalized_symbol = str(symbol or "").strip().upper()
held = float(quantity or 0.0)
target = float(target_value or 0.0)
quote_price = max(0.0, float(price or 0.0))

# Universal: held + positive target + target close to 1-share price → retain
if held > 0.0 and target > 0.0 and quote_price > 0.0:
if target >= quote_price * _SMALL_ACCOUNT_RETENTION_MIN_TARGET_SHARE_RATIO_DEFAULT:
return True

# Legacy whitelist — unconditional retention (safety net)
if normalized_symbol in SMALL_ACCOUNT_EXISTING_WHOLE_SHARE_RETENTION_SYMBOLS:
return True

# Legacy per-symbol ratio-based retention (override / tighter threshold)
min_target_share_ratio = (
SMALL_ACCOUNT_EXISTING_WHOLE_SHARE_RETENTION_MIN_TARGET_SHARE_RATIO_BY_SYMBOL.get(normalized_symbol)
)
if min_target_share_ratio is None:
return False
quote_price = max(0.0, float(price or 0.0))
if quote_price <= 0.0:
return False
return max(0.0, float(target_value or 0.0)) >= quote_price * float(min_target_share_ratio)
return target >= quote_price * float(min_target_share_ratio)


def _should_bootstrap_whole_share_buy(symbol, *, target_value, limit_price) -> bool:
Expand Down Expand Up @@ -1484,7 +1505,7 @@ def record_quote_snapshot(symbol, snapshot) -> None:
_can_afford_one_share = limit_price > 0.0 and investable >= limit_price
held_quantity = max(0.0, float(positions.get(symbol, {}).get("quantity", 0.0) or 0.0))
if (
_should_retain_existing_whole_share(symbol, target_value=target_value, price=price)
_should_retain_existing_whole_share(symbol, target_value=target_value, price=price, quantity=held_quantity)
and price > 0.0
and 0.0 < target_value < price
and held_quantity >= 1.0
Expand Down
Loading
Loading