prabalGaur commited on
Commit
2fdcf6c
·
verified ·
1 Parent(s): 72677c6

Upload community_contributions/chrys/agents/decision_agent.py with huggingface_hub

Browse files
community_contributions/chrys/agents/decision_agent.py ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """DECISION_AGENT: combine tech + sentiment, apply alert rules."""
2
+ from datetime import datetime, timezone, timedelta
3
+ from typing import List, Dict, Any
4
+
5
+ from agents.base import AgentBase
6
+ from models import TechResult, SentimentResult, DecisionRecord, AssetData
7
+
8
+ from config import (
9
+ ALERT_SCORE_THRESHOLD,
10
+ MAX_ALERTS_PER_HOUR,
11
+ ASSET_COOLDOWN_HOURS,
12
+ SCORE_BUMP_TO_OVERRIDE_COOLDOWN,
13
+ ASSETS_24_7,
14
+ MARKET_OPEN_HOUR_ET,
15
+ MARKET_CLOSE_HOUR_ET,
16
+ )
17
+
18
+
19
+ def _is_market_hours_et() -> bool:
20
+ # Simple check: use ET (UTC-5 or UTC-4). For robustness use zoneinfo in Py3.9+
21
+ try:
22
+ from zoneinfo import ZoneInfo
23
+ now = datetime.now(ZoneInfo("America/New_York"))
24
+ except Exception:
25
+ now = datetime.now(timezone.utc)
26
+ # Approximate ET = UTC-5
27
+ now = now.replace(tzinfo=timezone.utc) - timedelta(hours=5)
28
+ if now.weekday() >= 5: # Sat=5, Sun=6
29
+ return False
30
+ return MARKET_OPEN_HOUR_ET <= now.hour < MARKET_CLOSE_HOUR_ET
31
+
32
+
33
+ class DecisionAgent(AgentBase):
34
+ name = "DECISION_AGENT"
35
+ logger_name = "aria.decision_agent"
36
+
37
+ def __init__(self):
38
+ super().__init__()
39
+ self._last_alert_per_asset: Dict[str, datetime] = {}
40
+ self._last_score_per_asset: Dict[str, float] = {}
41
+ self._alerts_this_hour: List[datetime] = []
42
+
43
+ def _sentiment_points(self, sentiment: str) -> float:
44
+ if sentiment == "POSITIVE":
45
+ return 30.0
46
+ if sentiment == "NEGATIVE":
47
+ return -30.0
48
+ return 0.0
49
+
50
+ def _trim_hour_alerts(self):
51
+ cutoff = datetime.now(timezone.utc) - timedelta(hours=1)
52
+ self._alerts_this_hour = [t for t in self._alerts_this_hour if t > cutoff]
53
+
54
+ def run(
55
+ self,
56
+ tech_results: List[TechResult],
57
+ sentiment_results: List[SentimentResult],
58
+ asset_data: List[AssetData],
59
+ ) -> List[DecisionRecord]:
60
+ self.log("Computing final scores and alert decisions")
61
+ sentiment_by_asset = {s.asset: s for s in sentiment_results}
62
+ asset_by_name = {a.asset: a for a in asset_data}
63
+ market_open = _is_market_hours_et()
64
+ self._trim_hour_alerts()
65
+ records: List[DecisionRecord] = []
66
+ for tr in tech_results:
67
+ sent = sentiment_by_asset.get(tr.asset)
68
+ if sent:
69
+ sent_points = self._sentiment_points(sent.sentiment)
70
+ else:
71
+ sent_points = 0.0
72
+ final_score = (tr.tech_score * 0.70) + sent_points
73
+ decision = "SKIP"
74
+ skip_reason = None
75
+ priority = 0
76
+ if final_score >= ALERT_SCORE_THRESHOLD:
77
+ # Check 24/7: outside market hours only 24/7 assets
78
+ if not market_open and tr.asset not in ASSETS_24_7:
79
+ skip_reason = "Market closed; asset not 24/7"
80
+ else:
81
+ last = self._last_alert_per_asset.get(tr.asset)
82
+ now = datetime.now(timezone.utc)
83
+ if last and (now - last).total_seconds() < ASSET_COOLDOWN_HOURS * 3600:
84
+ prev_score = self._last_score_per_asset.get(tr.asset, 0)
85
+ if final_score < prev_score + SCORE_BUMP_TO_OVERRIDE_COOLDOWN:
86
+ skip_reason = "Cooldown"
87
+ if not skip_reason and len(self._alerts_this_hour) >= MAX_ALERTS_PER_HOUR:
88
+ skip_reason = "Max alerts per hour"
89
+ if not skip_reason:
90
+ decision = "ALERT"
91
+ if final_score >= 90:
92
+ priority = 2
93
+ elif final_score >= 75:
94
+ priority = 1
95
+ else:
96
+ priority = 0
97
+ records.append(DecisionRecord(
98
+ asset=tr.asset,
99
+ tech_score=tr.tech_score,
100
+ sentiment=sent.sentiment if sent else "MIXED",
101
+ final_score=round(final_score, 1),
102
+ decision=decision,
103
+ priority=priority,
104
+ skip_reason=skip_reason,
105
+ ))
106
+ if decision == "ALERT":
107
+ self._last_alert_per_asset[tr.asset] = datetime.now(timezone.utc)
108
+ self._last_score_per_asset[tr.asset] = final_score
109
+ self._alerts_this_hour.append(datetime.now(timezone.utc))
110
+ self.log(f"{tr.asset}: final={final_score:.1f} -> {decision}" + (f" ({skip_reason})" if skip_reason else ""))
111
+ return records