// Squeeze Momentum Indicator Inputs sources = input(close, 'Source', group = SqzMom) bbLength = input.int(20, 'Bollinger Bands Length', minval = 1, group = SqzMom) bbMult = input.float(2, 'Bollinger Bands MultFactor', step = 0.25, group = SqzMom) kcLength = input(20, 'Keltner\'s Channel Length', group = SqzMom) kcMult = input.float(1.5, 'Keltner\'s Channel MultFactor', step = 0.25, group = SqzMom) useTrueRange = input(true, 'Use TrueRange (Keltner\'s Channel)', group = SqzMom) addSignal = input.bool(false, title = 'add Signal to Calculate direction', group = SqzMom) signalLength = input(5, 'Signal Length', group = SqzMom) tooltip_sqz = 'show momentum direction, BB band and KC band' showBB = input.bool(false, 'show BBand', tooltip = tooltip_sqz, group = SqzMom) showKC = input.bool(false, 'show keltner channel', tooltip = tooltip_sqz, group = SqzMom) showDir= input.bool(false, 'show squeeze direction', tooltip = tooltip_sqz, group = SqzMom)
// Customizable thresholds lowerThreshold = input(-1.0, title = 'Lower Threshold', group = SqzMom) upperThreshold = input(1.0, title = 'Upper Threshold', group = SqzMom)
// SMA Input settings lengthMA = input(200, title="MA Period", group = Sma) showMA = input.bool(true, title="Show MA", group = Sma) addsave = input.bool(true, title = 'add MA as filter', group = Alerts)
// Plot the Supertrend line plot(supertrend, color = direction < 0 ? color.green : color.red, title = 'ST', style = plot.style_stepline_diamond)
// Determine if the trend is up or down upTrend = direction < 0 downTrend = direction > 0
// Track previous trend state var int previousDirection = na previousDirection := upTrend ? 1 : -1
// Calculate ATR for targets and stop loss atrValue = ta.atr(atrPeriod)
// Initialize target and stop loss levels var float entryPrice = na var float targetLevel1 = na var float targetLevel2 = na var float targetLevel3 = na var float stopLossLevel = na
// Initialize counters for lines and labels var int count_up = 0 var int count_down = 0
// Initialize a new variable to track if new lines and labels are drawn var bool newLinesDrawn = false
// Calculate BB basis = ta.sma(sources, bbLength) dev = bbMult * ta.stdev(sources, bbLength) bbUpper = basis + dev bbLower = basis - dev
// Calculate KC ma = ta.sma(sources, kcLength) trRange = useTrueRange ? ta.tr : high - low rangema = ta.sma(trRange, kcLength) kcUpper = ma + rangema * kcMult kcLower = ma - rangema * kcMult
sqzOn = bbLower > kcLower and bbUpper < kcUpper sqzOff = bbLower < kcLower and bbUpper > kcUpper noSqz = sqzOn == false and sqzOff == false
val = ta.linreg(sources - math.avg(math.avg(ta.highest(high, kcLength), ta.lowest(low, kcLength)), ta.sma(sources, kcLength)), kcLength, 0) signal = ta.sma(val, signalLength) dir = not addSignal ? val : val - signal
// Calculate SMA MA = ta.sma(sources,lengthMA) saveEntryBuy = addsave ? MA < close : false saveEntrySell = addsave ? MA > close : false
// Plot SMA plot(showMA ? MA : na, color=color.white, linewidth=2, title='MA')
triangUp = sqzOff and dir > dir[1] and dir >= upperThreshold triangDown = sqzOff and dir < dir[1] and dir <= lowerThreshold triangUpR = sqzOff and dir < dir[1] and dir >= upperThreshold triangDownR = sqzOff and dir > dir[1] and dir <= lowerThreshold insideThreshold = sqzOff and upperThreshold > dir and dir > lowerThreshold
// Calculate target and stop loss levels if upTrend and triangUp entryPrice := close targetLevel1 := close + atrValue * targetMultiplier1 targetLevel2 := close + atrValue * targetMultiplier2 targetLevel3 := close + atrValue * targetMultiplier3 stopLossLevel := close - atrValue * stopLossMultiplier count_up := count_up + 1 count_down := 0 else if downTrend and triangDown entryPrice := close targetLevel1 := close - atrValue * targetMultiplier1 targetLevel2 := close - atrValue * targetMultiplier2 targetLevel3 := close - atrValue * targetMultiplier3 stopLossLevel := close + atrValue * stopLossMultiplier count_down := count_down + 1 count_up := 0
// Draw lines and labels for targets and stop loss var line stopLossLine = na var line entryLine = na var line targetLine1 = na var line targetLine2 = na var line targetLine3 = na var label stopLossLabel = na var label entryLabel = na var label targetLabel1 = na var label targetLabel2 = na var label targetLabel3 = na
// Clear previous lines and labels if a new trend is confirmed if upTrend and triangUp and count_up == 1 // Clear previous lines and labels line.delete(stopLossLine) line.delete(entryLine) line.delete(targetLine1) line.delete(targetLine2) line.delete(targetLine3) label.delete(stopLossLabel) label.delete(entryLabel) label.delete(targetLabel1) label.delete(targetLabel2) label.delete(targetLabel3)
// Draw new lines + 10 bars into the future stopLossLine := line.new(bar_index, stopLossLevel, last_bar_index + 10, stopLossLevel, color = color.red, width = 2) entryLine := line.new(bar_index, close, last_bar_index + 10, close, color = color.green, width = 2) targetLine1 := line.new(bar_index, targetLevel1, last_bar_index + 10, targetLevel1, color = color.blue, width = 2) if saveEntryBuy targetLine2 := line.new(bar_index, targetLevel2, last_bar_index + 10, targetLevel2, color = color.blue, width = 2) targetLine3 := line.new(bar_index, targetLevel3, last_bar_index + 10, targetLevel3, color = color.blue, width = 2)
// Set the newLinesDrawn flag to true newLinesDrawn := true
// Trigger alert when squeeze is released if sqzOn and not sqzOn[1] alert('Squeeze On : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close) if sqzOff and not sqzOff[1] // Only trigger alert if the squeeze was previously on alert('Squeeze Off : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close) if triangDown[1] and not triangDown // Only trigger alert if the squeeze was previously on alert('Weak TrendDown or Reverse : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close) if triangUp[1] and not triangUp // Only trigger alert if the squeeze was previously on alert('Weak TrendUp or Reverse : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close) if insideThreshold and not insideThreshold[1] //trigger when entering threshold channel alert('inside threshold : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close) if triangUpR[1] and triangUp alert('Trend Up Cont : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close) if triangDownR[1] and triangDown alert('Trend Down Cont : '+ syminfo.tickerid + ' | ' + timeframe.period, alert.freq_once_per_bar_close)
// Alert for trend change when new lines and labels are drawn if newLinesDrawn saveEntry = addsave ? 'safe' : 'adrenaline' trendType = upTrend ? 'Buy' : 'Sell' stopLossValue = str.tostring(stopLossLevel, '#.###') entryValue = str.tostring(close, '#.###') targetValue1 = str.tostring(targetLevel1, '#.###') targetValue2 = saveEntryBuy or saveEntrySell ? str.tostring(targetLevel2, '#.###') : '---' targetValue3 = saveEntryBuy or saveEntrySell ? str.tostring(targetLevel3, '#.###') : '---'