#// © LazySprinter
#study("SPY Expected Move by VIX", overlay=true)
# converted and mod by Sam4Cok@Samer800 - 01/2023
input range = {default Day, Week}; # "Range (Day/Week)"
input ShowVwap = yes;
input IvSource = {Default hl2, hl3, ohlc4, close};
input src = hl2; # "Source"
input ManualIvSource = yes; # "Set Default to VIX for all charts"
input SelectIvSource = "VIX"; # "Manual IV Source"
input sensitivity = 99;
input Length = 25;
def na = Double.NaN;
script fixnan{
input source = close;
def fix = if !isNaN(source) then source else fix[1];
plot result = fix;
}
#--- Color
DefineGlobalColor("uc1" , CreateColor(137, 0, 0));
DefineGlobalColor("uc2" , CreateColor(196, 0, 0));
DefineGlobalColor("uc3" , CreateColor(235, 0, 0));
DefineGlobalColor("ufc1" , CreateColor(39, 0, 0));
DefineGlobalColor("ufc2" , CreateColor(59, 0, 0));
DefineGlobalColor("ufc3" , CreateColor(89, 0, 0));
#----
DefineGlobalColor("dc1" , CreateColor(0, 137, 0));
DefineGlobalColor("dc2" , CreateColor(0, 196, 0));
DefineGlobalColor("dc3" , CreateColor(0, 235, 0));
DefineGlobalColor("dfc1" , CreateColor(0, 39, 0));
DefineGlobalColor("dfc2" , CreateColor(0, 59, 0));
DefineGlobalColor("dfc3" , CreateColor(0, 89, 0));
def t = if range==range.DAY then GetDay() else GetWeek();
def start = isNaN(t[1]) or t > t[1];
def prd = FundamentalType.IMP_VOLATILITY;
def Source = if IvSource==IvSource.hl2 then FundamentalType.HL2 else
if IvSource==IvSource.hl3 then FundamentalType.HLC3 else
if IvSource==IvSource.ohlc4 then FundamentalType.OHLC4 else FundamentalType.CLOSE;
def ticker = if GetSymbol() == "AAPL" or GetSymbol() == "AMZN" or GetSymbol() == "IBM" or
GetSymbol() == "GOOG" or GetSymbol() == "GS" then 1 else 0;
def mansrc = SelectIvSource==GetSymbol();
def IVsrc = ticker or isNaN(Source) or mansrc;
def impVol = Fundamental(prd);
def srcIV = Fundamental(Source, SelectIvSource);
def cond = ManualIvSource and IVsrc;#if IVsrc then Fundamental(prd, SelectIvSource) else Fundamental(Source, SelectIvSource);
def IVselect = if cond then impVol else srcIV;
def IV = (src * (srcIV / 100) * sqrt(30/365)) / 30;
def sd = if (IVsrc or cond)then fixnan(impVol) else fixnan(IV);
#/ Dynamically shows the lines if price gets close to reduce the clutters
#show(source, l, input) =>
script show1 {
input source = hl2;
input l = hl2;
input sd = 0.25;
input sensitivity = 99;
input Length = 25;
def is_n;
is_n = fold i = 0 to Length with p=0 do
p or (AbsValue(l[i] - source[i]) < (sensitivity/100) * sd);
def near = fixnan(is_n);
def show = if near then l else Double.NaN;
plot return = show;
}
#// VWAP---
def sumSrc1 = src * volume;
def sumVol1 = volume;
def sumSrc = if start then sumSrc1 else sumSrc1 + (if (isNaN(sumSrc[1]) or sumSrc[1]==0) then sumSrc1 else sumSrc[1]);
def sumVol = if start then sumVol1 else sumVol1 + (if (isNaN(sumVol[1]) or sumVol[1]==0) then sumVol1 else sumVol[1]);
def vw1 = sumSrc / sumVol;
def vw = if start then na else vw1;
plot vwap = vw; # "VWAP"
vwap.SetHiding(!ShowVwap);
vwap.SetDefaultColor(Color.MAGENTA);
#-- VWAP Calc
def std1 = vw + sd;
def std_1 = vw - sd;
def std2 = vw + 2 * sd;
def std_2 = vw - 2 * sd;
def std3 = vw + 3 * sd;
def std_3 = vw - 3 * sd;
plot s1 = show1(src, std1 , sd, sensitivity, Length); # "+1 Std Dev"
s1.SetDefaultColor(GlobalColor("uc1"));
plot s_1 = show1(src, std_1, sd, sensitivity, Length); # "-1 Std Dev"
s_1.SetDefaultColor(GlobalColor("dc1"));
plot s2 = show1(src, std2 , sd, sensitivity, Length); # "+2 Std Dev"
s2.SetDefaultColor(GlobalColor("uc2"));
plot s_2 = show1(src, std_2, sd, sensitivity, Length); # "-2 Std Dev"
s_2.SetDefaultColor(GlobalColor("dc2"));
plot s3 = show1(src, std3 , sd, sensitivity, Length); # "+3 Std Dev"
s3.SetDefaultColor(GlobalColor("uc3"));
plot s_3 = show1(src, std_3, sd, sensitivity, Length); # "-3 Std Dev"
s_3.SetDefaultColor(GlobalColor("dc3"));
#---- Cloud
AddCloud(s1, vw, GlobalColor("ufc1"));
AddCloud(s2, s1, GlobalColor("ufc2"));
AddCloud(s3, s2, GlobalColor("ufc3"));
AddCloud(vw , s_1, GlobalColor("dfc1"));
AddCloud(s_1, s_2, GlobalColor("dfc2"));
AddCloud(s_2, s_3, GlobalColor("dfc3"));
#--- END CODE