Files

40 lines
1.7 KiB
Python
Raw Permalink Normal View History

2025-07-25 17:35:01 +08:00
import pandas as pd
2025-08-14 10:13:04 +08:00
from .base_strategy import BaseStrategy
from config import STRATEGY_CONFIG
2025-07-25 17:35:01 +08:00
2025-08-14 10:13:04 +08:00
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)
2025-08-11 18:06:53 +08:00
2025-07-25 17:35:01 +08:00
def _calculate_indicators(self, df):
2025-08-14 10:13:04 +08:00
low_min = df['low'].rolling(self.period).min()
high_max = df['high'].rolling(self.period).max()
2025-07-25 17:35:01 +08:00
rsv = (df['close'] - low_min) / (high_max - low_min) * 100
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):
2025-08-14 10:13:04 +08:00
rates = self.data_provider.get_historical_data(self.symbol, self.timeframe, self.period + 5)
if rates is None or len(rates) < self.period:
2025-07-25 17:35:01 +08:00
return 0
df = pd.DataFrame(rates)
df = self._calculate_indicators(df)
2025-08-14 10:13:04 +08:00
if df['k'].iloc[-1] > df['d'].iloc[-1] and df['k'].iloc[-2] < df['d'].iloc[-2]:
2025-07-25 17:35:01 +08:00
return 1
2025-08-14 10:13:04 +08:00
elif df['k'].iloc[-1] < df['d'].iloc[-1] and df['k'].iloc[-2] > df['d'].iloc[-2]:
2025-07-25 17:35:01 +08:00
return -1
return 0
def run_backtest(self, df):
df = df.copy()
df = self._calculate_indicators(df)
signals = pd.Series(0, index=df.index)
2025-08-14 10:13:04 +08:00
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