(JS) Squeeze Pro 2

J-Streak · study · 179 行 · 点赞 2,089 · TradingView 原页

本页源码来自 TradingView 公开发布的开源脚本,版权归原作者所有, 请遵循其原始许可(Pine 脚本常见 CC BY-NC-SA / MPL-2.0 / MIT)。 本项目仅用于研究检索与许可范围内的移植。

Pine Script

//@version=4
study(title="(JS) Squeeze Pro 2", shorttitle="(JS)SqzPro2", overlay=false)
//----------------------------{ Inputs }----------------------------------------
src                 = input(defval=close, title="Squeeze Input", type=input.source)
int     length      = input(20, title="Length", minval=1)
string  bbmatype    = input(defval='SMA', title='Bollinger Bands Calculation Type', options=['SMA', 'EMA', 'WMA', 'VWMA', 'RMA'])
string  kcmatype    = input(defval='SMA', title='Keltner Channel Calculation Type', options=['SMA', 'EMA', 'WMA', 'VWMA', 'RMA'])
int     color_1     = input(title="Color Format", defval=1, minval=1, maxval=5)
bool    drdiv       = input(defval=false, title="Draw Divergence")
bool    darkm       = input(defval=true, title="Gray Background for Dark Mode")
//Bollinger Bands Basis Line
smabb       = sma(src, length)
emabb       = ema(src, length)
wmabb       = wma(src, length)
vwmabb      = vwma(src, length)
rmabb       = rma(src, length)
basis       = bbmatype == 'SMA' ? smabb : bbmatype == 'EMA' ? emabb : 
   bbmatype == 'WMA' ? wmabb : bbmatype == 'VWMA' ? vwmabb : rmabb
//Keltner Channel Basis Line
smakc       = sma(src, length)
emakc       = ema(src, length)
wmakc       = wma(src, length)
vwmakc      = vwma(src, length)
rmakc       = rma(src, length)
basiskc     = bbmatype == 'SMA' ? smakc : kcmatype == 'EMA' ? emakc : 
   kcmatype == 'WMA' ? wmakc : kcmatype == 'VWMA' ? vwmakc : rmakc
// Keltner Channel Low & Mid
smakclm     = sma(tr, length)
emakclm     = ema(tr, length)
wmakclm     = wma(tr, length)
vwmakclm    = vwma(tr, length)
rmakclm     = rma(tr, length)
rangema     = kcmatype == 'SMA' ? smakclm : kcmatype == 'EMA' ? emakclm : 
   kcmatype == 'WMA' ? wmakclm : kcmatype == 'VWMA' ? vwmakclm : rmakclm
//Keltner Channel High
smakch      = sma(tr, length)
emakch      = ema(tr, length)
wmakch      = wma(tr, length)
vwmakch     = vwma(tr, length)
rmakch      = rma(tr, length)
rangemah    = kcmatype == 'SMA' ? smakch : kcmatype == 'EMA' ? emakch : 
   kcmatype == 'WMA' ? wmakch : kcmatype == 'VWMA' ? vwmakch : rmakch
