This commit is contained in:
2025-10-11 19:05:42 +08:00
commit 6fc10484d2
11 changed files with 2359 additions and 0 deletions

View File

@@ -0,0 +1,134 @@
# pragma pylint: disable=missing-docstring, invalid-name, pointless-string-statement
# flake8: noqa: F401
# isort: skip_file
# --- Do not remove these imports ---
import numpy as np
import pandas as pd
from datetime import datetime, timedelta, timezone
from pandas import DataFrame
from typing import Optional, Union
from freqtrade.strategy import (
IStrategy,
Trade,
Order,
PairLocks,
informative, # @informative decorator
# Hyperopt Parameters
BooleanParameter,
CategoricalParameter,
DecimalParameter,
IntParameter,
RealParameter,
# timeframe helpers
timeframe_to_minutes,
timeframe_to_next_date,
timeframe_to_prev_date,
# Strategy helper functions
merge_informative_pair,
stoploss_from_absolute,
stoploss_from_open,
)
import logging
logger = logging.getLogger(__name__)
# --------------------------------
# Add your lib to import here
import talib.abstract as ta
from technical import qtpylib
class CCIMultiTimeframeSpotStrategy(IStrategy):
# 策略参数
INTERFACE_VERSION = 3
minimal_roi = {"0": 100}
stoploss = -1
trailing_stop = False
timeframe = '4h'
use_exit_signal = True
exit_profit_only = False
ignore_roi_if_entry_signal = False
def TD(self, dataframe:DataFrame):
close = dataframe['close'].to_list()
td = [0,0,0,0]
up = 0
down = 0
for i in range(4, len(close)):
if close[i] > close[i-4]:
up += 1
down = 0
td.append(up)
else:
down -= 1
up = 0
td.append(down)
return td
@informative('1d')
def populate_indicators_1d(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['cci'] = ta.CCI(dataframe, timeperiod=26)
dataframe["adx"] = ta.ADX(dataframe)
macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9,)
dataframe["macd"] = macd["macd"]
dataframe["macdsignal"] = macd["macdsignal"]
dataframe["macdhist"] = macd["macdhist"]
logger.info(dataframe.tail())
return dataframe
@informative('1w')
def populate_indicators_1w(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
dataframe['cci'] = ta.CCI(dataframe, timeperiod=26)
logger.info(dataframe.tail())
return dataframe
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 计算4h CCI
dataframe['cci_4h'] = ta.CCI(dataframe, timeperiod=26)
dataframe['sma12_4h'] = ta.SMA(dataframe, timeperiod=12)
dataframe['sma26_4h'] = ta.SMA(dataframe, timeperiod=26)
dataframe["adx_4h"] = ta.ADX(dataframe)
macd = ta.MACD(dataframe, fastperiod=12, slowperiod=26, signalperiod=9,)
dataframe["macd_4h"] = macd["macd"]
dataframe["macdsignal_4h"] = macd["macdsignal"]
dataframe["macdhist_4h"] = macd["macdhist"]
dataframe['cci_hist_4h'] = ta.CCI(dataframe['macdhist_1d'], dataframe['macdhist_1d'], dataframe['macdhist_1d'], timeperiod=26)
# dataframe['adx_hist_4h'] = ta.ADX(dataframe['macdhist_4h'])
dataframe['TD'] = self.TD(dataframe)
logger.info(dataframe.tail())
return dataframe
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 入场1d与4h CCI < -100且4h CCI上升当前 > 前一根)
dataframe.loc[
(
(dataframe['cci_4h'] < -100) &
(dataframe['cci_1d'] < -100) &
(dataframe['cci_4h'] > dataframe['cci_4h'].shift(1)) &
# (dataframe['macdhist_1d'] < dataframe['macdhist_1d'].shift(1)) &
(dataframe['volume'] > 0)
),
'enter_long',
] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
# 离场1d与4h CCI > 100且4h CCI下降当前 < 前一根)
dataframe.loc[
(
(dataframe['cci_4h'] > 100) &
(dataframe['cci_1d'] > 100) &
(dataframe['cci_4h'] < dataframe['cci_4h'].shift(1)) &
# (dataframe['macdhist_1d'] > dataframe['macdhist_1d'].shift(1)) &
(dataframe['volume'] > 0)
),
'exit_long',
] = 1
return dataframe