feat: 添加边界防御机制使实盘行为与回测对齐

This commit is contained in:
2026-03-16 22:12:06 +08:00
parent 3c5a4c288a
commit a6aced2308
5 changed files with 3141 additions and 467 deletions

View File

@@ -118,7 +118,8 @@ class PragmaticCyberneticStrategy(Strategy):
self._closes.append(prev_bar.close)
T, FV, FB, ATR = self._calculate_indicators()
self.log(f'T: {T} FV: {FV} FB: {FB} ATR: {ATR}')
if self.trading:
self.log(f'T: {T} FV: {FV} FB: {FB} ATR: {ATR}')
if T is None or math.isnan(ATR): return
self._fbs.append(FB)
@@ -134,7 +135,8 @@ class PragmaticCyberneticStrategy(Strategy):
is_met = self.indicator is None or self.indicator.is_condition_met(*self.get_indicator_tuple())
self.log(f'is_met: {is_met}, pos: {pos}, current_fb: {current_fb}, prev_fb: {prev_fb}')
if self.trading:
self.log(f'is_met: {is_met}, pos: {pos}, current_fb: {current_fb}, prev_fb: {prev_fb}')
# ==========================================
# 核心一:经典出场逻辑 (你的原版设计)
@@ -192,18 +194,21 @@ class PragmaticCyberneticStrategy(Strategy):
# 核心二:经典入场逻辑 (完全不变)
# ==========================================
elif pos == 0 and is_met:
self.log(f'{prev_bar.close}, {T} , {current_fb} , {self.fb_entry_threshold}')
self.log(f'prev_bar.close > T: {prev_bar.close > T}, current_fb < -self.fb_entry_threshold : {current_fb < -self.fb_entry_threshold}')
self.log(f'prev_bar.close < T: {prev_bar.close < T}, current_fb > self.fb_entry_threshold: {current_fb > self.fb_entry_threshold}')
if self.trading:
self.log(f'{prev_bar.close}, {T} , {current_fb} , {self.fb_entry_threshold}')
self.log(f'prev_bar.close > T: {prev_bar.close > T}, current_fb < -self.fb_entry_threshold : {current_fb < -self.fb_entry_threshold}')
self.log(f'prev_bar.close < T: {prev_bar.close < T}, current_fb > self.fb_entry_threshold: {current_fb > self.fb_entry_threshold}')
if prev_bar.close > T and current_fb < -self.fb_entry_threshold:
if "BUY" in self.order_direction:
long_limit = self.round_to_tick(FV - (self.limit_offset_mult * ATR))
self.send_limit_order(long_limit, "BUY", self.trade_volume, "OPEN")
self.log(f"BUY LIMIT {long_limit}")
elif prev_bar.close < T and current_fb > self.fb_entry_threshold:
if "SELL" in self.order_direction:
short_limit = self.round_to_tick(FV + (self.limit_offset_mult * ATR))
self.send_limit_order(short_limit, "SELL", self.trade_volume, "OPEN")
self.log(f"SELL LIMIT {short_limit}")
# ==========================================
# 彻底隔离历史噪音:原生换月机制