//{Instead of putting the numbers in the formula, I chose to do it this way so you could set it in actual BB or KC too}
//Bollinger Bands Multiplier
mult        = 2.0
//Keltner Channel Multiplier
multKC      = 1.5
//Bollinger Bands Low Multiplier
multlow     = 1.5
//Keltner Channel High Multiplier
multhighKC  = 1
//Bollinger Bands Low
devl        = multlow * stdev(src, length)
upperBBl    = basis + devl
lowerBBl    = basis - devl
//Bollinger Bands Mid & High 
dev         = mult * stdev(src, length)
upperBB     = basis + dev
lowerBB     = basis - dev
//Keltner Channel Bands Low & Mid
upperKC     = basiskc + rangema * multKC
lowerKC     = basiskc - rangema * multKC
//Keltner Channel Bands High
upperKCh    = basiskc + rangemah * multhighKC
lowerKCh    = basiskc - rangemah * multhighKC
//------------------------{ Squeeze Basics }------------------------------------
//Zero Line
hline(0, color=color.white, linestyle=hline.style_solid, linewidth=1)
//Squeeze Momentum
sz          = linreg(src - avg(avg(highest(high, length), lowest(low, length)), ema(close, length)), length, 0)
//Squeeze Conditions
sc1         = sz >= 0
sc2         = sz < 0
sc3         = sz >= sz[1]
sc4         = sz < sz[1]
sc5         = sz < 1
sc6         = sz > -1
//Squeeze On
lowsqz      = lowerBBl > lowerKC and upperBBl < upperKC
lsf         = lowsqz == false
midsqz      = lowerBB > lowerKC and upperBB < upperKC
msf         = midsqz == false
highsqz     = lowerBB > lowerKCh and upperBB < upperKCh
hsf         = highsqz == false
//-----------------------{ Color Components }-----------------------------------
//Color Conditions
clr1        = sc1 and sc3 ? #00bcd4 : 
   sc1 and sc4 ? #0d47a1 : sc2 and sc4 ? #ba68c8 : sc2 and sc3 ? #9c27b0 : #673ab7
clr2        = sc1 and sc3 ? #15ff00 : sc1 and sc4 ? #388e3c : 
   sc2 and sc4 ? #f44336 : sc2 and sc3 ? #b71c1c : color.gray
clr3        = sc1 and sc3 ? #fff59d : 
   sc1 and sc4 ? #ffd600 : sc2 and sc4 ? #ffcc80 : sc2 and sc3 ? #ff9800 : #702700
clr4        = sc1 and sc3 ? #2196f3 : 
   sc1 and sc4 ? #0d47a1 : sc2 and sc4 ? #ef9a9a : sc2 and sc3 ? #d32f2f : #ce93d8
clr5        = sc5 and sc1 ? #ef6c00 : sc6 and sc2 ? #ef6c00 : sc1 and sc3 ? #fff176 : 
   sc1 and sc4 ? #ffd600 : sc2 and sc4 ? #ef9a9a : sc2 and sc3 ? #d32f2f : #ffb74d
choice      = color_1 == 1 ? clr1 : color_1 == 2 ? clr2 : 
   color_1 == 3 ? clr3 : color_1 == 4 ? clr4 : color_1 == 5 ? clr5 : na
//Background Conditions
bg1         = #673ab7
bg2         = color.gray
bg3         = #ffd600
bg4         = #ce93d8
bg5         = #ffb74d
bgc         = color_1 == 1 ? bg1 : color_1 == 2 ? bg2 : 
   color_1 == 3 ? bg3 : color_1 == 4 ? bg4 : color_1 == 5 ? bg5 : na
//-----------------{ Indicator Components and Plots }---------------------------
//Background Colors
bgcolor(color=darkm ? bgc : na, editable=false, transp=95, editable=false)
bgcolor(choice)
//Squeeze Dot Plot
sqzpro      = highsqz ? highsqz : midsqz ? midsqz : lowsqz ? lowsqz : na
sqzproc     = highsqz ? #ffeb3b : midsqz ? #e91e63 : lowsqz ? color.white : na
plotshape(sqzpro, title="Squeeze Dots Top", style=shape.circle, location=location.top, color=sqzproc)
plotshape(sqzpro, title="Squeeze Dots Bottom", style=shape.circle, location=location.bottom, color=sqzproc)
//Squeeze Plot
plot(sz, title="Squeeze Momentum", color=choice, style=plot.style_columns, linewidth=1)
//Divergence Formula {Compliments of Ricardo Santos}
ftf(_src) =>
    _src[4] < _src[2] and _src[3] < _src[2] and _src[2] > _src[1] and 
       _src[2] > _src[0]
fbf(_src) =>
    _src[4] > _src[2] and _src[3] > _src[2] and _src[2] < _src[1] and 
       _src[2] < _src[0]
ffract(_src) =>
    fbf__1 = fbf(_src)
    ftf(_src) ? 1 : fbf__1 ? -1 : 0
