- Split the `aggregation.py` file into a dedicated sub-package, improving modularity and maintainability. - Moved `TimeframeBucket`, `RealTimeCandleProcessor`, and `BatchCandleProcessor` classes into their respective files within the new `aggregation` sub-package. - Introduced utility functions for trade aggregation and validation, enhancing code organization. - Updated import paths throughout the codebase to reflect the new structure, ensuring compatibility. - Added safety net tests for the aggregation package to verify core functionality and prevent regressions during refactoring. These changes enhance the overall architecture of the aggregation module, making it more scalable and easier to manage.
78 lines
2.0 KiB
Python
78 lines
2.0 KiB
Python
"""
|
|
Utility functions for market data aggregation.
|
|
|
|
This module provides common utility functions for working with OHLCV candles
|
|
and trade data aggregation.
|
|
"""
|
|
|
|
import re
|
|
from typing import List, Tuple
|
|
|
|
from ..data_types import StandardizedTrade, OHLCVCandle
|
|
from .batch import BatchCandleProcessor
|
|
|
|
|
|
def aggregate_trades_to_candles(trades: List[StandardizedTrade],
|
|
timeframes: List[str],
|
|
symbol: str,
|
|
exchange: str) -> List[OHLCVCandle]:
|
|
"""
|
|
Simple utility function to aggregate a list of trades to candles.
|
|
|
|
Args:
|
|
trades: List of standardized trades
|
|
timeframes: List of timeframes to generate
|
|
symbol: Trading symbol
|
|
exchange: Exchange name
|
|
|
|
Returns:
|
|
List of completed candles
|
|
"""
|
|
processor = BatchCandleProcessor(symbol, exchange, timeframes)
|
|
return processor.process_trades_to_candles(iter(trades))
|
|
|
|
|
|
def validate_timeframe(timeframe: str) -> bool:
|
|
"""
|
|
Validate if timeframe is supported.
|
|
|
|
Args:
|
|
timeframe: Timeframe string (e.g., '1s', '5s', '10s', '1m', '5m', '1h')
|
|
|
|
Returns:
|
|
True if supported, False otherwise
|
|
"""
|
|
supported = ['1s', '5s', '10s', '15s', '30s', '1m', '5m', '15m', '30m', '1h', '4h', '1d']
|
|
return timeframe in supported
|
|
|
|
|
|
def parse_timeframe(timeframe: str) -> Tuple[int, str]:
|
|
"""
|
|
Parse timeframe string into number and unit.
|
|
|
|
Args:
|
|
timeframe: Timeframe string (e.g., '1s', '5m', '1h')
|
|
|
|
Returns:
|
|
Tuple of (number, unit)
|
|
|
|
Examples:
|
|
'1s' -> (1, 's')
|
|
'5m' -> (5, 'm')
|
|
'1h' -> (1, 'h')
|
|
'1d' -> (1, 'd')
|
|
"""
|
|
match = re.match(r'^(\d+)([smhd])$', timeframe.lower())
|
|
if not match:
|
|
raise ValueError(f"Invalid timeframe format: {timeframe}")
|
|
|
|
number = int(match.group(1))
|
|
unit = match.group(2)
|
|
return number, unit
|
|
|
|
|
|
__all__ = [
|
|
'aggregate_trades_to_candles',
|
|
'validate_timeframe',
|
|
'parse_timeframe'
|
|
] |