Add initial implementation of the Orderflow Backtest System with OBI and CVD metrics integration, including core modules for storage, strategies, and visualization. Introduced persistent metrics storage in SQLite, optimized memory usage, and enhanced documentation.

This commit is contained in:
Simon Moisy
2025-08-26 17:22:07 +08:00
parent 63f723820a
commit fa6df78c1e
52 changed files with 7039 additions and 1 deletions

3
parsers/__init__.py Normal file
View File

@@ -0,0 +1,3 @@
"""Parsing utilities for transforming raw persisted data into domain models."""

View File

@@ -0,0 +1,45 @@
from __future__ import annotations
from ast import literal_eval
from typing import Dict
import logging
from models import OrderbookLevel
class OrderbookParser:
"""Parser for orderbook side text into structured levels.
Maintains a price cache for memory efficiency and provides a method to
parse a side into a dictionary of price -> OrderbookLevel.
"""
def __init__(self, price_cache: dict[float, float] | None = None, debug: bool = False) -> None:
self._price_cache: dict[float, float] = price_cache or {}
self._debug = debug
def parse_side(self, text: str, side_dict: Dict[float, OrderbookLevel]) -> None:
"""Parse orderbook side data from text and populate the provided dictionary."""
if not text or text.strip() == "":
return
try:
arr = literal_eval(text)
for p, s, lc, oc in arr:
price = float(p)
size = float(s)
price = self._price_cache.get(price, price)
if size > 0:
side_dict[price] = OrderbookLevel(
price=price,
size=size,
liquidation_count=int(lc),
order_count=int(oc),
)
except Exception as e:
if self._debug:
logging.exception("Error parsing orderbook data")
logging.debug(f"Text sample: {text[:100]}...")
else:
logging.error(f"Failed to parse orderbook data: {type(e).__name__}")