//@version=6 indicator("錢來錢來TG V1688", shorttitle="錢來錢來TG1688", dynamic_requests=true, overlay=true, max_bars_back=1000, max_boxes_count=500, max_labels_count=500, max_lines_count=400) if barstate.isfirst and (not timeframe.isminutes or (timeframe.multiplier!=5 and timeframe.multiplier!=15 and timeframe.multiplier!=60 and timeframe.multiplier!=240)) runtime.error("請切換標準5m、15m、1H或4H圖表。") if barstate.isfirst and not chart.is_standard runtime.error("本版本僅支援標準K線。") // Consolidated active settings. Legacy A/B mode is no longer offered in the UI. // --- SMC 常數(逐字取自引擎)--- smcBULLISH_LEG = 1 smcBEARISH_LEG = 0 smcBULLISH = +1 smcBEARISH = -1 smcGREEN = #089981 smcRED = #F23645 smcGRAY = #878b94 smcMONO_BULL = #b2b5be smcMONO_BEAR = #5d606b smcHISTORICAL = '全部歷史' smcPRESENT = '僅最近' smcCOLORED = '彩色' smcMONOCHROME = '單色' smcBOS = 'BOS' smcCHOCH = 'CHoCH' smcMSS = 'MSS' smcTINY = size.tiny smcSMALL = size.small smcNORMAL = size.normal smcATR = 'Atr' smcRANGE = '累積平均波幅' smcCLOSE = '收盤價' smcHIGHLOW = '最高/最低' G159_0 = "01|入場與結構" // Resonance is dashboard-only; it never gates any signal or alert. // Channel drawings are illustrative only; no channel entry gate. bool useInternal = input.bool(true,"內部結構入場",group=G159_0, display=display.none) bool useSwing = input.bool(true,"波段結構入場",group=G159_0, display=display.none) bool showFib = input.bool(true,"僅顯示最新0.5~0.618回踩色塊",group=G159_0, display=display.none) string swingMode = input.string("附件擺動引擎", "轉折模型", options=["影片三K轉折", "附件擺動引擎"], group=G159_0, display=display.none) int internalLength = input.int(5, "內部結構長度", minval=2, maxval=30, group=G159_0, display=display.none) int swingLength = input.int(50, "入場波段結構長度", minval=5, maxval=100, group=G159_0, display=display.none) int pivotBars = input.int(1, "影片模型左右根數", minval=1, maxval=20, group=G159_0, display=display.none, active=swingMode=="影片三K轉折") bool showEvents = input.bool(true, "顯示本圖BOS/CHOCH", group=G159_0, display=display.none) bool showSwings = input.bool(true, "顯示HH/HL/LH/LL", group=G159_0, display=display.none) bool showStrongWeak = input.bool(true, "顯示波段強弱高低點", group=G159_0, display=display.none) float nearbyATR = input.float(6.0, "強弱高低點顯示距離(ATR)", minval=1, step=0.5, group=G159_0, display=display.none) G159_1 = "02|回歸通道" showCurrentChannel = input.bool(false, '顯示當前圖表通道', group=G159_1, display=display.none) len = input.int(100, '通道計算長度', minval=2, maxval=500, group=G159_1, display=display.none) mult = input.float(2.0, '通道寬度倍數', group=G159_1, display=display.none) InpChLabels = input.bool(true, '右緣標籤', group=G159_1, display=display.none) mapChannels = input.bool(false, '啟用多週期通道映射', tooltip='預設僅顯示當前圖表通道。開啟後,勾選下方較高週期;與本圖相同的週期不重複繪製。低於本圖的週期不映射。', group=G159_1, display=display.none) s5 = input.bool(false, '5m 通道', group=G159_1, display=display.none, active=mapChannels) s15 = input.bool(false, '15m 通道', group=G159_1, display=display.none, active=mapChannels) s30 = input.bool(false, '30m 通道', group=G159_1, display=display.none, active=mapChannels) s60 = input.bool(false, '1h 通道', group=G159_1, display=display.none, active=mapChannels) s120 = input.bool(false, '2h 通道', group=G159_1, display=display.none, active=mapChannels) s180 = input.bool(false, '3h 通道', group=G159_1, display=display.none, active=mapChannels) s240 = input.bool(false, '4h 通道', group=G159_1, display=display.none, active=mapChannels) s360 = input.bool(false, '6h 通道', group=G159_1, display=display.none, active=mapChannels) sD = input.bool(false, '1d 通道', group=G159_1, display=display.none, active=mapChannels) G159_2 = "03|OB訂單塊" smcShowOBSwing = input.bool(true, '顯示 OB(swing)', group=G159_2, display=display.none) smcShowOBInternal = input.bool(false, '顯示 OB(internal)', group=G159_2, display=display.none) smcSwingsLengthInput = input.int(50, 'OB波段偵測長度', group=G159_2, minval = 10, maxval = 500, display=display.none) smcSwingOBSize = input.int(5, '波段 OB 數量', group=G159_2, minval = 1, maxval = 20, display=display.none) smcInternalOBSize = input.int(5, '內部 OB 數量', group=G159_2, minval = 1, maxval = 20, display=display.none) smcOBFilterInput = input.string(smcATR, 'OB 過濾方式', group=G159_2, options = [smcATR, smcRANGE], tooltip = '資料量少時建議用「累積平均波幅」', display=display.none) smcOBMitigInput = input.string(smcHIGHLOW, 'OB 失效判定', group=G159_2, options = [smcCLOSE, smcHIGHLOW], display=display.none) smcOBBorderTR = input.int(55, '內部 OB 框線淡度', minval = 0, maxval = 100, group=G159_2, display=display.none) smcSwingBullOBCol = input.color(color.new(#1848cc, 80), '波段 多方 OB', group=G159_2, display=display.none) smcSwingBearOBCol = input.color(color.new(#b22833, 80), '波段 空方 OB', group=G159_2, display=display.none) smcInternalBullOBCol = input.color(color.new(#3179f5, 80), '內部 多方 OB', group=G159_2, display=display.none) smcInternalBearOBCol = input.color(color.new(#f77c80, 80), '內部 空方 OB', group=G159_2, display=display.none) G159_3 = "04|FVG與IFVG" smcShowFVG = input.bool(true, '顯示 FVG', group=G159_3, display=display.none) smcShowIFVG = input.bool(true, '顯示 IFVG(逆向失衡)', group=G159_3, tooltip = 'FVG 被收盤穿破失效後翻轉成反向 S/R 區並換色(多方FVG被跌破→變空方阻力;空方FVG被漲破→變多方支撐),再被穿破才刪。需 FVG 引擎運作。', display=display.none) smcFvgTimeframeInput = input.timeframe('', 'FVG 時間框架(留空=現圖)', group=G159_3, display=display.none) smcFvgThresholdInput = input.bool(true, 'FVG 自動門檻', group=G159_3, tooltip = '過濾掉不顯著的 FVG', display=display.none) smcStyleInput = input.string(smcCOLORED, '配色風格', group=G159_3, options = [smcCOLORED, smcMONOCHROME], display=display.none) smcFvgBullColor = input.color(color.new(#00ff68, 70), '多方 FVG', group=G159_3, display=display.none) smcFvgBearColor = input.color(color.new(#ff0008, 70), '空方 FVG', group=G159_3, display=display.none) smcIfvgBullColor = input.color(color.new(#2962ff, 65), '多方 IFVG(支撐)', group=G159_3, display=display.none) smcIfvgBearColor = input.color(color.new(#ff6d00, 65), '空方 IFVG(阻力)', group=G159_3, display=display.none) G159_4 = "05|儀錶板" bool showBoard = input.bool(true, "顯示三週期趨勢球燈", group=G159_4, display=display.none) boardLocation = input.string("右下", "位置", options=["右下","右上","左下","左上","中下","中上","右中","左中","正中"], group=G159_4, display=display.none) boardScale = input.string("標準", "字體與燈號大小", options=["小","標準","大"], group=G159_4, display=display.none) G159_5 = "06|通知與紀錄" bool enableAlerts = input.bool(true,"啟用觀/B/S/離通知",tooltip="發送所選週期的觀/B/S/觀離/離及風險事件。關閉會停止全部JSON通知;修改後重建快訊。",group=G159_5,display=display.none) string tvWebhookSecret = input.string("", "Webhook 密鑰(至少24字元)", group=G159_5, display=display.none, tooltip="僅填專用Webhook密鑰,不是交易所API金鑰。留空時不發JSON快訊。") string notifyMode = input.string("5m多週期通知", "通知模式", options=["5m多週期通知","本圖通知"], group=G159_5, tooltip="5m圖啟用多週期;15m/1H/4H圖自動使用本圖通知,不停止指標。多週期快訊請在5m建立一份任何alert()快訊。5m於收盤通知;高週期使用上一根已收盤資料,於下一根5m收盤送出(通常延後5分鐘)。") bool notify5 = input.bool(true,"5m",inline="notifyTF",group=G159_5) bool notify15 = input.bool(true,"15m",inline="notifyTF",group=G159_5) bool notify60 = input.bool(true,"1H",inline="notifyTF",group=G159_5) bool notify240 = input.bool(true,"4H",inline="notifyTF",group=G159_5) // Non-5m charts keep their own signals instead of stopping the indicator. bool debugLogs = input.bool(false, "紀錄入場事件", group=G159_5, display=display.none) // Derived from user-supplied Fannn V2.6 SMC: leg, parsed-extreme OB, FVG. // V1.68: independent internal/swing engines; confirmed source snapshots and historical origins. // New: explicit HH/HL bias, bounded source state, MSS displacement, entry sequence. // Research parameters are engineering choices, not an official ICT algorithm. string GB = "01|方向共振" bool useH1 = true bool useH4 = false bool useD1 = false string GP = "02|1H位置與入場" string modelMode = "CHOCH→BOS回踩+反轉K" bool fibMode = modelMode=="CHOCH→BOS回踩+反轉K" bool enableA = modelMode=="A" or modelMode=="A+B" bool enableB = modelMode=="B" or modelMode=="A+B" string poiMode = "OB或FVG" string entryZoneMode = "FVG" string triggerMode = "CHOCH+位移確認" string entryMode = "回踩收回" int contextBars = 24 int fvgWait = 0 int retestBars = 12 int slTicks = 1 string GF = "03|CHOCH→BOS固定回踩" string GM = "03|CHOCH位移過濾" float mssATR = 1.0 float mssShare = 0.6 float mssEdge = 0.25 bool needSweep = false int sweepBars = 12 string GZ = "04|附件OB/FVG核心" bool fvgThreshold = true string obInvalid = "最高/最低" int historyLimit = 500 int zoneLife = 300 string GV = "05|顯示與通知" int nearbyCount = 3 bool showZones = false color bullColor = #089981 color bearColor = #F23645 type Zone float top float bottom int side int kind int born int indexBorn int originTime type State int bias int hiClass int loClass float lastHigh float lastLow int breakSide int breakKind int mssSide int fvgSide float gapTop float gapBottom int closedAt int swingSide float swingPrice int swingAt float brokenLevel int brokenTime int trendSide float trailHigh float trailLow int trailHighTime int trailLowTime Zone obLong1 Zone obLong2 Zone obLong3 Zone obLong4 Zone obLong5 Zone obShort1 Zone obShort2 Zone obShort3 Zone obShort4 Zone obShort5 Zone fvgLong1 Zone fvgLong2 Zone fvgLong3 Zone fvgLong4 Zone fvgLong5 Zone fvgShort1 Zone fvgShort2 Zone fvgShort3 Zone fvgShort4 Zone fvgShort5 f_pick(array zones, int side, int kind, int rank) => Zone result = na int count = 0 int i = array.size(zones) - 1 while i >= 0 Zone z = array.get(zones, i) if z.side == side and z.kind == kind count += 1 if count == rank result := z break i -= 1 result f_engine(bool previous, bool internal, bool buildZones=true) => int n = internal ? internalLength : swingMode == "影片三K轉折" ? pivotBars : swingLength float ph = ta.pivothigh(high, n, n) float pl = ta.pivotlow(low, n, n) // Source smcLeg: alternating extrema versus the following n candles. bool newLegHigh = high[n] > ta.highest(high, n) bool newLegLow = low[n] < ta.lowest(low, n) var int leg = 0 int oldLeg = leg leg := newLegHigh ? 0 : newLegLow ? 1 : leg bool highPivot = not internal and swingMode == "影片三K轉折" ? not na(ph) : leg != oldLeg and leg == 0 bool lowPivot = not internal and swingMode == "影片三K轉折" ? not na(pl) : leg != oldLeg and leg == 1 var float swingHigh = na var float swingLow = na var int highTime = na var int lowTime = na var float trailHigh = na var float trailLow = na var int trailHighTime = na var int trailLowTime = na var int highIndex = na var int lowIndex = na var bool highUsed = false var bool lowUsed = false var int hiClass = 0 var int loClass = 0 var int breakBias = 0 int swingSide = 0 float swingPrice = na int swingAt = na if highPivot float value = high[n] hiClass := na(swingHigh) ? 0 : value > swingHigh ? 1 : value < swingHigh ? -1 : 0 swingHigh := value highIndex := bar_index - n highTime := time[n] trailHigh := value trailHighTime := time[n] highUsed := false swingSide := 1 swingPrice := value swingAt := time[n] if lowPivot float value = low[n] loClass := na(swingLow) ? 0 : value > swingLow ? 1 : value < swingLow ? -1 : 0 swingLow := value lowIndex := bar_index - n lowTime := time[n] trailLow := value trailLowTime := time[n] lowUsed := false swingSide := -1 swingPrice := value swingAt := time[n] if not na(trailHigh) and high >= trailHigh trailHigh := high trailHighTime := time if not na(trailLow) and low <= trailLow trailLow := low trailLowTime := time int bias = hiClass == 1 and loClass == 1 ? 1 : hiClass == -1 and loClass == -1 ? -1 : 0 float vol = ta.atr(200) bool highVol = not na(vol) and high-low >= 2*vol float parsedHigh = highVol ? low : high float parsedLow = highVol ? high : low var array parsedHighs = array.new() var array parsedLows = array.new() var array parsedTimes = array.new() var array zones = array.new() // Bounds are immutable. State snapshots never expose a mutable zone object. int zi = array.size(zones)-1 while buildZones and zi >= 0 Zone z = array.get(zones,zi) float invalidValue = (z.kind == 2 or obInvalid == "最高/最低") ? (z.side == 1 ? low : high) : close bool invalid = z.side == 1 ? invalidValue < z.bottom : invalidValue > z.top if invalid or bar_index-z.indexBorn > zoneLife array.remove(zones,zi) zi -= 1 var int outerLeg = 0 int oldOuter = outerLeg bool outerHigh = high[swingLength] > ta.highest(high,swingLength) bool outerLow = low[swingLength] < ta.lowest(low,swingLength) outerLeg := outerHigh ? 0 : outerLow ? 1 : outerLeg var float outerHighLevel = na var float outerLowLevel = na if outerLeg != oldOuter if outerLeg == 0 outerHighLevel := high[swingLength] else outerLowLevel := low[swingLength] bool crossUp = ta.crossover(close,swingHigh) bool crossDown = ta.crossunder(close,swingLow) int side = crossUp and not highUsed and (not internal or na(outerHighLevel) or swingHigh!=outerHighLevel) ? 1 : crossDown and not lowUsed and (not internal or na(outerLowLevel) or swingLow!=outerLowLevel) ? -1 : 0 float brokenLevel = side == 1 ? swingHigh : side == -1 ? swingLow : na int brokenTime = side == 1 ? highTime : side == -1 ? lowTime : na int kind = side == 0 ? 0 : breakBias == 0 or side == breakBias ? 1 : 2 var int sweptLowAt = na var int sweptHighAt = na bool longSweepOK = not na(sweptLowAt) and bar_index-sweptLowAt <= sweepBars and sweptLowAt < bar_index bool shortSweepOK = not na(sweptHighAt) and bar_index-sweptHighAt <= sweepBars and sweptHighAt < bar_index float priorATR = ta.atr(14)[1] float body = math.abs(close-open) float span = high-low bool displaced = not na(priorATR) and priorATR > 0 and span > 0 and body >= mssATR*priorATR and body/span >= mssShare bool upDisplacement = displaced and close > open and (high-close)/span <= mssEdge bool downDisplacement = displaced and close < open and (close-low)/span <= mssEdge int mss = kind == 2 and (side == 1 ? upDisplacement and (not needSweep or longSweepOK) : downDisplacement and (not needSweep or shortSweepOK)) ? side : 0 if not na(swingLow) and low < swingLow and close > swingLow sweptLowAt := bar_index if not na(swingHigh) and high > swingHigh and close < swingHigh sweptHighAt := bar_index if side != 0 breakBias := side highUsed := side == 1 ? true : highUsed lowUsed := side == -1 ? true : lowUsed int origin = side == 1 ? highIndex : lowIndex int stored = array.size(parsedHighs) int firstStored = bar_index-stored // Same half-open [pivot, break) range as attachment. Skip if truncated. if buildZones and not na(origin) and origin >= firstStored and origin < bar_index int first = origin-firstStored int best = first float extreme = side == 1 ? array.get(parsedLows,first) : array.get(parsedHighs,first) for j = first to stored-1 float value = side == 1 ? array.get(parsedLows,j) : array.get(parsedHighs,j) if side == 1 ? value < extreme : value > extreme extreme := value best := j float a = array.get(parsedHighs,best) float b = array.get(parsedLows,best) float top = math.max(a,b) float bottom = math.min(a,b) bool survives = side == 1 ? (obInvalid == "收盤價" ? close : low) >= bottom : (obInvalid == "收盤價" ? close : high) <= top if top > bottom and survives array.push(zones,Zone.new(top,bottom,side,1,time_close,bar_index,array.get(parsedTimes,best))) if buildZones array.push(parsedHighs,parsedHigh) array.push(parsedLows,parsedLow) array.push(parsedTimes,time) if array.size(parsedHighs)>historyLimit array.shift(parsedHighs) array.shift(parsedLows) array.shift(parsedTimes) // Attachment FVG: outer wick gap + middle candle direction/body threshold. float delta = na(open[1]) or open[1] == 0 ? 0 : (close[1]-open[1])/(open[1]*100) float threshold = fvgThreshold ? ta.cum(math.abs(delta))/math.max(bar_index,1)*2 : 0 bool bullGap = low > high[2] and close[1] > high[2] and delta > threshold bool bearGap = high < low[2] and close[1] < low[2] and -delta > threshold int gapSide = bullGap ? 1 : bearGap ? -1 : 0 float gapTop = bullGap ? low : bearGap ? low[2] : na float gapBottom = bullGap ? high[2] : bearGap ? high : na if buildZones and gapSide != 0 array.push(zones,Zone.new(gapTop,gapBottom,gapSide,2,time_close,bar_index,time[1])) while array.size(zones)>40 array.shift(zones) State now = State.new(bias,hiClass,loClass,swingHigh,swingLow,side,kind,mss,gapSide,gapTop,gapBottom,time_close,swingSide,swingPrice,swingAt,brokenLevel,brokenTime,breakBias,trailHigh,trailLow,trailHighTime,trailLowTime,f_pick(zones,1,1,1),f_pick(zones,1,1,2),f_pick(zones,1,1,3),f_pick(zones,1,1,4),f_pick(zones,1,1,5),f_pick(zones,-1,1,1),f_pick(zones,-1,1,2),f_pick(zones,-1,1,3),f_pick(zones,-1,1,4),f_pick(zones,-1,1,5),f_pick(zones,1,2,1),f_pick(zones,1,2,2),f_pick(zones,1,2,3),f_pick(zones,1,2,4),f_pick(zones,1,2,5),f_pick(zones,-1,2,1),f_pick(zones,-1,2,2),f_pick(zones,-1,2,3),f_pick(zones,-1,2,4),f_pick(zones,-1,2,5)) previous ? now[1] : now // Primitive-only HTF boundary: never return nested State/Zone objects. f_direction() => int n = swingMode=="影片三K轉折" ? pivotBars : swingLength float ph = ta.pivothigh(high,n,n) float pl = ta.pivotlow(low,n,n) bool nh = high[n]>ta.highest(high,n) bool nl = low[n]hh ? 1 : high[n]ll ? 1 : low[n] State st = f_engine(false,true) Zone ob = st.breakSide==1 ? st.obLong1 : st.obShort1 bool fresh = false if not na(ob) fresh := ob.born==time_close float ot = fresh ? ob.top : na float ol = fresh ? ob.bottom : na int os = fresh ? ob.side : 0 int origin = fresh ? ob.originTime : na int gapSide = st.fvgSide float gapTop = st.gapTop float gapBottom = st.gapBottom var int pendingChoch = 0 if st.breakKind==2 pendingChoch := st.breakSide int completedSide = gapSide!=0 and gapSide==pendingChoch ? gapSide : 0 if completedSide!=0 pendingChoch := 0 // Each member refers to the previous completed H1 candle. [time_close[1],bar_index[1],high[1],low[1],close[1],ot[1],ol[1],os[1],origin[1],gapSide[1],gapTop[1],gapBottom[1],time[2],completedSide[1]] int b1 = request.security(syminfo.tickerid,"60",f_direction(),lookahead=barmerge.lookahead_on) int b4 = request.security(syminfo.tickerid,"240",f_direction(),lookahead=barmerge.lookahead_on) int bd = request.security(syminfo.tickerid,"D",f_direction(),lookahead=barmerge.lookahead_on) [ht,hi,hh,hl,hc,ot,ol,os,origin,fs,ft,fb,fo,h1CompletedSide] = request.security(syminfo.tickerid,"60",f_h1Packet(),lookahead=barmerge.lookahead_on) State localState = f_engine(false,true) State localSwing = f_engine(false,false) var array h1Pool = array.new() var int lastH1 = na if not na(ht) and (na(lastH1) or ht!=lastH1) lastH1 := ht int i = array.size(h1Pool)-1 while i>=0 Zone z = array.get(h1Pool,i) float check = z.kind==2 or obInvalid=="最高/最低" ? (z.side==1 ? hl : hh) : hc bool invalid = z.side==1 ? checkz.top if invalid or hi-z.indexBorn>zoneLife array.remove(h1Pool,i) i -= 1 if os!=0 and not na(ot) array.push(h1Pool,Zone.new(ot,ol,os,1,ht,hi,origin)) if fs!=0 and not na(ft) array.push(h1Pool,Zone.new(ft,fb,fs,2,ht,hi,fo)) while array.size(h1Pool)>40 array.shift(h1Pool) bool anySelected = useH1 or useH4 or useD1 int direction = anySelected and (not useH1 or b1==1) and (not useH4 or b4==1) and (not useD1 or bd==1) ? 1 : anySelected and (not useH1 or b1==-1) and (not useH4 or b4==-1) and (not useD1 or bd==-1) ? -1 : 0 f_zones(State s) => array result = array.new() if not na(s) array.push(result,s.obLong1) array.push(result,s.obLong2) array.push(result,s.obLong3) array.push(result,s.obLong4) array.push(result,s.obLong5) array.push(result,s.obShort1) array.push(result,s.obShort2) array.push(result,s.obShort3) array.push(result,s.obShort4) array.push(result,s.obShort5) array.push(result,s.fvgLong1) array.push(result,s.fvgLong2) array.push(result,s.fvgLong3) array.push(result,s.fvgLong4) array.push(result,s.fvgLong5) array.push(result,s.fvgShort1) array.push(result,s.fvgShort2) array.push(result,s.fvgShort3) array.push(result,s.fvgShort4) array.push(result,s.fvgShort5) result array poiZones = array.copy(h1Pool) array displayCandidates = array.new() for side in array.from(1,-1) for kind in array.from(1,2) for rank=1 to 5 Zone z = f_pick(h1Pool,side,kind,rank) if not na(z) array.push(displayCandidates,z) State visualH1 = barstate.isconfirmed ? localState : localState[1] array displayZones = timeframe.in_seconds()==3600 ? f_zones(visualH1) : displayCandidates bool entryTF = timeframe.isminutes and (timeframe.multiplier==5 or timeframe.multiplier==15) var int phaseA = 0 var int setupSideA = 0 var int armedAtA = na var int shiftedAtA = na var int zoneAtA = na var Zone contextZoneA = na var float entryTopA = na var float entryBottomA = na var string lastReasonA = "等待1H位置" var int countDirectionA = 0 var int countTouchA = 0 var int countShiftA = 0 var int countGapA = 0 var int countBuyA = 0 var int countSellA = 0 var int countLostA = 0 var int countExpiredA = 0 var int countBrokenA = 0 var int countChochA = 0 var int countDisplacedA = 0 bool buySignalA = false bool sellSignalA = false float signalSLA = na float signalTPA = na if barstate.isconfirmed and entryTF and enableA if direction!=0 countDirectionA += 1 if localState.breakKind==2 countChochA += 1 if localState.mssSide!=0 countDisplacedA += 1 if phaseA>0 bool lostDirection = direction != setupSideA bool contextBroken = false bool contextStillListed = false if not na(contextZoneA) contextBroken := setupSideA==1 ? closecontextZoneA.top for z in poiZones if not na(z) contextStillListed := contextStillListed or (z.born==contextZoneA.born and z.kind==contextZoneA.kind and z.side==contextZoneA.side and z.top==contextZoneA.top and z.bottom==contextZoneA.bottom) contextBroken := contextBroken or not contextStillListed bool expired = phaseA==1 ? bar_index-armedAtA>contextBars : phaseA==2 ? (fvgWait>0 and bar_index-shiftedAtA>fvgWait) : bar_index-zoneAtA>retestBars bool zoneBroken = phaseA==3 and (setupSideA==1 ? lowentryTopA) if lostDirection or contextBroken or expired or zoneBroken countLostA += lostDirection ? 1 : 0 countExpiredA += expired ? 1 : 0 countBrokenA += contextBroken or zoneBroken ? 1 : 0 phaseA := 0 lastReasonA := lostDirection ? "方向不一致" : expired ? "等待逾時" : "區塊失效" if phaseA==0 and direction!=0 // Newest of currently exposed valid H1 zones; no zone known after bar open. Zone selected = na for z in poiZones if not na(z) bool allowedKind = poiMode=="OB或FVG" or (poiMode=="OB" and z.kind==1) or (poiMode=="FVG" and z.kind==2) bool survivesNow = direction==1 ? close>=z.bottom : close<=z.top if z.side==direction and z.born<=time and allowedKind and survivesNow and low<=z.top and high>=z.bottom if na(selected) selected := z else if z.born>selected.born selected := z if not na(selected) countTouchA += 1 contextZoneA := selected setupSideA := direction armedAtA := bar_index phaseA := 1 lastReasonA := "到達1H位置,等待低週期結構" if phaseA==1 and bar_index>armedAtA bool shiftOK = triggerMode=="CHOCH+位移確認" ? localState.mssSide==setupSideA : localState.breakKind==2 and localState.breakSide==setupSideA if shiftOK countShiftA += 1 shiftedAtA := bar_index phaseA := 2 lastReasonA := "結構確認,等待回踩區形成" if phaseA==2 bool found = false if entryZoneMode=="FVG" and localState.fvgSide==setupSideA // A subsequent same-direction FVG is valid; it need not contain the CHOCH candle. if fvgWait==0 or bar_index-shiftedAtA<=fvgWait entryTopA := localState.gapTop entryBottomA := localState.gapBottom found := true if entryZoneMode=="OB" Zone z = setupSideA==1 ? localState.obLong1 : localState.obShort1 if not na(z) if z.born==time_close entryTopA := z.top entryBottomA := z.bottom found := true if found countGapA += 1 phaseA := 3 zoneAtA := bar_index lastReasonA := "回踩區成立,等待後續K回踩" if phaseA==3 and bar_index>zoneAtA bool touched = low<=entryTopA and high>=entryBottomA bool reclaimed = setupSideA==1 ? close>entryTopA : close0 countBuyA += setupSideA==1 ? 1 : 0 countSellA += setupSideA==-1 ? 1 : 0 buySignalA := setupSideA==1 sellSignalA := setupSideA==-1 signalTPA := close+setupSideA*risk phaseA := 0 lastReasonA := "已確認入場;下一次須重新走完整流程" var int consumedH1B = na var int phaseB = 0 var int setupSideB = 0 var int armedAtB = na var int shiftedAtB = na var int zoneAtB = na var Zone contextZoneB = na var float entryTopB = na var float entryBottomB = na var string lastReasonB = "等待1H位置" var int countDirectionB = 0 var int countTouchB = 0 var int countShiftB = 0 var int countGapB = 0 var int countBuyB = 0 var int countSellB = 0 var int countLostB = 0 var int countExpiredB = 0 var int countBrokenB = 0 var int countChochB = 0 var int countDisplacedB = 0 bool buySignalB = false bool sellSignalB = false float signalSLB = na float signalTPB = na if barstate.isconfirmed and entryTF and enableB if direction!=0 countDirectionB += 1 if localState.breakKind==2 countChochB += 1 if localState.mssSide!=0 countDisplacedB += 1 if phaseB>0 bool lostDirection = direction != setupSideB bool contextBroken = false bool contextStillListed = false if not na(contextZoneB) contextBroken := setupSideB==1 ? closecontextZoneB.top for z in poiZones if not na(z) contextStillListed := contextStillListed or (z.born==contextZoneB.born and z.kind==contextZoneB.kind and z.side==contextZoneB.side and z.top==contextZoneB.top and z.bottom==contextZoneB.bottom) contextBroken := contextBroken or not contextStillListed bool expired = phaseB==1 ? bar_index-armedAtB>contextBars : phaseB==2 ? (fvgWait>0 and bar_index-shiftedAtB>fvgWait) : bar_index-zoneAtB>retestBars bool zoneBroken = phaseB==3 and (setupSideB==1 ? lowentryTopB) if lostDirection or contextBroken or expired or zoneBroken countLostB += lostDirection ? 1 : 0 countExpiredB += expired ? 1 : 0 countBrokenB += contextBroken or zoneBroken ? 1 : 0 phaseB := 0 lastReasonB := lostDirection ? "方向不一致" : expired ? "等待逾時" : "區塊失效" if phaseB==0 and direction!=0 // One attempt per completed H1 CHOCH+FVG event; no H1 touch requirement. Zone selected = na if h1CompletedSide==direction and (na(consumedH1B) or consumedH1B!=ht) for z in poiZones if not na(z) if z.born==ht and z.kind==2 and z.side==direction selected := z break if not na(selected) countTouchB += 1 consumedH1B := ht contextZoneB := selected setupSideB := direction armedAtB := bar_index phaseB := 1 lastReasonB := "1H CHOCH+FVG完成,等待低週期結構" if phaseB==1 and bar_index>armedAtB bool shiftOK = triggerMode=="CHOCH+位移確認" ? localState.mssSide==setupSideB : localState.breakKind==2 and localState.breakSide==setupSideB if shiftOK countShiftB += 1 shiftedAtB := bar_index phaseB := 2 lastReasonB := "結構確認,等待回踩區形成" if phaseB==2 bool found = false if entryZoneMode=="FVG" and localState.fvgSide==setupSideB // A subsequent same-direction FVG is valid; it need not contain the CHOCH candle. if fvgWait==0 or bar_index-shiftedAtB<=fvgWait entryTopB := localState.gapTop entryBottomB := localState.gapBottom found := true if entryZoneMode=="OB" Zone z = setupSideB==1 ? localState.obLong1 : localState.obShort1 if not na(z) if z.born==time_close entryTopB := z.top entryBottomB := z.bottom found := true if found countGapB += 1 phaseB := 3 zoneAtB := bar_index lastReasonB := "回踩區成立,等待後續K回踩" if phaseB==3 and bar_index>zoneAtB bool touched = low<=entryTopB and high>=entryBottomB bool reclaimed = setupSideB==1 ? close>entryTopB : close0 countBuyB += setupSideB==1 ? 1 : 0 countSellB += setupSideB==-1 ? 1 : 0 buySignalB := setupSideB==1 sellSignalB := setupSideB==-1 signalTPB := close+setupSideB*risk phaseB := 0 lastReasonB := "已確認入場;下一次須重新走完整流程" type Setup int side int phase float zero float one int chochBar int bosBar int bosTime int entrySide // V1.69: slope resonance, evaluated independently inside each source timeframe. // Match the existing displayed channel: local endpoints use [1], not a forming bar. htfLRC() => re = ta.linreg(close, len, 0) rs = ta.linreg(close, len, len - 1) rr = ta.correlation(close, bar_index, len) sd = ta.stdev(close, len, false) * math.sqrt(math.max(1.0 - rr * rr, 0.0)) dv = mult * sd // 防重繪:回傳「上一根已收盤 HTF 棒」的值(全部 [1]),端點也錨到已收盤棒。 // → 形成中的 HTF 棒不再讓通道每 tick 跳動;歷史==即時,無殘影。代價:通道晚一根 HTF 更新。 [rs[1], re[1], dv[1], time[len], time[1]] // Evaluate the displayed, previously confirmed channel at this candle's x-coordinate. // Uses time interpolation exactly as drawTF; this also handles session/weekend gaps. f_entryChannelBounds() => [rs,re,dv,ts,te] = htfLRC() float center = not na(ts) and not na(te) and te>ts ? re+(re-rs)*(time-te)/(te-ts) : na [center-dv,center+dv] // Source timeframe -> next level. Evaluated inside each alert's source context too. f_nextTrendTF(simple string tf) => float seconds = timeframe.in_seconds(tf) seconds<300 ? "5" : seconds<900 ? "15" : seconds<3600 ? "60" : seconds<14400 ? "240" : seconds<86400 ? "D" : seconds<604800 ? "W" : seconds tf=="60" ? "1H" : tf=="240" ? "4H" : tf=="D" or tf=="1D" ? "1D" : tf=="W" or tf=="1W" ? "1W" : tf=="M" or tf=="1M" ? "1月" : tf=="3M" ? "3月" : tf=="12M" ? "12月" : tf+"m" // Original setup conditions below are independent of dashboard resonance. // Reversal candle research definition; evaluated in each source timeframe. bool useReversalK = input.bool(true,"CHOCH+BOS 加入反轉K確認",group="反轉K",tooltip="多單:陽K、下影線至少等於實體、收盤在全幅上半部;空單鏡像。只在原入場棒收盤確認,不回填。關閉可回到原入場條件。") f_reversalK(int direction) => float body = math.abs(close-open) float lowerWick = math.min(open,close)-low float upperWick = high-math.max(open,close) float midpoint = (high+low)*0.5 bool bullish = close>open and lowerWick>=body and close>=midpoint bool bearish = close=body and close<=midpoint not useReversalK or (high>low and (direction==1 ? bullish : direction==-1 ? bearish : false)) f_setup(State event, bool enabled, bool confirmed, bool allowLong, bool allowShort) => var int side = 0 var int phase = 0 var float zero = na var float one = na var int chochBar = na var int bosBar = na var int bosTime = na // A lone opposite CHOCH is only a candidate; the active pair stays intact. var int pendingSide = 0 var float pendingZero = na var int pendingBar = na int entry = 0 if confirmed and enabled if event.breakKind==2 pendingSide := event.breakSide pendingZero := event.brokenLevel pendingBar := bar_index if phase==0 phase := 1 if debugLogs log.info("候選CHOCH:方向={0},水平={1};既有完整組保留",pendingSide,pendingZero) else if pendingSide!=0 and event.breakKind==1 and event.breakSide==pendingSide and bar_index>pendingBar // Only a complete new pair replaces the old one. Process before touch evaluation. side := pendingSide zero := pendingZero one := event.brokenLevel chochBar := pendingBar bosBar := bar_index bosTime := time phase := (one-zero)*side>0 ? 2 : 4 pendingSide := 0 pendingZero := na pendingBar := na if debugLogs log.info("完整CHOCH+BOS替換:方向={0},0={1},1={2},階段={3}",side,zero,one,phase) if phase==2 and bar_index>bosBar float p50 = zero+(one-zero)*0.5 float p618 = zero+(one-zero)*0.618 float top = math.max(p50,p618) float bottom = math.min(p50,p618) // Close-confirmed entry only; no assumed limit fill at the wick. // Same-bar rejection: short closes below the band but above BOS; long mirrors it. // A rejected touch does not consume the pair; wait for a later valid retest. bool touched = low<=top and high>=bottom bool closeValid = side==-1 ? close>one : side==1 ? closetop : side==-1 ? close s.phase==0 ? "等待CHOCH" : s.phase==1 ? "等待同向BOS" : s.phase==2 ? "等待回踩收回+BOS位置+反轉K確認" : s.phase==3 ? "已標記,等新完整組" : "區間無效,等新完整組" // QQE delayed release: each call site/timeframe owns its own state. string G_QDELAY = "QQE 動能放行(各圖獨立)" string qDelayMode = input.string("同向零軸+線外", "QQE 放行模式", options=["關閉(原始訊號)", "同向零軸+線外"], group=G_QDELAY) bool qShowObservation = input.bool(true, "顯示本圖觀察標籤", group=G_QDELAY, tooltip="只控制本圖顯示,不關閉多週期JSON事件。當根原訊號成立但QQE未符合,標記觀;觀察不建立SL/TP、不因價格觸及舊價位取消。持續等待同向陽/陰K且QQE符合確認。確認或取代候選均保留原觀察標籤;歷史顯示受平台標籤上限限制。") int qDelayLength = input.int(6, "Secondary RSI Length", minval=2, group=G_QDELAY) int qDelaySmooth = input.int(5, "Secondary RSI Smoothing", minval=1, group=G_QDELAY) float qDelayFactor = input.float(1.61, "Secondary QQE Factor", minval=0.01, step=0.01, group=G_QDELAY, tooltip="柱體/藍線比較只使用Secondary QQE。Primary與BB彩柱門檻不參與這個模式。") f_qDelayValues() => float oscillator = ta.ema(ta.rsi(close, qDelayLength), qDelaySmooth) int volatilityLength = 2*qDelayLength-1 float width = ta.ema(ta.ema(math.abs(ta.change(oscillator)),volatilityLength),volatilityLength)*qDelayFactor float lowerCandidate = oscillator-width float upperCandidate = oscillator+width var float floorBand = na var float ceilingBand = na floorBand := oscillator[1]>floorBand[1] and oscillator>floorBand[1] ? math.max(floorBand[1],lowerCandidate) : lowerCandidate ceilingBand := oscillator[1] [histogram,trail] = f_qDelayValues() var int pendingSide = 0 var int pendingBar = na var int pendingBos = na var int pendingOwner = 0 var float pendingTarget = na float releasedTarget = na int releasedOwner = 0 var string status = "等待原始反轉K訊號" int released = 0 int observation = 0 if confirmed if qDelayMode=="關閉(原始訊號)" pendingSide := 0 released := rawSide releasedOwner := owner releasedTarget := rawSide!=0 ? selected.one : na status := "QQE關閉|原始訊號" else if rawSide!=0 pendingSide := rawSide pendingBar := bar_index pendingBos := selected.bosBar pendingOwner := owner pendingTarget := selected.one status := rawSide==1 ? "B候選|等待QQE" : "S候選|等待QQE" if pendingSide!=0 bool ready = not na(histogram) and not na(trail) bool relative = pendingSide*(histogram-trail)>0 bool allowed = ready and relative and pendingSide*histogram>0 if allowed and (bar_index==pendingBar or pendingSide*(close-open)>0) released := pendingSide releasedOwner := pendingOwner releasedTarget := pendingTarget status := (released==1 ? "B" : "S")+"放行|等待"+str.tostring(bar_index-pendingBar)+"根K" if debugLogs log.info("QQE{0};柱體={1},藍線={2},確認價={3}",status,histogram,trail,close) pendingSide := 0 else observation := bar_index==pendingBar ? pendingSide : 0 status := pendingSide==1 ? "觀多|持續等待同向K+QQE" : "觀空|持續等待同向K+QQE" [released,pendingSide,status,observation,histogram,trail,releasedTarget,releasedOwner] // Structure lamps are manual context, not an added resonance gate. bool entryAllowLong = true bool entryAllowShort = true Setup internalSetup = f_setup(localState,fibMode and useInternal,barstate.isconfirmed,entryAllowLong,entryAllowShort) Setup swingSetup = f_setup(localSwing,fibMode and useSwing,barstate.isconfirmed,entryAllowLong,entryAllowShort) // One owner for signals, alerts and the displayed retracement box. // A candidate CHOCH does not change bosBar, so it cannot replace the owner. // New completed pairs replace old pairs before evaluating this bar's signal. // Same-bar completions deterministically prefer the internal pair. bool hasInternalPair = useInternal and not na(internalSetup.bosBar) bool hasSwingPair = useSwing and not na(swingSetup.bosBar) bool latestIsInternal = hasInternalPair and (not hasSwingPair or internalSetup.bosBar>=swingSetup.bosBar) bool latestIsSwing = hasSwingPair and not latestIsInternal int rawSelectedEntry = latestIsInternal ? internalSetup.entrySide : latestIsSwing ? swingSetup.entrySide : 0 Setup qSelectedSetup = latestIsInternal ? internalSetup : swingSetup [selectedEntry,qPendingSide,qReleaseStatus,qObservationSide,qExitHistogram,qExitTrail,qReferenceTarget,qReleasedOwner] = f_qDelayRelease(qSelectedSetup,rawSelectedEntry,latestIsInternal ? 1 : 2,barstate.isconfirmed) // Stable source identity: raw B/S -> observation -> QQE confirmation. var string qSourceId = "" if barstate.isconfirmed and rawSelectedEntry!=0 qSourceId := syminfo.tickerid+":"+timeframe.period+":"+str.tostring(time_close)+":"+str.tostring(rawSelectedEntry) var array