Pullback and Throwback Candle [TrendX_]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © TrendX_

//@version=5
//      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                              xxxxxxxxxxxxxxxxxxxxxxxxxx
//              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                              xxxxxxxxxxxxxxxxxxxxxxxxxx
//                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX              xxxxxxxxxxxxxxxxxxxxxxxxxx
//                              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxx
//                                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxx
//                                          XXX.....X....X....X..XX:X...XX      xx 
//                                              X:XXX:..:X:...X:X.X:X:XX.XXXXX  
//                                          XXXXX:XXX:  .X:...X:XX..X:..XX      xx
//                                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxx
//                                  XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxx
//                              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX      xxxxxxxxxxxxxxxxxxxxxxxxxx
//                          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX              xxxxxxxxxxxxxxxxxxxxxxxxxx
//                      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                      xxxxxxxxxxxxxxxxxxxxxxxxxx
//              XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                  xxxxxxxxxxxxxxxxxxxxxxxxxx
//          XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                          xxxxxxxxxxxxxxxxxxxxxxxxxx
//      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX                                                  xxxxxxxxxxxxxxxxxxxxxxxxxx
//

indicator("Pullback and Throwback Candle [TrendX_]" ,overlay=true )



/// ____ RSI condition ____ ////

//Input
T_price       = input.source(defval = close, title = "Source of Price data"                                                  )
rsiLen        = input.int(   defval = 34   , title = "Length of RSI"        , group = "RSI setting"                          )
rsiType       = input.string(defval = "RMA", title = "Smoothing type of RSI", group = "RSI setting", options = ["RMA", "EMA"])
OB            = input.int(   defval = 75   , title ="RSI overbought"        , group = "RSI setting"                          )
OS            = input.int(   defval = 35   , title ="RSI oversold"          , group = "RSI setting"                          )

//Setting RSI function
rsiFunc(rsiType, srcRSI, rsiLen) =>
    op1       = ta.ema(srcRSI, rsiLen)
    op2       = ta.rma(srcRSI, rsiLen)
    rsiType  == "RMA" ? op2 : op1

//Define RSI
ups           = rsiFunc(rsiType, math.max(ta.change(hlc3) , 0), rsiLen)
lows          = rsiFunc(rsiType, -math.min(ta.change(hlc3), 0), rsiLen)
rsi           = lows == 0 ? 100 : ups == 0 ? 0 : 100 - (100 / (1 + ups / lows))

//Define Overbought and Oversold RSi logic
rsiOS         = rsi < OS and rsi
rsiOB         = rsi > OB and rsi

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



/// ____ Pullback and Throwback by TrendX ____ ////

//Input
PBcondition   = ta.lowestbars( low , 8) >= -1
TBcondition   = ta.highestbars(high, 8) >= -1

//Define Pullback (PB) and Throwback (TB)
PB            = (T_price[2] < T_price[3]) and (T_price[1] < T_price[2]) and (T_price > T_price[1]) and PBcondition
TB            = (T_price[2] > T_price[3]) and (T_price[1] > T_price[2]) and (T_price < T_price[1]) and TBcondition

//Plotting PB and TB zone
plotshape(     PB and rsiOS, style = shape.triangleup  , size = size.tiny, color = color.green, location = location.belowbar, display = display.none )
plotshape(     TB and rsiOB, style = shape.triangledown, size = size.tiny, color = color.red  , location = location.abovebar, display = display.none )
bgcolor(                                                                   color = (PB and rsiOS) ? color.rgb(50 , 205, 50, 80) : na                 )
bgcolor(                                                                   color = (TB and rsiOB) ? color.rgb(255,   0,  0, 70) : na                 )

//Alerts
alertcondition(PB and rsiOS, "PB", "PB at {{ticker}}")
alertcondition(TB and rsiOB, "TB", "TB at {{ticker}}")

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////