# Choose how the candles are being colored (green/red) in Multiple user defined TimeFrames by one of 4 methods.
# Labels show current color status
# Trend method is similar but not exactly like TTM_Trend
# Move study from lower to upper and select "upper" at the input useupperlower to display dots on upper price panel. Uncheck at input screen "use left axis"
# Using the study on the lower panel, select "lower" at the input useupperlower to lower to display dots on lower panel
# Dots can be turned off/on at the input screen
# Summary line shows only if all colors are the same, otherwise blank.
declare lower;
input method = {default "HASmooth", "Open_Close", "Close_Close[1]", "HAOpen_HAClose", "Trend", "SuperTrend"};
input useupper_lower = {default upper, lower};
input showdotplot = yes;
input agg1 = AggregationPeriod.MIN;
input agg2 = AggregationPeriod.TWO_MIN;
input agg3 = AggregationPeriod.THREE_MIN;
input agg4 = AggregationPeriod.five_MIN;
#Heiken Ashi defined
def HAclose1 = ohlc4(period = agg1);
def HAopen1 = CompoundValue( 1, ( HAopen1[1] + HAclose1[1] ) / 2, HAclose1 );
def HAclose2 = ohlc4(period = agg2);
def HAopen2 = CompoundValue( 1, ( HAopen2[1] + HAclose2[1] ) / 2, HAclose2 );
def HAclose3 = ohlc4(period = agg3);
def HAopen3 = CompoundValue( 1, ( HAopen3[1] + HAclose3[1] ) / 2, HAclose3 );
def HAclose4 = ohlc4(period = agg4);
def HAopen4 = CompoundValue( 1, ( HAopen4[1] + HAclose4[1] ) / 2, HAclose4 );
#Trend defined
input trend_lookback = 6;
def HH1 = (Highest(high(period = agg1) , trend_lookback) + Lowest(low(period = agg1), trend_lookback)) / 2;
def Trend1 = close(period = agg1) > HH1;
def HH2 = (Highest(high(period = agg2) , trend_lookback) + Lowest(low(period = agg2), trend_lookback)) / 2;
def Trend2 = close(period = agg2) > HH2;
def HH3 = (Highest(high(period = agg3) , trend_lookback) + Lowest(low(period = agg3), trend_lookback)) / 2;
def Trend3 = close(period = agg3) > HH3;
def HH4 = (Highest(high(period = agg4) , trend_lookback) + Lowest(low(period = agg4), trend_lookback)) / 2;
def Trend4 = close(period = agg4) > HH4;
# SuperTrend - "Strategy" by Mobius
# Chat Room Request
# V03.10.2015
input AtrMult = 0.7;
input nATR = 4;
input AvgType = AverageType.HULL;
input price = FundamentalType.HL2;
#SuperTrend1
def ATR1 = MovingAverage(AvgType, TrueRange(high(period = agg1), close(period = agg1), low(period = agg1)), nATR);
def UP1 = Fundamental(price, period = agg1) + (AtrMult * ATR1);
def DN1 = Fundamental(price, period = agg1) + (-AtrMult * ATR1);
def ST1 = if close(period = agg1) < ST1[1]
then UP1
else DN1;
def T1 = if close(period = agg1) > ST1
then 1
else 0;
#SuperTrend2
def ATR2 = MovingAverage(AvgType, TrueRange(high(period = agg2), close(period = agg2), low(period = agg2)), nATR);
def UP2 = Fundamental(price, period = agg2) + (AtrMult * ATR2);
def DN2 = Fundamental(price, period = agg2) + (-AtrMult * ATR2);
def ST2 = if close(period = agg2) < ST2[1]
then UP2
else DN2;
def T2 = if close(period = agg2) > ST2 then 1 else 0;
#SuperTrend3
def ATR3 = MovingAverage(AvgType, TrueRange(high(period = agg3), close(period = agg3), low(period = agg3)), nATR);
def UP3 = Fundamental(price, period = agg3) + (AtrMult * ATR3);
def DN3 = Fundamental(price, period = agg3) + (-AtrMult * ATR3);
def ST3 = if close(period = agg3) < ST3[1]
then UP3
else DN3;
def T3 = if close(period = agg3) > ST3
then 1
else 0;
#SuperTrend4
def ATR4 = MovingAverage(AvgType, TrueRange(high(period = agg4), close(period = agg4), low(period = agg4)), nATR);
def UP4 = Fundamental(price, period = agg4) + (AtrMult * ATR4);
def DN4 = Fundamental(price, period = agg4) + (-AtrMult * ATR4);
def ST4 = if close(period=agg4) < ST4[1]
then UP4
else DN4;
def T4 = if close(period = agg4) > ST4
then 1
else 0;
# HAsmoothed:
input avgha = averageType.simple;
input lenha = 10;
def o1 = MovingAverage(avgha, HAopen1, lenha);
def c1 = MovingAverage(avgha, HAclose1, lenha);
def o2 = MovingAverage(avgha, HAopen2, lenha);
def c2 = MovingAverage(avgha, HAclose2, lenha);
def o3 = MovingAverage(avgha, HAopen3, lenha);
def c3 = MovingAverage(avgha, HAclose3, lenha);
def o4 = MovingAverage(avgha, HAopen4, lenha);
def c4 = MovingAverage(avgha, HAclose4, lenha);
#Method applied to each aggregation
def Agg1per;
def Agg2per;
def Agg3per;
def Agg4per;
plot Agg1lower;
plot Agg2lower;
plot Agg3lower;
plot Agg4lower;
if method == method."Close_Close[1]" {
Agg1per= close(period = agg1) > close(period = agg1)[1];
Agg2per= close(period = agg2) > close(period = agg2)[1];
Agg3per= close(period = agg3) > close(period = agg3)[1];
Agg4per= close(period = agg4) > close(period = agg4)[1];
}else if method == method."HAOpen_HAClose" {
Agg1per= HAclose1 > HAopen1;
Agg2per= HAclose2 > HAopen2;
Agg3per= HAclose3 > HAopen3;
Agg4per= HAclose4 > HAopen4;
}else if method == method."HASmooth" {
Agg1per= c1 > o1;
Agg2per= c2 > o2;
Agg3per= c3 > o3;
Agg4per= c4 > o4;
}else if method == method."Trend" {
Agg1per= Trend1;
Agg2per= Trend2;
Agg3per= Trend3;
Agg4per= Trend4;
}else if method == method."SuperTrend" {
Agg1per= T1;
Agg2per= T2;
Agg3per= T3;
Agg4per= T4;
}else {
Agg1per= close(period = agg1) > open(period = agg1);
Agg2per= close(period = agg2) > open(period = agg2);
Agg3per= close(period = agg3) > open(period = agg3);
Agg4per= close(period = agg4) > open(period = agg4);
}
#Labels with selection to turn them on/off
input showlabels = yes;
AddLabel(showlabels,
if agg1 / 60000 <= 240
then (agg1 / 60000 + " Min")
else if agg1 / 60000 == 1440
then "Day"
else if agg1 / 60000 == 10080
then "Week"
else if agg1 / 60000 == 43200
then "Month"
else " ",
if Agg1per == 1
then Color.GREEN
else Color.RED);
AddLabel(showlabels,
if agg2 / 60000 <= 240
then (agg2 / 60000 + " Min")
else if agg2 / 60000 == 1440
then "Day"
else if agg2 / 60000 == 10080
then "Week"
else if agg2 / 60000 == 43200
then "Month"
else " ",
if Agg2per == 1
then Color.GREEN
else Color.RED);
AddLabel(showlabels,
if agg3 / 60000 <= 240
then (agg3 / 60000 + " Min")
else if agg3 / 60000 == 1440
then "Day"
else if agg3 / 60000 == 10080
then "Week"
else if agg3 / 60000 == 43200
then "Month"
else " ",
if Agg3per == 1
then Color.GREEN
else Color.RED);
AddLabel(showlabels,
if agg4 / 60000 <= 240
then (agg4 / 60000 + " Min")
else if agg4 / 60000 == 1440
then "Day"
else if agg4 / 60000 == 10080
then "Week"
else if agg4 / 60000 == 43200
then "Month"
else " ",
if Agg4per == 1
then Color.GREEN
else Color.RED);
# Label identifying method and color green/red if all green/red otherwise white
def aggsum = Agg1per + Agg2per + Agg3per + Agg4per;
AddLabel(showlabels,
if method == method."Close_Close[1]"
then "C_C[1]"
else if method == method."HAOpen_HAClose"
then "HAC_HAO"
else if method == method."HASmooth"
then "HASmooth"
else if method == method."Trend"
then "Trend"
else if method == method."SuperTrend"
then "ST"
else "C_O" ,
if aggsum >= 3
then Color.GREEN
else if aggsum <= 1
then Color.RED
else Color.WHITE);
plot lineh = if IsNaN(close) or showdotplot == no
then Double.NaN
else if useupper_lower == useupper_lower.upper then 7 else Double.NaN;
plot linel = if IsNaN(close) or showdotplot == no
then Double.NaN
else if useupper_lower == useupper_lower.upper then -7 else Double.NaN;
lineh.SetDefaultColor(Color.DARK_GRAY);
linel.SetDefaultColor(Color.DARK_GRAY);
if IsNaN(close) or showdotplot == no {
agg1lower = Double.NaN;
agg2lower = Double.nan;
agg3lower = Double.nan;
agg4lower = Double.nan;
} else {
agg1lower = if !IsNaN(Agg1per) then if useupper_lower == useupper_lower.upper then -8.75 else 6 else Double.NaN;
agg2lower = if !IsNaN(Agg2per) then if useupper_lower == useupper_lower.upper then -9.00 else 5 else Double.NaN;
agg3lower = if !IsNaN(Agg3per) then if useupper_lower == useupper_lower.upper then -9.25 else 4 else Double.NaN;
agg4lower = if !IsNaN(Agg4per) then if useupper_lower == useupper_lower.upper then -9.50 else 3 else Double.NaN;
}
agg1lower.SetPaintingStrategy(PaintingStrategy.POINTS);
agg1lower.AssignValueColor(if Agg1per == 1
then Color.GREEN
else Color.RED);
agg2lower.SetPaintingStrategy(PaintingStrategy.POINTS);
agg2lower.AssignValueColor(if Agg2per == 1
then Color.GREEN
else Color.RED);
agg3lower.SetPaintingStrategy(PaintingStrategy.POINTS);
agg3lower.AssignValueColor(if Agg3per == 1
then Color.GREEN
else Color.RED);
agg4lower.SetPaintingStrategy(PaintingStrategy.POINTS);
agg4lower.AssignValueColor(if Agg4per == 1
then Color.GREEN
else Color.RED);
plot linediv = if IsNaN(close) or showdotplot == no
then Double.NaN
else if useupper_lower == useupper_lower.upper then -8.5 else 6.25;
plot sumagg = if IsNaN(close) or showdotplot == no
then Double.NaN
else if aggsum >= 3 or aggsum <= 1 then if useupper_lower == useupper_lower.upper then -8.25 else 6.5 else Double.NaN;
sumagg.SetPaintingStrategy(PaintingStrategy.POINTS);
sumagg.AssignValueColor(if aggsum >= 4
then Color.GREEN
else if aggsum <= 0
then Color.RED
else Color.WHITE);
input showcloud = yes;
def cloudcolor = if aggsum == 4 then 1 else if cloudcolor[1] == 1 and aggsum !=0 then 1 else 0;
def ccolor = cloudcolor;
AddCloud(if showcloud and ccolor == 1 then Double.POSITIVE_INFINITY else Double.NaN, Double.NEGATIVE_INFINITY, Color.LIGHT_GREEN, Color.LIGHT_GREEN);
AddCloud(if showcloud and ccolor == 0 then Double.POSITIVE_INFINITY else Double.NaN, Double.NEGATIVE_INFINITY, Color.LIGHT_RED, color2 = Color.LIGHT_RED);
#Squeeze Added
input showsqueeze = yes;
def bbupper = reference BollingerBands(length = 20, "num dev dn" = -2.0, "num dev up" = 2.0).UpperBand;
def kcupper = KeltnerChannels(length = 20, factor = 1.5).Upper_Band;
def Squeeze = bbupper - kcupper < 0;
plot Squeezeplot = if showsqueeze and Squeeze then linediv else Double.NaN;
Squeezeplot.SetDefaultColor(Color.black);
Squeezeplot.SetPaintingStrategy(PaintingStrategy.POINTS);
Squeezeplot.SetLineWeight(1);
Squeezeplot.HideBubble();
#Count of Periods in consecutive squeeze
rec countsq = if Squeeze then countsq[1] + 1 else 0;
rec count1sq = if !Squeeze then count1sq[1] + 1 else 0;
#Expansion Bubbles
input n = 5;
def n1 = n + 1;
def c = close;
input showsqueezebubble = yes;
AddChartBubble(showsqueezebubble and !IsNaN(c[n1]) and IsNaN(c[n]),
linediv[n1] ,
( if Squeeze[n1] then "S \n"+Round(countsq[n1], 2) else "NS \n" + count1sq[n1]) ,
if aggsum[n1]>=3
then color.green
else if aggsum[n1]<=1
then color.red
else color.white);
input usealerts = no;
Alert(usealerts and between(aggsum[1],0,2) and aggsum>=3 and !squeeze, "Candles - All Green", Alert.Bar, Sound.Ding);
Alert(usealerts and between(aggsum[1],1,3) and aggsum<=1 and !squeeze, "Candles - All Red ", Alert.Bar, Sound.Ding);