//@version=6 strategy(“Brooks_Final_V53_ThreeState_Logic”, overlay=true, initial_capital=100000, process_orders_on_close=true)
// — 1. 参数设置 — t0_enabled = input.bool(false, “开启T+0日内交易”) qty_per_hand = input.int(1, “每手股数”) risk_reward_ratio = input.float(1.0, “P1盈亏比”) slope_threshold = input.float(0.01, “趋势判定斜率阈值”, step=0.01)
// — 2. 全局变量 — var float dynamic_res = 0.0 var float dynamic_sup = 0.0 var int state = 1 // 1=区间, 2=上涨趋势, 3=下跌趋势 var int bull_count = 0 var float stop_level = 0.0 var float tp_p1 = 0.0 var int last_entry_date = 0
// — 3. 指标 — ema50 = ta.ema(close, 50) atr = ta.atr(14) h20 = ta.highest(high, 20) l20 = ta.lowest(low, 20)
// — 4. 动态边界 — if bar_index == 0 dynamic_res := h20 dynamic_sup := l20
if bar_index % 50 == 0 dynamic_res := h20 dynamic_sup := l20 else if close > dynamic_res dynamic_res := close if close < dynamic_sup dynamic_sup := close
// — 5. 状态机 (三态模式:1=区间, 2=上涨趋势, 3=下跌趋势) — is_bull = close > open is_bear = close < open
// 计算斜率:当前阻力/支撑 vs 20根K线前 float res_slope = dynamic_res - dynamic_res[20] float sup_slope = dynamic_sup - dynamic_sup[20]
// 状态转移逻辑 if (res_slope > slope_threshold) state := 2 // 上涨趋势 else if (sup_slope < -slope_threshold) state := 3 // 下跌趋势 else state := 1 // 回归区间
// 仅在上涨趋势下进行趋势回调计数 bull_count := (state == 2 and is_bear) ? bull_count + 1 : (is_bull ? 0 : bull_count)
// — 6. 进场决策 — bool is_near_support = (low <= dynamic_sup * 1.01) bool is_bull_reversal = is_bull and (close > open)
// 震荡区入场:仅在区间(State 1)且出现阳线反转 bool is_range_entry = (state == 1 and is_near_support and is_bull_reversal)
// 趋势区入场:仅在上涨趋势(State 2),价格在EMA50上且出现回调回调信号 bool is_trend_entry = (state == 2 and close > ema50 and is_bull and bull_count <= 3)
// 核心:下跌趋势(State 3)下强制 go_long 为 false go_long = (state != 3) and (is_range_entry or is_trend_entry)
// — 7. 订单 — if strategy.position_size == 0 and go_long strategy.entry(“Long_P1”, strategy.long, qty=qty_per_hand, comment=is_range_entry ? “R” : “T”) strategy.entry(“Long_P2”, strategy.long, qty=qty_per_hand, comment=is_range_entry ? “R” : “T”) stop_level := low - atr * 1 tp_p1 := close + (close - stop_level) * risk_reward_ratio last_entry_date := year * 10000 + month * 100 + dayofmonth
// — 8. 离场 — if strategy.position_size > 0 stop_level := math.max(stop_level, low - atr) is_today = (year * 10000 + month * 100 + dayofmonth == last_entry_date) can_exit = t0_enabled or not is_today
if can_exit
if close >= tp_p1
strategy.close("Long_P1", "P1_TP")
if close <= stop_level or close < dynamic_sup
strategy.close_all("Exit_Stop")
// — 9. 画图 — // 绿色=上涨(2), 红色=下跌(3), 灰色=区间(1) color bg_color = state == 2 ? color.new(color.green, 90) : (state == 3 ? color.new(color.red, 90) : color.new(color.gray, 95)) bgcolor(bg_color)
plot(dynamic_res, color=color.blue, title=”Dynamic Resistance”, linewidth=2) plot(dynamic_sup, color=color.red, title=”Dynamic Support”, linewidth=2) plot(strategy.position_size > 0 ? stop_level : na, color=color.orange, linewidth=2, style=plot.style_linebr, title=”Trail Stop”) plot(strategy.position_size > 0 ? tp_p1 : na, color=color.green, linewidth=2, style=plot.style_linebr, title=”P1 TP”)
plotshape(go_long and strategy.position_size == 0, style=shape.triangleup, location=location.belowbar, color=is_range_entry ? color.red : color.green, size=size.small, title=”Entry Marker”)