Squeeze and Stoch

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

//@version=4

study("SQZMOM + Stochastic [LazyBear]", shorttitle="SQZMOM_STOCH", overlay=false)

// Squeeze Momentum Configuration


lengthBB = input(20, title="BB Length")
multBB = input(2.0, title="BB MultFactor")
lengthKC = input(20, title="KC Length")
multKC = input(1.5, title="KC MultFactor")
useTrueRange = input(true, title="Use TrueRange (KC)")

// Bollinger Bands (BB) Calculation


source = close
basis = sma(source, lengthBB)
dev = multBB * stdev(source, lengthBB)
upperBB = basis + dev
lowerBB = basis - dev

// Keltner Channels (KC) Calculation


ma = sma(source, lengthKC)
trValue = useTrueRange ? tr : (high - low)
rangeMa = sma(trValue, lengthKC)
upperKC = ma + rangeMa * multKC
lowerKC = ma - rangeMa * multKC

// Squeeze Conditions
sqzOn = (lowerBB > lowerKC) and (upperBB < upperKC) // Squeeze is on
sqzOff = (lowerBB < lowerKC) and (upperBB > upperKC) // Squeeze is off
noSqz = not sqzOn and not sqzOff // No Squeeze

// Momentum Calculation
highAvg = highest(high, lengthKC)
lowAvg = lowest(low, lengthKC)
avgHighLow = avg(highAvg, lowAvg)
smaClose = sma(close, lengthKC)
avgValue = avg(avgHighLow, smaClose)
sourceMinusAvg = source - avgValue
val = linreg(sourceMinusAvg, lengthKC, 0)

// Scale the histogram value to make it larger


scaleFactor = 40000 // Multiply the value by 40,000
valScaled = val * scaleFactor // Scale the value

// Squeeze Colors
bcolor = valScaled > 0 ?
(valScaled > nz(valScaled[1]) ? color.green : color.new(color.green,
50)) :
(valScaled < nz(valScaled[1]) ? color.red : color.new(color.red, 50))
scolor = noSqz ? color.blue : (sqzOn ? color.black : color.gray)

// Squeeze Momentum Plots


plot(valScaled, color=bcolor, style=plot.style_histogram, linewidth=4,
title="Squeeze Momentum")
plot(0, color=scolor, style=plot.style_cross, linewidth=2, title="Squeeze State")

// Reference lines for the scale


hline(40000, "Upper Range", color=color.new(color.gray, 80),
linestyle=hline.style_dashed)
hline(-40000, "Lower Range", color=color.new(color.gray, 80),
linestyle=hline.style_dashed)
// Stochastic Oscillator Configuration
kLength = input(14, title="K Length")
dLength = input(3, title="D Length")
smoothK = input(3, title="Smooth K")
stochOverbought = input(80, title="Overbought Level")
stochOversold = input(20, title="Oversold Level")

// Stochastic Calculation
k = sma(stoch(close, high, low, kLength), smoothK)
d = sma(k, dLength)

// Stochastic Plots
hline(stochOverbought, "Overbought", color=color.red, linestyle=hline.style_dashed)
hline(stochOversold, "Oversold", color=color.green, linestyle=hline.style_dashed)
plot(k, color=color.blue, linewidth=2, title="%K")
plot(d, color=color.orange, linewidth=2, title="%D")

You might also like