First commit
This commit is contained in:
+45
@@ -0,0 +1,45 @@
|
||||
"""Hard risk rules. These always have the final word — not the strategy, not the LLM."""
|
||||
from dataclasses import dataclass
|
||||
|
||||
from .brokers import Broker
|
||||
from .config import settings
|
||||
from .sentiment import Verdict
|
||||
|
||||
|
||||
@dataclass
|
||||
class Decision:
|
||||
allowed: bool
|
||||
reason: str
|
||||
amount: float = 0.0
|
||||
|
||||
|
||||
def check_buy(broker: Broker, symbol: str, verdict: Verdict, day_start_equity: float,
|
||||
kill_switch: bool) -> Decision:
|
||||
if kill_switch:
|
||||
return Decision(False, "kill switch is on")
|
||||
if broker.has_position(symbol):
|
||||
return Decision(False, "already holding this asset")
|
||||
|
||||
equity = broker.equity()
|
||||
if day_start_equity > 0 and (equity - day_start_equity) / day_start_equity <= -settings.daily_loss_limit_pct:
|
||||
return Decision(False, f"daily loss limit of {settings.daily_loss_limit_pct:.0%} reached")
|
||||
if len(broker.positions()) >= settings.max_open_positions:
|
||||
return Decision(False, f"max {settings.max_open_positions} open positions")
|
||||
|
||||
if verdict.sentiment == "unavailable" and settings.block_buys_if_filter_fails:
|
||||
return Decision(False, "news filter unavailable, playing it safe")
|
||||
if verdict.sentiment == "bearish" and verdict.confidence >= settings.veto_confidence:
|
||||
return Decision(False, f"bearish news ({verdict.confidence:.0%}): {verdict.summary}")
|
||||
|
||||
# Base size: 10% of equity, but never more than the hard per-order cap.
|
||||
cap = settings.max_order_eur * (settings.eur_usd_rate if broker.name == "alpaca" else 1)
|
||||
size = min(equity * settings.max_position_pct, cap)
|
||||
if verdict.sentiment == "bearish": # weak bearish: half size
|
||||
size *= 0.5
|
||||
note = "half size (mildly bearish news)"
|
||||
elif verdict.sentiment in ("neutral", "unavailable"):
|
||||
size *= 0.75
|
||||
note = "75% size (no clear news edge)"
|
||||
else:
|
||||
note = "full size (supportive news)"
|
||||
return Decision(True, note, round(size, 2))
|
||||
Reference in New Issue
Block a user