Add FastAPI backend for energy trading system
Implements FastAPI backend with ML model support for energy trading, including price prediction models and RL-based battery trading policy. Features dashboard, trading, backtest, and settings API routes with WebSocket support for real-time updates.
This commit is contained in:
83
backend/app/services/strategy_service.py
Normal file
83
backend/app/services/strategy_service.py
Normal file
@@ -0,0 +1,83 @@
|
||||
from typing import Dict, List, Optional
|
||||
from datetime import datetime
|
||||
from app.models.enums import StrategyEnum
|
||||
from app.models.schemas import StrategyStatus
|
||||
from app.utils.logger import get_logger
|
||||
|
||||
logger = get_logger(__name__)
|
||||
|
||||
|
||||
class StrategyService:
|
||||
def __init__(self):
|
||||
self._strategies: Dict[StrategyEnum, StrategyStatus] = {}
|
||||
self._initialize_strategies()
|
||||
|
||||
def _initialize_strategies(self):
|
||||
for strategy in StrategyEnum:
|
||||
self._strategies[strategy] = StrategyStatus(
|
||||
strategy=strategy, enabled=False, last_execution=None, total_trades=0, profit_loss=0.0
|
||||
)
|
||||
|
||||
async def execute_strategy(self, strategy: StrategyEnum, config: Optional[Dict] = None) -> Dict:
|
||||
logger.info(f"Executing strategy: {strategy.value}")
|
||||
status = self._strategies.get(strategy)
|
||||
|
||||
if not status or not status.enabled:
|
||||
raise ValueError(f"Strategy {strategy.value} is not enabled")
|
||||
|
||||
results = await self._run_strategy_logic(strategy, config or {})
|
||||
|
||||
status.last_execution = datetime.utcnow()
|
||||
status.total_trades += results.get("trades", 0)
|
||||
status.profit_loss += results.get("profit", 0)
|
||||
|
||||
return {"strategy": strategy.value, "status": status.dict(), "results": results}
|
||||
|
||||
async def _run_strategy_logic(self, strategy: StrategyEnum, config: Dict) -> Dict:
|
||||
if strategy == StrategyEnum.FUNDAMENTAL:
|
||||
return await self._run_fundamental_strategy(config)
|
||||
elif strategy == StrategyEnum.TECHNICAL:
|
||||
return await self._run_technical_strategy(config)
|
||||
elif strategy == StrategyEnum.ML:
|
||||
return await self._run_ml_strategy(config)
|
||||
elif strategy == StrategyEnum.MINING:
|
||||
return await self._run_mining_strategy(config)
|
||||
return {"trades": 0, "profit": 0}
|
||||
|
||||
async def _run_fundamental_strategy(self, config: Dict) -> Dict:
|
||||
logger.debug("Running fundamental strategy")
|
||||
return {"trades": 0, "profit": 0}
|
||||
|
||||
async def _run_technical_strategy(self, config: Dict) -> Dict:
|
||||
logger.debug("Running technical strategy")
|
||||
return {"trades": 0, "profit": 0}
|
||||
|
||||
async def _run_ml_strategy(self, config: Dict) -> Dict:
|
||||
logger.debug("Running ML strategy")
|
||||
return {"trades": 0, "profit": 0}
|
||||
|
||||
async def _run_mining_strategy(self, config: Dict) -> Dict:
|
||||
logger.debug("Running mining strategy")
|
||||
return {"trades": 0, "profit": 0}
|
||||
|
||||
async def get_strategy_status(self, strategy: StrategyEnum) -> StrategyStatus:
|
||||
return self._strategies.get(strategy, StrategyStatus(strategy=strategy, enabled=False))
|
||||
|
||||
async def get_all_strategies(self) -> List[StrategyStatus]:
|
||||
return list(self._strategies.values())
|
||||
|
||||
async def toggle_strategy(self, strategy: StrategyEnum, action: str) -> StrategyStatus:
|
||||
status = self._strategies.get(strategy)
|
||||
if not status:
|
||||
raise ValueError(f"Unknown strategy: {strategy.value}")
|
||||
|
||||
if action == "start":
|
||||
status.enabled = True
|
||||
logger.info(f"Strategy {strategy.value} started")
|
||||
elif action == "stop":
|
||||
status.enabled = False
|
||||
logger.info(f"Strategy {strategy.value} stopped")
|
||||
else:
|
||||
raise ValueError(f"Invalid action: {action}. Use 'start' or 'stop'")
|
||||
|
||||
return status
|
||||
Reference in New Issue
Block a user