Chỉ báo Williams, RSI stochastic, RSI, Stochastic.
//@version=5
indicator('Williams %R, RSI, Stochastic and Stochastic RSI', shorttitle='Willy, RSI, Stochastic', overlay=false)
source = input(title='Source', defval=close)
showWilly = input(title='Show Williams %R', defval=true)
oscillator = input.string(title='Indicator', options=['RSI', 'Stochastic', 'Stochastic RSI', 'None'], defval='Stochastic')
showDivergence = input(title='Show divergence', defval=true)
showHiddneDivergence = input(title='Show hidden divergence', defval=true)
showMidline = input(title='Show midline', defval=true)
// Divergence
lbR = input(title='Div Pivot Lookback Right', defval=5)
lbL = input(title='Div Pivot Lookback Left', defval=5)
rangeUpper = input(title='Div Max of Lookback Range', defval=60)
rangeLower = input(title='Div Min of Lookback Range', defval=5)
delayPlotTilClosed = input(title='Div Delay plot until candle is closed', defval=false)
// (mab) Colors
col_darkSpringGreen = #137547
col_lightGreen = #0f7506
col_rubyRed = #f01c2db2
col_redPigment = #ef101083
col_outrageousOrange = #ca2c00
col_mysticMaroon = #bc387d
col_darkBluePurple = #311b92
col_GreenBlueCrayola = #2761ff
col_vividSkyBlue = #0066ff
col_maximumYellowRed = #fec149
col_navajo_white = #FEDB9B
col_seeshell = #000000
col_none = color.new(color.white, 100)
bullColor = col_darkSpringGreen
bearColor = col_rubyRed
// Select the second oscillator
isRsi = oscillator == 'RSI'
isStochastic = oscillator == 'Stochastic'
isStochasticRsi = oscillator == 'Stochastic RSI'
isNone = oscillator == 'None'
// Repaint
repaint = not delayPlotTilClosed or barstate.ishistory or barstate.isconfirmed
// Location for labels
labelLocation = time + 6 * (time[1] - time[51]) / 50
// Offset for lines and background
plotOffset = 50
// Draw line to separate Willy and second oscillator
plot(showWilly and not isNone ? 0.0 : na, title='Separator line', color=color.new(#fa00ed, 0), style=plot.style_linebr, linewidth=2, join=true)
plot(showWilly and not isNone ? 0.0 : na, title='Separator line', color=color.new(#fa00ed, 0), style=plot.style_linebr, linewidth=2, join=true, show_last=plotOffset, offset=plotOffset - 1)
// Check if condition is true in range (helper function)
_inRange(cond) =>
bars = ta.barssince(cond == true)
rangeLower <= bars and bars <= rangeUpper
//
// Willy - this is a Williams %R with 13 ema like it is used by TRI
//
willyLength = input.int(title='Willy length', defval=34, minval=1)
willyEma = input.int(title='Willy EMA', defval=13, minval=1)
willyOversold = input.float(title='Willy oversold', defval=-80.0, maxval=0.0)
willyOverbought = input.float(title='Willy overbought', defval=-20.0, maxval=0.0)
willyColor1 = col_vividSkyBlue
willyColor2 = col_outrageousOrange
willyColorBackground = col_GreenBlueCrayola
willyStupidOverboughtColor = col_outrageousOrange
willyStupidOversoldColor = col_lightGreen
// Calculate willy
willyUpper = ta.highest(willyLength)
willyLower = ta.lowest(willyLength)
willyValue1 = -100 * (willyUpper - close) / (willyUpper - willyLower)
willyValue2 = ta.ema(willyValue1, willyEma)
isWillyStupidOverbought = willyValue2 > willyOverbought
isWillyStupidOversold = willyValue2 < willyOversold
willyMidline = (willyOversold + willyOverbought) / 2.0
// Highlight when Willy is stupid
bandHighlight1 = plot(showWilly and isWillyStupidOverbought ? 0.0 : na, title='Willy band 1', display=display.none)
bandHighlight2 = plot(showWilly and isWillyStupidOverbought ? willyOverbought : na, title='Willy band 2', display=display.none)
bandHighlight3 = plot(showWilly and isWillyStupidOversold ? willyOversold : na, title='Willy band 3', display=display.none)
bandHighlight4 = plot(showWilly and isWillyStupidOversold ? -100.0 : na, title='Willy band 4', display=display.none)
fill(bandHighlight1, bandHighlight2, color=willyStupidOverboughtColor, title='Willy stupid overbought', transp=50)
fill(bandHighlight3, bandHighlight4, color=willyStupidOversoldColor, title='Willy stupid oversold', transp=50)
// Plot Willy
plot(showWilly ? willyValue1 : na, title='Willy', color=willyColor1, linewidth=2)
plot(showWilly ? willyValue2 : na, title='Willy EMA', color=willyColor2, linewidth=1, display=display.none)
willyBand1 = plot(showWilly ? willyOversold : na, title='Willy oversold line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true)
willyBand1_O = plot(showWilly ? willyOversold : na, title='Willy oversold line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true, show_last=plotOffset, offset=plotOffset - 1)
willyBand2 = plot(showWilly ? willyOverbought : na, title='Willy overbought line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true)
willyBand2_O = plot(showWilly ? willyOverbought : na, title='Willy overbought line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true, show_last=plotOffset, offset=plotOffset - 1)
plot(showWilly ? willyMidline : na, title='Midline', color=color.new(color.green, 0), style=plot.style_line, linewidth=2, join=true)
plot(showWilly ? willyMidline : na, title='Midline', color=color.new(color.green, 0), style=plot.style_line, linewidth=2, join=true, show_last=plotOffset, offset=plotOffset - 1)
fill(willyBand1, willyBand2, color=willyColorBackground, title='Willy background', transp=90)
fill(willyBand1_O, willyBand2_O, color=willyColorBackground, title='Willy background', show_last=1, transp=90)
// Label willy indicator
if barstate.islast and showWilly
labelW = label.new(labelLocation, willyMidline, xloc=xloc.bar_time, text='Willy', textcolor=willyColor1, style=label.style_none, textalign=text.align_left)
label.delete(labelW[1])
//
// Find Willy divergence
//
// Find pivot
willyPlFound = na(ta.pivotlow(willyValue1, lbL, lbR)) ? false : true
willyPhFound = na(ta.pivothigh(willyValue1, lbL, lbR)) ? false : true
// Willy: Higher high
willyHH = willyValue1[lbR] > ta.valuewhen(willyPhFound, willyValue1[lbR], 1) and _inRange(willyPhFound[1])
// Willy: Lower high
willyLH = willyValue1[lbR] < ta.valuewhen(willyPhFound, willyValue1[lbR], 1) and _inRange(willyPhFound[1])
// Willy: Higher low
willyHL = willyValue1[lbR] > ta.valuewhen(willyPlFound, willyValue1[lbR], 1) and _inRange(willyPlFound[1])
// Willy: Lower low
willyLL = willyValue1[lbR] < ta.valuewhen(willyPlFound, willyValue1[lbR], 1) and _inRange(willyPlFound[1])
// Price: Lower Low
willyPriceLL = low[lbR] < ta.valuewhen(willyPlFound, low[lbR], 1)
// Price: Higher low
willyPriceHL = low[lbR] > ta.valuewhen(willyPlFound, low[lbR], 1)
// Price: Higher high
willyPriceHH = high[lbR] > ta.valuewhen(willyPhFound, high[lbR], 1)
// Price: Lower high
willyPriceLH = high[lbR] < ta.valuewhen(willyPhFound, high[lbR], 1)
// Regular bullish condition
willyBullCond = showDivergence and willyPriceLL and willyHL and willyPlFound and repaint
// Regular bearish condition
willyBearCond = showDivergence and willyPriceHH and willyLH and willyPhFound and repaint
// Hidden bullish condition
willyHiddenBullCond = showHiddneDivergence and willyPriceHL and willyLL and willyPlFound and repaint
// Hidden bearish cndition
willyHiddenBearCond = showHiddneDivergence and willyPriceLH and willyHH and willyPhFound and repaint
// Plot divergence
plot(showWilly and willyPlFound ? willyValue1[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=2, color=willyBullCond ? bullColor : col_none, transp=20)
plotshape(showWilly and willyBullCond ? willyValue1[lbR] : na, offset=-lbR, title='Regular Bullish Label', text=' Bull ', style=shape.labelup, location=location.absolute, color=bullColor, textcolor=color.new(col_seeshell, 0), transp=0)
plot(showWilly and willyPhFound ? willyValue1[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=2, color=willyBearCond ? bearColor : col_none, transp=0)
plotshape(showWilly and willyBearCond ? willyValue1[lbR] : na, offset=-lbR, title='Regular Bearish Label', text=' Bear ', style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=color.new(col_seeshell, 0), transp=0)
plot(showWilly and willyPlFound ? willyValue1[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=willyHiddenBullCond ? bullColor : col_none, transp=20)
plotshape(showWilly and willyHiddenBullCond ? willyValue1[lbR] : na, offset=-lbR, title='Hidden Bullish Label', text=' H Bull ', style=shape.labelup, location=location.absolute, color=bullColor, textcolor=color.new(col_seeshell, 20), transp=20)
plot(showWilly and willyPhFound ? willyValue1[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=2, color=willyHiddenBearCond ? bearColor : col_none, transp=20)
plotshape(showWilly and willyHiddenBearCond ? willyValue1[lbR] : na, offset=-lbR, title='Hidden Bearish Label', text=' H Bear ', style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=color.new(col_seeshell, 20), transp=20)
//
// RSI
//
showRsi2 = input(title='Show RSI 2', defval=false)
rsiLength1 = input.int(title='RSI 1 length', defval=14, minval=1)
rsiLength2 = input.int(title='RSI 2 length', defval=7, minval=1)
rsiOversold = input.float(title='RSI oversold', defval=30.0, minval=0.0)
rsiOverbought = input.float(title='RSI overbought', defval=70.0, minval=0.0)
rsiColor1 = col_maximumYellowRed
rsiColor2 = col_redPigment
rsiColorBackground = col_darkBluePurple
rsiValue1 = ta.rsi(source, rsiLength1)
rsiValue2 = ta.rsi(source, rsiLength2)
//
// Stochastic
//
stocPeriod = input.int(title='Stochastic period', defval=14, minval=1)
stocSmoothK = input.int(title='Stochastic smooth K', defval=10, minval=1)
stocSmoothD = input.int(title='Stochastic smooth D', defval=3, minval=1)
stocOversold = input.float(title='Stochastic oversold', defval=20.0, minval=0.0)
stocOverbought = input.float(title='Stochastic overbought', defval=80.0, minval=0.0)
stocColor1 = col_GreenBlueCrayola
stocColor2 = col_redPigment
stocColorBackground = col_mysticMaroon
stocValueK = ta.sma(ta.stoch(close, high, low, stocPeriod), stocSmoothK)
stocValueD = ta.sma(stocValueK, stocSmoothD)
//
// Stochastic RSI
//
stocRsiPeriod = input.int(title='Stochastic RSI period', defval=14, minval=1)
stocRsiSmoothK = input.int(title='Stochastic RSI smooth K', defval=10, minval=1)
stocRsiSmoothD = input.int(title='Stochastic RSI smooth D', defval=3, minval=1)
stocRsiLength = input.int(title='Stochastic RSI length', defval=14, minval=1)
stocRsiOversold = input.float(title='Stochastic RSI oversold', defval=20.0, minval=0.0)
stocRsiOverbought = input.float(title='Stochastic RSI overbought', defval=80.0, minval=0.0)
stocRsiColor1 = col_vividSkyBlue
stocRsiColor2 = col_outrageousOrange
stocRsiColorBackground = col_navajo_white
stocRsi = ta.rsi(source, stocRsiLength)
stocRsiValueK = ta.sma(ta.stoch(stocRsi, stocRsi, stocRsi, stocRsiPeriod), stocRsiSmoothK)
stocRsiValueD = ta.sma(stocRsiValueK, stocRsiSmoothD)
//
// Assign the values of the selected oscillator
//
float oscillatorValue1 = na
float oscillatorValue2 = na
float oversold = na
float overbought = na
float midline = na
color oscillatorColor1 = na
color oscillatorColor2 = na
color backgroundColor = na
oscillatorValue1 := isNone ? na : isRsi ? rsiValue1 : isStochastic ? stocValueK : isStochasticRsi ? stocRsiValueK : na
oscillatorValue2 := isNone ? na : isRsi and showRsi2 ? rsiValue2 : isStochastic ? stocValueD : isStochasticRsi ? stocRsiValueD : na
oversold := isNone ? na : isRsi ? rsiOversold : isStochastic ? stocOversold : isStochasticRsi ? stocRsiOversold : na
overbought := isNone ? na : isRsi ? rsiOverbought : isStochastic ? stocOverbought : isStochasticRsi ? stocRsiOverbought : na
oscillatorColor1 := isNone ? na : isRsi ? rsiColor1 : isStochastic ? stocColor1 : isStochasticRsi ? stocRsiColor1 : na
oscillatorColor2 := isNone ? na : isRsi ? rsiColor2 : isStochastic ? stocColor2 : isStochasticRsi ? stocRsiColor2 : na
backgroundColor := isNone ? na : isRsi ? rsiColorBackground : isStochastic ? stocColorBackground : isStochasticRsi ? stocRsiColorBackground : na
midline := isNone ? na : showMidline ? (overbought + oversold) / 2 : na
// Plot the oscillator
plot(oscillatorValue1, title='Indicator 1', color=oscillatorColor1, linewidth=2)
plot(oscillatorValue2, title='Indicator 2', color=oscillatorColor2, linewidth=1)
band1 = plot(oversold, title='Oversold line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true)
band1_O = plot(oversold, title='Oversold line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true, show_last=plotOffset, offset=plotOffset - 1)
band2 = plot(overbought, title='Overbought line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true)
band2_O = plot(overbought, title='Overbought line', color=color.new(col_seeshell, 0), style=plot.style_line, linewidth=1, join=true, show_last=plotOffset, offset=plotOffset - 1)
plot(midline, title='Midline', color=color.new(color.green, 0), style=plot.style_line, linewidth=2, join=true)
plot(midline, title='Midline', color=color.new(color.green, 0), style=plot.style_line, linewidth=2, join=true, show_last=plotOffset, offset=plotOffset - 1)
fill(band1, band2, color=backgroundColor, title='Background', transp=90)
fill(band1_O, band2_O, color=backgroundColor, title='Background', show_last=1, transp=90)
// Label the oscillator
if barstate.islast and not isNone
labelI = label.new(labelLocation, midline, xloc=xloc.bar_time, text=oscillator, textcolor=oscillatorColor1, style=label.style_none, textalign=text.align_left)
label.delete(labelI[1])
//
// Find divergence in second oscillator
//
// Find pivot
oscillatorPlFound = na(ta.pivotlow(oscillatorValue1, lbL, lbR)) ? false : true
oscillatorPhFound = na(ta.pivothigh(oscillatorValue1, lbL, lbR)) ? false : true
// Second oscillator: Higher high
oscillatorHH = oscillatorValue1[lbR] > ta.valuewhen(oscillatorPhFound, oscillatorValue1[lbR], 1) and _inRange(oscillatorPhFound[1])
// Second oscillator: Lower high
oscillatorLH = oscillatorValue1[lbR] < ta.valuewhen(oscillatorPhFound, oscillatorValue1[lbR], 1) and _inRange(oscillatorPhFound[1])
// Second oscillator: Higher low
oscillatorHL = oscillatorValue1[lbR] > ta.valuewhen(oscillatorPlFound, oscillatorValue1[lbR], 1) and _inRange(oscillatorPlFound[1])
// Second oscillator: Lower low
oscillatorLL = oscillatorValue1[lbR] < ta.valuewhen(oscillatorPlFound, oscillatorValue1[lbR], 1) and _inRange(oscillatorPlFound[1])
// Price: Lower Low
oscillatorPriceLL = low[lbR] < ta.valuewhen(oscillatorPlFound, low[lbR], 1)
// Price: Higher low
oscillatorPriceHL = low[lbR] > ta.valuewhen(oscillatorPlFound, low[lbR], 1)
// Price: Higher high
oscillatorPriceHH = high[lbR] > ta.valuewhen(oscillatorPhFound, high[lbR], 1)
// Price: Lower high
oscillatorPriceLH = high[lbR] < ta.valuewhen(oscillatorPhFound, high[lbR], 1)
// Regular bullish condition
oscillatorBullCond = showDivergence and oscillatorPriceLL and oscillatorHL and oscillatorPlFound and repaint
// Regular bearish condition
oscillatorBearCond = showDivergence and oscillatorPriceHH and oscillatorLH and oscillatorPhFound and repaint
// Hidden bullish condition
oscillatorHiddenBullCond = showHiddneDivergence and oscillatorPriceHL and oscillatorLL and oscillatorPlFound and repaint
// Hidden bearish cndition
oscillatorHiddenBearCond = showHiddneDivergence and oscillatorPriceLH and oscillatorHH and oscillatorPhFound and repaint
// Plot divergence
plot(oscillatorPlFound ? oscillatorValue1[lbR] : na, offset=-lbR, title='Regular Bullish', linewidth=3, color=oscillatorBullCond ? bullColor : col_none, transp=20)
plotshape(oscillatorBullCond ? oscillatorValue1[lbR] : na, offset=-lbR, title='Regular Bullish Label', text=' Bull ', style=shape.labelup, location=location.absolute, color=bullColor, textcolor=color.new(col_seeshell, 0), transp=0)
plot(oscillatorPhFound ? oscillatorValue1[lbR] : na, offset=-lbR, title='Regular Bearish', linewidth=2, color=oscillatorBearCond ? bearColor : col_none, transp=20)
plotshape(oscillatorBearCond ? oscillatorValue1[lbR] : na, offset=-lbR, title='Regular Bearish Label', text=' Bear ', style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=color.new(col_seeshell, 0), transp=0)
plot(oscillatorPlFound ? oscillatorValue1[lbR] : na, offset=-lbR, title='Hidden Bullish', linewidth=2, color=oscillatorHiddenBullCond ? bullColor : col_none, transp=20)
plotshape(oscillatorHiddenBullCond ? oscillatorValue1[lbR] : na, offset=-lbR, title='Hidden Bullish Label', text=' H Bull ', style=shape.labelup, location=location.absolute, color=bullColor, textcolor=color.new(col_seeshell, 20), transp=20)
plot(oscillatorPhFound ? oscillatorValue1[lbR] : na, offset=-lbR, title='Hidden Bearish', linewidth=2, color=oscillatorHiddenBearCond ? bearColor : col_none, transp=20)
plotshape(oscillatorHiddenBearCond ? oscillatorValue1[lbR] : na, offset=-lbR, title='Hidden Bearish Label', text=' H Bear ', style=shape.labeldown, location=location.absolute, color=bearColor, textcolor=color.new(col_seeshell, 20), transp=20)