Range Detector Indicator [Misu]
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// Author : @Misu
// @version=5
indicator("Range Detector Indicator [Misu]", shorttitle="Range DI [Misu]", overlay=true)

import Fontiramisu/fontilab/12 as fontilab
// import Fontiramisu/fontLib/87 as fontilab 


// ] —————— Input Vars —————— [
// Get user input
var devTooltip              = "Deviation is a multiplier that affects how much the price should deviate from the previous pivot in order for the bar to become a new pivot."
var depthTooltip            = "The minimum number of bars that will be taken into account when analyzing pivots."
src                         = input.source(close, "Source", group="Settings")
thresholdMultiplier         = input.float(title="Deviation", defval=2.5, minval=0, tooltip=devTooltip, group="Pivot Settings")
depth                       = input.int(title="Depth", defval=50, minval=1, tooltip=depthTooltip, group="Pivot Settings")
isRange                     = input.bool(true, "Activate Range Detection", group = "Range Settings")
rPerOffset                  = input.float(title="Bands % Offset", defval=0.5, step=0.1, group="Trend Settings")
colorbars                   = input.bool(true, "Color Bars", group = "UI Settings")
upperBShow                  = input.bool(true, "Upper Band", group = "UI Settings")
lowerBShow                  = input.bool(true, "Lower Band", group = "UI Settings")
midBShow                    = input.bool(false, "Mid Band", group = "UI Settings")

// ] —————— Find Dev Pivots —————— [
// Prepare pivot variables
var line lineLast           = na
var int iLast               = 0 // Index last
var int iPrev               = 0 // Index previous
var float pLast             = 0 // Price last
var float pLastHigh         = 0 // Price last
var float pLastLow          = 0 // Price last
var isHighLast              = false // If false then the last pivot was a pivot low 
isPivotFound                = false

// Get pivot information from dev pivot finding function
[dupLineLast, dupIsHighLast, dupIPrev, dupILast, dupPLast, dupPLastHigh, dupPLastLow] = 
     fontilab.getDeviationPivots(thresholdMultiplier, depth, lineLast, isHighLast, iLast, pLast, true, close, high, low)


if not na(dupIsHighLast)
    lineLast        := dupLineLast
    isHighLast      := dupIsHighLast
    iPrev           := dupIPrev
    iLast           := dupILast
    pLast           := dupPLast
    pLastHigh       := dupPLastHigh
    pLastLow        := dupPLastLow
    isPivotFound    := true
    

// ] —————— Find Trend —————— [
var tDirUp      = true
var pDirStrike  = 0
var upB         = close
var lowB        = close
var midB        = close
var nStrike     = isRange ? 2 : 1


[midBDup, upBDup, lowBDup, pDirStrikeDup, tDirUpDup] = fontilab.getInterTrend(src, upB, lowB, pLast, tDirUp, pDirStrike, isPivotFound, isHighLast, nStrike, rPerOffset, depth)

pDirStrike  := nz(pDirStrikeDup)
tDirUp      := nz(tDirUpDup)
upB         := nz(upBDup)
lowB        := nz(lowBDup)
midB        := nz(midBDup)

var stateITB    = 0
stateITB        := pDirStrike < nStrike ? 0 : not tDirUp ? -1 : tDirUp == 1 ? 1 : nz(stateITB[1])
isTurnGreenITB  = (stateITB[1] == 0 or stateITB[1] == -1) and stateITB == 1
isTurnRedITB    = (stateITB[1] == 0 or stateITB[1] == 1) and stateITB == -1
isTurnOrangeITB = (stateITB[1] == 1 or stateITB[1] == -1) and stateITB == 0

// ] —————— Plot —————— [

var color colorTrend = na
colorTrend := pDirStrike < nStrike ? color.orange : not tDirUp ? color.red : tDirUp == 1 ? color.green : nz(colorTrend[1])

barcolor(colorbars ? colorTrend : na)

plot(upperBShow ? upB : na, "Upper Band", color = colorTrend, linewidth = 2)
plot(lowerBShow ? lowB : na, "Lower Band", color = colorTrend, linewidth = 2)
plot(midBShow ? midB : na, "Mid Band", color = colorTrend, linewidth = 2)


if isTurnGreenITB
    label.new(x = bar_index, y = low - (ta.atr(30) * 0.3), xloc = xloc.bar_index, text = "L", style = label.style_label_up, color = color.green, size = size.small, textcolor = color.white, textalign = text.align_center)
else if isTurnRedITB
    label.new(x = bar_index, y = high + (ta.atr(30) * 0.3), xloc = xloc.bar_index, text = "S", style = label.style_label_down, color = color.red, size = size.small, textcolor = color.white, textalign = text.align_center)
else if isTurnOrangeITB
    label.new(x = bar_index, y = high + (ta.atr(30) * 0.3), xloc = xloc.bar_index, text = "R", style = label.style_label_down, color = color.orange, size = size.small, textcolor = color.white, textalign = text.align_center)

// ] —————— Alerts —————— [
alertcondition(isTurnGreenITB, title = "Long", message = "Pivot Trend Bands [Misu]: Long\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(isTurnRedITB, title = "Short", message = "Pivot Trend Bands [Misu]: Short\nSymbol: {{ticker}}\nPrice: {{close}}")
alertcondition(isTurnOrangeITB, title = "Range", message = "Pivot Trend Bands [Misu]: Range\nSymbol: {{ticker}}\nPrice: {{close}}")

// ]
