Real-Time Alert & Signal Pipeline
from dataclasses import dataclass, field
from datetime import datetime
from typing import Callable, Optional
import json
@dataclass
class AlertCondition:
name: str
check_fn: Callable # returns True/False
priority: str = "MEDIUM" # HIGH, MEDIUM, LOW
cooldown_minutes: int = 60
last_triggered: Optional[datetime] = None
@dataclass
class AlertRule:
id: str
name: str
conditions: list[AlertCondition]
logic: str = "ALL" # ALL (AND) or ANY (OR)
channels: list[str] = field(default_factory=lambda: ["console"])
message_template: str = ""
class AlertPipeline:
def __init__(self):
self.rules: list[AlertRule] = []
self.triggered_alerts: list[dict] = []
def add_rule(self, rule: AlertRule):
self.rules.append(rule)
def check_all(self, context: dict) -> list[dict]:
"""Check all rules against current market context."""
triggered = []
now = datetime.utcnow()
for rule in self.rules:
results = []
for cond in rule.conditions:
if cond.last_triggered and (now - cond.last_triggered).seconds < cond.cooldown_minutes * 60:
results.append(False)
continue
try:
results.append(cond.check_fn(context))
except:
results.append(False)
fire = all(results) if rule.logic == "ALL" else any(results)
if fire:
alert = {
"rule_id": rule.id, "name": rule.name, "time": now.isoformat(),
"priority": max((c.priority for c in rule.conditions), key=lambda p: {"HIGH": 3, "MEDIUM": 2, "LOW": 1}[p]),
"channels": rule.channels,
"message": rule.message_template.format(**context) if rule.message_template else rule.name,
}
triggered.append(alert)
for cond in rule.conditions:
cond.last_triggered = now
self.triggered_alerts.extend(triggered)
return triggered
def format_for_telegram(self, alert: dict) -> str:
return f"🚨 *{alert['priority']}* — {alert['name']}\n{alert['message']}\n⏰ {alert['time']}"
def format_for_mt5(self, alert: dict) -> str:
return f"Alert(\"{alert['name']}\", \"{alert['message']}\");"
# Preset alert conditions
def price_above(symbol: str, level: float):
return AlertCondition(f"{symbol} > {level}", lambda ctx: ctx.get(f"{symbol}_price", 0) > level, "HIGH")
def rsi_extreme(symbol: str, overbought: float = 70, oversold: float = 30):
return AlertCondition(f"{symbol} RSI extreme",
lambda ctx: ctx.get(f"{symbol}_rsi", 50) > overbought or ctx.get(f"{symbol}_rsi", 50) < oversold, "MEDIUM")
def correlation_shift(pair: str, threshold: float = 0.3):
return AlertCondition(f"{pair} corr shift",
lambda ctx: abs(ctx.get(f"{pair}_corr_deviation", 0)) > threshold, "HIGH")
1---2name: realtime-alert-pipeline3description: Condition monitoring, multi-trigger alerts, and notification pipeline for trading signals. Use this skill whenever the user asks about "set an alert", "price alert", "notify me when", "trigger alert", "condition monitoring", "signal pipeline", "push notification trading", "alert system", "watchlist alerts", "multi-condition trigger", "composite alert", or any request to set up automated monitoring and alerting. Works with mt5-chart-browser for data and all analysis skills for condition generation.4---56# Real-Time Alert & Signal Pipeline78```python9from dataclasses import dataclass, field10from datetime import datetime11from typing import Callable, Optional12import json1314@dataclass15class AlertCondition:16 name: str17 check_fn: Callable # returns True/False18 priority: str = "MEDIUM" # HIGH, MEDIUM, LOW19 cooldown_minutes: int = 6020 last_triggered: Optional[datetime] = None2122@dataclass23class AlertRule:24 id: str25 name: str26 conditions: list[AlertCondition]27 logic: str = "ALL" # ALL (AND) or ANY (OR)28 channels: list[str] = field(default_factory=lambda: ["console"])29 message_template: str = ""3031class AlertPipeline:3233 def __init__(self):34 self.rules: list[AlertRule] = []35 self.triggered_alerts: list[dict] = []3637 def add_rule(self, rule: AlertRule):38 self.rules.append(rule)3940 def check_all(self, context: dict) -> list[dict]:41 """Check all rules against current market context."""42 triggered = []43 now = datetime.utcnow()44 for rule in self.rules:45 results = []46 for cond in rule.conditions:47 if cond.last_triggered and (now - cond.last_triggered).seconds < cond.cooldown_minutes * 60:48 results.append(False)49 continue50 try:51 results.append(cond.check_fn(context))52 except:53 results.append(False)5455 fire = all(results) if rule.logic == "ALL" else any(results)56 if fire:57 alert = {58 "rule_id": rule.id, "name": rule.name, "time": now.isoformat(),59 "priority": max((c.priority for c in rule.conditions), key=lambda p: {"HIGH": 3, "MEDIUM": 2, "LOW": 1}[p]),60 "channels": rule.channels,61 "message": rule.message_template.format(**context) if rule.message_template else rule.name,62 }63 triggered.append(alert)64 for cond in rule.conditions:65 cond.last_triggered = now66 self.triggered_alerts.extend(triggered)67 return triggered6869 def format_for_telegram(self, alert: dict) -> str:70 return f"🚨 *{alert['priority']}* — {alert['name']}\n{alert['message']}\n⏰ {alert['time']}"7172 def format_for_mt5(self, alert: dict) -> str:73 return f"Alert(\"{alert['name']}\", \"{alert['message']}\");"7475# Preset alert conditions76def price_above(symbol: str, level: float):77 return AlertCondition(f"{symbol} > {level}", lambda ctx: ctx.get(f"{symbol}_price", 0) > level, "HIGH")7879def rsi_extreme(symbol: str, overbought: float = 70, oversold: float = 30):80 return AlertCondition(f"{symbol} RSI extreme",81 lambda ctx: ctx.get(f"{symbol}_rsi", 50) > overbought or ctx.get(f"{symbol}_rsi", 50) < oversold, "MEDIUM")8283def correlation_shift(pair: str, threshold: float = 0.3):84 return AlertCondition(f"{pair} corr shift",85 lambda ctx: abs(ctx.get(f"{pair}_corr_deviation", 0)) > threshold, "HIGH")86```