script("Band-Channel Trends - Ms Brown folder", kind="indicator") input sourceChoice = 0 input filterLength = 12 input bandLength = 20 input bandMultiplier = 1.9 input atrLength = 14 input atrFloor = 0.75 input channelLength = 22 input residenceLength = 12 input structureLength = 5 input confirmBars = 2 input minTick = 0.01 input showDefense = 1 input showBands = 1 input showChannel = 1 input showSignals = 1 input colorBars = 1 input longColor = "#39FF14" input shortColor = "#8A2BE2" filterPeriod = math.clamp(math.floor(filterLength), 3, 500) bandPeriod = math.clamp(math.floor(bandLength), 2, 500) atrPeriod = math.clamp(math.floor(atrLength), 1, 500) channelPeriod = math.clamp(math.floor(channelLength), 2, 500) residencePeriod = math.clamp(math.floor(residenceLength), 2, 500) structurePeriod = math.clamp(math.floor(structureLength), 2, 500) confirmation = math.clamp(math.floor(confirmBars), 1, 4) tick = math.max(minTick, 0.000000000001) source = sourceChoice == 1 ? close : sourceChoice == 2 ? open : sourceChoice == 3 ? high : sourceChoice == 4 ? low : sourceChoice == 5 ? (high + low) / 2 : sourceChoice == 6 ? (open + high + low + close) / 4 : sourceChoice == 7 ? (high + low + close + close) / 4 : sourceChoice == 8 ? volume : (high + low + close) / 3 a1 = math.exp(-1.414 * 3.141592653589793 / filterPeriod) b1 = 2 * a1 * math.cos(1.414 * 3.141592653589793 / filterPeriod) c2 = b1 c3 = -a1 * a1 c1 = 1 - c2 - c3 center = na(center[1]) or na(center[2]) ? source : c1 * (source + source[1]) * 0.5 + c2 * center[1] + c3 * center[2] // Pine ta.atr seeds Wilder RMA with an SMA, unlike the legacy StScript ta.atr seed. trueRange = ta.tr() atrSeed = ta.sma(trueRange, atrPeriod) atrValue = na(atrValue[1]) ? atrSeed : (atrValue[1] * (atrPeriod - 1) + trueRange) / atrPeriod residual = source - center residualSquared = residual * residual meanSquare = ta.sma(residualSquared, bandPeriod) rms = na(meanSquare) ? na : math.sqrt(meanSquare) bandDistance = na(rms) or na(atrValue) ? na : math.max(rms * math.max(0.1, bandMultiplier), atrValue * math.max(0.1, atrFloor)) upperBand = na(bandDistance) ? na : center + bandDistance lowerBand = na(bandDistance) ? na : center - bandDistance // Pine highest/lowest ignore unavailable early history. Exclude the current candle. var channelHighValue = na var channelLowValue = na var recentHighValue = na var recentLowValue = na lookback = math.max(channelPeriod, structurePeriod) for j in 1..lookback { if j <= barIndex { if j <= channelPeriod { channelHighValue = na(channelHighValue) ? high[j] : math.max(channelHighValue, high[j]) channelLowValue = na(channelLowValue) ? low[j] : math.min(channelLowValue, low[j]) } if j <= structurePeriod { recentHighValue = na(recentHighValue) ? high[j] : math.max(recentHighValue, high[j]) recentLowValue = na(recentLowValue) ? low[j] : math.min(recentLowValue, low[j]) } } } channelHigh = channelHighValue channelLow = channelLowValue recentHigh = recentHighValue recentLow = recentLowValue bullishClose = close > center ? 1 : 0 bullishResidence = ta.sma(bullishClose, residencePeriod) bearishResidence = na(bullishResidence) ? na : 1 - bullishResidence longWeight = na(bullishResidence) ? na : 0.15 + 0.60 * bullishResidence shortWeight = na(bearishResidence) ? na : 0.15 + 0.60 * bearishResidence longBoundary = lowerBand * (1 - longWeight) + channelLow * longWeight shortBoundary = upperBand * (1 - shortWeight) + channelHigh * shortWeight ready = not na(bandDistance) and not na(channelHigh) and not na(channelLow) and not na(recentHigh) and not na(recentLow) and not na(bullishResidence) and not na(atrValue) trend = nz(trend[1], 0) defense = nz(defense[1], na) breachCount = nz(breachCount[1], 0) longSignal = 0 shortSignal = 0 if isClosed and ready { if trend == 0 { trend = close >= center ? 1 : -1 breachCount = 0 defense = trend == 1 ? math.min(longBoundary, close - atrValue * 0.75) : math.max(shortBoundary, close + atrValue * 0.75) } else { if trend == 1 { breached = close < defense breachCount = breached ? breachCount + 1 : 0 decisiveBreak = breached and close < recentLow confirmedBreak = breached and breachCount >= confirmation if decisiveBreak or confirmedBreak { trend = -1 defense = math.max(shortBoundary, close + atrValue * 0.75) breachCount = 0 shortSignal = 1 } else { if not breached { defense = math.max(defense, math.min(longBoundary, close - tick)) } } } else { breached = close > defense breachCount = breached ? breachCount + 1 : 0 decisiveBreak = breached and close > recentHigh confirmedBreak = breached and breachCount >= confirmation if decisiveBreak or confirmedBreak { trend = 1 defense = math.min(longBoundary, close - atrValue * 0.75) breachCount = 0 longSignal = 1 } else { if not breached { defense = math.min(defense, math.max(shortBoundary, close + tick)) } } } } } longDefense = showDefense == 1 and trend == 1 ? defense : na shortDefense = showDefense == 1 and trend == -1 ? defense : na visibleUpper = showBands == 1 ? upperBand : na visibleLower = showBands == 1 ? lowerBand : na visibleChannelHigh = showChannel == 1 ? channelHigh : na visibleChannelLow = showChannel == 1 ? channelLow : na plot(longDefense, title="LONG Defense", color=longColor, width=3) plot(shortDefense, title="SHORT Defense", color=shortColor, width=3) plot(visibleUpper, title="Upper Band", color=shortColor, width=1, opacity=0.22) plot(visibleLower, title="Lower Band", color=longColor, width=1, opacity=0.22) fill(visibleUpper, visibleLower, color="#808080", opacity=0.06) plot(visibleChannelHigh, title="Channel High", color=shortColor, width=1, opacity=0.17) plot(visibleChannelLow, title="Channel Low", color=longColor, width=1, opacity=0.17) plotShape(low, when=showSignals == 1 and longSignal == 1, shape="triangleUp", position="below", color=longColor, size=6) plotShape(high, when=showSignals == 1 and shortSignal == 1, shape="triangleDown", position="above", color=shortColor, size=6) plotText(low, when=showSignals == 1 and longSignal == 1, text="LONG", position="below", color=longColor, fontSize=10) plotText(high, when=showSignals == 1 and shortSignal == 1, text="SHORT", position="above", color=shortColor, fontSize=10) barcolor(colorBars == 1 and trend == 1, color=longColor) barcolor(colorBars == 1 and trend == -1, color=shortColor) alertcondition(longSignal, title="LONG Signal", message="SchizoQuant Band-Channel Persistence Trend: LONG") alertcondition(shortSignal, title="SHORT Signal", message="SchizoQuant Band-Channel Persistence Trend: SHORT")