OPEN-SOURCE SCRIPT

ST+SQZMOM v.3

// This Pine Script™ code is subject to the terms of the Mozilla Public License 2.0 at mozilla.org/MPL/2.0/
// © syahdan modifying LazyBear script and many sources
//======================================================================================================================

//version=6
indicator('ST+SQZMOM v.3', overlay = true, max_lines_count = 40)
import TradingView/ta/9

//======================================================================================================================

//groups
Alerts = 'alert'
Supertrend = 'supertrend'
SqzMom = 'sqzMom'
Sma = 'ma'
TS = 'tp/sl'

// Input switches for alerts
alertTrendChange = input.bool(true, title='Enable Trend Change Alert', group = Alerts)
alertSQZMOM = input.bool(true, title='Enable SQZMOM Alerts', group = Alerts)

// Supertrend Inputs
atrPeriod = input.int(10, 'ATR Length', minval = 1, group = Supertrend)
factor = input.float(2.5, 'Factor', minval = 0.01, step = 0.01, group = Supertrend)

// 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)

// Hardcoded tp + sl multipliers
targetMultiplier1 = input.int(1, 'TP1 multiply', minval = 1, group = TS)
targetMultiplier2 = input.int(2, 'TP2 multiply', minval = 2, group = TS)
targetMultiplier3 = input.int(3, 'TP3 multiply', minval = 3, group = TS)
stopLossMultiplier = input.int(3, 'SL multiply', minval = 3, group = TS)

//======================================================================================================================

// Calculate Supertrend
[supertrend, direction] = ta.supertrend(factor, atrPeriod)

// 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

// Plotting Squeeze Momentum Indicator

plotshape(sqzOn or noSqz, 'In Squeeze', shape.square, location.top, color=sqzOn or noSqz ? color.new(color.orange, 0) : color.new(color.white, 0))
plotshape(triangUp or triangUpR, 'Squeeze Release UpTrend', shape.triangleup, location.top, color=triangUp ? color.new(color.green, 0) : color.new(color.yellow, 0))
plotshape(triangDown or triangDownR, 'Squeeze Release DownTrend', shape.triangledown, location.top, color=triangDown ? color.new(color.red, 0) : color.new(color.yellow, 0))
plotshape(insideThreshold, 'inside threshold', shape.diamond, location.top, color.new(color.white, 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

// Draw new labels with three decimal places
stopLossLabel := label.new(last_bar_index + 10, stopLossLevel, 'SL: ' + str.tostring(stopLossLevel, '#.###'), style = label.style_label_left, color = color.red, textcolor = color.white)
entryLabel := label.new(last_bar_index + 10, close, 'Entry: ' + str.tostring(close, '#.###'), style = label.style_label_left, color = color.green, textcolor = color.white)
targetLabel1 := label.new(last_bar_index + 10, targetLevel1, 'TP 1: ' + str.tostring(targetLevel1, '#.###'), style = label.style_label_left, color = color.blue, textcolor = color.white)
if saveEntryBuy
targetLabel2 := label.new(last_bar_index + 10, targetLevel2, 'TP 2: ' + str.tostring(targetLevel2, '#.###'), style = label.style_label_left, color = color.blue, textcolor = color.white)
targetLabel3 := label.new(last_bar_index +10, targetLevel3, 'TP 3: ' + str.tostring(targetLevel3, '#.###'), style = label.style_label_left, color = color.blue, textcolor = color.white)

if downTrend and triangDown and count_down == 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 saveEntrySell
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

// Draw new labels with three decimal places
stopLossLabel := label.new(last_bar_index + 10, stopLossLevel, 'SL: ' + str.tostring(stopLossLevel, '#.###'), style = label.style_label_left, color = color.red, textcolor = color.white)
entryLabel := label.new(last_bar_index + 10, close, 'Entry: ' + str.tostring(close, '#.###'), style = label.style_label_left, color = color.green, textcolor = color.white)
targetLabel1 := label.new(last_bar_index + 10, targetLevel1, 'TP 1: ' + str.tostring(targetLevel1, '#.###'), style = label.style_label_left, color = color.blue, textcolor = color.white)
if saveEntrySell
targetLabel2 := label.new(last_bar_index + 10, targetLevel2, 'TP 2: ' + str.tostring(targetLevel2, '#.###'), style = label.style_label_left, color = color.blue, textcolor = color.white)
targetLabel3 := label.new(last_bar_index + 10, targetLevel3, 'TP 3: ' + str.tostring(targetLevel3, '#.###'), style = label.style_label_left, color = color.blue, textcolor = color.white)


// Plot momentum strength/direction
plotarrow(showDir ? dir : na, 'Momentum Strength/Direction', color.new(color.aqua, 50), color.new(color.orange, 50), show_last = 500)
plot(showBB ? bbUpper : na, 'BBUpper', color.new(color.blue, 25), show_last = 500)
plot(showBB ? bbLower : na, 'BBLower', color.new(color.blue, 25), show_last = 500)
plot(showKC ? kcUpper : na, 'KCUpper', color.new(color.red, 25), show_last = 500)
plot(showKC ? kcLower : na, 'KCLower', color.new(color.red, 25), show_last = 500)

//======================================================================================================================

// 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, '#.###') : '---'

alertMessage = 'Mode : '+ saveEntry +'\n' +
'Pair : ' + syminfo.tickerid + ' | ' + timeframe.period + '\n' +
'Trend : ' + trendType + '\n' +
'SL : ' + stopLossValue + '\n' +
'Ent : ' + entryValue + '\n' +
'TP1 : ' + targetValue1 + '\n' +
'TP2 : ' + targetValue2 + '\n' +
'TP3 : ' + targetValue3

if alertTrendChange
alert(alertMessage, alert.freq_once_per_bar_close)

// Reset the newLinesDrawn flag
newLinesDrawn := false
Bands and ChannelsBreadth IndicatorsCandlestick analysis

Script open-source

Dans le plus pur esprit TradingView, l'auteur de ce script l'a publié en open-source, afin que les traders puissent le comprendre et le vérifier. Bravo à l'auteur! Vous pouvez l'utiliser gratuitement, mais la réutilisation de ce code dans une publication est régie par nos Règles. Vous pouvez le mettre en favori pour l'utiliser sur un graphique.

Vous voulez utiliser ce script sur un graphique ?

Clause de non-responsabilité