mirror of
https://github.com/silencesdg/mt5_python_ea_suite.git
synced 2026-07-29 03:37:43 +00:00
4cb4f4a15e
- 重构核心模块:DataProvider 依赖注入、RiskController 门面、信号注册表 - 新增 FastAPI 代理服务 (run/server.py),支持局域网远程调用 MT5 - 新增 RemoteDataProvider + AttrDict,远端无缝替代 LiveDataProvider - 新增序列化模块,MT5 对象转 JSON 兼容格式 - 重构入口点至 run/ 包,支持 python -m run.realtime/server/backtest/optimize - 更新 CLAUDE.md 文档 Generated with [Claude Code](https://claude.ai/code) via [Happy](https://happy.engineering) Co-Authored-By: Claude <noreply@anthropic.com> Co-Authored-By: Happy <yesreply@happy.engineering>
43 lines
1.8 KiB
Python
43 lines
1.8 KiB
Python
import pandas as pd
|
|
from .base_strategy import BaseStrategy
|
|
from config import STRATEGY_CONFIG
|
|
|
|
class KDJStrategy(BaseStrategy):
|
|
def __init__(self, data_provider, symbol, timeframe, period=None):
|
|
super().__init__(data_provider, symbol, timeframe)
|
|
# 从配置中获取参数,如果传入参数则使用传入的参数
|
|
config = STRATEGY_CONFIG.get('kdj', {})
|
|
self.period = period if period is not None else config.get('period', 14)
|
|
|
|
def _calculate_indicators(self, df):
|
|
low_min = df['low'].rolling(self.period).min()
|
|
high_max = df['high'].rolling(self.period).max()
|
|
# 避免除零:当最高价==最低价时,RSV 取 50(中性)
|
|
price_range = high_max - low_min
|
|
rsv = (df['close'] - low_min) / price_range.replace(0, float('nan')) * 100
|
|
rsv = rsv.fillna(50)
|
|
df['k'] = rsv.ewm(com=2).mean()
|
|
df['d'] = df['k'].ewm(com=2).mean()
|
|
df['j'] = 3 * df['k'] - 2 * df['d']
|
|
return df
|
|
|
|
def generate_signal(self):
|
|
rates = self.data_provider.get_historical_data(self.symbol, self.timeframe, self.period + 5)
|
|
if rates is None or len(rates) < self.period:
|
|
return 0
|
|
df = pd.DataFrame(rates)
|
|
df = self._calculate_indicators(df)
|
|
|
|
if df['k'].iloc[-1] > df['d'].iloc[-1] and df['k'].iloc[-2] < df['d'].iloc[-2]:
|
|
return 1
|
|
elif df['k'].iloc[-1] < df['d'].iloc[-1] and df['k'].iloc[-2] > df['d'].iloc[-2]:
|
|
return -1
|
|
return 0
|
|
|
|
def run_backtest(self, df):
|
|
df = df.copy()
|
|
df = self._calculate_indicators(df)
|
|
signals = pd.Series(0, index=df.index)
|
|
signals[(df['k'] > df['d']) & (df['k'].shift(1) < df['d'].shift(1))] = 1
|
|
signals[(df['k'] < df['d']) & (df['k'].shift(1) > df['d'].shift(1))] = -1
|
|
return signals |