以下内容均由ChatGPT生成,非作者观点,读者可根据内容自行参考 策略思路 基于freqtrade框架的RSI、MACD、Bollinger Bands (BBands)的量化交易策略的设计思路。 使用TA-Lib库计算RSI、MACD和Bollinger Bands指标。具体来说,RSI指标通过 策略分析 策略设计: 基于上述思路 我们实现核心的买入和卖出代码 #买入 def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: # 筛选RSI低于买入阈值,且MACD值小于信号线的所有交易对 dataframe.loc[ (dataframe['rsi'] < self.rsi_buy) & (dataframe['macd'] < dataframe['macd_signal']), 'rsi_macd_buy'] = 1
# 进一步筛选BBands中间线以下的交易对 dataframe.loc[ (dataframe['bbands_lowerband'] < dataframe['close']), 'bbands_buy'] = 1
# 将两个条件合并,得到最终的买入信号 dataframe['buy'] = dataframe['rsi_macd_buy'] & dataframe['bbands_buy']
return dataframe
策略风险:
策略优化: 策略代码 from freqtrade.strategy import IStrategy, merge_informative_pair from pandas import DataFrame from functools import reduce import talib.abstract as ta import qtpylib
class RSIMACDBBands(IStrategy): minimal_roi = { '0': 0.05 }
stoploss = -0.1
# Buy hyperspace params: buy_params = { 'buy_adx': 20, 'buy_rsi': 30, 'buy_short_adx': 50, 'buy_short_rsi': 70 }
# Sell hyperspace params: sell_params = { 'sell_adx': 50, 'sell_rsi': 70, 'sell_short_adx': 20, 'sell_short_rsi': 30 }
# ROI table: roi_table = { '0': 0.05, '30': 0.03, '60': 0.02, '90': 0.01 }
# Trailing stop: trailing_stop = True trailing_stop_positive = 0.01 trailing_stop_positive_offset = 0.02
# Optimal timeframe for the strategy timeframe = '5m'
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame: dataframe['rsi'] = ta.RSI(dataframe) dataframe['macd'], dataframe['macdsignal'], dataframe['macdhist'] = ta.MACD(dataframe['close'], fastperiod=12, slowperiod=26, signalperiod=9) dataframe['bb_upperband'], dataframe['bb_middleband'], dataframe['bb_lowerband'] = ta.BBANDS(dataframe['close'], timeperiod=20, nbdevup=2, nbdevdn=2, matype=0) dataframe['adx'] = ta.ADX(dataframe)
return dataframe
def populate_buy_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: conditions_long = [] conditions_short = []
conditions_long.append( (dataframe['adx'] > self.buy_adx.value) & (dataframe['close'] < dataframe['bb_lowerband']) & ((qtpylib.crossed_above(dataframe['rsi'], self.buy_rsi.value))) )
conditions_short.append( (dataframe['adx'] < self.buy_short_adx.value) & (dataframe['close'] > dataframe['bb_upperband']) & ((qtpylib.crossed_below(dataframe['rsi'], self.sell_rsi.value))) )
dataframe.loc[ reduce(lambda x, y: x & y, conditions_long), 'enter_long', ] = 1
dataframe.loc[ reduce(lambda x, y: x & y, conditions_short), 'enter_short', ] = 1
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame: conditions_long = [] conditions_short = []
conditions_exit_long.append( (dataframe['adx'] < self.sell_adx.value) & (dataframe['close'] > dataframe['bb_middleband']) & ((qtpylib.crossed_below(dataframe['rsi'], self.buy_short_rsi.value))) )
conditions_exit_short.append( ((dataframe['adx'] > self.sell_short_adx.value)) & (dataframe['close'] < dataframe['bb_lowerband']) & ((qtpylib.crossed_above(dataframe['rsi'], self.sell_short_rsi.value))) ) dataframe.loc[ reduce(lambda x, y: x & y, conditions_exit_long), 'exit_long', ] = 1
dataframe.loc[ reduce(lambda x, y: x & y, conditions_exit_short), 'exit_short', ] = 1
return dataframe
以上代码均由chatGPT实现,各位读者可作为参考使用 投资需谨慎,市场有风险,本文仅做策略分享 |
|