Files
ai_stock/utils_id.py

14 lines
545 B
Python
Raw Permalink Normal View History

2025-12-08 15:30:19 +08:00
# -*- coding: utf-8 -*-
import hashlib
def stable_symbol_id(symbol: str, exchange: str = "US") -> int:
"""Generate a stable positive 64-bit int ID from symbol+exchange.
Collisions are extremely unlikely for our scale.
"""
base = f"{exchange}:{symbol}".upper().encode("utf-8")
h = hashlib.sha1(base).digest()
# take first 8 bytes as unsigned 64-bit integer
val = int.from_bytes(h[:8], byteorder="big", signed=False)
# constrain to 63-bit to avoid CSV tools issues with signedness
return val & ((1 << 63) - 1)