summaryrefslogtreecommitdiff
path: root/services/strategy-engine/src/strategy_engine/main.py
blob: 83bb867215c548d961d730cb17bf06fe60d93412 (plain)
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
"""Strategy Engine Service entry point."""
import asyncio
import logging
from pathlib import Path

from shared.broker import RedisBroker

from strategy_engine.config import StrategyConfig
from strategy_engine.engine import StrategyEngine
from strategy_engine.plugin_loader import load_strategies

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

# The strategies directory lives alongside the installed package
STRATEGIES_DIR = Path(__file__).parent.parent.parent.parent / "strategies"


async def run() -> None:
    config = StrategyConfig()
    broker = RedisBroker(config.redis_url)

    strategies_dir = STRATEGIES_DIR
    strategies = load_strategies(strategies_dir)

    # Configure each strategy with params from config
    for strategy in strategies:
        params = config.strategy_params.get(strategy.name, {})
        strategy.configure(params)

    logger.info(
        "Loaded %d strategies: %s",
        len(strategies),
        [s.name for s in strategies],
    )

    engine = StrategyEngine(broker=broker, strategies=strategies)

    try:
        for symbol in config.symbols:
            stream = f"candles.{symbol.replace('/', '_')}"
            last_id = "$"
            logger.info("Starting engine loop for stream=%s", stream)

            while True:
                last_id = await engine.process_once(stream, last_id)
    finally:
        await broker.close()


def main() -> None:
    asyncio.run(run())


if __name__ == "__main__":
    main()