fractaltop  = ffract(sz) > 0 ? sz[2] : na
fractalbot  = ffract(sz) < 0 ? sz[2] : na
high_prev   = valuewhen(fractaltop, sz[2], 1)
high_price  = valuewhen(fractaltop, high[2], 1)
low_prev    = valuewhen(fractalbot, sz[2], 1)
low_price   = valuewhen(fractalbot, low[2], 1)
regbeardiv  = fractaltop and high[2] > high_price and sz[2] < high_prev
hidbeardiv  = fractaltop and high[2] < high_price and sz[2] > high_prev
regbulldiv  = fractalbot and low[2] < low_price and sz[2] > low_prev
hidbulldiv  = fractalbot and low[2] > low_price and sz[2] < low_prev
//Divergence Plot
plot(title='Bearish Fractal', series=drdiv ? fractaltop ? sz[2] : na : na, color=regbeardiv ? choice : na, linewidth=2, offset=-2, editable=false)
plot(title='Bullish Fractal', series=drdiv ? fractalbot ? sz[2] : na : na, color=regbulldiv ? choice : na, linewidth=2, offset=-2, editable=false)
plot(title='Bearish Divergence', series=drdiv ? fractaltop ? sz[2] : na : na, style=plot.style_circles, color=regbeardiv ? choice : na, linewidth=3, offset=-2, editable=false)
plot(title='Bullish Divergence', series=drdiv ? fractalbot ? sz[2] : na : na, style=plot.style_circles, color=regbulldiv ? choice : na, linewidth=3, offset=-2, editable=false)
//------------------------{ Alert Conditions }----------------------------------
//Dot Count Regular
ll6         = barssince(lsf) == 6
ml6         = barssince(msf) == 6
hl6         = barssince(hsf) == 6
//Dot Count Prolonged
ll12        = barssince(lsf) == 12
ml12        = barssince(msf) == 12
hl12        = barssince(hsf) == 12
//End Of Squeeze
eol         = barssince(lowsqz) == 1
eom         = barssince(midsqz) == 1
eoh         = barssince(highsqz) == 1
//Start Of Squeeze
sol         = barssince(lsf) == 1
som         = barssince(msf) == 1
soh         = barssince(hsf) == 1
//-----------------------------{ Alerts }---------------------------------------
//6th Dot Alerts
alertcondition(ll6, title="6th White Dot", message="Early Squeeze - 6th White Dot")
alertcondition(ml6, title="6th Red Dot", message="Squeeze - 6th Red Dot")
alertcondition(hl6, title="6th Yellow Dot", message="Power Squeeze - 6th Yellow Dot")
//12th Dot Alerts
alertcondition(ll12, title="12th White Dot", message="Early Squeeze - 12th White Dot")
alertcondition(ml12, title="12th Red Dot", message="Squeeze - 12th Red Dot")
alertcondition(hl12, title="12th Yellow Dot", message="Power Squeeze - 12th Yellow Dot")
//End of Squeeze Alerts
alertcondition(eol, title="White Dot - End of Squeeze", message="White Dot - End of Early Squeeze")
alertcondition(eom, title="Red Dot - End of Squeeze", message="Yellow Dot - End of Squeeze")
alertcondition(eoh, title="Yellow Dot - End of Squeeze", message="Red Dot - End of Power Squeeze")
//Start of Squeeze Alerts
alertcondition(sol, title="Early Squeeze Started", message="White Dot - Early Squeeze Started")
alertcondition(som, title="Squeeze Started", message="Red Dot - Squeeze Started")
alertcondition(soh, title="Power Squeeze Started", message="Yellow Dot - Power Squeeze Started")
//Zero Line Alerts
alertcondition(sz[1] < 0 and sz > 0, title="Zero Line Cross Bullish", message="Zero Line Cross Bullish")
alertcondition(sz[1] > 0 and sz < 0, title="Zero Line Cross Bearish", message="Zero Line Cross Bearish")

← 返回列表