//@version=6 strategy(“Brooks_Final_V53_With_Breakeven”, overlay=true, initial_capital=100000, process_orders_on_close=true)
// — 交易设置 — t0_enabled = input.bool(false, “开启T+0日内交易”) qty_per_hand = input.int(1, “每手股数”, minval=1)
// 基础指标 ema20 = ta.ema(close, 20) ema50 = ta.ema(close, 50) atr_value = ta.atr(14)
// 涨幅与形态逻辑 kline_change_pct = float((close - open) / open * 100) is_strong_bar = (close > open) and (math.abs(close - open) / (high - low) > 0.6) entry_and_growth_logic = (kline_change_pct > 2.01 and kline_change_pct < 6.99)
// — 变量记录 — var int last_entry_date = 0 var float entry_price = 0.0 var float trend_stop_level = 0.0
// — 进场逻辑 — if (strategy.position_size == 0 and ema20 > ema50 and close > ema20 and is_strong_bar and entry_and_growth_logic) strategy.entry(“Long_P1”, strategy.long, qty=qty_per_hand, comment=”P1_B”) strategy.entry(“Long_P2”, strategy.long, qty=qty_per_hand, comment=”P2_B”)
entry_price := close
trend_stop_level := low - syminfo.mintick
last_entry_date := year * 10000 + month * 100 + dayofmonth
// — 离场逻辑 — if (strategy.position_size > 0) bool can_exit_today = t0_enabled or (year * 10000 + month * 100 + dayofmonth > last_entry_date)
// 1. 平手保护:如果盈利超过 2%,将止损提升至保本价
if (high >= entry_price * 1.02)
trend_stop_level := math.max(trend_stop_level, entry_price)
// 2. 趋势跟随:动态调整止损(随行就市)
trend_stop_level := math.max(trend_stop_level, high - (atr_value * 2))
if (can_exit_today)
// Part 1: 固定 5% 止盈 + 动态止损
strategy.exit("Exit_P1", "Long_P1", limit=entry_price * 1.05, stop=trend_stop_level, comment_profit="P1_W", comment_loss="P1_S")
// Part 2: 仅跟随动态止损
strategy.exit("Exit_P2", "Long_P2", stop=trend_stop_level, comment_loss="P2_S")
// — 绘图 — plot(ema20, color=color.blue, title=”EMA20”) plot(ema50, color=color.red, title=”EMA50”) plot(strategy.position_size > 0 ? trend_stop_level : na, color=color.orange, style=plot.style_linebr, linewidth=2, title=”动态趋势止损线”)