Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @version=4
- // @author=The_Caretaker
- // The Caretakers On Chart Rsi Div V4
- study ( title = "Caretakers Rsi Div Indicator Version 4" , shorttitle = "CT Rsi Div V4" , overlay = true, format = format.price , precision = 2 )
- // Inputs
- RsiLen = input ( 14 , minval = 1 , title = "RSI Length" )
- RsiEmaLen = input ( 22 , minval = 1 , title = "RSI EMA Length" )
- Labels = input ( true , "Bull & Bear Arrows" )
- ArrowOffSet = input ( 10 , minval = 1 , maxval = 100, title = "ArrowOffset 1" )
- GuideLines = input ( true , "Guide Lines")
- OffSet1 = input ( 0 , minval = 0 , maxval = 100, title = "Line Offset" )
- VrtBars = input ( true , "Vertical Signal Bars" )
- LookBackBars = input ( 2000 , "Div Lookback Bars" , type = input.integer , minval = 1, maxval = 4000 )
- // Calculations
- Rsi = rsi ( close , RsiLen )
- RsiEma = ema ( Rsi , RsiEmaLen)
- // Divergence Detection Function Declarations
- HiBars1 (S) => S[4] < S[2] and S[3] < S[2] and S[2] > S[1] and S[2] > S[0]
- LoBars1 (S) => S[4] > S[2] and S[3] > S[2] and S[2] < S[1] and S[2] < S[0]
- HiBars2 (S) => S[6] < S[3] and S[5] < S[3] and S[4] < S[3] and S[3] > S[2] and S[3] > S[1] and S[3] > S[0]
- LoBars2 (S) => S[6] > S[3] and S[5] > S[3] and S[4] > S[3] and S[3] < S[2] and S[3] < S[1] and S[3] < S[0]
- HiBars3 (S) => S[8] < S[4] and S[7] < S[4] and S[6] < S[4] and S[5] < S[4] and S[4] > S[3] and S[4] > S[2] and S[4] > S[1] and S[4] > S[0]
- LoBars3 (S) => S[8] > S[4] and S[7] > S[4] and S[6] > S[4] and S[5] > S[4] and S[4] < S[3] and S[4] < S[2] and S[4] < S[1] and S[4] < S[0]
- HiBars4 (S) => S[10] < S[5] and S[9] < S[5] and S[8] < S[5] and S[7] < S[5] and S[6] < S[5] and S[5] > S[4] and S[5] > S[3] and S[5] > S[2] and S[5] > S[1] and S[5] > S[0]
- LoBars4 (S) => S[10] > S[5] and S[9] > S[5] and S[8] > S[5] and S[7] > S[5] and S[6] > S[5] and S[5] < S[4] and S[5] < S[3] and S[5] < S[2] and S[5] < S[1] and S[5] < S[0]
- HiBars5 (S) => S[12] < S[6] and S[11] < S[6] and S[10] < S[6] and S[9] < S[6] and S[8] < S[6] and S[7] < S[6] and S[6] > S[5] and S[6] > S[4] and S[6] > S[3] and S[6] > S[2] and S[6] > S[1] and S[6] > S[0]
- LoBars5 (S) => S[12] > S[6] and S[11] > S[6] and S[10] > S[6] and S[9] > S[6] and S[8] > S[6] and S[7] > S[6] and S[6] < S[5] and S[6] < S[4] and S[6] < S[3] and S[6] < S[2] and S[6] < S[1] and S[6] < S[0]
- HiBars6 (S) => S[14] < S[7] and S[13] < S[7] and S[12] < S[7] and S[11] < S[7] and S[10] < S[7] and S[9] < S[7] and S[8] < S[7] and S[7] > S[6] and S[7] > S[5] and S[7] > S[4] and S[7] > S[3] and S[7] > S[2] and S[7] > S[1] and S[7] > S[0]
- LoBars6 (S) => S[14] > S[7] and S[13] > S[7] and S[12] > S[7] and S[11] > S[7] and S[10] > S[7] and S[9] > S[7] and S[8] > S[7] and S[7] < S[6] and S[7] < S[5] and S[7] < S[4] and S[7] < S[3] and S[7] < S[2] and S[7] < S[1] and S[7] < S[0]
- HiBars7 (S) => S[16] < S[8] and S[15] < S[8] and S[14] < S[8] and S[13] < S[8] and S[12] < S[8] and S[11] < S[8] and S[10] < S[8] and S[9] < S[8] and S[8] > S[7] and S[8] > S[6] and S[8] > S[5] and S[8] > S[4] and S[8] > S[3] and S[8] > S[2] and S[8] > S[1] and S[8] > S[0]
- LoBars7 (S) => S[16] > S[8] and S[15] > S[8] and S[14] > S[8] and S[13] > S[8] and S[12] > S[8] and S[11] > S[8] and S[10] > S[8] and S[9] > S[8] and S[8] < S[7] and S[8] < S[6] and S[8] < S[5] and S[8] < S[4] and S[8] < S[3] and S[8] < S[2] and S[8] < S[1] and S[8] < S[0]
- HiBars8 (S) => S[18] < S[9] and S[17] < S[9] and S[16] < S[9] and S[15] < S[9] and S[14] < S[9] and S[13] < S[9] and S[12] < S[9] and S[11] < S[9] and S[10] < S[9] and S[9] > S[8] and S[9] > S[7] and S[9] > S[6] and S[9] > S[5] and S[9] > S[4] and S[9] > S[3] and S[9] > S[2] and S[9] > S[1] and S[9] > S[0]
- LoBars8 (S) => S[18] > S[9] and S[17] > S[9] and S[16] > S[9] and S[15] > S[9] and S[14] > S[9] and S[13] > S[9] and S[12] > S[9] and S[11] > S[9] and S[10] > S[9] and S[9] < S[8] and S[9] < S[7] and S[9] < S[6] and S[9] < S[5] and S[9] < S[4] and S[9] < S[3] and S[9] < S[2] and S[9] < S[1] and S[9] < S[0]
- AllBars1 (x) => HiBars1 (x) ? 1 : LoBars1 (x) ? -1 : 0
- AllBars2 (x) => HiBars2 (x) ? 1 : LoBars2 (x) ? -1 : 0
- AllBars3 (x) => HiBars3 (x) ? 1 : LoBars3 (x) ? -1 : 0
- AllBars4 (x) => HiBars4 (x) ? 1 : LoBars4 (x) ? -1 : 0
- AllBars5 (x) => HiBars5 (x) ? 1 : LoBars5 (x) ? -1 : 0
- AllBars6 (x) => HiBars6 (x) ? 1 : LoBars6 (x) ? -1 : 0
- AllBars7 (x) => HiBars7 (x) ? 1 : LoBars7 (x) ? -1 : 0
- AllBars8 (S) => HiBars8 (S) ? 1 : LoBars8 (S) ? -1 : 0
- // Divergence Detection
- AllBarsTopRsi1 = AllBars1 ( Rsi ) > 0 ? Rsi[2] : na
- AllBarsLowRsi1 = AllBars1 ( Rsi ) < 0 ? Rsi[2] : na
- AllBarsTopRsi2 = AllBars2 ( Rsi ) > 0 ? Rsi[3] : na
- AllBarsLowRsi2 = AllBars2 ( Rsi ) < 0 ? Rsi[3] : na
- AllBarsTopRsi3 = AllBars3 ( Rsi ) > 0 ? Rsi[4] : na
- AllBarsLowRsi3 = AllBars3 ( Rsi ) < 0 ? Rsi[4] : na
- AllBarsTopRsi4 = AllBars4 ( Rsi ) > 0 ? Rsi[5] : na
- AllBarsLowRsi4 = AllBars4 ( Rsi ) < 0 ? Rsi[5] : na
- AllBarsTopRsi5 = AllBars5 ( Rsi ) > 0 ? Rsi[6] : na
- AllBarsLowRsi5 = AllBars5 ( Rsi ) < 0 ? Rsi[6] : na
- AllBarsTopRsi6 = AllBars6 ( Rsi ) > 0 ? Rsi[7] : na
- AllBarsLowRsi6 = AllBars6 ( Rsi ) < 0 ? Rsi[7] : na
- AllBarsTopRsi7 = AllBars7 ( Rsi ) > 0 ? Rsi[8] : na
- AllBarsLowRsi7 = AllBars7 ( Rsi ) < 0 ? Rsi[8] : na
- SrcHigh1 = close [2]
- SrcLow1 = close [2]
- SrcHigh2 = close [3]
- SrcLow2 = close [3]
- SrcHigh3 = close [4]
- SrcLow3 = close [4]
- SrcHigh4 = close [5]
- SrcLow4 = close [5]
- SrcHigh5 = close [6]
- SrcLow5 = close [6]
- SrcHigh6 = close [7]
- SrcLow6 = close [7]
- SrcHigh7 = close [8]
- SrcLow7 = close [8]
- high_prev1 = valuewhen ( AllBarsTopRsi1 , Rsi [2] , 0 ) [2]
- high_price1 = valuewhen ( AllBarsTopRsi1 , SrcHigh1 , 0 ) [2]
- low_prev1 = valuewhen ( AllBarsLowRsi1 , Rsi [2] , 0 ) [2]
- low_price1 = valuewhen ( AllBarsLowRsi1 , SrcLow1 , 0 ) [2]
- high_prev2 = valuewhen ( AllBarsTopRsi2 , Rsi [3] , 0 ) [3]
- high_price2 = valuewhen ( AllBarsTopRsi2 , SrcHigh2 , 0 ) [3]
- low_prev2 = valuewhen ( AllBarsLowRsi2 , Rsi [3] , 0 ) [3]
- low_price2 = valuewhen ( AllBarsLowRsi2 , SrcLow2 , 0 ) [3]
- high_prev3 = valuewhen ( AllBarsTopRsi3 , Rsi [4] , 0 ) [4]
- high_price3 = valuewhen ( AllBarsTopRsi3 , SrcHigh3 , 0 ) [4]
- low_prev3 = valuewhen ( AllBarsLowRsi3 , Rsi [4] , 0 ) [4]
- low_price3 = valuewhen ( AllBarsLowRsi3 , SrcLow3 , 0 ) [4]
- high_prev4 = valuewhen ( AllBarsTopRsi4 , Rsi [5] , 0 ) [5]
- high_price4 = valuewhen ( AllBarsTopRsi4 , SrcHigh4 , 0 ) [5]
- low_prev4 = valuewhen ( AllBarsLowRsi4 , Rsi [5] , 0 ) [5]
- low_price4 = valuewhen ( AllBarsLowRsi4 , SrcLow4 , 0 ) [5]
- high_prev5 = valuewhen ( AllBarsTopRsi5 , Rsi [6] , 0 ) [6]
- high_price5 = valuewhen ( AllBarsTopRsi5 , SrcHigh5 , 0 ) [6]
- low_prev5 = valuewhen ( AllBarsLowRsi5 , Rsi [6] , 0 ) [6]
- low_price5 = valuewhen ( AllBarsLowRsi5 , SrcLow5 , 0 ) [6]
- high_prev6 = valuewhen ( AllBarsTopRsi6 , Rsi [7] , 0 ) [7]
- high_price6 = valuewhen ( AllBarsTopRsi6 , SrcHigh6 , 0 ) [7]
- low_prev6 = valuewhen ( AllBarsLowRsi6 , Rsi [7] , 0 ) [7]
- low_price6 = valuewhen ( AllBarsLowRsi6 , SrcLow6 , 0 ) [7]
- high_prev7 = valuewhen ( AllBarsTopRsi7 , Rsi [8] , 0 ) [8]
- high_price7 = valuewhen ( AllBarsTopRsi7 , SrcHigh7 , 0 ) [8]
- low_prev7 = valuewhen ( AllBarsLowRsi7 , Rsi [8] , 0 ) [8]
- low_price7 = valuewhen ( AllBarsLowRsi7 , SrcLow7 , 0 ) [8]
- // Divergence Detection
- RegBearDiv1 = AllBarsTopRsi1 and SrcHigh1 > high_price1 and Rsi[2] < high_prev1
- HidBearDiv1 = AllBarsTopRsi1 and SrcHigh1 < high_price1 and Rsi[2] > high_prev1
- RegBullDiv1 = AllBarsLowRsi1 and SrcLow1 < low_price1 and Rsi[2] > low_prev1
- HidBullDiv1 = AllBarsLowRsi1 and SrcLow1 > low_price1 and Rsi[2] < low_prev1
- RegBearDiv2 = AllBarsTopRsi2 and SrcHigh2 > high_price2 and Rsi[3] < high_prev2
- HidBearDiv2 = AllBarsTopRsi2 and SrcHigh2 < high_price2 and Rsi[3] > high_prev2
- RegBullDiv2 = AllBarsLowRsi2 and SrcLow2 < low_price2 and Rsi[3] > low_prev2
- HidBullDiv2 = AllBarsLowRsi2 and SrcLow2 > low_price2 and Rsi[3] < low_prev2
- RegBearDiv3 = AllBarsTopRsi3 and SrcHigh3 > high_price3 and Rsi[4] < high_prev3
- HidBearDiv3 = AllBarsTopRsi3 and SrcHigh3 < high_price3 and Rsi[4] > high_prev3
- RegBullDiv3 = AllBarsLowRsi3 and SrcLow3 < low_price3 and Rsi[4] > low_prev3
- HidBullDiv3 = AllBarsLowRsi3 and SrcLow3 > low_price3 and Rsi[4] < low_prev3
- RegBearDiv4 = AllBarsTopRsi4 and SrcHigh4 > high_price4 and Rsi[5] < high_prev4
- HidBearDiv4 = AllBarsTopRsi4 and SrcHigh4 < high_price4 and Rsi[5] > high_prev4
- RegBullDiv4 = AllBarsLowRsi4 and SrcLow4 < low_price4 and Rsi[5] > low_prev4
- HidBullDiv4 = AllBarsLowRsi4 and SrcLow4 > low_price4 and Rsi[5] < low_prev4
- RegBearDiv5 = AllBarsTopRsi5 and SrcHigh5 > high_price5 and Rsi[6] < high_prev5
- HidBearDiv5 = AllBarsTopRsi5 and SrcHigh5 < high_price5 and Rsi[6] > high_prev5
- RegBullDiv5 = AllBarsLowRsi5 and SrcLow5 < low_price5 and Rsi[6] > low_prev5
- HidBullDiv5 = AllBarsLowRsi5 and SrcLow5 > low_price5 and Rsi[6] < low_prev5
- RegBearDiv6 = AllBarsTopRsi6 and SrcHigh6 > high_price6 and Rsi[7] < high_prev6
- HidBearDiv6 = AllBarsTopRsi6 and SrcHigh6 < high_price6 and Rsi[7] > high_prev6
- RegBullDiv6 = AllBarsLowRsi6 and SrcLow6 < low_price6 and Rsi[7] > low_prev6
- HidBullDiv6 = AllBarsLowRsi6 and SrcLow6 > low_price6 and Rsi[7] < low_prev6
- RegBearDiv7 = AllBarsTopRsi7 and SrcHigh7 > high_price7 and Rsi[8] < high_prev7
- HidBearDiv7 = AllBarsTopRsi7 and SrcHigh7 < high_price7 and Rsi[8] > high_prev7
- RegBullDiv7 = AllBarsLowRsi7 and SrcLow7 < low_price7 and Rsi[8] > low_prev7
- HidBullDiv7 = AllBarsLowRsi7 and SrcLow7 > low_price7 and Rsi[8] < low_prev7
- // Divergence Color Settings
- BearDiv1Color = RegBearDiv1 ? color.red : HidBearDiv1 ? color.orange : na
- BullDiv1Color = RegBullDiv1 ? color.lime : HidBullDiv1 ? color.blue : na
- BearDiv2Color = RegBearDiv2 ? color.red : HidBearDiv2 ? color.orange : na
- BullDiv2Color = RegBullDiv2 ? color.lime : HidBullDiv2 ? color.blue : na
- BearDiv3Color = RegBearDiv3 ? color.red : HidBearDiv3 ? color.orange : na
- BullDiv3Color = RegBullDiv3 ? color.lime : HidBullDiv3 ? color.blue : na
- BearDiv4Color = RegBearDiv4 ? color.red : HidBearDiv4 ? color.orange : na
- BullDiv4Color = RegBullDiv4 ? color.lime : HidBullDiv4 ? color.blue : na
- BearDiv5Color = RegBearDiv5 ? color.red : HidBearDiv5 ? color.orange : na
- BullDiv5Color = RegBullDiv5 ? color.lime : HidBullDiv5 ? color.blue : na
- BearDiv6Color = RegBearDiv6 ? color.red : HidBearDiv6 ? color.orange : na
- BullDiv6Color = RegBullDiv6 ? color.lime : HidBullDiv6 ? color.blue : na
- BearDiv7Color = RegBearDiv7 ? color.red : HidBearDiv7 ? color.orange : na
- BullDiv7Color = RegBullDiv7 ? color.lime : HidBullDiv7 ? color.blue : na
- // Divergence Plots
- DivSrc = close
- plot ( title = 'Bear Div 1A' , series = GuideLines and AllBarsTopRsi1 ? DivSrc[2] + OffSet1 : na, color=BearDiv1Color, linewidth = 4 , transp = 0 , offset = -2 , show_last = LookBackBars )
- plot ( title = 'Bull Div 1A' , series = GuideLines and AllBarsLowRsi1 ? DivSrc[2] - OffSet1 : na, color=BullDiv1Color, linewidth = 4 , transp = 0 , offset = -2 , show_last = LookBackBars )
- plot ( title = 'Bear Div 2A' , series = GuideLines and AllBarsTopRsi2 ? DivSrc[3] + OffSet1 : na, color=BearDiv2Color, linewidth = 4 , transp = 0 , offset = -3 , show_last = LookBackBars )
- plot ( title = 'Bull Div 2A' , series = GuideLines and AllBarsLowRsi2 ? DivSrc[3] - OffSet1 : na, color=BullDiv2Color, linewidth = 4 , transp = 0 , offset = -3 , show_last = LookBackBars )
- plot ( title = 'Bear Div 3A' , series = GuideLines and AllBarsTopRsi3 ? DivSrc[4] + OffSet1 : na, color=BearDiv3Color, linewidth = 4 , transp = 0 , offset = -4 , show_last = LookBackBars )
- plot ( title = 'Bull Div 3A' , series = GuideLines and AllBarsLowRsi3 ? DivSrc[4] - OffSet1 : na, color=BullDiv3Color, linewidth = 4 , transp = 0 , offset = -4 , show_last = LookBackBars )
- plot ( title = 'Bear Div 4A' , series = GuideLines and AllBarsTopRsi4 ? DivSrc[5] + OffSet1 : na, color=BearDiv4Color, linewidth = 4 , transp = 0 , offset = -5 , show_last = LookBackBars )
- plot ( title = 'Bull Div 4A' , series = GuideLines and AllBarsLowRsi4 ? DivSrc[5] - OffSet1 : na, color=BullDiv4Color, linewidth = 4 , transp = 0 , offset = -5 , show_last = LookBackBars )
- plot ( title = 'Bear Div 5A' , series = GuideLines and AllBarsTopRsi5 ? DivSrc[6] + OffSet1 : na, color=BearDiv5Color, linewidth = 4 , transp = 0 , offset = -6 , show_last = LookBackBars )
- plot ( title = 'Bull Div 5A' , series = GuideLines and AllBarsLowRsi5 ? DivSrc[6] - OffSet1 : na, color=BullDiv5Color, linewidth = 4 , transp = 0 , offset = -6 , show_last = LookBackBars )
- plot ( title = 'Bear Div 6A' , series = GuideLines and AllBarsTopRsi6 ? DivSrc[7] + OffSet1 : na, color=BearDiv6Color, linewidth = 4 , transp = 0 , offset = -7 , show_last = LookBackBars )
- plot ( title = 'Bull Div 6A' , series = GuideLines and AllBarsLowRsi6 ? DivSrc[7] - OffSet1 : na, color=BullDiv6Color, linewidth = 4 , transp = 0 , offset = -7 , show_last = LookBackBars )
- plot ( title = 'Bear Div 7A' , series = GuideLines and AllBarsTopRsi7 ? DivSrc[8] + OffSet1 : na, color=BearDiv7Color, linewidth = 4 , transp = 0 , offset = -8 , show_last = LookBackBars )
- plot ( title = 'Bull Div 7A' , series = GuideLines and AllBarsLowRsi7 ? DivSrc[8] - OffSet1 : na, color=BullDiv7Color, linewidth = 4 , transp = 0 , offset = -8 , show_last = LookBackBars )
- plotshape ( Labels ? (RegBearDiv1 or HidBearDiv1 ? close[2] + ArrowOffSet : na) : na , "Bear1", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv1Color, offset = -2)
- plotshape ( Labels ? (RegBearDiv2 or HidBearDiv2 ? close[3] + ArrowOffSet : na) : na , "Bear2", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv2Color, offset = -3)
- plotshape ( Labels ? (RegBearDiv3 or HidBearDiv3 ? close[4] + ArrowOffSet : na) : na , "Bear3", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv3Color, offset = -4)
- plotshape ( Labels ? (RegBearDiv4 or HidBearDiv4 ? close[5] + ArrowOffSet : na) : na , "Bear4", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv4Color, offset = -5)
- plotshape ( Labels ? (RegBearDiv5 or HidBearDiv5 ? close[6] + ArrowOffSet : na) : na , "Bear5", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv5Color, offset = -6)
- plotshape ( Labels ? (RegBearDiv6 or HidBearDiv6 ? close[7] + ArrowOffSet : na) : na , "Bear6", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv6Color, offset = -7)
- plotshape ( Labels ? (RegBearDiv7 or HidBearDiv7 ? close[8] + ArrowOffSet : na) : na , "Bear7", shape.triangledown, location.abovebar, size = size.tiny, color = BearDiv7Color, offset = -8)
- plotshape ( Labels ? (RegBullDiv1 or HidBullDiv1 ? close[2] - ArrowOffSet : na) : na , "Bull1", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv1Color, offset = -2)
- plotshape ( Labels ? (RegBullDiv2 or HidBullDiv2 ? close[3] - ArrowOffSet : na) : na , "Bull2", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv2Color, offset = -3)
- plotshape ( Labels ? (RegBullDiv3 or HidBullDiv3 ? close[4] - ArrowOffSet : na) : na , "Bull3", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv3Color, offset = -4)
- plotshape ( Labels ? (RegBullDiv4 or HidBullDiv4 ? close[5] - ArrowOffSet : na) : na , "Bull4", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv4Color, offset = -5)
- plotshape ( Labels ? (RegBullDiv5 or HidBullDiv5 ? close[6] - ArrowOffSet : na) : na , "Bull5", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv5Color, offset = -6)
- plotshape ( Labels ? (RegBullDiv6 or HidBullDiv6 ? close[7] - ArrowOffSet : na) : na , "Bull6", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv6Color, offset = -7)
- plotshape ( Labels ? (RegBullDiv7 or HidBullDiv7 ? close[8] - ArrowOffSet : na) : na , "Bull7", shape.triangleup, location.belowbar, size = size.tiny, color = BullDiv7Color, offset = -8)
- // End
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement