Volatility indicator based on ATR 
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © JuicyWolf

//@version=5
indicator("Volatility indicator", overlay=true)


////////////////////////////// INPUTS ///////////////////////// INPUTS ///////////////////////////////////// INPUTS //////////////////////////////
WEInput = input.bool(title="weekend overlay", defval=true)
TradingStyle = input.string(defval="normal", title="Change signals sensibility", options=["greedy","normal","safe"])
InputAtrBands = input.bool(title="atr bands", defval=true)



atr = ta.atr(14)



[middle, upper, lower] = ta.bb(close, 20, 2)

f_bb(src, length, mult) =>
    float basis = ta.sma(src, length)
    float dev = mult * ta.stdev(src, length)
    [basis, basis + dev, basis - dev]

[PineMiddle, PineUpper, PineLower] = f_bb(close, 20, 2)




tfDurationSeconds = (timeframe.multiplier*(timeframe.isminutes?60:timeframe.isseconds?1:timeframe.isdaily?(86400):timeframe.isweekly?(86400*7):(86400*30)))

nbCandlesPerDay = 60*60*24 / tfDurationSeconds

HighestAtr = ta.highest(atr, nbCandlesPerDay)
HighestAtr1 = ta.highest(atr[nbCandlesPerDay], nbCandlesPerDay)
HighestAtr2 = ta.highest(atr[nbCandlesPerDay*2], nbCandlesPerDay)
HighestAtr3 = ta.highest(atr[nbCandlesPerDay*3], nbCandlesPerDay)
HighestAtr4 = ta.highest(atr[nbCandlesPerDay*4], nbCandlesPerDay)
HighestAtr5 = ta.highest(atr[nbCandlesPerDay*5], nbCandlesPerDay)
HighestAtr6 = ta.highest(atr[nbCandlesPerDay*6], nbCandlesPerDay)
HighestAtr7 = ta.highest(atr[nbCandlesPerDay*7], nbCandlesPerDay)

LowestAtr = ta.lowest(atr, nbCandlesPerDay)
LowestAtr1 = ta.lowest(atr[nbCandlesPerDay], nbCandlesPerDay)
LowestAtr2 = ta.lowest(atr[nbCandlesPerDay*2], nbCandlesPerDay)
LowestAtr3 = ta.lowest(atr[nbCandlesPerDay*3], nbCandlesPerDay)
LowestAtr4 = ta.lowest(atr[nbCandlesPerDay*4], nbCandlesPerDay)
LowestAtr5 = ta.lowest(atr[nbCandlesPerDay*5], nbCandlesPerDay)
LowestAtr6 = ta.lowest(atr[nbCandlesPerDay*6], nbCandlesPerDay)
LowestAtr7 = ta.lowest(atr[nbCandlesPerDay*7], nbCandlesPerDay)




///////////////// HIGHEST ATR ///////////////////// HIGHEST ATR //////////////////////////// HIGHEST ATR /////////////////////////// HIGHEST ATR /////////////////////////

AverageAtrHigh = if dayofweek == dayofweek.monday
    math.avg(HighestAtr3, HighestAtr4, HighestAtr5, HighestAtr6, HighestAtr7)
else if dayofweek == dayofweek.tuesday
    math.avg(HighestAtr, HighestAtr3, HighestAtr4, HighestAtr5, HighestAtr6)
else if dayofweek == dayofweek.wednesday
    math.avg(HighestAtr, HighestAtr1, HighestAtr4, HighestAtr5, HighestAtr6)
else if dayofweek == dayofweek.thursday
    math.avg(HighestAtr, HighestAtr1, HighestAtr2, HighestAtr5, HighestAtr6)
else if dayofweek == dayofweek.friday
    math.avg(HighestAtr, HighestAtr1, HighestAtr2, HighestAtr3, HighestAtr6)
else if dayofweek == dayofweek.saturday or dayofweek == dayofweek.sunday
    0



//////////////////////// LOWEST ATR ////////////////////////////// LOWEST ATR //////////////////////////////////////// LOWEST ATR ////////////////////////////////////////

AverageAtrLow = if dayofweek == dayofweek.monday
    math.avg(LowestAtr3, LowestAtr4, LowestAtr5, LowestAtr6, LowestAtr7)
else if dayofweek == dayofweek.tuesday
    math.avg(LowestAtr, LowestAtr3, LowestAtr4, LowestAtr5, LowestAtr6)
else if dayofweek == dayofweek.wednesday
    math.avg(LowestAtr, LowestAtr1, LowestAtr4, LowestAtr5, LowestAtr6)
else if dayofweek == dayofweek.thursday
    math.avg(LowestAtr, LowestAtr1, LowestAtr2, LowestAtr5, LowestAtr6)
else if dayofweek == dayofweek.friday
    math.avg(LowestAtr, LowestAtr1, LowestAtr2, LowestAtr3, LowestAtr6)




AverageAtrHighStyle = if TradingStyle == "greedy"
    (AverageAtrHigh/10)*9
else if TradingStyle == "normal"
    (AverageAtrHigh/10)*10
else if TradingStyle == "safe"
    (AverageAtrHigh/10)*11



Time = time(timeframe.period, "0000-0000:23456")



OverVolat = if atr > AverageAtrHighStyle and Time and close > PineMiddle
    true
OverVolat1 = if atr > AverageAtrHighStyle and Time and close < PineMiddle
    true
UnderVolat = if atr < AverageAtrLow and Time and close > PineMiddle
    true
UnderVolat1 = if atr < AverageAtrLow and Time and close < PineMiddle
    true



Color = if dayofweek == 1 and WEInput == true
    color.new(color=color.aqua, transp=80)
else 
    color.new(color=color.aqua, transp=100)

Color1 = if dayofweek == 7 and WEInput == true
    color=color.new(color=color.aqua, transp=80)
else 
    color.new(color=color.aqua, transp=100)


bgcolor(color=Color)
bgcolor(color=Color1)



plot(InputAtrBands == true?ta.sma(AverageAtrHigh+close, 15):na)
plot(InputAtrBands == true?ta.sma(close - AverageAtrHigh, 15):na)
a = InputAtrBands == true?ta.sma(((AverageAtrHigh+close)+(close-AverageAtrHigh))/2, 15):na
plot(a, color=color.purple)
plot(AverageAtrHigh)


plotshape(series=OverVolat, style=shape.cross, color=color.red, text="", size=size.tiny, location=location.abovebar)
plotshape(series=OverVolat1, style=shape.cross, color=color.red, text="", size=size.tiny, location=location.belowbar)
plotshape(series=UnderVolat, style=shape.cross, color=color.green, text="", size=size.tiny, location=location.abovebar)
plotshape(series=UnderVolat1, style=shape.cross, color=color.green, text="", size=size.tiny, location=location.belowbar)

