Initial public release
This commit is contained in:
@@ -0,0 +1,82 @@
|
||||
import time
|
||||
import pandas as pd
|
||||
import pandas_ta as ta
|
||||
from hydra.data.models import Candle
|
||||
|
||||
_MIN_CANDLES = 210 # EMA_200 requires at least 200 candles; 210 gives buffer
|
||||
|
||||
# Common technical indicators used for trading signals.
|
||||
# Uses ta.Study (the current pandas-ta API) instead of the deprecated
|
||||
# ta.Strategy("All") which is not available in newer pandas-ta versions.
|
||||
_DEFAULT_STUDY = ta.Study(
|
||||
name="hydra",
|
||||
ta=[
|
||||
{"kind": "rsi", "length": 14},
|
||||
{"kind": "ema", "length": 9},
|
||||
{"kind": "ema", "length": 20},
|
||||
{"kind": "ema", "length": 50},
|
||||
{"kind": "ema", "length": 200},
|
||||
{"kind": "sma", "length": 20},
|
||||
{"kind": "sma", "length": 50},
|
||||
{"kind": "macd"},
|
||||
{"kind": "bbands"},
|
||||
{"kind": "atr"},
|
||||
{"kind": "adx"},
|
||||
{"kind": "stoch"},
|
||||
{"kind": "stochrsi"},
|
||||
{"kind": "cci"},
|
||||
{"kind": "willr"},
|
||||
{"kind": "obv"},
|
||||
{"kind": "mfi"},
|
||||
{"kind": "mom"},
|
||||
{"kind": "roc"},
|
||||
{"kind": "tsi"},
|
||||
{"kind": "vwap"},
|
||||
{"kind": "supertrend"},
|
||||
{"kind": "kc"},
|
||||
{"kind": "donchian"},
|
||||
{"kind": "aroon"},
|
||||
{"kind": "ao"},
|
||||
{"kind": "er"},
|
||||
],
|
||||
)
|
||||
|
||||
|
||||
class IndicatorCalculator:
|
||||
"""Compute technical indicators for a candle list using pandas-ta."""
|
||||
|
||||
def compute(self, candles: list[Candle]) -> dict:
|
||||
"""
|
||||
Run a comprehensive set of pandas-ta indicators on candles.
|
||||
Returns {} if fewer than _MIN_CANDLES candles provided.
|
||||
NaN values are converted to None.
|
||||
"""
|
||||
if len(candles) < _MIN_CANDLES:
|
||||
return {}
|
||||
|
||||
df = pd.DataFrame([
|
||||
{
|
||||
"open": c.open, "high": c.high,
|
||||
"low": c.low, "close": c.close,
|
||||
"volume": c.volume,
|
||||
}
|
||||
for c in candles
|
||||
])
|
||||
|
||||
# cores=0 disables multiprocessing (avoids overhead for small DataFrames)
|
||||
df.ta.study(_DEFAULT_STUDY, cores=0)
|
||||
|
||||
last = df.iloc[-1].to_dict()
|
||||
result: dict = {}
|
||||
for key, val in last.items():
|
||||
if key in ("open", "high", "low", "close", "volume"):
|
||||
continue
|
||||
if isinstance(val, float) and pd.isna(val):
|
||||
result[key] = None
|
||||
elif hasattr(val, "item"): # numpy scalar → Python native
|
||||
result[key] = val.item()
|
||||
else:
|
||||
result[key] = val
|
||||
|
||||
result["calculated_at"] = int(time.time() * 1000)
|
||||
return result
|
||||
@@ -0,0 +1,85 @@
|
||||
import asyncio
|
||||
import json
|
||||
from hydra.data.storage.base import OhlcvStore
|
||||
from hydra.indicator.calculator import IndicatorCalculator
|
||||
from hydra.logging.setup import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
_CHANNEL = "hydra:candle:new"
|
||||
_KEY_PREFIX = "hydra:indicator"
|
||||
|
||||
|
||||
class IndicatorEngine:
|
||||
def __init__(
|
||||
self,
|
||||
store: OhlcvStore,
|
||||
redis_client,
|
||||
calculator: IndicatorCalculator,
|
||||
):
|
||||
self._store = store
|
||||
self._redis = redis_client
|
||||
self._calculator = calculator
|
||||
|
||||
async def _handle_event(
|
||||
self, market: str, symbol: str, timeframe: str
|
||||
) -> None:
|
||||
"""Compute indicators for one (market, symbol, timeframe) and cache in Redis."""
|
||||
try:
|
||||
candles = await self._store.query(market, symbol, timeframe, limit=250)
|
||||
result = self._calculator.compute(candles)
|
||||
if not result:
|
||||
return
|
||||
key = f"{_KEY_PREFIX}:{market}:{symbol}:{timeframe}"
|
||||
await self._redis.set(key, json.dumps(result))
|
||||
logger.debug("indicator_cached", market=market, symbol=symbol, tf=timeframe)
|
||||
except Exception as e:
|
||||
logger.warning(
|
||||
"indicator_error",
|
||||
market=market, symbol=symbol, tf=timeframe, error=str(e),
|
||||
)
|
||||
|
||||
async def cold_start(self) -> None:
|
||||
"""On startup, compute indicators for all symbols already in the DB."""
|
||||
symbols = await self._store.get_symbols()
|
||||
logger.info("indicator_cold_start", count=len(symbols))
|
||||
for row in symbols:
|
||||
await self._handle_event(row["market"], row["symbol"], row["timeframe"])
|
||||
|
||||
async def run(self) -> None:
|
||||
"""Subscribe to hydra:candle:new and process events."""
|
||||
pubsub = self._redis.pubsub()
|
||||
await pubsub.subscribe(_CHANNEL)
|
||||
logger.info("indicator_engine_subscribed", channel=_CHANNEL)
|
||||
async for message in pubsub.listen():
|
||||
if message["type"] != "message":
|
||||
continue
|
||||
try:
|
||||
payload = json.loads(message["data"])
|
||||
await self._handle_event(
|
||||
payload["market"], payload["symbol"], payload["timeframe"]
|
||||
)
|
||||
except Exception as e:
|
||||
logger.warning("indicator_subscribe_error", error=str(e))
|
||||
|
||||
|
||||
async def main() -> None:
|
||||
import redis.asyncio as aioredis
|
||||
from hydra.data.storage import create_store
|
||||
from hydra.config.settings import get_settings
|
||||
|
||||
settings = get_settings()
|
||||
store = create_store()
|
||||
await store.init()
|
||||
r = aioredis.from_url(settings.redis_url, decode_responses=True)
|
||||
calculator = IndicatorCalculator()
|
||||
engine = IndicatorEngine(store=store, redis_client=r, calculator=calculator)
|
||||
try:
|
||||
await engine.cold_start()
|
||||
await engine.run()
|
||||
finally:
|
||||
await r.aclose()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
asyncio.run(main())
|
||||
Reference in New Issue
Block a user