#//@HariprasathGopal
#study("Spread Analysis")
# Converted by Sam4Cok@Samer800 - 02/2023
declare lower;
declare Zerobase;
input BackgroundColor = yes;
input ShowMovAvgLine = yes;
input MovAvgLength = 30;
input MovAvgType = AverageType.SIMPLE;
def na = Double.NaN;
def pos = Double.POSITIVE_INFINITY;
def neg = Double.NEGATIVE_INFINITY;
def spread = high - low;
def avg_spread = MovingAverage(MovAvgType, spread, MovAvgLength);
def Lev1 = (spread > 4 * avg_spread);
def Lev2 = (spread > 2 * avg_spread) and (spread < 4 * avg_spread);
def Lev3 = (spread < avg_spread * 0.5);
def Lev4 = (spread > avg_spread * 0.5) and (spread < avg_spread * 1.1);
def color = if Lev1 then 4 else
if Lev2 then 3 else
if Lev3 then 2 else
if Lev4 then 1 else 0;
def Line1 = if !BackgroundColor then na else 4 * avg_spread;
def Line2 = if !BackgroundColor then na else 2 * avg_spread;
def Line3 = if !BackgroundColor then na else 1.1 * avg_spread;
def Line4 = if !BackgroundColor then na else 0.5 * avg_spread;
AddCloud(pos , Line1, CreateColor(179,0,179));
AddCloud(Line1, Line2, CreateColor(179,0,0));
AddCloud(Line2, Line3, CreateColor(0,0,179));
AddCloud(Line3, Line4, CreateColor(0,179,0));
AddCloud(Line4, neg , CreateColor(179,179,0));
# Plot the new Chart
plot AvgSpread = if !ShowMovAvgLine then na else avg_spread;
AvgSpread.SetDefaultColor(Color.CYAN);
plot SpreadHist = spread;
SpreadHist.SetLineWeight(3);
SpreadHist.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
SpreadHist.AssignValueColor(if color==4 then Color.MAGENTA else
if color==3 then Color.RED else
if color==2 then Color.BLUE else
if color==1 then CreateColor(0,128,0) else Color.YELLOW);
#--- END CODE