declare lower;
script smi {
input agg = AggregationPeriod.MIN;
input over_bought = 40.0;
input over_sold = -40.0;
input percentDLength = 3;
input percentKLength = 5;
def min_low = Lowest(low(period = agg), percentKLength);
def max_high = Highest(high(period = agg), percentKLength);
def rel_diff = close(period = agg) - (max_high + min_low) / 2;
def diff = max_high - min_low;
def avgrel = ExpAverage(ExpAverage(rel_diff, percentDLength), percentDLength);
def avgdiff = ExpAverage(ExpAverage(diff, percentDLength), percentDLength);
plot SMI = if avgdiff != 0 then avgrel / (avgdiff / 2) * 100 else 0;
plot AvgSMI = ExpAverage(SMI, percentDLength);
def overbought = over_bought;
def oversold = over_sold;
}
def smi1 = if smi() crosses above smi().AvgSMI
then 1
else if smi() crosses below smi().AvgSMI
then -1 else smi1[1];
plot s1 = if IsNaN(close) then Double.NaN else if smi1 then 1 else 1;
s1.SetPaintingStrategy(PaintingStrategy.POINTS);
s1.SetLineWeight(5);
s1.AssignValueColor(if smi1 == 1 then Color.GREEN else if smi1 == -1 then Color.RED else Color.GRAY);
s1.hidebubble();
def smi2 = if smi(agg = "THREE_MIN") crosses above smi(agg = "THREE_MIN").AvgSMI
then 1
else if smi(agg = "THREE_MIN") crosses below smi(agg = "THREE_MIN").AvgSMI
then -1 else smi2[1];
plot s2 = if IsNaN(close) then Double.NaN else if smi2 then 2 else 2;
s2.SetPaintingStrategy(PaintingStrategy.POINTS);
s2.SetLineWeight(5);
s2.AssignValueColor(if smi2 == 1 then Color.GREEN else if smi2 == -1 then Color.RED else Color.GRAY);
s2.hidebubble();
def smi3 = if smi(agg = "FIVE_MIN") crosses above smi(agg = "FIVE_MIN").AvgSMI
then 1
else if smi(agg = "FIVE_MIN") crosses below smi(agg = "FIVE_MIN").AvgSMI
then -1 else smi1[1];
plot s3 = if IsNaN(close) then Double.NaN else if smi3 then 3 else 3;
s3.SetPaintingStrategy(PaintingStrategy.POINTS);
s3.SetLineWeight(5);
s3.AssignValueColor(if smi3 == 1 then Color.GREEN else if smi3 == -1 then Color.RED else Color.GRAY);
s3.hidebubble();
def sum = if smi1 == 1 and smi2 == 1 and smi3 == 1
then 1
else if smi1 == -1 and smi2 == -1 and smi3 == -1
then -1 else sum[1];
plot line1 = if IsNaN(close) then Double.NaN else 4;
line1.SetPaintingStrategy(PaintingStrategy.DASHES);
line1.SetDefaultColor(Color.WHITE);
line1.HideBubble();
plot sum1 = if IsNaN(close) then Double.NaN else if sum then 5 else 5;
sum1.SetPaintingStrategy(PaintingStrategy.POINTS);
sum1.SetLineWeight(5);
sum1.AssignValueColor(if sum == 1 then Color.GREEN else if sum == -1 then Color.RED else Color.GRAY);
sum1.hidebubble();
plot top = if IsNaN(close) then Double.NaN else 6;
top.SetPaintingStrategy(PaintingStrategy.DASHES);
top.SetDefaultColor(Color.BLACK);
top.HideBubble();
plot bottom = if IsNaN(close) then Double.NaN else 0;
bottom.SetPaintingStrategy(PaintingStrategy.DASHES);
bottom.SetDefaultColor(Color.BLACK);
bottom.HideBubble();
input showbubbles = yes;
input bubblemover = 3;
def bm = bubblemover;
def bm1 = bm + 1;
AddChartBubble(showbubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]), s1[bm1], 1, Color.WHITE);
AddChartBubble(showbubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]), s2[bm1], 3, Color.WHITE);
AddChartBubble(showbubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]), s3[bm1], 5, Color.WHITE);
AddChartBubble(showbubbles and !IsNaN(close[bm1]) and IsNaN(close[bm]), sum1[bm1], "Sum", Color.WHITE);