Expected SPX Movement by timeframe
// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
// © rhvarelav

//THIS INDICATOR ONLY WORKS FOR SPX CHART

//This code will help you to measure the expected movement of SPX in a previous selected timeframe based on the current value of VIX index

//E.g. if the current value of VIX is 30 we calculate first the expected move of the Fwd TM.
// If you selected the Daily timeframe  it will calculate the expected move of SPX in the next Day by dividing the current VIX Value by the squared root of 252
// (THe 252 value corresponds to the approximate amount of trading sessions of the year)

// If you selected the Weekly timeframe  it will calculate the expected move of SPX in the next Week by dividing the current VIX Value by the squared root of 52
// (THe 52 value corresponds to the  amount of weeks of the year)

// If you selected the Monthly timeframe  it will calculate the expected move of SPX in the next Week by dividing the current VIX Value by the squared root of 12
// (THe 12 value corresponds to the  amount of months of the year)

//// For lower timeframes you have to calculate the amount of ticks in each trading session of the year in order to get that specific range

//Once you have that calculation it it'll provide the range expressed as percentage of the expected move for the following period.

//This script will plot that information in a range of 2 lines which represents the expected move of the SPX for the next period

//The red flag indicator tells if that period closed between the 2 previous values marked by the range


//@version=5

indicator(title="Expected SPX Movement by timeframe", overlay=true)


print(txt) =>
    // Create label on the first bar.
    var lbl = label.new(bar_index, na, txt, xloc.bar_index, yloc.price, color(na), label.style_none, color.gray, size.large, text.align_left)
    // On next bars, update the label's x and y position, and the text it displays.
    label.set_xy(lbl, bar_index, ta.highest(10)[1])
    label.set_text(lbl, txt)

//print("Period per timeframe: " +timeframe.period)
//print("Hello world!\n\n\n\n")

vixData = request.security("CBOE:VIX", timeframe.period, close)



scaleFreq = if timeframe.period == "D" 
    math.sqrt(252)
else if timeframe.period == "W"
    math.sqrt(52)
else if timeframe.period == "M"
    math.sqrt(12)
else if timeframe.period == "15"
    math.sqrt(6552) 
else if timeframe.period == "60"
    math.sqrt(1638) 
else if timeframe.period == "120"
    math.sqrt(819) 
else if timeframe.period == "240"
    math.sqrt(409) 
else
    math.sqrt(252) 



probPercMove = vixData / scaleFreq

changeUpper = close + (close * (probPercMove/100))
changeLower = close - (close * (probPercMove/100))


p1 = plot(changeUpper,'Probable Upper Level',#ffffff)
p2 = plot(changeLower,'Probable Lower Level',#ffffff)

fill(p1, p2, color=color.new(#ffffff, 90))

plotshape(not(close<= changeUpper[1] and close >= changeLower[1]), style=shape.flag, location=location.belowbar, color=color.rgb(188, 76, 76), size=size.small)
