feat: 전략 백테스팅 기능 추가

- Introduced a `backtest_strategy` endpoint to enable strategy backtesting with user-specified parameters.
- Implemented a generic backtesting engine allowing rebalancing, equity curve tracking, and performance metric calculations.
- Added `BacktestMixin` for strategies to support backtesting-related operations.
- Extended BAA strategy to support backtesting with ticker data download and portfolio simulation.
- Updated `urls.py` to include the new backtesting endpoint.
- Enhanced logging and error handling throughout the backtesting process.
This commit is contained in:
2026-02-08 13:54:05 +09:00
parent 3be9d8eeba
commit b64a76a8b9
6 changed files with 605 additions and 39 deletions

View File

@@ -1,7 +1,9 @@
from abc import ABC, abstractmethod
from typing import Dict, Any
from typing import Dict, Any, List
import json
import pandas as pd
class BaseQuantStrategy(ABC):
"""
@@ -58,6 +60,29 @@ class BaseQuantStrategy(ABC):
return True
class BacktestMixin(ABC):
"""백테스트 가능한 전략을 위한 믹스인 클래스"""
@abstractmethod
def get_backtest_tickers(self, parameters: Dict[str, Any]) -> List[str]:
"""백테스트에 필요한 모든 티커 목록 반환"""
pass
@abstractmethod
def bulk_download_data(self, tickers: List[str], start_date, end_date) -> Dict[str, pd.Series]:
"""전체 기간 데이터 일괄 다운로드"""
pass
@abstractmethod
def calculate_portfolio_for_date(self, parameters: Dict[str, Any], as_of_date, data_cache: Dict[str, pd.Series]) -> Dict[str, Any]:
"""특정 날짜의 포트폴리오 배분 계산 (캐시 데이터 사용)
Returns:
{'mode': str, 'portfolio_weights': dict[str, float]}
"""
pass
class StrategyRegistry:
"""전략 구현체 레지스트리"""