mirror of
https://github.com/silencesdg/mt5_python_ea_suite.git
synced 2026-08-16 20:28:12 +00:00
基本完毕
This commit is contained in:
+17
-39
@@ -1,21 +1,17 @@
|
||||
import MetaTrader5 as mt5
|
||||
import pandas as pd
|
||||
from utils import get_rates, has_open_position, close_all, send_order
|
||||
from logger import logger
|
||||
from .base_strategy import BaseStrategy
|
||||
from config import STRATEGY_CONFIG
|
||||
|
||||
|
||||
class Strategy:
|
||||
def __init__(self):
|
||||
self.symbol = "XAUUSD"
|
||||
self.timeframe = mt5.TIMEFRAME_M1
|
||||
self.kdj_period = 9
|
||||
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):
|
||||
"""
|
||||
计算KDJ指标
|
||||
"""
|
||||
low_min = df['low'].rolling(self.kdj_period).min()
|
||||
high_max = df['high'].rolling(self.kdj_period).max()
|
||||
low_min = df['low'].rolling(self.period).min()
|
||||
high_max = df['high'].rolling(self.period).max()
|
||||
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()
|
||||
@@ -23,40 +19,22 @@ class Strategy:
|
||||
return df
|
||||
|
||||
def generate_signal(self):
|
||||
"""
|
||||
KDJ策略实盘
|
||||
K线向上穿越D线(金叉)买入,K线向下穿越D线(死叉)卖出
|
||||
"""
|
||||
rates = get_rates(self.symbol, self.timeframe, self.kdj_period + 30)
|
||||
if rates is None or len(rates) < self.kdj_period:
|
||||
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)
|
||||
|
||||
# Golden cross
|
||||
if df['k'].iloc[-2] < df['d'].iloc[-2] and df['k'].iloc[-1] > df['d'].iloc[-1]:
|
||||
logger.info(f"KDJ Golden Cross, creating buy signal: {self.symbol}")
|
||||
if df['k'].iloc[-1] > df['d'].iloc[-1] and df['k'].iloc[-2] < df['d'].iloc[-2]:
|
||||
return 1
|
||||
# Dead cross
|
||||
elif df['k'].iloc[-2] > df['d'].iloc[-2] and df['k'].iloc[-1] < df['d'].iloc[-1]:
|
||||
logger.info(f"KDJ Dead Cross, creating sell signal: {self.symbol}")
|
||||
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):
|
||||
"""
|
||||
KDJ回测方法
|
||||
根据金叉和死叉生成信号
|
||||
"""
|
||||
df = df.copy()
|
||||
df = self._calculate_indicators(df)
|
||||
|
||||
signals = pd.Series(0, index=df.index)
|
||||
for i in range(1, len(df)):
|
||||
# Golden cross
|
||||
if df['k'].iloc[i-1] < df['d'].iloc[i-1] and df['k'].iloc[i] > df['d'].iloc[i]:
|
||||
signals.iat[i] = 1
|
||||
# Dead cross
|
||||
elif df['k'].iloc[i-1] > df['d'].iloc[i-1] and df['k'].iloc[i] < df['d'].iloc[i]:
|
||||
signals.iat[i] = -1
|
||||
return signals
|
||||
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
|
||||
Reference in New Issue
Block a user