添加ema52显示
This commit is contained in:
+63
-44
@@ -23,6 +23,7 @@ logger = logging.getLogger(__name__)
|
||||
# freqtrade trade -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC --strategy-path ./user_data/Chan/strategies
|
||||
# freqtrade backtesting -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC --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 download-data -c ./user_data/Chan/config/ChanLun_BTC_30.json -t 1m 1h 1d 1M --pairs BTC/USDT --timerange=20170101-
|
||||
# freqtrade hyperopt --hyperopt-loss SharpeHyperOptLossDaily --spaces roi --strategy ChanLun_BTC --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 --strategy-path ./user_data/Chan/strategies --timerange 20250721-20250901
|
||||
# freqtrade plot-dataframe -c ./user_data/Chan/config/ChanLun_BTC_30.json --strategy ChanLun_BTC --strategy-path ./user_data/Chan/strategies --timerange 20250721-20250901
|
||||
@@ -76,36 +77,56 @@ class ChanLun_BTC(IStrategy):
|
||||
|
||||
# 关闭分批止盈/仓位调整
|
||||
position_adjustment_enable = False
|
||||
startup_candle_count = 2880
|
||||
time3 = 3
|
||||
time5 = 5
|
||||
time15 = 15
|
||||
time30 = 30
|
||||
time60 = 60
|
||||
time2h = 120
|
||||
time4h = 240
|
||||
time1d = 1440
|
||||
startup_candle_count = 1600
|
||||
time3m = 3
|
||||
time5m = 5
|
||||
time10m = 10
|
||||
time15m = 15
|
||||
time30m = 30
|
||||
time_m = [3, 5, 10, 15, 30]
|
||||
|
||||
time1h = 60
|
||||
time2h = 2
|
||||
time4h = 4
|
||||
time6h = 6
|
||||
time8h = 8
|
||||
time12h = 12
|
||||
time16h = 16
|
||||
time_h = [2, 4, 6, 8, 12, 16]
|
||||
|
||||
time2d = 2
|
||||
time3d = 3
|
||||
time1w = 7
|
||||
time_d = [2, 3, 7]
|
||||
|
||||
time2M = 2
|
||||
time3M = 3
|
||||
time_M = [2, 3]
|
||||
|
||||
last_time = datetime.now()
|
||||
chan = ChanLun()
|
||||
last_order = None
|
||||
last_trade = None
|
||||
|
||||
pair = 'BTC/USDT:USDT'
|
||||
def informative_pairs(self):
|
||||
timeframes = ['1h', '1d', '1M']
|
||||
informative_pairs = [(self.pair, tf) for tf in timeframes]
|
||||
return informative_pairs
|
||||
def populate_indicators(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
|
||||
dataframe = self.add_indicators(dataframe)
|
||||
# 仅保留15m(用于BSP)与60m(用于ATR过滤/止损)两个重采样
|
||||
dataframe_15 = resample_to_interval(dataframe, self.get_ticker_indicator() * 15)
|
||||
dataframe_60 = resample_to_interval(dataframe, self.get_ticker_indicator() * 60)
|
||||
# 计算多周期BSP(以15m为基准),并合并到15m数据上
|
||||
# 先给重采样帧补指标
|
||||
dataframe_15 = self.add_indicators(dataframe_15)
|
||||
dataframe_60 = self.add_indicators(dataframe_60)
|
||||
# 计算15m BSP
|
||||
bsp_15 = self.chan.cal_bsp(dataframe, self.get_ticker_indicator())
|
||||
# 合并15m与60m到主DF,生成 resample_*_* 列
|
||||
dataframe = resampled_merge(dataframe, dataframe_15)
|
||||
dataframe = resampled_merge(dataframe, dataframe_60)
|
||||
self.init_dataframes(dataframe)
|
||||
return dataframe
|
||||
def init_dataframes(self, dataframe_m):
|
||||
dataframe_1h = self.dp.get_pair_dataframe(pair=self.pair, timeframe='1h')
|
||||
dataframe_1d = self.dp.get_pair_dataframe(pair=self.pair, timeframe='1d')
|
||||
dataframe_1M = self.dp.get_pair_dataframe(pair=self.pair, timeframe='1M')
|
||||
self.chan.init_dataframes(dataframe_m, dataframe_1h, dataframe_1d, dataframe_1M)
|
||||
self.print_all_ema52()
|
||||
def print_all_ema52(self):
|
||||
for key, value in self.chan.get_ema52_dict().items():
|
||||
print(key, value)
|
||||
def print_all_ema24(self):
|
||||
for key, value in self.chan.get_ema24_dict().items():
|
||||
print(key, value)
|
||||
def add_indicators(self, df):
|
||||
fast = 12
|
||||
slow = 26
|
||||
@@ -212,9 +233,9 @@ class ChanLun_BTC(IStrategy):
|
||||
return -0.05
|
||||
dataframe, _ = self.dp.get_analyzed_dataframe(trade.pair, self.timeframe)
|
||||
last_candle = dataframe.iloc[-1].squeeze()
|
||||
ema52_str = 'resample_{}_ema52'.format(self.get_ticker_indicator()*self.time15)
|
||||
ema52_str = 'resample_{}_ema52'.format(self.time15m)
|
||||
ema52_val = float(last_candle.get(ema52_str, 0) or 0)
|
||||
close_str = 'resample_{}_close'.format(self.get_ticker_indicator()*self.time15)
|
||||
close_str = 'resample_{}_close'.format(self.time15m)
|
||||
close_val = float(last_candle.get(close_str, 0) or 0)
|
||||
if close_val < ema52_val:
|
||||
return -0.01
|
||||
@@ -240,7 +261,7 @@ class ChanLun_BTC(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.time60)
|
||||
atr_str = 'resample_{}_atr'.format(self.time1h)
|
||||
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}")
|
||||
@@ -263,7 +284,7 @@ class ChanLun_BTC(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.time15)
|
||||
atr_str = 'resample_{}_atr'.format(elf.time15)
|
||||
# 保存开仓时的ATR值用于止损计算
|
||||
if (trade.nr_of_successful_entries == 1) and (order.ft_order_side == trade.entry_side):
|
||||
entry_atr = last_candle[atr_str] * 4
|
||||
@@ -271,13 +292,13 @@ class ChanLun_BTC(IStrategy):
|
||||
#logger.info(f"保存开仓时ATR值: {entry_atr}")
|
||||
return None
|
||||
def populate_entry_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
shift15 = self.time15
|
||||
shift60 = self.time60
|
||||
bsp_col = 'resample_{}_bsp_mtf'.format(self.get_ticker_indicator()*shift15)
|
||||
score_col = 'resample_{}_mtf_score'.format(self.get_ticker_indicator()*shift15)
|
||||
macdh_col = 'resample_{}_macdhist'.format(self.get_ticker_indicator()*shift15)
|
||||
c60_col = 'resample_{}_close'.format(self.get_ticker_indicator()*shift60)
|
||||
e60_col = 'resample_{}_ema52'.format(self.get_ticker_indicator()*shift60)
|
||||
shift15 = self.time15m
|
||||
shift60 = self.time1h
|
||||
bsp_col = 'resample_{}_bsp_mtf'.format(shift15)
|
||||
score_col = 'resample_{}_mtf_score'.format(shift15)
|
||||
macdh_col = 'resample_{}_macdhist'.format(shift15)
|
||||
c60_col = 'resample_{}_close'.format(shift60)
|
||||
e60_col = 'resample_{}_ema52'.format(shift60)
|
||||
# 强化过滤:15m BSP + 分数阈值 + 60m 趋势同向 + 15m MACD柱同向
|
||||
if all(col in dataframe.columns for col in [bsp_col, score_col, macdh_col, c60_col, e60_col]):
|
||||
dataframe.loc[
|
||||
@@ -298,12 +319,12 @@ class ChanLun_BTC(IStrategy):
|
||||
['enter_short', 'enter_tag']] = (1, 'short_bsp15_v2')
|
||||
return dataframe
|
||||
def populate_exit_trend(self, dataframe: DataFrame, metadata: dict) -> DataFrame:
|
||||
shift15 = self.time15
|
||||
shift60 = self.time60
|
||||
bsp_col = 'resample_{}_bsp_mtf'.format(self.get_ticker_indicator()*shift15)
|
||||
score_col = 'resample_{}_mtf_score'.format(self.get_ticker_indicator()*shift15)
|
||||
c60_col = 'resample_{}_close'.format(self.get_ticker_indicator()*shift60)
|
||||
e60_col = 'resample_{}_ema52'.format(self.get_ticker_indicator()*shift60)
|
||||
shift15 = self.time15m
|
||||
shift60 = self.time1h
|
||||
bsp_col = 'resample_{}_bsp_mtf'.format(shift15)
|
||||
score_col = 'resample_{}_mtf_score'.format(shift15)
|
||||
c60_col = 'resample_{}_close'.format(shift60)
|
||||
e60_col = 'resample_{}_ema52'.format(shift60)
|
||||
# 反向强信号或60m趋势反向时平仓
|
||||
if all(col in dataframe.columns for col in [bsp_col, score_col, c60_col, e60_col]):
|
||||
dataframe.loc[
|
||||
@@ -323,6 +344,4 @@ class ChanLun_BTC(IStrategy):
|
||||
proposed_leverage: float, max_leverage: float, entry_tag: Optional[str], side: str,
|
||||
**kwargs) -> float:
|
||||
return self.lev
|
||||
|
||||
def get_ticker_indicator(self):
|
||||
return int(self.timeframe[:-1])
|
||||
|
||||
Reference in New Issue
Block a user