4.0
- fixing data-type circular import - removing HOLD signal from saving to database to reduce data size
This commit is contained in:
@@ -14,7 +14,7 @@ IMPORTANT: Mirrors Indicator Patterns
|
||||
|
||||
from .base import BaseStrategy
|
||||
from .factory import StrategyFactory
|
||||
from .data_types import StrategySignal, SignalType, StrategyResult
|
||||
from data.common.data_types import StrategySignal, SignalType, StrategyResult
|
||||
from .manager import StrategyManager, StrategyConfig, StrategyType, StrategyCategory, get_strategy_manager
|
||||
from .data_integration import StrategyDataIntegrator, StrategyDataIntegrationConfig, get_strategy_data_integrator
|
||||
from .validation import StrategySignalValidator, ValidationConfig
|
||||
|
||||
@@ -20,7 +20,7 @@ from data.common.data_types import OHLCVCandle
|
||||
from data.common.indicators import TechnicalIndicators
|
||||
from components.charts.config.indicator_defs import convert_database_candles_to_ohlcv
|
||||
from .factory import StrategyFactory
|
||||
from .data_types import StrategyResult
|
||||
from data.common.data_types import StrategyResult
|
||||
from utils.logger import get_logger
|
||||
|
||||
# Initialize logger
|
||||
|
||||
@@ -1,72 +0,0 @@
|
||||
"""
|
||||
Strategy Data Types and Signal Definitions
|
||||
|
||||
This module provides data types for strategy calculations, signals,
|
||||
and results in a standardized format.
|
||||
"""
|
||||
|
||||
from dataclasses import dataclass
|
||||
from datetime import datetime
|
||||
from typing import Dict, Optional, Any, List
|
||||
from enum import Enum
|
||||
|
||||
|
||||
class SignalType(str, Enum):
|
||||
"""
|
||||
Types of trading signals that strategies can generate.
|
||||
"""
|
||||
BUY = "buy"
|
||||
SELL = "sell"
|
||||
HOLD = "hold"
|
||||
ENTRY_LONG = "entry_long"
|
||||
EXIT_LONG = "exit_long"
|
||||
ENTRY_SHORT = "entry_short"
|
||||
EXIT_SHORT = "exit_short"
|
||||
STOP_LOSS = "stop_loss"
|
||||
TAKE_PROFIT = "take_profit"
|
||||
|
||||
|
||||
@dataclass
|
||||
class StrategySignal:
|
||||
"""
|
||||
Container for individual strategy signals.
|
||||
|
||||
Attributes:
|
||||
timestamp: Signal timestamp (right-aligned with candle)
|
||||
symbol: Trading symbol
|
||||
timeframe: Candle timeframe
|
||||
signal_type: Type of signal (buy/sell/hold etc.)
|
||||
price: Price at which signal was generated
|
||||
confidence: Signal confidence score (0.0 to 1.0)
|
||||
metadata: Additional signal metadata (e.g., indicator values)
|
||||
"""
|
||||
timestamp: datetime
|
||||
symbol: str
|
||||
timeframe: str
|
||||
signal_type: SignalType
|
||||
price: float
|
||||
confidence: float = 1.0
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
|
||||
|
||||
@dataclass
|
||||
class StrategyResult:
|
||||
"""
|
||||
Container for strategy calculation results.
|
||||
|
||||
Attributes:
|
||||
timestamp: Candle timestamp (right-aligned)
|
||||
symbol: Trading symbol
|
||||
timeframe: Candle timeframe
|
||||
strategy_name: Name of the strategy that generated this result
|
||||
signals: List of signals generated for this timestamp
|
||||
indicators_used: Dictionary of indicator values used in calculation
|
||||
metadata: Additional calculation metadata
|
||||
"""
|
||||
timestamp: datetime
|
||||
symbol: str
|
||||
timeframe: str
|
||||
strategy_name: str
|
||||
signals: List[StrategySignal]
|
||||
indicators_used: Dict[str, float]
|
||||
metadata: Optional[Dict[str, Any]] = None
|
||||
@@ -207,25 +207,31 @@ class StrategySignalBroadcaster:
|
||||
result = signal.strategy_result
|
||||
context = signal.context
|
||||
|
||||
signal_data.append({
|
||||
'strategy_name': context.strategy_name,
|
||||
'strategy_config': context.strategy_config,
|
||||
'symbol': context.symbol,
|
||||
'timeframe': context.timeframe,
|
||||
'exchange': context.exchange,
|
||||
'timestamp': result.timestamp,
|
||||
'signal_type': result.signal.signal_type.value if result.signal else 'HOLD',
|
||||
'price': float(result.price) if result.price else None,
|
||||
'confidence': result.confidence,
|
||||
'signal_metadata': result.metadata or {},
|
||||
'generation_time': signal.generation_time
|
||||
})
|
||||
# Only store BUY or SELL signals, not HOLD
|
||||
if result.signal and result.signal.signal_type in [SignalType.BUY, SignalType.SELL]:
|
||||
signal_data.append({
|
||||
'strategy_name': context.strategy_name,
|
||||
'strategy_config': context.strategy_config,
|
||||
'symbol': context.symbol,
|
||||
'timeframe': context.timeframe,
|
||||
'exchange': context.exchange,
|
||||
'timestamp': result.timestamp,
|
||||
'signal_type': result.signal.signal_type.value,
|
||||
'price': float(result.price) if result.price else None,
|
||||
'confidence': result.confidence,
|
||||
'signal_metadata': result.metadata or {},
|
||||
'generation_time': signal.generation_time
|
||||
})
|
||||
|
||||
# Batch insert into database
|
||||
self.db_ops.strategy.store_signals_batch(signal_data)
|
||||
|
||||
if self.logger:
|
||||
self.logger.debug(f"Stored batch of {len(signals)} real-time signals")
|
||||
if signal_data: # Only call store_signals_batch if there are signals to store
|
||||
# Batch insert into database
|
||||
self.db_ops.strategy.store_signals_batch(signal_data)
|
||||
|
||||
if self.logger:
|
||||
self.logger.debug(f"Stored batch of {len(signal_data)} real-time signals")
|
||||
else:
|
||||
if self.logger:
|
||||
self.logger.debug("No BUY/SELL signals to store in this batch.")
|
||||
|
||||
except Exception as e:
|
||||
if self.logger:
|
||||
|
||||
Reference in New Issue
Block a user