75 lines
2.3 KiB
Python
75 lines
2.3 KiB
Python
"""
|
|
추세 추종(Trend Following) 전략 구현체
|
|
|
|
이동평균선, 모멘텀 등 추세를 따라가는 전략들을 포함합니다.
|
|
"""
|
|
|
|
from typing import Dict, Any
|
|
import time
|
|
import random
|
|
from ..base import BaseQuantStrategy, strategy
|
|
|
|
|
|
@strategy
|
|
class MovingAverageCrossover(BaseQuantStrategy):
|
|
"""이동평균선 교차 전략"""
|
|
|
|
@property
|
|
def name(self) -> str:
|
|
return "MovingAverageCrossover"
|
|
|
|
@property
|
|
def description(self) -> str:
|
|
return "단기 이동평균선이 장기 이동평균선을 상향 돌파할 때 매수, 하향 돌파할 때 매도하는 전략"
|
|
|
|
@property
|
|
def version(self) -> str:
|
|
return "1.0.0"
|
|
|
|
@property
|
|
def default_parameters(self) -> Dict[str, Any]:
|
|
return {
|
|
"short_window": 20,
|
|
"long_window": 50,
|
|
"initial_capital": 100000,
|
|
"position_size": 0.1
|
|
}
|
|
|
|
def validate_parameters(self, parameters: Dict[str, Any]) -> bool:
|
|
required_params = ["short_window", "long_window", "initial_capital"]
|
|
for param in required_params:
|
|
if param not in parameters:
|
|
return False
|
|
|
|
if parameters["short_window"] >= parameters["long_window"]:
|
|
return False
|
|
|
|
return True
|
|
|
|
def execute(self, parameters: Dict[str, Any] = None) -> Dict[str, Any]:
|
|
if parameters is None:
|
|
parameters = self.default_parameters
|
|
|
|
if not self.validate_parameters(parameters):
|
|
raise ValueError("Invalid parameters")
|
|
|
|
# 시뮬레이션 실행
|
|
time.sleep(1) # 실행 시간 시뮬레이션
|
|
|
|
# 모의 결과 생성
|
|
profit_rate = random.uniform(-0.15, 0.25)
|
|
trades_count = random.randint(15, 60)
|
|
win_rate = random.uniform(0.45, 0.75)
|
|
|
|
return {
|
|
"strategy": self.name,
|
|
"version": self.version,
|
|
"profit_loss": round(parameters["initial_capital"] * profit_rate, 2),
|
|
"profit_rate": round(profit_rate * 100, 2),
|
|
"trades_executed": trades_count,
|
|
"win_rate": round(win_rate, 3),
|
|
"execution_time": "1.2s",
|
|
"parameters_used": parameters,
|
|
"final_capital": round(parameters["initial_capital"] * (1 + profit_rate), 2)
|
|
}
|