AmitN STDEV V5
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © amitni82

//@version=5
indicator("AmitN STDEV V5", max_bars_back = 365,overlay = true)

lookbackPeriod = input.int(365, "Lookback period for STDEV")
numberOfDays = input.int(7, "Number of candles to find the range")
extendRange = input.bool(true, "Extend the range lines")

// Daily log returns
logClose = math.log(close / close[1])

// Average/Mean
smalogClose = ta.sma(logClose, lookbackPeriod)

// Daily Volatility SD
dailyVolatility = ta.stdev(logClose, lookbackPeriod)

// Spot CMP - Close of current candle
spotCmp = close // request.security(syminfo.tickerid, "D", close, barmerge.gaps_on , barmerge.lookahead_off)

// Custom deviations
averageMean = numberOfDays * smalogClose
volatilitySD = dailyVolatility * math.sqrt(numberOfDays)

// Price range calculations
minPriceX1SD = spotCmp * math.exp(averageMean - (1 * volatilitySD))
maxPriceX1SD = spotCmp * math.exp(averageMean + 1 * volatilitySD)
minPriceX2SD = spotCmp * math.exp(averageMean - (2 * volatilitySD))
maxPriceX2SD = spotCmp * math.exp(averageMean + (2 * volatilitySD))

var tbl = table.new(position.top_right, 5, 7, border_color = color.white, border_width = 1, frame_color = color.gray, frame_width = 2)

if barstate.islast

    line.new(bar_index, maxPriceX1SD, bar_index + (extendRange ? numberOfDays : 7), maxPriceX1SD, color= color.aqua, style = line.style_solid, width = 4)
    label.new(bar_index + 7, maxPriceX1SD, str.tostring(math.round_to_mintick(maxPriceX1SD)) + "  (" + str.tostring(math.round_to_mintick(maxPriceX1SD-close)) + " ~ " + str.tostring(math.round(((maxPriceX1SD /close) - 1) * 100, 2))+ "%)", textcolor = color.white, size = size.large)

    line.new(bar_index, minPriceX1SD, bar_index + (extendRange ? numberOfDays : 7), minPriceX1SD, color= color.aqua, style = line.style_solid, width = 4)
    label.new(bar_index + 7, minPriceX1SD, str.tostring(math.round_to_mintick(minPriceX1SD)) + "  (" + str.tostring(math.round_to_mintick(minPriceX1SD-close)) + " ~ " + str.tostring(math.round(((minPriceX1SD /close) - 1) * 100, 2))+ "%)", textcolor = color.white, size = size.large)
    
    line.new(bar_index, maxPriceX2SD, bar_index + (extendRange ? numberOfDays : 7), maxPriceX2SD, color= color.maroon, style = line.style_solid, width = 5)
    label.new(bar_index + 7, maxPriceX2SD, str.tostring(math.round_to_mintick(maxPriceX2SD)) + "  (" + str.tostring(math.round_to_mintick(maxPriceX2SD-close)) + " ~ " + str.tostring(math.round(((maxPriceX2SD /close) - 1) * 100, 2))+ "%)", textcolor = color.white, size = size.large)
    
    line.new(bar_index, minPriceX2SD, bar_index + (extendRange ? numberOfDays : 7), minPriceX2SD, color= color.maroon, style = line.style_solid, width = 5)
    label.new(bar_index + 7, minPriceX2SD, str.tostring(math.round_to_mintick(minPriceX2SD)) + "  (" + str.tostring(math.round_to_mintick(minPriceX2SD-close)) + " ~ " + str.tostring(math.round(((minPriceX2SD /close) - 1) * 100, 2))+ "%)", textcolor = color.white, size = size.large)
   
    table.cell(tbl, 0, 0, "Range for", text_color = color.white, bgcolor = color.rgb(91, 92, 95), width = 12, height = 4, text_size = size.large)
    table.cell(tbl, 0, 1, str.tostring(numberOfDays) + " candles", text_color = color.white, bgcolor = color.rgb(91, 92, 95), width = 7, height = 4, text_size = size.large)
    table.cell(tbl, 0, 2, str.tostring(numberOfDays) + " candles", text_color = color.white, bgcolor = color.rgb(91, 92, 95), width = 7, height = 4, text_size = size.large)

    table.cell(tbl, 1, 0, "StdDev", text_color = color.white, bgcolor = color.rgb(91, 92, 95), width = 7, height = 4, text_size = size.large)
    table.cell(tbl, 1, 1, "1", text_color = color.white, bgcolor = color.gray, width = 7, height = 4, text_size = size.auto)
    table.cell(tbl, 1, 2, "2", text_color = color.white, bgcolor = color.gray, width = 7, height = 4, text_size = size.auto)

    table.cell(tbl, 2, 0, "Min", text_color = color.white, bgcolor = color.rgb(91, 92, 95), width = 7, height = 4, text_size = size.large)
    table.cell(tbl, 2, 1, str.tostring(math.round_to_mintick(minPriceX1SD)), text_color = color.white, bgcolor = color.gray, width = 7, height = 4, text_size = size.auto)
    table.cell(tbl, 2, 2, str.tostring(math.round_to_mintick(minPriceX2SD)), text_color = color.white, bgcolor = color.gray, width = 7, height = 4, text_size = size.auto)

    table.cell(tbl, 3, 0, "Max", text_color = color.white, bgcolor = color.rgb(91, 92, 95), width = 7, height = 4, text_size = size.large)
    table.cell(tbl, 3, 1, str.tostring(math.round_to_mintick(maxPriceX1SD)), text_color = color.white, bgcolor = color.gray, width = 7, height = 4, text_size = size.auto)
    table.cell(tbl, 3, 2, str.tostring(math.round_to_mintick(maxPriceX2SD)), text_color = color.white, bgcolor = color.gray, width = 7, height = 4, text_size = size.auto)
