mirror of
https://github.com/silencesdg/mt5_python_ea_suite.git
synced 2026-08-14 03:08:09 +00:00
add ml
This commit is contained in:
@@ -15,6 +15,15 @@ class BaseStrategy:
|
||||
self.timeframe = timeframe
|
||||
self.name = self.__class__.__name__
|
||||
|
||||
def set_params(self, params):
|
||||
"""动态设置策略参数"""
|
||||
for key, value in params.items():
|
||||
if hasattr(self, key):
|
||||
setattr(self, key, value)
|
||||
logger.debug(f"{self.name}: 参数 '{key}' 已更新为 {value}")
|
||||
else:
|
||||
logger.warning(f"{self.name}: 尝试设置不存在的参数 '{key}'")
|
||||
|
||||
def _log_signal(self, signal, reason=None):
|
||||
"""记录信号日志"""
|
||||
if signal != 0:
|
||||
|
||||
@@ -38,13 +38,16 @@ class DailyBreakoutStrategy(BaseStrategy):
|
||||
|
||||
def run_backtest(self, df):
|
||||
df = df.copy()
|
||||
df['time'] = pd.to_datetime(df['time'], unit='s')
|
||||
df['date'] = df['time'].dt.date
|
||||
# BUG FIX: The 'time' column does not exist in a properly formed dataframe.
|
||||
# Time information should be derived from the DatetimeIndex.
|
||||
df['date'] = df.index.date
|
||||
|
||||
# LOGIC FIX: daily_lows should be the minimum of the day, not the maximum.
|
||||
daily_highs = df.groupby('date')['high'].transform('max')
|
||||
daily_lows = df.groupby('date')['low'].transform('max')
|
||||
daily_lows = df.groupby('date')['low'].transform('min')
|
||||
|
||||
signals = pd.Series(0, index=df.index)
|
||||
# Signal when close breaks yesterday's high/low
|
||||
signals[df['close'] > daily_highs.shift(1)] = 1
|
||||
signals[df['close'] < daily_lows.shift(1)] = -1
|
||||
return signals
|
||||
Reference in New Issue
Block a user