Compare commits
12 Commits
feature/is
...
feature/is
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b961c53a92 | ||
| 76a7ee7cdb | |||
| 17112b864a | |||
|
|
28bcc7acd7 | ||
|
|
39b9f179f4 | ||
| bd2b3241b2 | |||
| 561faaaafa | |||
| a33d6a145f | |||
| 7e6c912214 | |||
|
|
d6edbc0fa2 | ||
|
|
c7640a30d7 | ||
|
|
60a22d6cd4 |
@@ -175,7 +175,7 @@ class SmartVolatilityScanner:
|
|||||||
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
||||||
score = min(100.0, volatility_score + liquidity_score)
|
score = min(100.0, volatility_score + liquidity_score)
|
||||||
signal = "momentum" if change_rate >= 0 else "oversold"
|
signal = "momentum" if change_rate >= 0 else "oversold"
|
||||||
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 4.0)))
|
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 2.0)))
|
||||||
|
|
||||||
candidates.append(
|
candidates.append(
|
||||||
ScanCandidate(
|
ScanCandidate(
|
||||||
@@ -282,7 +282,7 @@ class SmartVolatilityScanner:
|
|||||||
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
liquidity_score = volume_rank_bonus.get(stock_code, 0.0)
|
||||||
score = min(100.0, volatility_score + liquidity_score)
|
score = min(100.0, volatility_score + liquidity_score)
|
||||||
signal = "momentum" if change_rate >= 0 else "oversold"
|
signal = "momentum" if change_rate >= 0 else "oversold"
|
||||||
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 4.0)))
|
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 2.0)))
|
||||||
candidates.append(
|
candidates.append(
|
||||||
ScanCandidate(
|
ScanCandidate(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
@@ -338,7 +338,7 @@ class SmartVolatilityScanner:
|
|||||||
|
|
||||||
score = min(volatility_pct / 10.0, 1.0) * 100.0
|
score = min(volatility_pct / 10.0, 1.0) * 100.0
|
||||||
signal = "momentum" if change_rate >= 0 else "oversold"
|
signal = "momentum" if change_rate >= 0 else "oversold"
|
||||||
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 4.0)))
|
implied_rsi = max(0.0, min(100.0, 50.0 + (change_rate * 2.0)))
|
||||||
candidates.append(
|
candidates.append(
|
||||||
ScanCandidate(
|
ScanCandidate(
|
||||||
stock_code=stock_code,
|
stock_code=stock_code,
|
||||||
|
|||||||
138
src/main.py
138
src/main.py
@@ -42,7 +42,7 @@ from src.logging.decision_logger import DecisionLogger
|
|||||||
from src.logging_config import setup_logging
|
from src.logging_config import setup_logging
|
||||||
from src.markets.schedule import MarketInfo, get_next_market_open, get_open_markets
|
from src.markets.schedule import MarketInfo, get_next_market_open, get_open_markets
|
||||||
from src.notifications.telegram_client import NotificationFilter, TelegramClient, TelegramCommandHandler
|
from src.notifications.telegram_client import NotificationFilter, TelegramClient, TelegramCommandHandler
|
||||||
from src.strategy.models import DayPlaybook
|
from src.strategy.models import DayPlaybook, MarketOutlook
|
||||||
from src.strategy.playbook_store import PlaybookStore
|
from src.strategy.playbook_store import PlaybookStore
|
||||||
from src.strategy.pre_market_planner import PreMarketPlanner
|
from src.strategy.pre_market_planner import PreMarketPlanner
|
||||||
from src.strategy.scenario_engine import ScenarioEngine
|
from src.strategy.scenario_engine import ScenarioEngine
|
||||||
@@ -81,6 +81,7 @@ def safe_float(value: str | float | None, default: float = 0.0) -> float:
|
|||||||
TRADE_INTERVAL_SECONDS = 60
|
TRADE_INTERVAL_SECONDS = 60
|
||||||
SCAN_INTERVAL_SECONDS = 60 # Scan markets every 60 seconds
|
SCAN_INTERVAL_SECONDS = 60 # Scan markets every 60 seconds
|
||||||
MAX_CONNECTION_RETRIES = 3
|
MAX_CONNECTION_RETRIES = 3
|
||||||
|
_BUY_COOLDOWN_SECONDS = 600 # 10-minute cooldown after insufficient-balance rejection
|
||||||
|
|
||||||
# Daily trading mode constants (for Free tier API efficiency)
|
# Daily trading mode constants (for Free tier API efficiency)
|
||||||
DAILY_TRADE_SESSIONS = 4 # Number of trading sessions per day
|
DAILY_TRADE_SESSIONS = 4 # Number of trading sessions per day
|
||||||
@@ -190,8 +191,15 @@ def _determine_order_quantity(
|
|||||||
candidate: ScanCandidate | None,
|
candidate: ScanCandidate | None,
|
||||||
settings: Settings | None,
|
settings: Settings | None,
|
||||||
broker_held_qty: int = 0,
|
broker_held_qty: int = 0,
|
||||||
|
playbook_allocation_pct: float | None = None,
|
||||||
|
scenario_confidence: int = 80,
|
||||||
) -> int:
|
) -> int:
|
||||||
"""Determine order quantity using volatility-aware position sizing."""
|
"""Determine order quantity using volatility-aware position sizing.
|
||||||
|
|
||||||
|
Priority:
|
||||||
|
1. playbook_allocation_pct (AI-specified) scaled by scenario_confidence
|
||||||
|
2. Fallback: volatility-score-based allocation from scanner candidate
|
||||||
|
"""
|
||||||
if action == "SELL":
|
if action == "SELL":
|
||||||
return broker_held_qty
|
return broker_held_qty
|
||||||
if current_price <= 0 or total_cash <= 0:
|
if current_price <= 0 or total_cash <= 0:
|
||||||
@@ -200,6 +208,22 @@ def _determine_order_quantity(
|
|||||||
if settings is None or not settings.POSITION_SIZING_ENABLED:
|
if settings is None or not settings.POSITION_SIZING_ENABLED:
|
||||||
return 1
|
return 1
|
||||||
|
|
||||||
|
# Use AI-specified allocation_pct if available
|
||||||
|
if playbook_allocation_pct is not None:
|
||||||
|
# Confidence scaling: confidence 80 → 1.0x, confidence 95 → 1.19x
|
||||||
|
confidence_scale = scenario_confidence / 80.0
|
||||||
|
effective_pct = min(
|
||||||
|
settings.POSITION_MAX_ALLOCATION_PCT,
|
||||||
|
max(
|
||||||
|
settings.POSITION_MIN_ALLOCATION_PCT,
|
||||||
|
playbook_allocation_pct * confidence_scale,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
budget = total_cash * (effective_pct / 100.0)
|
||||||
|
quantity = int(budget // current_price)
|
||||||
|
return max(0, quantity)
|
||||||
|
|
||||||
|
# Fallback: volatility-score-based allocation
|
||||||
target_score = max(1.0, settings.POSITION_VOLATILITY_TARGET_SCORE)
|
target_score = max(1.0, settings.POSITION_VOLATILITY_TARGET_SCORE)
|
||||||
observed_score = candidate.score if candidate else target_score
|
observed_score = candidate.score if candidate else target_score
|
||||||
observed_score = max(1.0, min(100.0, observed_score))
|
observed_score = max(1.0, min(100.0, observed_score))
|
||||||
@@ -275,6 +299,7 @@ async def trading_cycle(
|
|||||||
stock_code: str,
|
stock_code: str,
|
||||||
scan_candidates: dict[str, dict[str, ScanCandidate]],
|
scan_candidates: dict[str, dict[str, ScanCandidate]],
|
||||||
settings: Settings | None = None,
|
settings: Settings | None = None,
|
||||||
|
buy_cooldown: dict[str, float] | None = None,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Execute one trading cycle for a single stock."""
|
"""Execute one trading cycle for a single stock."""
|
||||||
cycle_start_time = asyncio.get_event_loop().time()
|
cycle_start_time = asyncio.get_event_loop().time()
|
||||||
@@ -457,6 +482,34 @@ async def trading_cycle(
|
|||||||
)
|
)
|
||||||
stock_playbook = playbook.get_stock_playbook(stock_code)
|
stock_playbook = playbook.get_stock_playbook(stock_code)
|
||||||
|
|
||||||
|
# 2.1. Apply market_outlook-based BUY confidence threshold
|
||||||
|
if decision.action == "BUY":
|
||||||
|
base_threshold = (settings.CONFIDENCE_THRESHOLD if settings else 80)
|
||||||
|
outlook = playbook.market_outlook
|
||||||
|
if outlook == MarketOutlook.BEARISH:
|
||||||
|
min_confidence = 90
|
||||||
|
elif outlook == MarketOutlook.BULLISH:
|
||||||
|
min_confidence = 75
|
||||||
|
else:
|
||||||
|
min_confidence = base_threshold
|
||||||
|
if match.confidence < min_confidence:
|
||||||
|
logger.info(
|
||||||
|
"BUY suppressed for %s (%s): confidence %d < %d (market_outlook=%s)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
match.confidence,
|
||||||
|
min_confidence,
|
||||||
|
outlook.value,
|
||||||
|
)
|
||||||
|
decision = TradeDecision(
|
||||||
|
action="HOLD",
|
||||||
|
confidence=match.confidence,
|
||||||
|
rationale=(
|
||||||
|
f"BUY confidence {match.confidence} < {min_confidence} "
|
||||||
|
f"(market_outlook={outlook.value})"
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
if decision.action == "HOLD":
|
if decision.action == "HOLD":
|
||||||
open_position = get_open_position(db_conn, stock_code, market.code)
|
open_position = get_open_position(db_conn, stock_code, market.code)
|
||||||
if open_position:
|
if open_position:
|
||||||
@@ -568,6 +621,7 @@ async def trading_cycle(
|
|||||||
if decision.action == "SELL"
|
if decision.action == "SELL"
|
||||||
else 0
|
else 0
|
||||||
)
|
)
|
||||||
|
matched_scenario = match.matched_scenario
|
||||||
quantity = _determine_order_quantity(
|
quantity = _determine_order_quantity(
|
||||||
action=decision.action,
|
action=decision.action,
|
||||||
current_price=current_price,
|
current_price=current_price,
|
||||||
@@ -575,6 +629,8 @@ async def trading_cycle(
|
|||||||
candidate=candidate,
|
candidate=candidate,
|
||||||
settings=settings,
|
settings=settings,
|
||||||
broker_held_qty=broker_held_qty,
|
broker_held_qty=broker_held_qty,
|
||||||
|
playbook_allocation_pct=matched_scenario.allocation_pct if matched_scenario else None,
|
||||||
|
scenario_confidence=match.confidence,
|
||||||
)
|
)
|
||||||
if quantity <= 0:
|
if quantity <= 0:
|
||||||
logger.info(
|
logger.info(
|
||||||
@@ -588,7 +644,22 @@ async def trading_cycle(
|
|||||||
return
|
return
|
||||||
order_amount = current_price * quantity
|
order_amount = current_price * quantity
|
||||||
|
|
||||||
# 4. Risk check BEFORE order
|
# 4. Check BUY cooldown (set when a prior BUY failed due to insufficient balance)
|
||||||
|
if decision.action == "BUY" and buy_cooldown is not None:
|
||||||
|
cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
cooldown_until = buy_cooldown.get(cooldown_key, 0.0)
|
||||||
|
now = asyncio.get_event_loop().time()
|
||||||
|
if now < cooldown_until:
|
||||||
|
remaining = int(cooldown_until - now)
|
||||||
|
logger.info(
|
||||||
|
"Skip BUY %s (%s): insufficient-balance cooldown active (%ds remaining)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
remaining,
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
# 5a. Risk check BEFORE order
|
||||||
try:
|
try:
|
||||||
risk.validate_order(
|
risk.validate_order(
|
||||||
current_pnl_pct=pnl_pct,
|
current_pnl_pct=pnl_pct,
|
||||||
@@ -636,12 +707,24 @@ async def trading_cycle(
|
|||||||
# Check if KIS rejected the order (rt_cd != "0")
|
# Check if KIS rejected the order (rt_cd != "0")
|
||||||
if result.get("rt_cd", "") != "0":
|
if result.get("rt_cd", "") != "0":
|
||||||
order_succeeded = False
|
order_succeeded = False
|
||||||
|
msg1 = result.get("msg1") or ""
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
||||||
stock_code,
|
stock_code,
|
||||||
result.get("rt_cd"),
|
result.get("rt_cd"),
|
||||||
result.get("msg1"),
|
msg1,
|
||||||
)
|
)
|
||||||
|
# Set BUY cooldown when the rejection is due to insufficient balance
|
||||||
|
if decision.action == "BUY" and buy_cooldown is not None and "주문가능금액" in msg1:
|
||||||
|
cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
buy_cooldown[cooldown_key] = (
|
||||||
|
asyncio.get_event_loop().time() + _BUY_COOLDOWN_SECONDS
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY cooldown set for %s: %.0fs (insufficient balance)",
|
||||||
|
stock_code,
|
||||||
|
_BUY_COOLDOWN_SECONDS,
|
||||||
|
)
|
||||||
logger.info("Order result: %s", result.get("msg1", "OK"))
|
logger.info("Order result: %s", result.get("msg1", "OK"))
|
||||||
|
|
||||||
# 5.5. Notify trade execution (only on success)
|
# 5.5. Notify trade execution (only on success)
|
||||||
@@ -749,6 +832,9 @@ async def run_daily_session(
|
|||||||
|
|
||||||
logger.info("Starting daily trading session for %d markets", len(open_markets))
|
logger.info("Starting daily trading session for %d markets", len(open_markets))
|
||||||
|
|
||||||
|
# BUY cooldown: prevents retrying stocks rejected for insufficient balance
|
||||||
|
daily_buy_cooldown: dict[str, float] = {} # "{market_code}:{stock_code}" -> expiry timestamp
|
||||||
|
|
||||||
# Process each open market
|
# Process each open market
|
||||||
for market in open_markets:
|
for market in open_markets:
|
||||||
# Use market-local date for playbook keying
|
# Use market-local date for playbook keying
|
||||||
@@ -1021,6 +1107,21 @@ async def run_daily_session(
|
|||||||
continue
|
continue
|
||||||
order_amount = stock_data["current_price"] * quantity
|
order_amount = stock_data["current_price"] * quantity
|
||||||
|
|
||||||
|
# Check BUY cooldown (insufficient balance)
|
||||||
|
if decision.action == "BUY":
|
||||||
|
daily_cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
daily_cooldown_until = daily_buy_cooldown.get(daily_cooldown_key, 0.0)
|
||||||
|
now = asyncio.get_event_loop().time()
|
||||||
|
if now < daily_cooldown_until:
|
||||||
|
remaining = int(daily_cooldown_until - now)
|
||||||
|
logger.info(
|
||||||
|
"Skip BUY %s (%s): insufficient-balance cooldown active (%ds remaining)",
|
||||||
|
stock_code,
|
||||||
|
market.name,
|
||||||
|
remaining,
|
||||||
|
)
|
||||||
|
continue
|
||||||
|
|
||||||
# Risk check
|
# Risk check
|
||||||
try:
|
try:
|
||||||
risk.validate_order(
|
risk.validate_order(
|
||||||
@@ -1077,12 +1178,23 @@ async def run_daily_session(
|
|||||||
)
|
)
|
||||||
if result.get("rt_cd", "") != "0":
|
if result.get("rt_cd", "") != "0":
|
||||||
order_succeeded = False
|
order_succeeded = False
|
||||||
|
daily_msg1 = result.get("msg1") or ""
|
||||||
logger.warning(
|
logger.warning(
|
||||||
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
"Overseas order not accepted for %s: rt_cd=%s msg=%s",
|
||||||
stock_code,
|
stock_code,
|
||||||
result.get("rt_cd"),
|
result.get("rt_cd"),
|
||||||
result.get("msg1"),
|
daily_msg1,
|
||||||
)
|
)
|
||||||
|
if decision.action == "BUY" and "주문가능금액" in daily_msg1:
|
||||||
|
daily_cooldown_key = f"{market.code}:{stock_code}"
|
||||||
|
daily_buy_cooldown[daily_cooldown_key] = (
|
||||||
|
asyncio.get_event_loop().time() + _BUY_COOLDOWN_SECONDS
|
||||||
|
)
|
||||||
|
logger.info(
|
||||||
|
"BUY cooldown set for %s: %.0fs (insufficient balance)",
|
||||||
|
stock_code,
|
||||||
|
_BUY_COOLDOWN_SECONDS,
|
||||||
|
)
|
||||||
logger.info("Order result: %s", result.get("msg1", "OK"))
|
logger.info("Order result: %s", result.get("msg1", "OK"))
|
||||||
|
|
||||||
# Notify trade execution (only on success)
|
# Notify trade execution (only on success)
|
||||||
@@ -1246,10 +1358,18 @@ def _start_dashboard_server(settings: Settings) -> threading.Thread | None:
|
|||||||
if not settings.DASHBOARD_ENABLED:
|
if not settings.DASHBOARD_ENABLED:
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
# Validate dependencies before spawning the thread so startup failures are
|
||||||
|
# reported synchronously (avoids the misleading "started" → "failed" log pair).
|
||||||
|
try:
|
||||||
|
import uvicorn # noqa: F401
|
||||||
|
from src.dashboard import create_dashboard_app # noqa: F401
|
||||||
|
except ImportError as exc:
|
||||||
|
logger.warning("Dashboard server unavailable (missing dependency): %s", exc)
|
||||||
|
return None
|
||||||
|
|
||||||
def _serve() -> None:
|
def _serve() -> None:
|
||||||
try:
|
try:
|
||||||
import uvicorn
|
import uvicorn
|
||||||
|
|
||||||
from src.dashboard import create_dashboard_app
|
from src.dashboard import create_dashboard_app
|
||||||
|
|
||||||
app = create_dashboard_app(settings.DB_PATH)
|
app = create_dashboard_app(settings.DB_PATH)
|
||||||
@@ -1260,7 +1380,7 @@ def _start_dashboard_server(settings: Settings) -> threading.Thread | None:
|
|||||||
log_level="info",
|
log_level="info",
|
||||||
)
|
)
|
||||||
except Exception as exc:
|
except Exception as exc:
|
||||||
logger.warning("Dashboard server failed to start: %s", exc)
|
logger.warning("Dashboard server stopped unexpectedly: %s", exc)
|
||||||
|
|
||||||
thread = threading.Thread(
|
thread = threading.Thread(
|
||||||
target=_serve,
|
target=_serve,
|
||||||
@@ -1701,6 +1821,9 @@ async def run(settings: Settings) -> None:
|
|||||||
# Active stocks per market (dynamically discovered by scanner)
|
# Active stocks per market (dynamically discovered by scanner)
|
||||||
active_stocks: dict[str, list[str]] = {} # market_code -> [stock_codes]
|
active_stocks: dict[str, list[str]] = {} # market_code -> [stock_codes]
|
||||||
|
|
||||||
|
# BUY cooldown: prevents retrying a stock rejected for insufficient balance
|
||||||
|
buy_cooldown: dict[str, float] = {} # "{market_code}:{stock_code}" -> expiry timestamp
|
||||||
|
|
||||||
# Initialize latency control system
|
# Initialize latency control system
|
||||||
criticality_assessor = CriticalityAssessor(
|
criticality_assessor = CriticalityAssessor(
|
||||||
critical_pnl_threshold=-2.5, # Near circuit breaker at -3.0%
|
critical_pnl_threshold=-2.5, # Near circuit breaker at -3.0%
|
||||||
@@ -2043,6 +2166,7 @@ async def run(settings: Settings) -> None:
|
|||||||
stock_code,
|
stock_code,
|
||||||
scan_candidates,
|
scan_candidates,
|
||||||
settings,
|
settings,
|
||||||
|
buy_cooldown,
|
||||||
)
|
)
|
||||||
break # Success — exit retry loop
|
break # Success — exit retry loop
|
||||||
except CircuitBreakerTripped as exc:
|
except CircuitBreakerTripped as exc:
|
||||||
|
|||||||
@@ -46,6 +46,18 @@ class StockCondition(BaseModel):
|
|||||||
|
|
||||||
The ScenarioEngine evaluates all non-None fields as AND conditions.
|
The ScenarioEngine evaluates all non-None fields as AND conditions.
|
||||||
A condition matches only if ALL specified fields are satisfied.
|
A condition matches only if ALL specified fields are satisfied.
|
||||||
|
|
||||||
|
Technical indicator fields:
|
||||||
|
rsi_below / rsi_above — RSI threshold
|
||||||
|
volume_ratio_above / volume_ratio_below — volume vs previous day
|
||||||
|
price_above / price_below — absolute price level
|
||||||
|
price_change_pct_above / price_change_pct_below — intraday % change
|
||||||
|
|
||||||
|
Position-aware fields (require market_data enrichment from open position):
|
||||||
|
unrealized_pnl_pct_above — matches if unrealized P&L > threshold (e.g. 3.0 → +3%)
|
||||||
|
unrealized_pnl_pct_below — matches if unrealized P&L < threshold (e.g. -2.0 → -2%)
|
||||||
|
holding_days_above — matches if position held for more than N days
|
||||||
|
holding_days_below — matches if position held for fewer than N days
|
||||||
"""
|
"""
|
||||||
|
|
||||||
rsi_below: float | None = None
|
rsi_below: float | None = None
|
||||||
@@ -56,6 +68,10 @@ class StockCondition(BaseModel):
|
|||||||
price_below: float | None = None
|
price_below: float | None = None
|
||||||
price_change_pct_above: float | None = None
|
price_change_pct_above: float | None = None
|
||||||
price_change_pct_below: float | None = None
|
price_change_pct_below: float | None = None
|
||||||
|
unrealized_pnl_pct_above: float | None = None
|
||||||
|
unrealized_pnl_pct_below: float | None = None
|
||||||
|
holding_days_above: int | None = None
|
||||||
|
holding_days_below: int | None = None
|
||||||
|
|
||||||
def has_any_condition(self) -> bool:
|
def has_any_condition(self) -> bool:
|
||||||
"""Check if at least one condition field is set."""
|
"""Check if at least one condition field is set."""
|
||||||
@@ -70,6 +86,10 @@ class StockCondition(BaseModel):
|
|||||||
self.price_below,
|
self.price_below,
|
||||||
self.price_change_pct_above,
|
self.price_change_pct_above,
|
||||||
self.price_change_pct_below,
|
self.price_change_pct_below,
|
||||||
|
self.unrealized_pnl_pct_above,
|
||||||
|
self.unrealized_pnl_pct_below,
|
||||||
|
self.holding_days_above,
|
||||||
|
self.holding_days_below,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -332,7 +332,8 @@ class PreMarketPlanner:
|
|||||||
f' "stock_code": "...",\n'
|
f' "stock_code": "...",\n'
|
||||||
f' "scenarios": [\n'
|
f' "scenarios": [\n'
|
||||||
f' {{\n'
|
f' {{\n'
|
||||||
f' "condition": {{"rsi_below": 30, "volume_ratio_above": 2.0}},\n'
|
f' "condition": {{"rsi_below": 30, "volume_ratio_above": 2.0,'
|
||||||
|
f' "unrealized_pnl_pct_above": 3.0, "holding_days_above": 5}},\n'
|
||||||
f' "action": "BUY|SELL|HOLD",\n'
|
f' "action": "BUY|SELL|HOLD",\n'
|
||||||
f' "confidence": 85,\n'
|
f' "confidence": 85,\n'
|
||||||
f' "allocation_pct": 10.0,\n'
|
f' "allocation_pct": 10.0,\n'
|
||||||
@@ -436,6 +437,10 @@ class PreMarketPlanner:
|
|||||||
price_below=cond_data.get("price_below"),
|
price_below=cond_data.get("price_below"),
|
||||||
price_change_pct_above=cond_data.get("price_change_pct_above"),
|
price_change_pct_above=cond_data.get("price_change_pct_above"),
|
||||||
price_change_pct_below=cond_data.get("price_change_pct_below"),
|
price_change_pct_below=cond_data.get("price_change_pct_below"),
|
||||||
|
unrealized_pnl_pct_above=cond_data.get("unrealized_pnl_pct_above"),
|
||||||
|
unrealized_pnl_pct_below=cond_data.get("unrealized_pnl_pct_below"),
|
||||||
|
holding_days_above=cond_data.get("holding_days_above"),
|
||||||
|
holding_days_below=cond_data.get("holding_days_below"),
|
||||||
)
|
)
|
||||||
|
|
||||||
if not condition.has_any_condition():
|
if not condition.has_any_condition():
|
||||||
|
|||||||
@@ -206,6 +206,37 @@ class ScenarioEngine:
|
|||||||
if condition.price_change_pct_below is not None:
|
if condition.price_change_pct_below is not None:
|
||||||
checks.append(price_change_pct is not None and price_change_pct < condition.price_change_pct_below)
|
checks.append(price_change_pct is not None and price_change_pct < condition.price_change_pct_below)
|
||||||
|
|
||||||
|
# Position-aware conditions
|
||||||
|
unrealized_pnl_pct = self._safe_float(market_data.get("unrealized_pnl_pct"))
|
||||||
|
if condition.unrealized_pnl_pct_above is not None or condition.unrealized_pnl_pct_below is not None:
|
||||||
|
if "unrealized_pnl_pct" not in market_data:
|
||||||
|
self._warn_missing_key("unrealized_pnl_pct")
|
||||||
|
if condition.unrealized_pnl_pct_above is not None:
|
||||||
|
checks.append(
|
||||||
|
unrealized_pnl_pct is not None
|
||||||
|
and unrealized_pnl_pct > condition.unrealized_pnl_pct_above
|
||||||
|
)
|
||||||
|
if condition.unrealized_pnl_pct_below is not None:
|
||||||
|
checks.append(
|
||||||
|
unrealized_pnl_pct is not None
|
||||||
|
and unrealized_pnl_pct < condition.unrealized_pnl_pct_below
|
||||||
|
)
|
||||||
|
|
||||||
|
holding_days = self._safe_float(market_data.get("holding_days"))
|
||||||
|
if condition.holding_days_above is not None or condition.holding_days_below is not None:
|
||||||
|
if "holding_days" not in market_data:
|
||||||
|
self._warn_missing_key("holding_days")
|
||||||
|
if condition.holding_days_above is not None:
|
||||||
|
checks.append(
|
||||||
|
holding_days is not None
|
||||||
|
and holding_days > condition.holding_days_above
|
||||||
|
)
|
||||||
|
if condition.holding_days_below is not None:
|
||||||
|
checks.append(
|
||||||
|
holding_days is not None
|
||||||
|
and holding_days < condition.holding_days_below
|
||||||
|
)
|
||||||
|
|
||||||
return len(checks) > 0 and all(checks)
|
return len(checks) > 0 and all(checks)
|
||||||
|
|
||||||
def _evaluate_global_condition(
|
def _evaluate_global_condition(
|
||||||
@@ -266,5 +297,9 @@ class ScenarioEngine:
|
|||||||
details["current_price"] = self._safe_float(market_data.get("current_price"))
|
details["current_price"] = self._safe_float(market_data.get("current_price"))
|
||||||
if condition.price_change_pct_above is not None or condition.price_change_pct_below is not None:
|
if condition.price_change_pct_above is not None or condition.price_change_pct_below is not None:
|
||||||
details["price_change_pct"] = self._safe_float(market_data.get("price_change_pct"))
|
details["price_change_pct"] = self._safe_float(market_data.get("price_change_pct"))
|
||||||
|
if condition.unrealized_pnl_pct_above is not None or condition.unrealized_pnl_pct_below is not None:
|
||||||
|
details["unrealized_pnl_pct"] = self._safe_float(market_data.get("unrealized_pnl_pct"))
|
||||||
|
if condition.holding_days_above is not None or condition.holding_days_below is not None:
|
||||||
|
details["holding_days"] = self._safe_float(market_data.get("holding_days"))
|
||||||
|
|
||||||
return details
|
return details
|
||||||
|
|||||||
@@ -205,6 +205,84 @@ class TestDetermineOrderQuantity:
|
|||||||
)
|
)
|
||||||
assert result == 2
|
assert result == 2
|
||||||
|
|
||||||
|
def test_determine_order_quantity_uses_playbook_allocation_pct(self) -> None:
|
||||||
|
"""playbook_allocation_pct should take priority over volatility-based sizing."""
|
||||||
|
settings = MagicMock(spec=Settings)
|
||||||
|
settings.POSITION_SIZING_ENABLED = True
|
||||||
|
settings.POSITION_MAX_ALLOCATION_PCT = 30.0
|
||||||
|
settings.POSITION_MIN_ALLOCATION_PCT = 1.0
|
||||||
|
# playbook says 20%, confidence 80 → scale=1.0 → 20%
|
||||||
|
# 1,000,000 * 20% = 200,000 // 50,000 price = 4 shares
|
||||||
|
result = _determine_order_quantity(
|
||||||
|
action="BUY",
|
||||||
|
current_price=50000.0,
|
||||||
|
total_cash=1000000.0,
|
||||||
|
candidate=None,
|
||||||
|
settings=settings,
|
||||||
|
playbook_allocation_pct=20.0,
|
||||||
|
scenario_confidence=80,
|
||||||
|
)
|
||||||
|
assert result == 4
|
||||||
|
|
||||||
|
def test_determine_order_quantity_confidence_scales_allocation(self) -> None:
|
||||||
|
"""Higher confidence should produce a larger allocation (up to max)."""
|
||||||
|
settings = MagicMock(spec=Settings)
|
||||||
|
settings.POSITION_SIZING_ENABLED = True
|
||||||
|
settings.POSITION_MAX_ALLOCATION_PCT = 30.0
|
||||||
|
settings.POSITION_MIN_ALLOCATION_PCT = 1.0
|
||||||
|
# confidence 96 → scale=1.2 → 10% * 1.2 = 12%
|
||||||
|
# 1,000,000 * 12% = 120,000 // 50,000 price = 2 shares
|
||||||
|
result = _determine_order_quantity(
|
||||||
|
action="BUY",
|
||||||
|
current_price=50000.0,
|
||||||
|
total_cash=1000000.0,
|
||||||
|
candidate=None,
|
||||||
|
settings=settings,
|
||||||
|
playbook_allocation_pct=10.0,
|
||||||
|
scenario_confidence=96,
|
||||||
|
)
|
||||||
|
# scale = 96/80 = 1.2 → effective_pct = 12.0
|
||||||
|
# budget = 1_000_000 * 0.12 = 120_000 → qty = 120_000 // 50_000 = 2
|
||||||
|
assert result == 2
|
||||||
|
|
||||||
|
def test_determine_order_quantity_confidence_clamped_to_max(self) -> None:
|
||||||
|
"""Confidence scaling should not exceed POSITION_MAX_ALLOCATION_PCT."""
|
||||||
|
settings = MagicMock(spec=Settings)
|
||||||
|
settings.POSITION_SIZING_ENABLED = True
|
||||||
|
settings.POSITION_MAX_ALLOCATION_PCT = 15.0
|
||||||
|
settings.POSITION_MIN_ALLOCATION_PCT = 1.0
|
||||||
|
# playbook 20% * scale 1.5 = 30% → clamped to 15%
|
||||||
|
# 1,000,000 * 15% = 150,000 // 50,000 price = 3 shares
|
||||||
|
result = _determine_order_quantity(
|
||||||
|
action="BUY",
|
||||||
|
current_price=50000.0,
|
||||||
|
total_cash=1000000.0,
|
||||||
|
candidate=None,
|
||||||
|
settings=settings,
|
||||||
|
playbook_allocation_pct=20.0,
|
||||||
|
scenario_confidence=120, # extreme → scale = 1.5
|
||||||
|
)
|
||||||
|
assert result == 3
|
||||||
|
|
||||||
|
def test_determine_order_quantity_fallback_when_no_playbook(self) -> None:
|
||||||
|
"""Without playbook_allocation_pct, falls back to volatility-based sizing."""
|
||||||
|
settings = MagicMock(spec=Settings)
|
||||||
|
settings.POSITION_SIZING_ENABLED = True
|
||||||
|
settings.POSITION_VOLATILITY_TARGET_SCORE = 50.0
|
||||||
|
settings.POSITION_BASE_ALLOCATION_PCT = 10.0
|
||||||
|
settings.POSITION_MAX_ALLOCATION_PCT = 30.0
|
||||||
|
settings.POSITION_MIN_ALLOCATION_PCT = 1.0
|
||||||
|
# Same as test_buy_with_position_sizing_calculates_correctly (no playbook)
|
||||||
|
result = _determine_order_quantity(
|
||||||
|
action="BUY",
|
||||||
|
current_price=50000.0,
|
||||||
|
total_cash=1000000.0,
|
||||||
|
candidate=None,
|
||||||
|
settings=settings,
|
||||||
|
playbook_allocation_pct=None, # explicit None → fallback
|
||||||
|
)
|
||||||
|
assert result == 2
|
||||||
|
|
||||||
|
|
||||||
class TestSafeFloat:
|
class TestSafeFloat:
|
||||||
"""Test safe_float() helper function."""
|
"""Test safe_float() helper function."""
|
||||||
@@ -2114,3 +2192,546 @@ def test_start_dashboard_server_enabled_starts_thread() -> None:
|
|||||||
assert thread == mock_thread
|
assert thread == mock_thread
|
||||||
mock_thread_cls.assert_called_once()
|
mock_thread_cls.assert_called_once()
|
||||||
mock_thread.start.assert_called_once()
|
mock_thread.start.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
def test_start_dashboard_server_returns_none_when_uvicorn_missing() -> None:
|
||||||
|
"""Returns None (no thread) and logs a warning when uvicorn is not installed."""
|
||||||
|
settings = Settings(
|
||||||
|
KIS_APP_KEY="test_key",
|
||||||
|
KIS_APP_SECRET="test_secret",
|
||||||
|
KIS_ACCOUNT_NO="12345678-01",
|
||||||
|
GEMINI_API_KEY="test_gemini_key",
|
||||||
|
DASHBOARD_ENABLED=True,
|
||||||
|
)
|
||||||
|
import builtins
|
||||||
|
real_import = builtins.__import__
|
||||||
|
|
||||||
|
def mock_import(name: str, *args: object, **kwargs: object) -> object:
|
||||||
|
if name == "uvicorn":
|
||||||
|
raise ImportError("No module named 'uvicorn'")
|
||||||
|
return real_import(name, *args, **kwargs)
|
||||||
|
|
||||||
|
with patch("builtins.__import__", side_effect=mock_import):
|
||||||
|
thread = _start_dashboard_server(settings)
|
||||||
|
|
||||||
|
assert thread is None
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# BUY cooldown tests (#179)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestBuyCooldown:
|
||||||
|
"""Tests for BUY cooldown after insufficient-balance rejection."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_broker(self) -> MagicMock:
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_current_price = AsyncMock(return_value=(100.0, 1.0, 0.0))
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output2": [{"tot_evlu_amt": "1000000", "dnca_tot_amt": "500000",
|
||||||
|
"pchs_amt_smtl_amt": "500000"}]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
return broker
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_market(self) -> MagicMock:
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
return market
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_overseas_market(self) -> MagicMock:
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "NASDAQ"
|
||||||
|
market.code = "US_NASDAQ"
|
||||||
|
market.exchange_code = "NAS"
|
||||||
|
market.is_domestic = False
|
||||||
|
return market
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_overseas_broker(self) -> MagicMock:
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "1.0", "rate": "0.0",
|
||||||
|
"high": "1.05", "low": "0.95", "tvol": "1000000"}}
|
||||||
|
)
|
||||||
|
broker.get_overseas_balance = AsyncMock(return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"frcr_dncl_amt_2": "50000", "frcr_evlu_tota": "50000",
|
||||||
|
"frcr_buy_amt_smtl": "0"}],
|
||||||
|
})
|
||||||
|
broker.send_overseas_order = AsyncMock(
|
||||||
|
return_value={"rt_cd": "1", "msg1": "모의투자 주문가능금액이 부족합니다."}
|
||||||
|
)
|
||||||
|
return broker
|
||||||
|
|
||||||
|
def _make_buy_match_overseas(self, stock_code: str = "MLECW") -> ScenarioMatch:
|
||||||
|
return ScenarioMatch(
|
||||||
|
stock_code=stock_code,
|
||||||
|
matched_scenario=None,
|
||||||
|
action=ScenarioAction.BUY,
|
||||||
|
confidence=85,
|
||||||
|
rationale="Test buy",
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cooldown_set_on_insufficient_balance(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_broker: MagicMock,
|
||||||
|
mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY cooldown entry is created after 주문가능금액 rejection."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
buy_cooldown: dict[str, float] = {}
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=MagicMock(
|
||||||
|
notify_trade_execution=AsyncMock(),
|
||||||
|
notify_fat_finger=AsyncMock(),
|
||||||
|
notify_circuit_breaker=AsyncMock(),
|
||||||
|
notify_scenario_matched=AsyncMock(),
|
||||||
|
),
|
||||||
|
market=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
buy_cooldown=buy_cooldown,
|
||||||
|
)
|
||||||
|
|
||||||
|
assert "US_NASDAQ:MLECW" in buy_cooldown
|
||||||
|
assert buy_cooldown["US_NASDAQ:MLECW"] > 0
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cooldown_skips_buy(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_broker: MagicMock,
|
||||||
|
mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY is skipped when cooldown is active for the stock."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
|
||||||
|
import asyncio
|
||||||
|
# Set an active cooldown (expires far in the future)
|
||||||
|
buy_cooldown: dict[str, float] = {
|
||||||
|
"US_NASDAQ:MLECW": asyncio.get_event_loop().time() + 600
|
||||||
|
}
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=MagicMock(
|
||||||
|
notify_trade_execution=AsyncMock(),
|
||||||
|
notify_fat_finger=AsyncMock(),
|
||||||
|
notify_circuit_breaker=AsyncMock(),
|
||||||
|
notify_scenario_matched=AsyncMock(),
|
||||||
|
),
|
||||||
|
market=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
buy_cooldown=buy_cooldown,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Order should NOT have been sent
|
||||||
|
mock_overseas_broker.send_overseas_order.assert_not_called()
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_cooldown_not_set_on_other_errors(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""Cooldown is NOT set for non-balance-related rejections."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
# Different rejection reason
|
||||||
|
overseas_broker = MagicMock()
|
||||||
|
overseas_broker.get_overseas_price = AsyncMock(
|
||||||
|
return_value={"output": {"last": "1.0", "rate": "0.0",
|
||||||
|
"high": "1.05", "low": "0.95", "tvol": "1000000"}}
|
||||||
|
)
|
||||||
|
overseas_broker.get_overseas_balance = AsyncMock(return_value={
|
||||||
|
"output1": [],
|
||||||
|
"output2": [{"frcr_dncl_amt_2": "50000", "frcr_evlu_tota": "50000",
|
||||||
|
"frcr_buy_amt_smtl": "0"}],
|
||||||
|
})
|
||||||
|
overseas_broker.send_overseas_order = AsyncMock(
|
||||||
|
return_value={"rt_cd": "1", "msg1": "기타 오류 메시지"}
|
||||||
|
)
|
||||||
|
buy_cooldown: dict[str, float] = {}
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=MagicMock(
|
||||||
|
notify_trade_execution=AsyncMock(),
|
||||||
|
notify_fat_finger=AsyncMock(),
|
||||||
|
notify_circuit_breaker=AsyncMock(),
|
||||||
|
notify_scenario_matched=AsyncMock(),
|
||||||
|
),
|
||||||
|
market=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
buy_cooldown=buy_cooldown,
|
||||||
|
)
|
||||||
|
|
||||||
|
# Cooldown should NOT be set for non-balance errors
|
||||||
|
assert "US_NASDAQ:MLECW" not in buy_cooldown
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_no_cooldown_param_still_works(
|
||||||
|
self, mock_broker: MagicMock, mock_overseas_broker: MagicMock,
|
||||||
|
mock_overseas_market: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""trading_cycle works normally when buy_cooldown is None (default)."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_overseas("MLECW"))
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=mock_overseas_broker,
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=_make_playbook("US_NASDAQ"),
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=MagicMock(),
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=MagicMock(
|
||||||
|
notify_trade_execution=AsyncMock(),
|
||||||
|
notify_fat_finger=AsyncMock(),
|
||||||
|
notify_circuit_breaker=AsyncMock(),
|
||||||
|
notify_scenario_matched=AsyncMock(),
|
||||||
|
),
|
||||||
|
market=mock_overseas_market,
|
||||||
|
stock_code="MLECW",
|
||||||
|
scan_candidates={},
|
||||||
|
# buy_cooldown not passed → defaults to None
|
||||||
|
)
|
||||||
|
|
||||||
|
# Should attempt the order (and fail), but not crash
|
||||||
|
mock_overseas_broker.send_overseas_order.assert_called_once()
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# market_outlook BUY confidence threshold tests (#173)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestMarketOutlookConfidenceThreshold:
|
||||||
|
"""Tests for market_outlook-based BUY confidence suppression in trading_cycle."""
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_broker(self) -> MagicMock:
|
||||||
|
broker = MagicMock()
|
||||||
|
broker.get_current_price = AsyncMock(return_value=(50000.0, 1.0, 0.0))
|
||||||
|
broker.get_balance = AsyncMock(
|
||||||
|
return_value={
|
||||||
|
"output2": [
|
||||||
|
{
|
||||||
|
"tot_evlu_amt": "10000000",
|
||||||
|
"dnca_tot_amt": "5000000",
|
||||||
|
"pchs_amt_smtl_amt": "9500000",
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
)
|
||||||
|
broker.send_order = AsyncMock(return_value={"msg1": "OK"})
|
||||||
|
return broker
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_market(self) -> MagicMock:
|
||||||
|
market = MagicMock()
|
||||||
|
market.name = "Korea"
|
||||||
|
market.code = "KR"
|
||||||
|
market.exchange_code = "KRX"
|
||||||
|
market.is_domestic = True
|
||||||
|
return market
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_telegram(self) -> MagicMock:
|
||||||
|
telegram = MagicMock()
|
||||||
|
telegram.notify_trade_execution = AsyncMock()
|
||||||
|
telegram.notify_scenario_matched = AsyncMock()
|
||||||
|
telegram.notify_fat_finger = AsyncMock()
|
||||||
|
return telegram
|
||||||
|
|
||||||
|
def _make_buy_match_with_confidence(
|
||||||
|
self, confidence: int, stock_code: str = "005930"
|
||||||
|
) -> ScenarioMatch:
|
||||||
|
from src.strategy.models import StockScenario
|
||||||
|
scenario = StockScenario(
|
||||||
|
condition=StockCondition(rsi_below=30),
|
||||||
|
action=ScenarioAction.BUY,
|
||||||
|
confidence=confidence,
|
||||||
|
allocation_pct=10.0,
|
||||||
|
)
|
||||||
|
return ScenarioMatch(
|
||||||
|
stock_code=stock_code,
|
||||||
|
matched_scenario=scenario,
|
||||||
|
action=ScenarioAction.BUY,
|
||||||
|
confidence=confidence,
|
||||||
|
rationale="Test buy",
|
||||||
|
)
|
||||||
|
|
||||||
|
def _make_playbook_with_outlook(
|
||||||
|
self, outlook_str: str, market: str = "KR"
|
||||||
|
) -> DayPlaybook:
|
||||||
|
from src.strategy.models import MarketOutlook
|
||||||
|
outlook_map = {
|
||||||
|
"bearish": MarketOutlook.BEARISH,
|
||||||
|
"bullish": MarketOutlook.BULLISH,
|
||||||
|
"neutral": MarketOutlook.NEUTRAL,
|
||||||
|
"neutral_to_bullish": MarketOutlook.NEUTRAL_TO_BULLISH,
|
||||||
|
"neutral_to_bearish": MarketOutlook.NEUTRAL_TO_BEARISH,
|
||||||
|
}
|
||||||
|
return DayPlaybook(
|
||||||
|
date=date(2026, 2, 20),
|
||||||
|
market=market,
|
||||||
|
market_outlook=outlook_map[outlook_str],
|
||||||
|
)
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_bearish_outlook_raises_buy_confidence_threshold(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY with confidence 85 should be suppressed to HOLD in bearish market."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_with_confidence(85))
|
||||||
|
playbook = self._make_playbook_with_outlook("bearish")
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=playbook,
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
# HOLD should be logged (not BUY) — check decision_logger was called with HOLD
|
||||||
|
call_args = decision_logger.log_decision.call_args
|
||||||
|
assert call_args is not None
|
||||||
|
assert call_args.kwargs["action"] == "HOLD"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_bearish_outlook_allows_high_confidence_buy(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY with confidence 92 should proceed in bearish market (threshold=90)."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_with_confidence(92))
|
||||||
|
playbook = self._make_playbook_with_outlook("bearish")
|
||||||
|
risk = MagicMock()
|
||||||
|
risk.validate_order = MagicMock()
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=playbook,
|
||||||
|
risk=risk,
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
call_args = decision_logger.log_decision.call_args
|
||||||
|
assert call_args is not None
|
||||||
|
assert call_args.kwargs["action"] == "BUY"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_bullish_outlook_lowers_buy_confidence_threshold(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY with confidence 77 should proceed in bullish market (threshold=75)."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_with_confidence(77))
|
||||||
|
playbook = self._make_playbook_with_outlook("bullish")
|
||||||
|
risk = MagicMock()
|
||||||
|
risk.validate_order = MagicMock()
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=playbook,
|
||||||
|
risk=risk,
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
call_args = decision_logger.log_decision.call_args
|
||||||
|
assert call_args is not None
|
||||||
|
assert call_args.kwargs["action"] == "BUY"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_bullish_outlook_suppresses_very_low_confidence_buy(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY with confidence 70 should be suppressed even in bullish market (threshold=75)."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_with_confidence(70))
|
||||||
|
playbook = self._make_playbook_with_outlook("bullish")
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=playbook,
|
||||||
|
risk=MagicMock(),
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
call_args = decision_logger.log_decision.call_args
|
||||||
|
assert call_args is not None
|
||||||
|
assert call_args.kwargs["action"] == "HOLD"
|
||||||
|
|
||||||
|
@pytest.mark.asyncio
|
||||||
|
async def test_neutral_outlook_uses_default_threshold(
|
||||||
|
self,
|
||||||
|
mock_broker: MagicMock,
|
||||||
|
mock_market: MagicMock,
|
||||||
|
mock_telegram: MagicMock,
|
||||||
|
) -> None:
|
||||||
|
"""BUY with confidence 82 should proceed in neutral market (default=80)."""
|
||||||
|
engine = MagicMock(spec=ScenarioEngine)
|
||||||
|
engine.evaluate = MagicMock(return_value=self._make_buy_match_with_confidence(82))
|
||||||
|
playbook = self._make_playbook_with_outlook("neutral")
|
||||||
|
risk = MagicMock()
|
||||||
|
risk.validate_order = MagicMock()
|
||||||
|
|
||||||
|
decision_logger = MagicMock()
|
||||||
|
decision_logger.log_decision = MagicMock(return_value="decision-id")
|
||||||
|
|
||||||
|
with patch("src.main.log_trade"):
|
||||||
|
await trading_cycle(
|
||||||
|
broker=mock_broker,
|
||||||
|
overseas_broker=MagicMock(),
|
||||||
|
scenario_engine=engine,
|
||||||
|
playbook=playbook,
|
||||||
|
risk=risk,
|
||||||
|
db_conn=MagicMock(),
|
||||||
|
decision_logger=decision_logger,
|
||||||
|
context_store=MagicMock(get_latest_timeframe=MagicMock(return_value=None)),
|
||||||
|
criticality_assessor=MagicMock(
|
||||||
|
assess_market_conditions=MagicMock(return_value=MagicMock(value="NORMAL")),
|
||||||
|
get_timeout=MagicMock(return_value=5.0),
|
||||||
|
),
|
||||||
|
telegram=mock_telegram,
|
||||||
|
market=mock_market,
|
||||||
|
stock_code="005930",
|
||||||
|
scan_candidates={},
|
||||||
|
)
|
||||||
|
|
||||||
|
call_args = decision_logger.log_decision.call_args
|
||||||
|
assert call_args is not None
|
||||||
|
assert call_args.kwargs["action"] == "BUY"
|
||||||
|
|||||||
@@ -440,3 +440,135 @@ class TestEvaluate:
|
|||||||
assert result.action == ScenarioAction.BUY
|
assert result.action == ScenarioAction.BUY
|
||||||
assert result.match_details["rsi"] == 25.0
|
assert result.match_details["rsi"] == 25.0
|
||||||
assert isinstance(result.match_details["rsi"], float)
|
assert isinstance(result.match_details["rsi"], float)
|
||||||
|
|
||||||
|
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
# Position-aware condition tests (#171)
|
||||||
|
# ---------------------------------------------------------------------------
|
||||||
|
|
||||||
|
|
||||||
|
class TestPositionAwareConditions:
|
||||||
|
"""Tests for unrealized_pnl_pct and holding_days condition fields."""
|
||||||
|
|
||||||
|
def test_evaluate_condition_unrealized_pnl_above_matches(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""unrealized_pnl_pct_above should match when P&L exceeds threshold."""
|
||||||
|
condition = StockCondition(unrealized_pnl_pct_above=3.0)
|
||||||
|
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": 5.0}) is True
|
||||||
|
|
||||||
|
def test_evaluate_condition_unrealized_pnl_above_no_match(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""unrealized_pnl_pct_above should NOT match when P&L is below threshold."""
|
||||||
|
condition = StockCondition(unrealized_pnl_pct_above=3.0)
|
||||||
|
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": 2.0}) is False
|
||||||
|
|
||||||
|
def test_evaluate_condition_unrealized_pnl_below_matches(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""unrealized_pnl_pct_below should match when P&L is under threshold."""
|
||||||
|
condition = StockCondition(unrealized_pnl_pct_below=-2.0)
|
||||||
|
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": -3.5}) is True
|
||||||
|
|
||||||
|
def test_evaluate_condition_unrealized_pnl_below_no_match(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""unrealized_pnl_pct_below should NOT match when P&L is above threshold."""
|
||||||
|
condition = StockCondition(unrealized_pnl_pct_below=-2.0)
|
||||||
|
assert engine.evaluate_condition(condition, {"unrealized_pnl_pct": -1.0}) is False
|
||||||
|
|
||||||
|
def test_evaluate_condition_holding_days_above_matches(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""holding_days_above should match when position held longer than threshold."""
|
||||||
|
condition = StockCondition(holding_days_above=5)
|
||||||
|
assert engine.evaluate_condition(condition, {"holding_days": 7}) is True
|
||||||
|
|
||||||
|
def test_evaluate_condition_holding_days_above_no_match(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""holding_days_above should NOT match when position held shorter."""
|
||||||
|
condition = StockCondition(holding_days_above=5)
|
||||||
|
assert engine.evaluate_condition(condition, {"holding_days": 3}) is False
|
||||||
|
|
||||||
|
def test_evaluate_condition_holding_days_below_matches(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""holding_days_below should match when position held fewer days."""
|
||||||
|
condition = StockCondition(holding_days_below=3)
|
||||||
|
assert engine.evaluate_condition(condition, {"holding_days": 1}) is True
|
||||||
|
|
||||||
|
def test_evaluate_condition_holding_days_below_no_match(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""holding_days_below should NOT match when held more days."""
|
||||||
|
condition = StockCondition(holding_days_below=3)
|
||||||
|
assert engine.evaluate_condition(condition, {"holding_days": 5}) is False
|
||||||
|
|
||||||
|
def test_combined_pnl_and_holding_days(self, engine: ScenarioEngine) -> None:
|
||||||
|
"""Combined position-aware conditions should AND-evaluate correctly."""
|
||||||
|
condition = StockCondition(
|
||||||
|
unrealized_pnl_pct_above=3.0,
|
||||||
|
holding_days_above=5,
|
||||||
|
)
|
||||||
|
# Both met → match
|
||||||
|
assert engine.evaluate_condition(
|
||||||
|
condition,
|
||||||
|
{"unrealized_pnl_pct": 4.5, "holding_days": 7},
|
||||||
|
) is True
|
||||||
|
# Only pnl met → no match
|
||||||
|
assert engine.evaluate_condition(
|
||||||
|
condition,
|
||||||
|
{"unrealized_pnl_pct": 4.5, "holding_days": 3},
|
||||||
|
) is False
|
||||||
|
|
||||||
|
def test_missing_unrealized_pnl_does_not_match(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""Missing unrealized_pnl_pct key should not match the condition."""
|
||||||
|
condition = StockCondition(unrealized_pnl_pct_above=3.0)
|
||||||
|
assert engine.evaluate_condition(condition, {}) is False
|
||||||
|
|
||||||
|
def test_missing_holding_days_does_not_match(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""Missing holding_days key should not match the condition."""
|
||||||
|
condition = StockCondition(holding_days_above=5)
|
||||||
|
assert engine.evaluate_condition(condition, {}) is False
|
||||||
|
|
||||||
|
def test_match_details_includes_position_fields(
|
||||||
|
self, engine: ScenarioEngine
|
||||||
|
) -> None:
|
||||||
|
"""match_details should include position fields when condition specifies them."""
|
||||||
|
pb = _playbook(
|
||||||
|
scenarios=[
|
||||||
|
StockScenario(
|
||||||
|
condition=StockCondition(unrealized_pnl_pct_above=3.0),
|
||||||
|
action=ScenarioAction.SELL,
|
||||||
|
confidence=90,
|
||||||
|
rationale="Take profit",
|
||||||
|
)
|
||||||
|
]
|
||||||
|
)
|
||||||
|
result = engine.evaluate(
|
||||||
|
pb,
|
||||||
|
"005930",
|
||||||
|
{"unrealized_pnl_pct": 5.0},
|
||||||
|
{},
|
||||||
|
)
|
||||||
|
assert result.action == ScenarioAction.SELL
|
||||||
|
assert "unrealized_pnl_pct" in result.match_details
|
||||||
|
assert result.match_details["unrealized_pnl_pct"] == 5.0
|
||||||
|
|
||||||
|
def test_position_conditions_parse_from_planner(self) -> None:
|
||||||
|
"""StockCondition should accept and store new fields from JSON parsing."""
|
||||||
|
condition = StockCondition(
|
||||||
|
unrealized_pnl_pct_above=3.0,
|
||||||
|
unrealized_pnl_pct_below=None,
|
||||||
|
holding_days_above=5,
|
||||||
|
holding_days_below=None,
|
||||||
|
)
|
||||||
|
assert condition.unrealized_pnl_pct_above == 3.0
|
||||||
|
assert condition.holding_days_above == 5
|
||||||
|
assert condition.has_any_condition() is True
|
||||||
|
|||||||
@@ -350,6 +350,42 @@ class TestSmartVolatilityScanner:
|
|||||||
assert [c.stock_code for c in candidates] == ["ABCD"]
|
assert [c.stock_code for c in candidates] == ["ABCD"]
|
||||||
|
|
||||||
|
|
||||||
|
class TestImpliedRSIFormula:
|
||||||
|
"""Test the implied_rsi formula in SmartVolatilityScanner (issue #181)."""
|
||||||
|
|
||||||
|
def test_neutral_change_gives_neutral_rsi(self) -> None:
|
||||||
|
"""0% change → implied_rsi = 50 (neutral)."""
|
||||||
|
# formula: 50 + (change_rate * 2.0)
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (0.0 * 2.0)))
|
||||||
|
assert rsi == 50.0
|
||||||
|
|
||||||
|
def test_10pct_change_gives_rsi_70(self) -> None:
|
||||||
|
"""10% upward change → implied_rsi = 70 (momentum signal)."""
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (10.0 * 2.0)))
|
||||||
|
assert rsi == 70.0
|
||||||
|
|
||||||
|
def test_minus_10pct_gives_rsi_30(self) -> None:
|
||||||
|
"""-10% change → implied_rsi = 30 (oversold signal)."""
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (-10.0 * 2.0)))
|
||||||
|
assert rsi == 30.0
|
||||||
|
|
||||||
|
def test_saturation_at_25pct(self) -> None:
|
||||||
|
"""Saturation occurs at >=25% change (not 12.5% as with old coefficient 4.0)."""
|
||||||
|
rsi_12pct = max(0.0, min(100.0, 50.0 + (12.5 * 2.0)))
|
||||||
|
rsi_25pct = max(0.0, min(100.0, 50.0 + (25.0 * 2.0)))
|
||||||
|
rsi_30pct = max(0.0, min(100.0, 50.0 + (30.0 * 2.0)))
|
||||||
|
# At 12.5% change: RSI = 75 (not 100, unlike old formula)
|
||||||
|
assert rsi_12pct == 75.0
|
||||||
|
# At 25%+ saturation
|
||||||
|
assert rsi_25pct == 100.0
|
||||||
|
assert rsi_30pct == 100.0 # Capped
|
||||||
|
|
||||||
|
def test_negative_saturation(self) -> None:
|
||||||
|
"""Saturation at -25% gives RSI = 0."""
|
||||||
|
rsi = max(0.0, min(100.0, 50.0 + (-25.0 * 2.0)))
|
||||||
|
assert rsi == 0.0
|
||||||
|
|
||||||
|
|
||||||
class TestRSICalculation:
|
class TestRSICalculation:
|
||||||
"""Test RSI calculation in VolatilityAnalyzer."""
|
"""Test RSI calculation in VolatilityAnalyzer."""
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user