恭喜我突破2000粉,上点干货🎉。今天扭亏为盈,Happy Hunting,发完我就要去快乐的逛拼多多买买买啦,留言明天回~
```
# === 1. 指标计算 ===
bb_mid = SMA(close, 20)
bb_std = STD(close, 20)
bb_upper = bb_mid + 2 * bb_std
bb_lower = bb_mid - 2 * bb_std
bandwidth = (bb_upper - bb_lower) / bb_mid * 100
atr = ATR(14)
# 带宽阈值
squeeze_threshold_extreme = bandwidth_percentile(10) # 极窄带,直接不做
squeeze_threshold_normal = bandwidth_percentile(20) # 普通窄带,可作为观察区间
# ATR过滤
atr_min_threshold = ATR_percentile(30) # ATR太低说明波动太小,容易被噪音洗掉
# 百分位止盈
tp_percentile_long = historical_positive_excursion_percentile(60)
tp_percentile_short = historical_negative_excursion_percentile(60)
# 交易冷却
cooldown_bars = 5
# 状态变量
position = NONE
last_exit_bar = -999
# === 2. 市场状态识别 ===
# 只在非极窄带时考虑交易
if bandwidth <= squeeze_threshold_extreme:
allow_trade_regime = False
else:
allow_trade_regime = True
# 趋势 / 震荡过滤(示意,可替换成ADX、均线斜率、高低点结构)
if abs(slope(bb_mid, 5)) < slope_threshold:
trend_state = "RANGE"
else:
trend_state = "TREND"
# === 3. 入场确认逻辑 ===
# 做多确认:触及下轨后出现反转
long_reversal_confirm =
(low <= bb_lower) and
(close > open) and
(lower_shadow > body_size)
# 做空确认:触及上轨后出现反转
short_reversal_confirm =
(high >= bb_upper) and
(close < open) and
(upper_shadow > body_size)
# === 4. 交易频率控制 ===
can_trade =
(position == NONE) and
(current_bar - last_exit_bar >= cooldown_bars) and
(atr >= atr_min_threshold) and
(allow_trade_regime == True) and
(trend_state == "RANGE")
# === 5. 入场逻辑 ===
if can_trade:
# 做多:下轨附近 + 反转确认
if long_reversal_confirm:
entry_price = close
stop_loss = low - 0.5 * atr
# 分批止盈:提高单笔收益
take_profit_1 = bb_mid
take_profit_2 = entry_price + tp_percentile_long
open_long()
# 做空:上轨附近 + 反转确认
elif short_reversal_confirm:
entry_price = close
stop_loss = high + 0.5 * atr
take_profit_1 = bb_mid
take_profit_2 = entry_price - tp_percentile_short
open_short()
# === 6. 持仓管理 ===
if position == LONG:
# 第一目标:中轨,先止盈一部分
if close >= take_profit_1 and not tp1_hit:
close_partial(50%)
tp1_hit = True
# 第二目标:百分位止盈
if close >= take_profit_2:
close_all()
last_exit_bar = current_bar
# 止损
if close <= stop_loss:
close_all()
last_exit_bar = current_bar
if position == SHORT:
if close <= take_profit_1 and not tp1_hit:
close_partial(50%)
tp1_hit = True
if close <= take_profit_2:
close_all()
last_exit_bar = current_bar
if close >= stop_loss:
close_all()
last_exit_bar = current_bar
# === 7. 风险控制 ===
# 连续亏损后暂停交易,避免噪音环境连续打脸
if consecutive_losses >= 3:
suspend_trading_for(20 bars)
``````
# =========================================
# 新增优化1:提升盈亏比(分批止盈 + 扩展利润)
# =========================================
# 原有中轨止盈基础上,增加远端止盈
take_profit_2 = bb_upper # 做多:看向上轨
take_profit_2 = bb_lower # 做空:看向下轨
take_profit_3 = entry_price + 1.5 * atr # 做多延伸利润
take_profit_3 = entry_price - 1.5 * atr # 做空延伸利润
# 分批止盈结构
if position == LONG:
if close >= bb_mid and not tp1_hit:
close_partial(30%)
tp1_hit = True
if close >= take_profit_2 and not tp2_hit:
close_partial(40%)
tp2_hit = True
if close >= take_profit_3:
close_all()
if position == SHORT:
if close <= bb_mid and not tp1_hit:
close_partial(30%)
tp1_hit = True
if close <= take_profit_2 and not tp2_hit:
close_partial(40%)
tp2_hit = True
if close <= take_profit_3:
close_all()
# =========================================
# 新增优化2:限制单笔最大亏损(尾部风险控制)
# =========================================
max_loss_threshold = 1.5 * atr
if position == LONG:
if (entry_price - close) > max_loss_threshold:
close_all() # 强制止损
if position == SHORT:
if (close - entry_price) > max_loss_threshold:
close_all()
# =========================================
# 新增优化3:带宽区间过滤(只做最优区间)
# =========================================
# 带宽分段
bandwidth_low = bandwidth_percentile(10)
bandwidth_high = bandwidth_percentile(60)
# 只允许在中等带宽交易
if bandwidth < bandwidth_low:
allow_trade = False # 太窄,不做
elif bandwidth > bandwidth_high:
allow_trade = False # 太宽,减少噪音
else:
allow_trade = True # 最优区间
# 在主逻辑中加入
can_trade = can_trade and allow_trade
# =========================================
# 新增优化4:过滤低收益交易(避免手续费白做)
# =========================================
min_expected_profit = fee * 3
expected_profit_long = bb_mid - entry_price
expected_profit_short = entry_price - bb_mid
if position_signal == LONG and expected_profit_long < min_expected_profit:
skip_trade()
if position_signal == SHORT and expected_profit_short < min_expected_profit:
skip_trade()
``````
先过滤掉:
- Profit Factor <= 1
- Max Drawdown > 20%
- Total Trades < 10
剩下的组合里,再按:
score = Sharpe * 0.4 + ProfitFactor * 0.3 + TotalReturn * 0.2 - MaxDrawdown * 0.1
选最优
```










