// @version=2 indicator(precision = 2) // GH850 v1.5 —— 相对 v1.4 只加「趋势过滤」,其余判定逐项不变: // ★ 新增稳定趋势通道:对最近 20 根收盘价做最小二乘线性回归 y = a + b*x, // 取判定根处的拟合值作「趋势中枢线」;上下各 2 倍残差标准差为通道上下轨。 // · 中枢线逐 bar 平滑移动、锚点不跳变,无「摆动点连线」式锯齿抖动。 // · 只用已收盘根(close[k],k>=0),不 repaint;前 20 根预热期无效。 // · 早期 v1.5 曾用「分形摆动低/高点连线 + 线性外推」,因锚点频繁切换导致 // 趋势线视觉抖动,已弃用并替换为本最小二乘通道。 // ★ 新增开关 enableTrendFilter(★默认 false): // · false(默认)= 完全等同 v1.4,四类信号数量、判定链一字不变; // · true = 反弹多须价格在趋势中枢线上方(上升趋势)、 // 回落空须在中枢线下方(下降趋势), // 共振多 / 共振空同向过滤(可单独关,applyTrendToCombo = false)。 // ★ 新增 showTrendLine:只控制趋势通道绘制,不影响任何判定。 // ★ 关键约束保持不变: // · 路径②(共振多 / 共振空)与量能彻底解耦,屏蔽仍只看 reboundCore; // · 趋势线过滤挂在 reboundOk / reboundShortOk / path2 本体, // 不进 reboundCore,不改变「反弹多形态让位共振」的原有逻辑; // · 回落空与共振信号互不屏蔽,同根可并存。 // ══════════ 开关 ══════════ enableRebound = true // pRebound 超卖反弹(多) enableReboundShort = true // ★ v1.4 新增:超买回落(空),false = 关闭 enableVolFilter = true // ★ v1.4:量能过滤,只作用于反弹多 / 回落空 useShrinkVol = false // false = 放量确认;true = 缩量过滤(回到 v1.3 语义) enableBollPos = true // ★ v1.4:布林位置确认(多 c1mb1) enableBodyPos = true // ★ v1.4:K 线实体位置确认(收盘在 K 线上 / 下半部) enableTDCombo = true // pTDCombo TD+combo 双共振(用户要求保持打开) td9 = true td13 = true showAuxMarks = true // 三档强度 / TD 触发点参考标注,可关;不参与判定 showWeakMarks = false // 弱多 / 弱空标注,false = 不显示(仅关绘图,判定不变) showTdMarks = true // TD9 / TD13 触发点标注,false = 清除全部 TD 标注(判定不变) enableTrendFilter = false // ★ v1.5:趋势线过滤总开关,false = 与 v1.4 完全一致 applyTrendToCombo = true // ★ v1.5:趋势线过滤是否作用于共振多 / 空(仅总开关打开时有效) showTrendLine = true // ★ v1.5:绘制趋势线(仅绘图,不影响判定) // ══════════ 参数 ══════════ reboundRsi = 32 // 超卖阈值 reboundRsiHigh = 68 // ★ v1.4 新增:超买阈值(100 - 32 对称) minAtrPct = 0.0111 maxAtrPct = 0.6 comboWeak = 0.60 comboMid = 0.70 comboStrong = 0.85 winBars = 5 // 共振窗口(根);本版展开支持 1..5,默认 5 rsiP = 14 bollN = 20 bollM = 2 atrP = 14 volN = 20 // ★ v1.4:量能均线周期 minVolRatio = 1.2 // ★ v1.4:放量模式下限(由 1.0 收紧到 1.2) maxVolRatio = 0.8 // 缩量模式上限,仅 useShrinkVol = true 时生效 // ★ v1.5 稳定趋势通道参数(Pine v2 无循环,全部展开) // trendLen 改动需同步改下方 trSX/trSXX/trDen/终点系数 等由 N=20 推导的常数;JS 端用同值。 trendLen = 20 trendK = 2.0 // ══════════ 收盘序列:全部显式平移一根,杜绝未收盘根参与 ══════════ c1 = close[1] // 页面 last.c c2 = close[2] // 页面 prev.c c3 = close[3] // 页面 closes[len-3],2 根前(动量) rsiS = rsi(close, rsiP) rsi1 = rsiS[1] // 已提交值,定格 [mid, ub, lb] = boll(close, bollN, bollM) mb1 = mid[1] ub1 = ub[1] // 收盘 TR:整根用 [1] / [2],等价于 v1.1 的 ma(tr,14)[1](14 根 TR 简单平均) trC = max(high[1] - low[1], max(abs(high[1] - close[2]), abs(low[1] - close[2]))) atrC = ma(trC, atrP) atrPct = c1 > 0 ? atrC / c1 * 100 : 0 // ★ v1.4 收盘量比:volC / 20 根均量,全部以已收盘根为基准,等价 ma(volume,20)[1] volC = volume[1] avgVolC = ma(volC, volN) // ★ 修复:无成交量 / 均量为 0 时判定「量能不可用」,volOk 自动放行 volValid = avgVolC > 0 and volC > 0 volRatio = volValid ? volC / avgVolC : 1 // 锚点:用上一根已收盘 K 线的低 / 高点,文字位置不再随实时价跳动 lowC = low[1] highC = high[1] // ══════════ 市场过滤(页面 marketFilter) ══════════ atrOk = atrPct >= minAtrPct and atrPct <= maxAtrPct // ★ v1.4:volOk 只给反弹多 / 回落空用;路径②共振多 / 共振空不引用 // ★ 修复:量能数据不可用时自动放行,不再需要手动关 enableVolFilter volOk = (not enableVolFilter or not volValid) or (useShrinkVol ? volRatio < maxVolRatio : volRatio > minVolRatio) // ══════════ ★ v1.5 稳定趋势通道:最小二乘线性回归(全用已收盘根) ══════════ // 时间轴:close[1] = 判定根(最新已收盘),close[20] = 窗口最旧根。 // 对窗口 20 根已收盘 close 拟合 y = a + b*x,x = 0(最旧 close[20]) .. 19(最新 close[1])。 // Pine v2 无循环/数组/:=,用分组展开求 Σy / Σy² / Σxy;Σx=190、Σx²=2470、N=20 为常数。 // 中枢线取终点 x=19 处的拟合值 = a + b*19;通道 = 中枢 ± trendK*残差标准差。 // 残差平方和用等价式 Σ(y-ŷ)² = Σy² - a*Σy - b*Σxy(a、b 为最小二乘解时成立), // 这样 JS 端可用同一公式逐值对齐,避免展开 20 项残差带来的浮点差异。 sy1 = close[1]+close[2]+close[3]+close[4]+close[5] sy2 = close[6]+close[7]+close[8]+close[9]+close[10] sy3 = close[11]+close[12]+close[13]+close[14]+close[15] sy4 = close[16]+close[17]+close[18]+close[19]+close[20] trSY = sy1+sy2+sy3+sy4 // Σy qy1 = close[1]*close[1]+close[2]*close[2]+close[3]*close[3]+close[4]*close[4]+close[5]*close[5] qy2 = close[6]*close[6]+close[7]*close[7]+close[8]*close[8]+close[9]*close[9]+close[10]*close[10] qy3 = close[11]*close[11]+close[12]*close[12]+close[13]*close[13]+close[14]*close[14]+close[15]*close[15] qy4 = close[16]*close[16]+close[17]*close[17]+close[18]*close[18]+close[19]*close[19]+close[20]*close[20] trSYY = qy1+qy2+qy3+qy4 // Σy² xy1 = 19*close[1]+18*close[2]+17*close[3]+16*close[4]+15*close[5] xy2 = 14*close[6]+13*close[7]+12*close[8]+11*close[9]+10*close[10] xy3 = 9*close[11]+8*close[12]+7*close[13]+6*close[14]+5*close[15] xy4 = 4*close[16]+3*close[17]+2*close[18]+1*close[19] trSXY = xy1+xy2+xy3+xy4 // Σxy(x=0 对应 close[20],系数 0 省略) trDen = trendLen*2470 - 190*190 // N*Σx² - (Σx)² = 20*2470-190² = 13300 trendValid = not na(close[20]) trB = trendValid and trDen != 0 ? (trendLen*trSXY - 190*trSY)/trDen : 0 trA = trendValid ? (trSY - trB*190)/trendLen : na trendMid = trendValid ? trA + trB*(trendLen-1) : na trSS = trSYY - trA*trSY - trB*trSXY // 残差平方和 trSD = trendValid and trSS > 0 ? sqrt(trSS/trendLen) : 0 upTrendLine = trendValid ? trendMid + trendK*trSD : na dnTrendLine = trendValid ? trendMid - trendK*trSD : na // 趋势过滤:价格在回归中枢线上方=上升趋势(利多)、下方=下降趋势(利空);预热期不可得时放行 longTrendOk = not enableTrendFilter or not trendValid or c1 >= trendMid shortTrendOk = not enableTrendFilter or not trendValid or c1 <= trendMid // ══════════ 路径① 超卖反弹(多) / 路径①b 超买回落(空) ══════════ // ★ 新增过滤:只用已收盘根;每个都有独立开关,关掉即可单独验证该项效果 bodyMid = (highC + lowC) / 2 bollPosLong = not enableBollPos or c1 < mb1 bollPosShort = not enableBollPos or c1 > mb1 bodyPosLong = not enableBodyPos or c1 > bodyMid bodyPosShort = not enableBodyPos or c1 < bodyMid reboundFilterLong = bollPosLong and bodyPosLong reboundFilterShort = bollPosShort and bodyPosShort // 纯形态,保持 v1.3 原样 → reboundCore 不变,共振信号数量不受本次过滤影响 reboundRaw = rsi1 < reboundRsi and c1 > c2 reboundShortRaw = rsi1 > reboundRsiHigh and c1 < c2 // ★ 修复:屏蔽路径②只看形态 + 开关 + ATR,摘掉 volOk 与新增过滤 reboundCore = enableRebound and reboundRaw and atrOk // ★ v1.5:趋势线过滤挂在信号本体(不进 reboundCore) reboundOk = reboundCore and volOk and reboundFilterLong and longTrendOk // ★ v1.4 新增:与路径① 完全对称的做空分支(阈值 68,方向相反) reboundShortOk = enableReboundShort and reboundShortRaw and atrOk and volOk and reboundFilterShort and shortTrendOk // ══════════ 路径② combo 预测信号(三档 强88 / 中74 / 弱64) ══════════ devw = (ub1 - mb1) / 2 zval = devw > 0 ? (c1 - mb1) / devw : 0 relvol = mb1 > 0 ? devw / mb1 : 0 momN = relvol > 0 and c3 > 0 ? (c1 / c3 - 1) / relvol : 0 rsiN = (rsi1 - 50) / 50 combo = 0.4 * momN - 0.4 * zval + 0.2 * rsiN aCombo = combo > 0 ? combo : -combo fcLvl = aCombo >= comboStrong ? 1 : (aCombo >= comboMid ? 2 : (aCombo >= comboWeak ? 3 : 0)) fcDir = fcLvl > 0 ? (combo > 0 ? 1 : -1) : 0 scoreTD = fcLvl == 1 ? 88 : (fcLvl == 2 ? 74 : 64) // 供自动交易版阈值用,本指标不下单 // 注:v1.4 的「回落空」不走 combo 评分,自动交易版若要接入需单独定阈值 // ══════════ TD 连续计数:无状态展开(替代 var tdBuy / tdSell) ══════════ // v1.1:tdBuy := c1 < c5 ? tdBuy + 1 : 0,只在 ==9 / ==13 触发(不封顶累加)。 // 等价展开:「恰好连续 9 根 / 13 根满足 close[k] < close[k+4]」。 d1 = close[1] < close[5] d2 = close[2] < close[6] d3 = close[3] < close[7] d4 = close[4] < close[8] d5 = close[5] < close[9] d6 = close[6] < close[10] d7 = close[7] < close[11] d8 = close[8] < close[12] d9 = close[9] < close[13] d10 = close[10] < close[14] d11 = close[11] < close[15] d12 = close[12] < close[16] d13 = close[13] < close[17] d14 = close[14] < close[18] u1 = close[1] > close[5] u2 = close[2] > close[6] u3 = close[3] > close[7] u4 = close[4] > close[8] u5 = close[5] > close[9] u6 = close[6] > close[10] u7 = close[7] > close[11] u8 = close[8] > close[12] u9 = close[9] > close[13] u10 = close[10] > close[14] u11 = close[11] > close[15] u12 = close[12] > close[16] u13 = close[13] > close[17] u14 = close[14] > close[18] seq9D = d1 and d2 and d3 and d4 and d5 and d6 and d7 and d8 and d9 and not d10 seq13D = d1 and d2 and d3 and d4 and d5 and d6 and d7 and d8 and d9 and d10 and d11 and d12 and d13 and not d14 seq9U = u1 and u2 and u3 and u4 and u5 and u6 and u7 and u8 and u9 and not u10 seq13U = u1 and u2 and u3 and u4 and u5 and u6 and u7 and u8 and u9 and u10 and u11 and u12 and u13 and not u14 // ★ v1.3:把 9 / 13 拆成四路,绘图时才能写清楚是 TD9 还是 TD13; // seq9* 含 not d10/u10、seq13* 含 d10/u10,二者天然互斥,同一根不会重复标注。 hitB9 = td9 and seq9D hitB13 = td13 and seq13D hitS9 = td9 and seq9U hitS13 = td13 and seq13U hitB = hitB9 or hitB13 hitS = hitS9 or hitS13 // ══════════ 共振窗口:无状态展开(替代 4 个 var sinceXXX) ══════════ // TD 窗口 = 最近 winBars 根内出现过 9/13 触发(触发当根 since=0,共亮 5 根) tdBuyWin = hitB or (winBars > 1 and hitB[1]) or (winBars > 2 and hitB[2]) or (winBars > 3 and hitB[3]) or (winBars > 4 and hitB[4]) tdSellWin = hitS or (winBars > 1 and hitS[1]) or (winBars > 2 and hitS[2]) or (winBars > 3 and hitS[3]) or (winBars > 4 and hitS[4]) // 预测窗口 = 最近 winBars 根内「最后一次非零 fcDir」的方向; // 反向信号会把对方窗口置 99(永久失效直到同向再次出现),与 v1.1 一致。 f0 = fcDir f1 = fcDir[1] f2 = fcDir[2] f3 = fcDir[3] f4 = fcDir[4] z0 = f0 == 0 z1 = f1 == 0 z2 = f2 == 0 z3 = f3 == 0 fcBuyWin = (f0 > 0) or (winBars > 1 and z0 and f1 > 0) or (winBars > 2 and z0 and z1 and f2 > 0) or (winBars > 3 and z0 and z1 and z2 and f3 > 0) or (winBars > 4 and z0 and z1 and z2 and z3 and f4 > 0) fcSellWin = (f0 < 0) or (winBars > 1 and z0 and f1 < 0) or (winBars > 2 and z0 and z1 and f2 < 0) or (winBars > 3 and z0 and z1 and z2 and f3 < 0) or (winBars > 4 and z0 and z1 and z2 and z3 and f4 < 0) // ══════════ 两条路径合成(共振多 / 共振空:判定与 v1.3 逐项一致) ══════════ wantBuy = enableTDCombo and fcBuyWin and tdBuyWin and atrOk wantSell = enableTDCombo and fcSellWin and tdSellWin and atrOk // ★ 修复:改用 reboundCore(不含量能),共振多 / 共振空的出现不再随量能开关变化 // 回落空仍不屏蔽共振信号;反弹多形态出现时,路径②照旧让位 path2Buy = wantBuy and not reboundCore path2Sell = wantSell and not reboundCore // ══════════ 首次出现标记:无状态(替代 var activeDir) ══════════ // v1.1 的 activeDir 在本根开头读到的是【上一根】的 curDir; // 故 freshBuy = 方向由非多转多的那一刻,等价于 curDir[1] != 1。 // ★ 修复:curDir 改用 reboundCore,路径②方向状态机完全不含量能输入, // 共振多 / 共振空的首次标记不再随 minVolRatio / enableVolFilter 漂移 curDir = reboundCore ? 1 : (path2Buy ? 1 : (path2Sell ? -1 : 0)) prevDir = curDir[1] freshBuy = curDir == 1 and not (prevDir == 1) freshSell = curDir == -1 and not (prevDir == -1) // ★ v1.5:趋势线过滤只叠加在最终 tag 上,不进 curDir 方向状态机(与量能过滤同理), // 保证过滤只减少信号,不会因方向状态机重排反而新增信号。 comboLongOk = not enableTrendFilter or not applyTrendToCombo or longTrendOk comboShortOk = not enableTrendFilter or not applyTrendToCombo or shortTrendOk tagBuy2 = freshBuy and path2Buy and comboLongOk tagSell2 = freshSell and path2Sell and comboShortOk // ★ 路径① / ①b 各自独立首次触发,互不依赖 curDir reboundPrev = reboundOk[1] tagRebound = reboundOk and not reboundPrev reboundShortPrev = reboundShortOk[1] tagReboundShort = reboundShortOk and not reboundShortPrev // ══════════ 绘图(所有锚点均为已收盘值) ══════════ plot(mb1, title = '布林中轨(已收盘)', color = '#f0b90b') // —— ★ v1.5 稳定趋势通道(仅绘图,不影响判定)—— plot(showTrendLine and trendValid ? upTrendLine : na, title = '通道上轨(阻力)', color = '#ff6d00', linewidth = 1) plot(showTrendLine and trendValid ? dnTrendLine : na, title = '通道下轨(支撑)', color = '#2962ff', linewidth = 1) plot(showTrendLine and trendValid ? trendMid : na, title = '趋势中枢(回归线)', color = '#7c3aed', linewidth = 2) // —— 路径① 超卖反弹 —— plotText(tagRebound, title = '反弹多', text = '反弹多', color = '#00e676', refSeries = lowC, placement = 'bottom') // —— 路径①b 超买回落做空(★ v1.4 新增)—— plotText(tagReboundShort, title = '回落空', text = '回落空', color = '#ff1744', refSeries = highC, placement = 'top') // —— 路径② 双共振信号(默认打开)—— plotText(tagBuy2, title = '共振多', text = '共振多', color = '#00e676', refSeries = lowC, placement = 'bottom') plotText(tagSell2, title = '共振空', text = '共振空', color = '#ff1744', refSeries = highC, placement = 'top') // —— combo 强度参考:只是强度,不是开单信号;锚点已收盘定格 —— // ★ v1.3:弱档默认不显示(showWeakMarks = false),需要时改成 true 即可恢复; // 注意弱档仍参与 fcDir / 共振判定,这里只关绘图。 plotText(showAuxMarks and showWeakMarks and (fcLvl == 3) and (combo > 0), title = '弱多', text = '弱多', color = '#66bb6a', refSeries = lowC, placement = 'bottom') plotText(showAuxMarks and showWeakMarks and (fcLvl == 3) and (combo < 0), title = '弱空', text = '弱空', color = '#ef9a9a', refSeries = highC, placement = 'top') plotText(showAuxMarks and (fcLvl == 2) and (combo > 0), title = '中多', text = '中多', color = '#26a69a', refSeries = lowC, placement = 'bottom') plotText(showAuxMarks and (fcLvl == 1) and (combo > 0), title = '强多', text = '★多', color = '#69f0ae', refSeries = lowC, placement = 'bottom') plotText(showAuxMarks and (fcLvl == 2) and (combo < 0), title = '中空', text = '中空', color = '#ef5350', refSeries = highC, placement = 'top') plotText(showAuxMarks and (fcLvl == 1) and (combo < 0), title = '强空', text = '★空', color = '#ff8a80', refSeries = highC, placement = 'top') // —— TD 触发点:文字区分写清 TD9 / TD13(不再统一写 'TD')—— plotText(showAuxMarks and showTdMarks and hitB9, title = 'TD9买', text = 'TD9', color = '#26a69a', refSeries = lowC, placement = 'bottom') plotText(showAuxMarks and showTdMarks and hitB13, title = 'TD13买', text = 'TD13', color = '#00e5ff', refSeries = lowC, placement = 'bottom') plotText(showAuxMarks and showTdMarks and hitS9, title = 'TD9卖', text = 'TD9', color = '#ef5350', refSeries = highC, placement = 'top') plotText(showAuxMarks and showTdMarks and hitS13, title = 'TD13卖', text = 'TD13', color = '#ff1744', refSeries = highC, placement = 'top') // ══════════ 预警 ══════════ alertcondition(tagRebound, title = 'GH850-超卖反弹做多', direction = 'buy') alertcondition(tagReboundShort, title = 'GH850-超买回落做空', direction = 'sell') // ★ v1.4 新增 alertcondition(tagBuy2, title = 'GH850-双共振做多', direction = 'buy') alertcondition(tagSell2, title = 'GH850-双共振做空', direction = 'sell')