59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
"""
|
|
Trading Action Module
|
|
|
|
Defines the TradingAction class used throughout the trading system.
|
|
"""
|
|
|
|
from dataclasses import dataclass
|
|
from datetime import datetime
|
|
from typing import Dict, Any, List
|
|
|
|
@dataclass
|
|
class TradingAction:
|
|
"""Represents a trading action with full context"""
|
|
symbol: str
|
|
action: str # 'BUY', 'SELL', 'HOLD'
|
|
quantity: float
|
|
confidence: float
|
|
price: float
|
|
timestamp: datetime
|
|
reasoning: Dict[str, Any]
|
|
|
|
def __post_init__(self):
|
|
"""Validate the trading action after initialization"""
|
|
if self.action not in ['BUY', 'SELL', 'HOLD']:
|
|
raise ValueError(f"Invalid action: {self.action}. Must be 'BUY', 'SELL', or 'HOLD'")
|
|
|
|
if self.confidence < 0.0 or self.confidence > 1.0:
|
|
raise ValueError(f"Invalid confidence: {self.confidence}. Must be between 0.0 and 1.0")
|
|
|
|
if self.quantity < 0:
|
|
raise ValueError(f"Invalid quantity: {self.quantity}. Must be non-negative")
|
|
|
|
if self.price <= 0:
|
|
raise ValueError(f"Invalid price: {self.price}. Must be positive")
|
|
|
|
def to_dict(self) -> Dict[str, Any]:
|
|
"""Convert trading action to dictionary"""
|
|
return {
|
|
'symbol': self.symbol,
|
|
'action': self.action,
|
|
'quantity': self.quantity,
|
|
'confidence': self.confidence,
|
|
'price': self.price,
|
|
'timestamp': self.timestamp.isoformat(),
|
|
'reasoning': self.reasoning
|
|
}
|
|
|
|
@classmethod
|
|
def from_dict(cls, data: Dict[str, Any]) -> 'TradingAction':
|
|
"""Create trading action from dictionary"""
|
|
return cls(
|
|
symbol=data['symbol'],
|
|
action=data['action'],
|
|
quantity=data['quantity'],
|
|
confidence=data['confidence'],
|
|
price=data['price'],
|
|
timestamp=datetime.fromisoformat(data['timestamp']),
|
|
reasoning=data['reasoning']
|
|
) |