File size: 12,795 Bytes
bde2f3a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 | """
RugCharts OHLCV Aggregation Engine
===================================
Real-time candle building from trade events.
Produces OHLCV bars at 1m, 5m, 15m, 1h, 4h, 1d timeframes.
Stored in Redis sorted sets for O(log N) range queries.
Wired into DataBus as 'ohlcv' chain.
"""
import json
import logging
import os
import time
from datetime import UTC, datetime
import redis
logger = logging.getLogger("ohlcv_engine")
REDIS_HOST = os.getenv("REDIS_HOST", "rmi-redis")
REDIS_PORT = int(os.getenv("REDIS_PORT", "6379"))
REDIS_PASSWORD = os.getenv("REDIS_PASSWORD", "")
TIMEFRAMES = {
"1m": 60,
"5m": 300,
"15m": 900,
"1h": 3600,
"4h": 14400,
"1d": 86400,
}
CACHE_TTL = {
"1m": 300, # 5 min
"5m": 900, # 15 min
"15m": 1800, # 30 min
"1h": 3600, # 1 hour
"4h": 14400, # 4 hours
"1d": 86400, # 24 hours
}
MAX_CANDLES = 500 # Max candles returned per query
def _redis():
return redis.Redis(
host=REDIS_HOST,
port=REDIS_PORT,
password=REDIS_PASSWORD,
decode_responses=True,
socket_connect_timeout=2,
)
def _frame_key(token: str, chain: str, timeframe: str) -> str:
"""Redis sorted set key for OHLCV candles."""
return f"ohlcv:{chain}:{token}:{timeframe}"
def _candle_cache_key(token: str, chain: str, timeframe: str, limit: int, end_ts: int | None = None) -> str:
"""Cache key for bulk candle queries."""
end = end_ts or int(time.time())
return f"ohlcv_cache:{chain}:{token}:{timeframe}:{limit}:{end}"
class Candle:
"""Single OHLCV candle."""
__slots__ = ("close", "high", "low", "open", "timestamp", "trades", "volume")
def __init__(
self,
ts: int,
open_p: float = 0,
high: float = 0,
low: float = float("inf"),
close: float = 0,
volume: float = 0,
trades: int = 0,
):
self.timestamp = ts
self.open = open_p
self.high = high
self.low = low
self.close = close
self.volume = volume
self.trades = trades
def to_dict(self) -> dict:
return {
"timestamp": self.timestamp,
"datetime": datetime.fromtimestamp(self.timestamp, tz=UTC).isoformat(),
"open": round(self.open, 8),
"high": round(self.high, 8),
"low": round(self.low, 8),
"close": round(self.close, 8),
"volume": round(self.volume, 2),
"trades": self.trades,
}
def to_array(self) -> list:
"""Compact array format: [ts, o, h, l, c, v, n]"""
return [
self.timestamp,
round(self.open, 8),
round(self.high, 8),
round(self.low, 8),
round(self.close, 8),
round(self.volume, 2),
self.trades,
]
class OHLCVEngine:
"""Real-time OHLCV candle builder and query engine."""
def __init__(self):
self._active_candles: dict[str, Candle] = {} # key β current open candle
def _active_key(self, token: str, chain: str, timeframe: str, bucket_ts: int) -> str:
return f"{chain}:{token}:{timeframe}:{bucket_ts}"
def ingest_trade(
self,
token: str,
chain: str,
price: float,
volume: float,
timestamp: int | None = None,
commit: bool = True,
) -> dict[str, Candle]:
"""Ingest a single trade, updating all timeframe candles.
Returns dict of timeframe β updated Candle.
"""
ts = timestamp or int(time.time())
updated = {}
for tf_name, tf_seconds in TIMEFRAMES.items():
bucket_ts = (ts // tf_seconds) * tf_seconds
active_key = self._active_key(token, chain, tf_name, bucket_ts)
candle = self._active_candles.get(active_key)
if candle is None or candle.timestamp != bucket_ts:
# Close old candle and start new
if candle and commit:
self._persist_candle(token, chain, tf_name, candle)
candle = Candle(
ts=bucket_ts,
open_p=price,
high=price,
low=price,
close=price,
volume=volume,
trades=1,
)
self._active_candles[active_key] = candle
else:
# Update open candle
if candle.open == 0:
candle.open = price
candle.high = max(candle.high, price)
candle.low = min(candle.low, price)
candle.close = price
candle.volume += volume
candle.trades += 1
updated[tf_name] = candle
return updated
def _persist_candle(self, token: str, chain: str, timeframe: str, candle: Candle):
"""Write candle to Redis sorted set."""
try:
r = _redis()
key = _frame_key(token, chain, timeframe)
# Store as JSON in sorted set (score = timestamp)
r.zadd(key, {json.dumps(candle.to_dict()): candle.timestamp})
# Trim to MAX_CANDLES
r.zremrangebyrank(key, 0, -(MAX_CANDLES + 1))
r.expire(key, CACHE_TTL.get(timeframe, 3600))
r.close()
except Exception as e:
logger.warning(f"Failed to persist candle: {e}")
def flush_all(self):
"""Persist all active candles to Redis."""
for active_key, candle in list(self._active_candles.items()):
parts = active_key.split(":")
if len(parts) >= 4:
chain, token, tf_name = parts[0], parts[1], parts[2]
self._persist_candle(token, chain, tf_name, candle)
self._active_candles.clear()
def get_candles(
self,
token: str,
chain: str,
timeframe: str = "1h",
limit: int = 100,
end_ts: int | None = None,
) -> list[dict]:
"""Retrieve OHLCV candles for a token.
Args:
token: Token address
chain: Blockchain ID
timeframe: '1m', '5m', '15m', '1h', '4h', '1d'
limit: Max candles (default 100, max 500)
end_ts: End timestamp (default: now). Returns candles up to this time.
"""
if timeframe not in TIMEFRAMES:
timeframe = "1h"
limit = min(limit, MAX_CANDLES)
end = end_ts or int(time.time())
# Check cache first
cache_key = _candle_cache_key(token, chain, timeframe, limit, end)
try:
r = _redis()
cached = r.get(cache_key)
if cached:
r.close()
return json.loads(cached)
except Exception:
pass
try:
r = _redis() if "r" not in dir() or not r else r
key = _frame_key(token, chain, timeframe)
# Get candles up to end_ts
raw = r.zrangebyscore(key, 0, end, start=0, num=limit)
candles = []
for item in raw:
try:
c = json.loads(item)
candles.append(c)
except Exception:
pass
# Sort by timestamp ascending
candles.sort(key=lambda x: x["timestamp"])
# Include active candle if within range
tf_seconds = TIMEFRAMES[timeframe]
bucket_ts = (end // tf_seconds) * tf_seconds
active_key = self._active_key(token, chain, timeframe, bucket_ts)
active = self._active_candles.get(active_key)
if active and active.timestamp <= end:
# Avoid duplicating if already persisted
if not candles or candles[-1]["timestamp"] != active.timestamp:
candles.append(active.to_dict())
# Cache the result
r.setex(cache_key, 60, json.dumps(candles[-limit:] if len(candles) > limit else candles))
r.close()
return candles[-limit:] if len(candles) > limit else candles
except Exception as e:
logger.warning(f"OHLCV query failed: {e}")
return []
def get_latest_candle(self, token: str, chain: str, timeframe: str = "1h") -> dict | None:
"""Get the most recent candle (may be still-open)."""
candles = self.get_candles(token, chain, timeframe, limit=1)
return candles[0] if candles else None
def get_price_change(self, token: str, chain: str, periods: int = 24, timeframe: str = "1h") -> dict:
"""Calculate price change over N periods."""
candles = self.get_candles(token, chain, timeframe, limit=periods + 1)
if len(candles) < 2:
return {"change_pct": 0, "candles": 0}
first = candles[0]["close"]
last = candles[-1]["close"]
change_pct = ((last - first) / first * 100) if first > 0 else 0
return {
"change_pct": round(change_pct, 2),
"open": first,
"close": last,
"high": max(c["high"] for c in candles),
"low": min(c["low"] for c in candles),
"volume": sum(c["volume"] for c in candles),
"candles": len(candles),
}
def stats(self) -> dict:
"""Engine statistics."""
return {
"active_candles": len(self._active_candles),
"timeframes": list(TIMEFRAMES.keys()),
"max_candles_per_query": MAX_CANDLES,
}
# ββ Singleton ββββββββββββββββββββββββββββββββββββββββββββββββββββββ
ohlcv_engine = OHLCVEngine()
# ββ DataBus Provider ββββββββββββββββββββββββββββββββββββββββββββββββ
async def fetch_ohlcv(
token: str = "", chain: str = "ethereum", timeframe: str = "1h", limit: int = 100, **kw
) -> dict | None:
"""DataBus provider for OHLCV candle data.
Args:
token: Token address (use mint= or address= as aliases)
chain: Blockchain (ethereum, solana, bsc, base, etc.)
timeframe: 1m, 5m, 15m, 1h, 4h, 1d
limit: Number of candles (default 100, max 500)
"""
address = token or kw.get("mint", "") or kw.get("address", "")
if not address:
return None
candles = ohlcv_engine.get_candles(address, chain, timeframe, limit)
# Calculate summary stats
summary = {}
if candles:
prices = [c["close"] for c in candles]
volumes = [c["volume"] for c in candles]
summary = {
"current_price": prices[-1],
"price_change_pct": round(((prices[-1] - prices[0]) / prices[0] * 100), 2) if prices[0] > 0 else 0,
"high_24h": max(c["high"] for c in candles),
"low_24h": min(c["low"] for c in candles),
"volume_24h": sum(volumes),
"total_trades": sum(c["trades"] for c in candles),
}
# Also compute authenticity if we have volume data
authenticity = None
try:
from app.databus.volume_authenticity import quick_authenticity_score
auth = quick_authenticity_score(
volume_24h=summary.get("volume_24h", 0),
liquidity=float(kw.get("liquidity_usd", 0)),
unique_wallets=int(kw.get("unique_wallets", 0)),
tx_count=summary.get("total_trades", 0),
)
authenticity = {
"fake_volume_pct": auth.get("fake_volume_pct", 0),
"authentic_score": auth.get("authentic_score", 100),
"risk_level": auth.get("risk_level", "UNKNOWN"),
}
except Exception:
pass
return {
"candles": candles,
"summary": summary,
"authenticity": authenticity,
"timeframe": timeframe,
"token": address,
"chain": chain,
"source": "ohlcv_engine",
}
async def ingest_trade_data(
token: str = "",
chain: str = "ethereum",
price: float = 0,
volume: float = 0,
timestamp: int | None = None,
**kw,
) -> dict | None:
"""DataBus provider: Ingest a trade and update all OHLCV candles."""
address = token or kw.get("address", "") or kw.get("token", "")
if not address or price <= 0:
return None
price = float(price)
volume = float(volume)
updated = ohlcv_engine.ingest_trade(address, chain, price, volume, timestamp)
return {
"status": "ingested",
"token": address,
"chain": chain,
"price": price,
"volume": volume,
"updated_timeframes": list(updated.keys()),
"source": "ohlcv_engine",
}
|