diff --git a/test.py b/test.py index 3cf3f2b..3fb1ec7 100644 --- a/test.py +++ b/test.py @@ -28,29 +28,29 @@ class StrategyConfig: # ===== 动态阈值(关键:自适应行情)===== # entry_dev / tp / sl 都由 ATR/Price 动态计算: max(下限, 系数 * atr_ratio) - entry_dev_floor: float = 0.0010 # 0.10% 最小偏离阈值(保证平静行情也能出单) - tp_floor: float = 0.0005 # 0.05% 最小止盈 - sl_floor: float = 0.0015 # 0.15% 最小止损 + entry_dev_floor: float = 0.0010 # 0.10% 最小偏离阈值(保证平静行情也能出单) + tp_floor: float = 0.0005 # 0.05% 最小止盈 + sl_floor: float = 0.0015 # 0.15% 最小止损 - entry_k: float = 1.20 # entry_dev = max(floor, entry_k * atr_ratio) - tp_k: float = 0.60 # tp = max(floor, tp_k * atr_ratio) - sl_k: float = 1.20 # sl = max(floor, sl_k * atr_ratio) + entry_k: float = 1.20 # entry_dev = max(floor, entry_k * atr_ratio) + tp_k: float = 0.60 # tp = max(floor, tp_k * atr_ratio) + sl_k: float = 1.20 # sl = max(floor, sl_k * atr_ratio) - max_hold_sec: int = 120 # 2分钟超时退出 - cooldown_sec_after_exit: int = 10 # 平仓后冷却10秒,防抖 + max_hold_sec: int = 120 # 2分钟超时退出 + cooldown_sec_after_exit: int = 10 # 平仓后冷却10秒,防抖 # ===== 下单/仓位 ===== - risk_percent: float = 0.0015 # 每次用可用余额的0.15%作为保证金预算 + risk_percent: float = 0.0015 # 每次用可用余额的0.15%作为保证金预算 min_size: int = 1 max_size: int = 5000 # ===== 日内风控 ===== - daily_loss_limit: float = 0.02 # -2% 停机 - daily_profit_cap: float = 0.01 # +1% 封顶停机 + daily_loss_limit: float = 0.02 # -2% 停机 + daily_profit_cap: float = 0.01 # +1% 封顶停机 # ===== 危险模式过滤(避免趋势/插针)===== - atr_ratio_kill: float = 0.0045 # ATR/Price > 0.45% -> 危险模式,暂停开仓 - big_body_kill: float = 0.012 # 1m实体>1.2% -> 危险模式 + atr_ratio_kill: float = 0.0045 # ATR/Price > 0.45% -> 危险模式,暂停开仓 + big_body_kill: float = 0.012 # 1m实体>1.2% -> 危险模式 # ===== 轮询节奏(减少REST压力)===== klines_refresh_sec: int = 10 @@ -385,8 +385,8 @@ class BitmartFuturesMeanReversionBot: size = self.calculate_size(price) logger.info( - f"enter_check: price={price:.2f}, ema={ema_value:.2f}, dev={dev*100:.3f}% " - f"(阈值={entry_dev*100:.3f}%), size={size}, pos={self.pos}" + f"enter_check: price={price:.2f}, ema={ema_value:.2f}, dev={dev * 100:.3f}% " + f"(阈值={entry_dev * 100:.3f}%), size={size}, pos={self.pos}" ) if size <= 0: @@ -397,14 +397,14 @@ class BitmartFuturesMeanReversionBot: self.pos = 1 self.entry_price = price self.entry_ts = time.time() - self.ding(f"✅开多:dev={dev*100:.3f}% size={size} entry={price:.2f}") + self.ding(f"✅开多:dev={dev * 100:.3f}% size={size} entry={price:.2f}") elif dev >= entry_dev: if self.place_market_order(4, size): # 开空 self.pos = -1 self.entry_price = price self.entry_ts = time.time() - self.ding(f"✅开空:dev={dev*100:.3f}% size={size} entry={price:.2f}") + self.ding(f"✅开空:dev={dev * 100:.3f}% size={size} entry={price:.2f}") def maybe_exit(self, price: float, tp: float, sl: float): if self.pos == 0 or self.entry_price is None or self.entry_ts is None: @@ -419,19 +419,19 @@ class BitmartFuturesMeanReversionBot: if pnl >= tp: self.close_position_all() - self.ding(f"🎯止盈:pnl={pnl*100:.3f}% price={price:.2f} tp={tp*100:.3f}%") + self.ding(f"🎯止盈:pnl={pnl * 100:.3f}% price={price:.2f} tp={tp * 100:.3f}%") self.entry_price, self.entry_ts = None, None self.last_exit_ts = time.time() elif pnl <= -sl: self.close_position_all() - self.ding(f"🛑止损:pnl={pnl*100:.3f}% price={price:.2f} sl={sl*100:.3f}%", error=True) + self.ding(f"🛑止损:pnl={pnl * 100:.3f}% price={price:.2f} sl={sl * 100:.3f}%", error=True) self.entry_price, self.entry_ts = None, None self.last_exit_ts = time.time() elif hold >= self.cfg.max_hold_sec: self.close_position_all() - self.ding(f"⏱超时:hold={int(hold)}s pnl={pnl*100:.3f}% price={price:.2f}") + self.ding(f"⏱超时:hold={int(hold)}s pnl={pnl * 100:.3f}% price={price:.2f}") self.entry_price, self.entry_ts = None, None self.last_exit_ts = time.time() @@ -448,9 +448,9 @@ class BitmartFuturesMeanReversionBot: f"方向:{direction_str}\n" f"现价:{price:.2f}\n" f"EMA{self.cfg.ema_len}:{ema_value:.2f}\n" - f"dev:{dev*100:.3f}%(阈值{entry_dev*100:.3f}%)\n" - f"ATR比:{atr_ratio*100:.3f}%\n" - f"tp/sl:{tp*100:.3f}% / {sl*100:.3f}%\n" + f"dev:{dev * 100:.3f}%(阈值{entry_dev * 100:.3f}%)\n" + f"ATR比:{atr_ratio * 100:.3f}%\n" + f"tp/sl:{tp * 100:.3f}% / {sl * 100:.3f}%\n" f"可用余额:{bal:.2f} USDT 杠杆:{self.cfg.leverage}x\n" f"超时:{self.cfg.max_hold_sec}s 冷却:{self.cfg.cooldown_sec_after_exit}s" ) @@ -528,3 +528,5 @@ if __name__ == "__main__": cfg = StrategyConfig() bot = BitmartFuturesMeanReversionBot(cfg) bot.action() + +# 9274.08