#// This source code is subject to the terms of the Mozilla Public License 2.0 at https://mozilla.org/MPL/2.0/
#// © robertsullivan1956
#indicator("SPX Expected Move by:", overlay=true)
# Converted and mod by by Sam4Cok@Samer800 - 03/2023
input volSymbol = {"VIX", "VIX9D", "VIXMO", "VIX3M", "VIX6M", "VIXY", "VIIX.IV", "UVXY", default "VOLI"};#("VOLI", "VOL Symbol", ])
input UseCustomTimeframe = no;
input CustomTimeframe = AggregationPeriod.FIVE_MIN;
input CalcMethod = {Default "Trading days in one year","Trading days in current year"};
input displayShading = yes; # "Display color between 1 STD and 2 STD?"
def na = Double.NaN;
def year = CalcMethod==CalcMethod."Trading days in one year";
def current = GetAggregationPeriod();
def tf = if UseCustomTimeframe then CustomTimeframe else current;
def volValue = open(volSymbol, Period = tf);
def minute =(RegularTradingEnd(GetYYYYMMDD()) - GetTime()) / AggregationPeriod.DAY;
def yearstart = if year then GetYYYYMMDD() - 10000 else GetYEAR() * 10000 + 101;
def tradingDaysInYear = CountTradingDays(yearstart, GetYYYYMMDD());
#// first calculate the fraction of the trading day remaining.
def frac = minute;
def timeRemaining = sqrt(frac) / sqrt(tradingDaysInYear);
def sd = close * (volValue/100) * timeRemaining;
def sd2Above = close + sd;
def sd1Above = close + (sd/2);
def sd2Below = close - sd;
def sd1Below = close - (sd/2);
plot p1 = sd1Above;#, "1SD Above"
plot p2 = sd2Above;#, "2SD Above"
plot p3 = sd1Below;#, "1SD Below"
plot p4 = sd2Below;#, "2SD Below"
p1.SetDefaultColor(Color.DARK_GREEN);
p2.SetDefaultColor(Color.DARK_GREEN);
p3.SetDefaultColor(Color.DARK_RED);
p4.SetDefaultColor(Color.DARK_RED);
AddCloud(if displayShading then p2 else na, p1, Color.DARK_GREEN);
AddCloud(if displayShading then p3 else na, p4, Color.DARK_RED);
#--- END CODE