修改使用未来数据

This commit is contained in:
jackyu66git
2025-09-11 18:39:57 +08:00
parent e75aca320e
commit 4ea4b8cac5
6 changed files with 117 additions and 51 deletions
+22 -26
View File
@@ -22,7 +22,9 @@ logger = logging.getLogger(__name__)
# freqtrade trade -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies
# freqtrade backtesting -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies --timerange=20250901-
# freqtrade download-data -c ./user_data/Chan/config/ChanLun_BTC_30.json -t 1m --pairs BTC/USDT:USDT --timerange=20250405-
# freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi stoploss --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies -c ./user_data/Chan/config/ChanLun_BTC_30.json -e 200 --timerange=20250201-20250401
# freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies -c ./user_data/Chan/config/ChanLun_BTC_30.json -e 200 --timerange=20250201-20250901
# freqtrade edge -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies --timerange 20250721-20250901
# freqtrade plot-dataframe -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies --timerange 20250721-20250901
# sudo docker compose run --rm chanlun_btc backtesting -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC_30 --strategy-path ./user_data/Chan/strategies --timerange=20250721-
# sudo docker compose run --rm chanlun_btc download-data -c ./user_data/Chan/config/ChanLun_BTC_30.json --pairs BTC/USDT:USDT -t 1m --timerange 20240101-
@@ -67,8 +69,8 @@ class ChanLun_BTC_30(IStrategy):
use_custom_stoploss = True # 启用自定义止损
trailing_stop = False
trailing_stop_positive = 0.025
trailing_stop_positive_offset = 0.045
trailing_stop_positive = 0.03
trailing_stop_positive_offset = 0.06
trailing_only_offset_is_reached = False
# 关闭分批止盈/仓位调整
@@ -133,18 +135,8 @@ class ChanLun_BTC_30(IStrategy):
dataframe_4h['state'] = state_list
state_list = self.chan.get_klu_state_list(dataframe_1d)
dataframe_1d['state'] = state_list
#bi_list_1 = self.chan.get_bi_list(dataframe)
#bi_list_5 = self.chan.get_bi_list(dataframe_5)
#bi_list_15 = self.chan.get_bi_list(dataframe_15)
#bi_list_30 = self.chan.get_bi_list(dataframe_30)
#bi_list_60 = self.chan.get_bi_list(dataframe_60)
if self.last_time + timedelta(minutes=1) < datetime.now():
#self.print_bi(bi_list_1)
#self.print_bi(bi_list_5)
#self.print_bi(bi_list_15)
#self.print_bi(bi_list_30)
#self.print_bi(bi_list_60)
self.print_seg(dataframe_5)
print("-------------------------------------------------------------------------------")
self.last_time = datetime.now()
dataframe = resampled_merge(dataframe, dataframe_3)
@@ -228,9 +220,9 @@ class ChanLun_BTC_30(IStrategy):
new_entryprice = proposed_rate
if trade:
if trade.is_short:
new_entryprice = proposed_rate - 50
new_entryprice = proposed_rate - 5
else:
new_entryprice = proposed_rate + 50
new_entryprice = proposed_rate + 5
return new_entryprice
def custom_exit_price(self, pair: str, trade: Trade,
@@ -239,9 +231,9 @@ class ChanLun_BTC_30(IStrategy):
new_exitprice = proposed_rate
if trade:
if trade.is_short:
new_exitprice = proposed_rate + 50
new_exitprice = proposed_rate + 5
else:
new_exitprice = proposed_rate - 50
new_exitprice = proposed_rate - 5
return new_exitprice
def adjust_trade_position(self, trade: Trade, current_time: datetime,
@@ -260,6 +252,10 @@ class ChanLun_BTC_30(IStrategy):
止损 = 开仓价 ± 1 * ATR(开仓时的ATR)。
多单: 开仓价 - ATR;空单: 开仓价 + ATR。
"""
# 保本止损:当浮盈达到或超过 1% 时,将止损提至开仓价
#if current_profit is not None and current_profit >= 0.14:
#return stoploss_from_absolute(trade.open_rate, current_rate, is_short=trade.is_short)
entry_atr = trade.get_custom_data(key="entry_atr")
if entry_atr is None:
# 回退:取当前数据的 ATR 估算
@@ -292,10 +288,10 @@ class ChanLun_BTC_30(IStrategy):
if dataframe is None or len(dataframe) == 0:
return False
last = dataframe.iloc[-1]
atr_str = 'resample_{}_atr'.format(self.get_ticker_indicator()*self.time30)
atr_str = 'resample_{}_atr'.format(self.get_ticker_indicator()*self.time60)
atr_val = float(last.get(atr_str, 0) or 0)
if atr_val < 0.001:
logger.info(f"ATR过滤:atr={atr_val:.2f} < 100, 拒绝进场 {pair}")
#logger.info(f"ATR过滤:atr={atr_val:.2f} < 100, 拒绝进场 {pair}")
return False
return True
except Exception as e:
@@ -315,15 +311,15 @@ class ChanLun_BTC_30(IStrategy):
# Obtain pair dataframe (just to show how to access it)
dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe)
last_candle = dataframe.iloc[-1].squeeze()
atr_str = 'resample_{}_atr'.format(self.get_ticker_indicator()*self.time30)
atr_str = 'resample_{}_atr'.format(self.get_ticker_indicator()*self.time60)
# 保存开仓时的ATR值用于止损计算
if (trade.nr_of_successful_entries == 1) and (order.ft_order_side == trade.entry_side):
entry_atr = last_candle[atr_str] * 4
trade.set_custom_data(key="entry_atr", value=entry_atr)
logger.info(f"保存开仓时ATR值: {entry_atr}")
#logger.info(f"保存开仓时ATR值: {entry_atr}")
return None
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
shift_time = self.time30
shift_time = self.time60
state_str = 'resample_{}_state'.format(self.get_ticker_indicator()*shift_time)
dataframe.loc[
@@ -338,19 +334,19 @@ class ChanLun_BTC_30(IStrategy):
['enter_short', 'enter_tag']] = (1, 'short_30')
return dataframe
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
shift_time = self.time30
shift_time = self.time60
state_str = 'resample_{}_state'.format(self.get_ticker_indicator()*shift_time)
dataframe.loc[
(
(dataframe[state_str].shift(shift_time) == "20")
),
['exit_long', 'exit_tag']] = (1, 'long_close_15')
['exit_long', 'exit_tag']] = (1, 'long_close_30')
dataframe.loc[
(
(dataframe[state_str].shift(shift_time) == "-20")
),
['exit_short', 'exit_tag']] = (1, 'short_close_15')
['exit_short', 'exit_tag']] = (1, 'short_close_30')
return dataframe
def leverage(self, pair: str, current_time: datetime, current_rate: float,
proposed_leverage: float, max_leverage: float, entry_tag: Optional[str], side: str,