script("Smart Money Structure Suite - independent", kind="indicator") input preset = 0 input swingLength = 4 input confirmedOnly = 1 input maxZones = 24 input projectionBars = 16 input projectionMode = 0 input obSearchDepth = 20 input requireImpulseOB = 0 input mitigationBasis = 0 input keepMitigated = 0 input shrinkGaps = 1 input mergeGaps = 1 input mergeOverlap = 0.5 input tightGaps = 0 input gapSide = 0 input interimReset = 0 input interimLookback = 20 input interimBody = 0 input interimRunnerUp = 0 input impulseBodyOnly = 0 input interimBeyondOnly = 1 input impulseLength = 20 input impulseMultiplier = 1.8 input impulseBodyRatio = 0.5 input showImpulse = 1 input colorPreset = 0 input bullColor = "#76F7B8" input bearColor = "#FF3D1A" input gapColor = "#9C7CF4" input highColor = "#DCA53B" input lowColor = "#47C4D9" input zoneOpacity = 0.16 input labelSize = 10 bullPaint = colorPreset == 1 ? "#76F7B8" : colorPreset == 2 ? "#00D5FF" : colorPreset == 3 ? "#B47CFF" : colorPreset == 4 ? "#00FFCC" : colorPreset == 5 ? "#C5FF00" : bullColor bearPaint = colorPreset == 1 ? "#FF3D1A" : colorPreset == 2 ? "#FF4D8D" : colorPreset == 3 ? "#FF914D" : colorPreset == 4 ? "#FF00AA" : colorPreset == 5 ? "#FF3344" : bearColor leftBars = preset == 1 ? 2 : preset == 2 ? 6 : math.clamp(math.floor(swingLength), 1, 50) zoneLimit = math.clamp(math.floor(maxZones), 2, 48) forwardBars = math.clamp(math.floor(projectionBars), 1, 100) searchDepth = math.clamp(math.floor(obSearchDepth), 1, 100) shrink = preset == 1 ? 1 : preset == 2 ? 0 : shrinkGaps basis = preset == 1 ? 0 : preset == 2 ? 1 : mitigationBasis threshold = preset == 1 ? 1.3 : preset == 2 ? 2.2 : impulseMultiplier pivotHigh = ta.pivothigh(high, leftBars, leftBars) pivotLow = ta.pivotlow(low, leftBars, leftBars) bodyHigh = math.max(open, close) bodyLow = math.min(open, close) candleRange = high - low candleBody = math.abs(close - open) impulseMeasure = impulseBodyOnly == 1 ? candleBody : candleRange averageRange = ta.sma(impulseMeasure, math.clamp(math.floor(impulseLength), 1, 200)) impulse = not na(averageRange) and impulseMeasure > averageRange * threshold and candleBody >= candleRange * impulseBodyRatio confirmed = confirmedOnly == 0 or isClosed swingHigh = nz(swingHigh[1], na) swingLow = nz(swingLow[1], na) highAt = nz(highAt[1], 0) lowAt = nz(lowAt[1], 0) highBroken = nz(highBroken[1], 0) lowBroken = nz(lowBroken[1], 0) structure = nz(structure[1], 0) projectHighTaken = nz(projectHighTaken[1], 0) projectLowTaken = nz(projectLowTaken[1], 0) var bullBreak = 0 var bearBreak = 0 var bullChange = 0 var bearChange = 0 var fvgFormed = 0 var fvgTouched = 0 var fvgFilled = 0 var obFormed = 0 var obTouched = 0 var obFilled = 0 if confirmed { if not na(pivotHigh) { draw.label("H" + barIndex, barIndex - leftBars, pivotHigh, na(swingHigh) or pivotHigh >= swingHigh ? "HH" : "LH", color=na(swingHigh) or pivotHigh >= swingHigh ? bullPaint : bearPaint, fontSize=labelSize) swingHigh = pivotHigh highAt = barIndex - leftBars highBroken = 0 projectHighTaken = 0 } if not na(pivotLow) { draw.label("L" + barIndex, barIndex - leftBars, pivotLow, na(swingLow) or pivotLow < swingLow ? "LL" : "HL", color=na(swingLow) or pivotLow < swingLow ? bearPaint : bullPaint, fontSize=labelSize, position="below") swingLow = pivotLow lowAt = barIndex - leftBars lowBroken = 0 projectLowTaken = 0 } if not na(swingHigh) and highBroken == 0 and close > swingHigh { bullBreak = 1 bullChange = structure == -1 ? 1 : 0 draw.line("B" + barIndex, highAt, swingHigh, barIndex, swingHigh, color=bullPaint, style="dashed", text=bullChange == 1 ? "CHoCH" : "BOS", fontSize=labelSize) structure = 1 highBroken = 1 } else { if not na(swingLow) and lowBroken == 0 and close < swingLow { bearBreak = 1 bearChange = structure == 1 ? 1 : 0 draw.line("B" + barIndex, lowAt, swingLow, barIndex, swingLow, color=bearPaint, style="dashed", text=bearChange == 1 ? "CHoCH" : "BOS", fontSize=labelSize, position="below") structure = -1 lowBroken = 1 } } var zoneCount = array.size("ids") for zoneStep in 0..zoneCount - 1 { var slot = zoneCount - 1 - zoneStep var zoneTop = array.get("tops", slot) var zoneBottom = array.get("bottoms", slot) var zoneLeft = array.get("lefts", slot) var zoneDirection = array.get("directions", slot) var zoneKind = array.get("kinds", slot) var zoneId = array.get("ids", slot) var touched = array.get("touched", slot) var hit = zoneDirection == 1 ? low <= zoneTop and high >= zoneBottom : high >= zoneBottom and low <= zoneTop var filled = zoneDirection == 1 ? (basis == 1 ? close <= zoneBottom : low <= zoneBottom) : (basis == 1 ? close >= zoneTop : high >= zoneTop) if hit and touched == 0 { array.set("touched", slot, 1) if zoneKind == 1 { fvgTouched = 1 } else { obTouched = 1 } } if filled { if zoneKind == 1 { fvgFilled = 1 } else { obFilled = 1 } if keepMitigated == 1 { draw.box(zoneId, zoneLeft, zoneTop, barIndex, zoneBottom, color=zoneKind == 1 ? gapColor : zoneDirection == 1 ? bullPaint : bearPaint, opacity=zoneOpacity, text=zoneKind == 1 ? "FVG filled" : "OB filled", midline=zoneKind == 2, fontSize=labelSize) } else { draw.remove(zoneId) } array.remove("ids", slot) array.remove("tops", slot) array.remove("bottoms", slot) array.remove("lefts", slot) array.remove("directions", slot) array.remove("kinds", slot) array.remove("touched", slot) } else { if zoneKind == 1 and shrink == 1 and hit { if zoneDirection == 1 { zoneTop = math.min(zoneTop, basis == 1 ? close : low) } else { zoneBottom = math.max(zoneBottom, basis == 1 ? close : high) } array.set("tops", slot, zoneTop) array.set("bottoms", slot, zoneBottom) } var visibleZone = zoneKind == 2 or gapSide == 0 or (gapSide == 1 and zoneBottom > close) or (gapSide == -1 and zoneTop < close) if visibleZone { draw.box(zoneId, zoneLeft, zoneTop, projectionMode == 1 ? zoneLeft + forwardBars : barIndex + forwardBars, zoneBottom, color=zoneKind == 1 ? gapColor : zoneDirection == 1 ? bullPaint : bearPaint, opacity=zoneOpacity, extend=projectionMode == 2 ? "right" : "none", text=zoneKind == 1 ? "FVG" : "OB", midline=zoneKind == 2, fontSize=labelSize) } else { draw.remove(zoneId) } } } var newDirection = 0 var newTop = na var newBottom = na var newLeft = barIndex var newKind = 1 if barIndex >= 2 { if low > high[2] { newDirection = 1 newTop = low newBottom = high[2] newLeft = barIndex - 2 } if high < low[2] { newDirection = -1 newTop = low[2] newBottom = high newLeft = barIndex - 2 } } if newDirection != 0 { fvgFormed = 1 if preset == 1 or (preset == 0 and tightGaps == 1) { var middle = (newTop + newBottom) / 2 var halfSize = math.min((newTop - newBottom) / 2, nz(averageRange, candleRange) / 2) newTop = middle + halfSize newBottom = middle - halfSize } var merged = 0 if mergeGaps == 1 { for mergeSlot in 0..array.size("ids") - 1 { var oldTop = array.get("tops", mergeSlot) var oldBottom = array.get("bottoms", mergeSlot) var overlap = math.min(newTop, oldTop) - math.max(newBottom, oldBottom) var overlapSize = math.min(newTop - newBottom, oldTop - oldBottom) if merged == 0 and array.get("kinds", mergeSlot) == 1 and array.get("directions", mergeSlot) == newDirection and overlap > 0 and overlap >= overlapSize * math.clamp(mergeOverlap, 0, 1) { array.set("tops", mergeSlot, math.max(newTop, oldTop)) array.set("bottoms", mergeSlot, math.min(newBottom, oldBottom)) array.set("touched", mergeSlot, 0) var mergedLeft = array.get("lefts", mergeSlot) if gapSide == 0 or (gapSide == 1 and math.min(newBottom, oldBottom) > close) or (gapSide == -1 and math.max(newTop, oldTop) < close) { draw.box(array.get("ids", mergeSlot), mergedLeft, math.max(newTop, oldTop), projectionMode == 1 ? mergedLeft + forwardBars : barIndex + forwardBars, math.min(newBottom, oldBottom), color=gapColor, opacity=zoneOpacity, extend=projectionMode == 2 ? "right" : "none", text="FVG", fontSize=labelSize) } else { draw.remove(array.get("ids", mergeSlot)) } merged = 1 } } } if merged == 0 { var newId = "F" + barIndex array.push("ids", newId) array.push("tops", newTop) array.push("bottoms", newBottom) array.push("lefts", newLeft) array.push("directions", newDirection) array.push("kinds", newKind) array.push("touched", 0) if gapSide == 0 or (gapSide == 1 and newBottom > close) or (gapSide == -1 and newTop < close) { draw.box(newId, newLeft, newTop, projectionMode == 1 ? newLeft + forwardBars : barIndex + forwardBars, newBottom, color=gapColor, opacity=zoneOpacity, extend=projectionMode == 2 ? "right" : "none", text="FVG", fontSize=labelSize) } } } if (bullBreak == 1 or bearBreak == 1) and (requireImpulseOB == 0 or impulse) { var found = 0 for scan in 1..searchDepth { if found == 0 and scan <= barIndex and ((bullBreak == 1 and open[scan] > close[scan]) or (bearBreak == 1 and open[scan] < close[scan])) { found = 1 newDirection = bullBreak == 1 ? 1 : -1 newTop = high[scan] newBottom = low[scan] newLeft = barIndex - scan newKind = 2 var blockId = "O" + barIndex var newId = blockId array.push("ids", newId) array.push("tops", newTop) array.push("bottoms", newBottom) array.push("lefts", newLeft) array.push("directions", newDirection) array.push("kinds", newKind) array.push("touched", 0) draw.box(blockId, newLeft, newTop, projectionMode == 1 ? newLeft + forwardBars : barIndex + forwardBars, newBottom, color=newDirection == 1 ? bullPaint : bearPaint, opacity=zoneOpacity, extend=projectionMode == 2 ? "right" : "none", text="OB", midline=1, fontSize=labelSize) obFormed = 1 } } } for trimZone in 0..array.size("ids") - zoneLimit - 1 { draw.remove(array.get("ids", 0)) array.remove("ids", 0) array.remove("tops", 0) array.remove("bottoms", 0) array.remove("lefts", 0) array.remove("directions", 0) array.remove("kinds", 0) array.remove("touched", 0) } } if not na(swingHigh) and high >= swingHigh { projectHighTaken = 1 } if not na(swingLow) and low <= swingLow { projectLowTaken = 1 } sourceHigh = interimBody == 1 ? bodyHigh : high sourceLow = interimBody == 1 ? bodyLow : low resetWicks = interimReset == 0 ? not na(pivotHigh) or not na(pivotLow) : bullBreak == 1 or bearBreak == 1 interimHigh = nz(interimHigh[1], sourceHigh) interimLow = nz(interimLow[1], sourceLow) secondHigh = nz(secondHigh[1], sourceHigh) secondLow = nz(secondLow[1], sourceLow) if confirmed { if resetWicks { interimHigh = sourceHigh interimLow = sourceLow secondHigh = sourceHigh secondLow = sourceLow } else { if sourceHigh > interimHigh { secondHigh = interimHigh interimHigh = sourceHigh } else { if sourceHigh < interimHigh { secondHigh = math.max(secondHigh, sourceHigh) } } if sourceLow < interimLow { secondLow = interimLow interimLow = sourceLow } else { if sourceLow > interimLow { secondLow = math.min(secondLow, sourceLow) } } } if interimReset == 2 { var topOne = na var topTwo = na var lowOne = na var lowTwo = na for probe in 0..math.clamp(math.floor(interimLookback), 2, 100) - 1 { if probe <= barIndex { var probeHigh = sourceHigh[probe] var probeLow = sourceLow[probe] if na(topOne) or probeHigh > topOne { topTwo = topOne topOne = probeHigh } else { if na(topTwo) or probeHigh > topTwo { topTwo = probeHigh } } if na(lowOne) or probeLow < lowOne { lowTwo = lowOne lowOne = probeLow } else { if na(lowTwo) or probeLow < lowTwo { lowTwo = probeLow } } } } interimHigh = topOne secondHigh = nz(topTwo, topOne) interimLow = lowOne secondLow = nz(lowTwo, lowOne) } } wickHighTaken = confirmed and not na(interimHigh[1]) and sourceHigh > interimHigh[1] wickLowTaken = confirmed and not na(interimLow[1]) and sourceLow < interimLow[1] if not isClosed { if not na(swingHigh) and highBroken == 0 and projectHighTaken == 0 { draw.line("nextHigh", highAt, swingHigh, barIndex + forwardBars, swingHigh, color=bullPaint, style="dotted", text=structure == -1 ? "Next Bull CHoCH" : "Next Bull BOS", fontSize=labelSize) } else { draw.remove("nextHigh") } if not na(swingLow) and lowBroken == 0 and projectLowTaken == 0 { draw.line("nextLow", lowAt, swingLow, barIndex + forwardBars, swingLow, color=bearPaint, style="dotted", text=structure == 1 ? "Next Bear CHoCH" : "Next Bear BOS", fontSize=labelSize, position="below") } else { draw.remove("nextLow") } var wickHigh = interimRunnerUp == 1 ? secondHigh : interimHigh var wickLow = interimRunnerUp == 1 ? secondLow : interimLow if not na(swingHigh) and (interimBeyondOnly == 0 or wickHigh > swingHigh) { draw.line("wickHigh", barIndex - 1, wickHigh, barIndex + forwardBars, wickHigh, color=highColor, style="dotted", text="Interim Wick High", fontSize=labelSize) } if not na(swingLow) and (interimBeyondOnly == 0 or wickLow < swingLow) { draw.line("wickLow", barIndex - 1, wickLow, barIndex + forwardBars, wickLow, color=lowColor, style="dotted", text="Interim Wick Low", fontSize=labelSize, position="below") } } barcolor(confirmed and showImpulse == 1 and impulse and close > open, color=bullPaint) barcolor(confirmed and showImpulse == 1 and impulse and close < open, color=bearPaint) alertcondition(bullBreak == 1 and bullChange == 0, title="Bull BOS") alertcondition(bearBreak == 1 and bearChange == 0, title="Bear BOS") alertcondition(bullChange == 1, title="Bull CHoCH") alertcondition(bearChange == 1, title="Bear CHoCH") alertcondition(bullBreak == 1 or bearBreak == 1, title="Structure break") alertcondition(fvgFormed == 1, title="FVG formed") alertcondition(fvgTouched == 1, title="FVG first touch") alertcondition(fvgFilled == 1, title="FVG filled") alertcondition(obFormed == 1, title="OB formed") alertcondition(obTouched == 1, title="OB first touch") alertcondition(obFilled == 1, title="OB filled") alertcondition(wickHighTaken, title="Interim high taken") alertcondition(wickLowTaken, title="Interim low taken") alertcondition(confirmed and impulse and close > open, title="Bull impulse") alertcondition(confirmed and impulse and close < open, title="Bear impulse")