Stacked Moving Averages For ThinkOrSwim

rad14733

Options Expert
VIP
Lifetime
Here is my EMA_Stack... Watch it and see if it will get you most of the way to where you want to be...
Ruby:
def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";


def stackedDn = MovAvgExponential("length" = 8)."AvgExp" is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is less than MovAvgExponential("length" = 89)."AvgExp";


AddLabel(yes, " Stacked ", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
 
Last edited by a moderator:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Can the script also me modified to display StackUp and StackDown watchlist display column?

Here is my actual Watchlist Column code... It's almost identical to the code above... I just use colors without text because I need the screen real estate for other things... Green = StackedUp, Red = StackedDown, Gray = NotStacked...

Ruby:
#EMA_Stack
#Used to indicate that 8, 21, 34, 55, 89  EMA's are ALL stacked trend-wise
#Created by rad14733 for usethinkscript.com
#v1.0 2021-01-02

def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";


def stackedDn = MovAvgExponential("length" = 8)."AvgExp is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is less than MovAvgExponential("length" = 89)."AvgExp";

def state = if stackedUp then 1 else if stackedDn then -1 else 0;

AddLabel(yes, state, if stackedUp then Color.DARK_GREEN else if stackedDn then Color.DARK_RED else Color.DARK_GRAY);

AssignBackgroundColor(if stackedUp then Color.DARK_GREEN else if stackedDn then Color.DARK_RED else Color.DARK_GRAY);
 
add the following code as a custom study in your scan. This works and provides you with what you're looking for the EMA's stacked.

Code:
#EMA_Stack Label
#cabe1332

# start of code used for scanning

def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";

def state = if stackedUp then 1 else 0;
plot data = state;

# end of code


#EMA_Stack Label
#cabe1332

# start of code
def stackedUp = MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";

def stackedDn = MovAvgExponential("length" = 8)."AvgExp" is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 55)."AvgExp"
and MovAvgExponential("length" = 55)."AvgExp" is less than MovAvgExponential("length" = 89)."AvgExp";

def state = if stackedUp then 1 else if stackedDn then -1 else 0;

AddLabel(yes, "EMA Stacks", if stackedUp then Color.GREEN else Color.RED);

#end of code

I hope that helps. Good luck!
 
Last edited by a moderator:
stacked SMA scanner

Code:
def SMA8 = SimpleMovingAvg(close,8);
def SMA13 = SimpleMovingAvg(close,13);
def SMA21 = SimpleMovingAvg(close,21);
def SMA34 = SimpleMovingAvg(close,34);
def SMA55 = SimpleMovingAvg(close,55);
def SMA89 = SimpleMovingAvg(close,89);

def bullish = SMA8 > SMA13 and SMA13 > SMA21 and SMA21 > SMA34 and SMA34 > SMA55 and SMA55 > SMA89;

input price = close;
input length = 20;
input AverageType = averageType.SIMPLE;
input percentdiff = 10.0;
input choice = {default "Above","Below"};
def avg = movingAverage(averageType,price,length);

plot scan;
switch (Choice){
case "Above":
    scan = price >= avg * (1 + percentdiff / 100 );
case "Below":
    scan = price <= avg * (1 -  percentdiff / 100 );
}
 
Last edited by a moderator:
I think I use something similar to this but as a scan filter. I just added the following to a custom study filter so I'm only looking at results with stacked EMA's. Obv this could be done with SMA as well and honestly not sure if this is even the best way to do it. Sorry and ignore if I'm confused, but hope this helps somebody.
Code:
MovAvgExponential("length" = 8)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp" and MovAvgExponential("length" = 21)."AvgExp" > MovAvgExponential("length" = 34)."AvgExp" and MovAvgExponential("length" = 34)."AvgExp" > MovAvgExponential("length" = 55)."AvgExp" and MovAvgExponential("length" = 55)."AvgExp" > MovAvgExponential("length" = 89)."AvgExp"
 
here is the code for a more responsive label
Code:
declare lower;
input period = AggregationPeriod.DAY;

DefineGlobalColor("Long", Color.Green);
DefineGlobalColor("Short", Color.RED);
DefineGlobalColor("Neutral", Color.Yellow);
DefineGlobalColor("arrow Buy", Color.Cyan);
DefineGlobalColor("arrow Sell", Color.Orange);

Script SymbolHK4emaRank{
input period = AggregationPeriod.DAY;
def OP = Open(period= period);
def CP = Close(Period= period);
Def Price = (OP + CP)/2;
input averageType = AverageType.Exponential;
def D1 = MovingAverage(AveragetYPE,Price,3);
def D2 = MovingAverage(AverageType,price,6);
def D3 = MovingAverage(AverageType,price,9);
def D4 = MovingAverage(AverageType,price,12);
def W1 = MovingAverage(AveragetYPE,Price,15);
def W2 = MovingAverage(AverageType,price,30);
def W3 = MovingAverage(AverageType,price,45);
def W4 = MovingAverage(AverageType,price,60);

def DCondition1 = If(D1 > D2 and D2 > D3 and D3 > D4, 5,
 If((D1 > D2 and D2 > D4 and D4 > D3) or (D1 > D3 and D3 > D2 and D2 > D4) or
(D2 > D1 and D1 > D3 and D3 > D4), 4, If(D2 > D1 and D1 > D4 and D4 > D3, 3,
If((D1 > D3 and D3 > D4 and D4 > D2) or ( D1 > D4 and D4 > D2 and D2 > D3)  or
(D2 > D3 and D3 > D1 and D1 > D4)  or (D3 > D1 and D1 > D2 and D2 > D4) , 2,
If((D1 > D4 and D4 > D3 and D3 > D2) or (D3 > D2 and D2 > D1 and D1 > D4), 1, 0)))));
def DCondition2 = If(D4 > D3 and D3 > D2 and D2 > D1, -5,
If((D3 > D4 and D4 > D2 and D2 > D1) or (D4 > D2 and D2 > D3 and D3 > D1) or
(D4 > D3 and D3 > D1 and D1 > D2), -4, If(D3 > D4 and D4 > D1 and D1 > D2, -3,
If((D2 > D4 and D4 > D3 and D3 > D1) or (D3 > D2 and D2 > D4 and D4 > D1)  or
(D4 > D1 and D1 > D3 and D3 > D2)  or (D4 > D2 and D2 > D1 and D1 > D3) , -2,
If((D2 > D3 and D3 > D4 and D4 > D1) or (D4 > D1 and D1 > D2 and D2 > D3), -1, 0)))));
def WCondition1 = If(W1 > W2 and W2 > W3 and W3 > W4, 50,
 If((W1 > W2 and W2 > W4 and W4 > W3) or (W1 > W3 and W3 > W2 and W2 > W4) or
(W2 > W1 and W1 > W3 and W3 > W4), 40, If(W2 > W1 and W1 > W4 and W4 > W3, 30,
If((W1 > W3 and W3 > W4 and W4 > W2) or ( W1 > W4 and W4 > W2 and W2 > W3)  or
(W2 > W3 and W3 > W1 and W1 > W4)  or (W3 > W1 and W1 > W2 and W2 > W4) , 20,
If((W1 > W4 and W4 > W3 and W3 > W2) or (W3 > W2 and W2 > W1 and W1 > W4), 10, 0)))));
def WCondition2 = If(W4 > W3 and W3 > W2 and W2 > W1, -50,
If((W3 > W4 and W4 > W2 and W2 > W1) or (W4 > W2 and W2 > W3 and W3 > W1) or
(W4 > W3 and W3 > W1 and W1 > W2), -40, If(W3 > W4 and W4 > W1 and W1 > W2, -30,
If((W2 > W4 and W4 > W3 and W3 > W1) or (W3 > W2 and W2 > W4 and W4 > W1)  or
(W4 > W1 and W1 > W3 and W3 > W2)  or (W4 > W2 and W2 > W1 and W1 > W3) , -20,
If((W2 > W3 and W3 > W4 and W4 > W1) or (W4 > W1 and W1 > W2 and W2 > W3), -10, 0)))));
def WTrend = (WCondition1+WCondition2);
def TTrend = (WCondition1+WCondition2+ DCondition1+DCondition2);
Def Buy = TTrend > WTrend;
Def Sell = TTrend < WTrend;
plot result = if buy then 1 else if sell then -1 else 0;
}
Script SymbolHKarrows{
input period = AggregationPeriod.DAY;
def OP = Open(period= period);
def CP = Close(Period= period);
Def Price = (OP + CP)/2;
input averageType = AverageType.Exponential;
def D1 = MovingAverage(AveragetYPE,Price,3);
def D2 = MovingAverage(AverageType,price,6);
def D3 = MovingAverage(AverageType,price,9);
def D4 = MovingAverage(AverageType,price,12);
def W1 = MovingAverage(AveragetYPE,Price,15);
def W2 = MovingAverage(AverageType,price,30);
def W3 = MovingAverage(AverageType,price,45);
def W4 = MovingAverage(AverageType,price,60);

def DCondition1 = If(D1 > D2 and D2 > D3 and D3 > D4, 5,
 If((D1 > D2 and D2 > D4 and D4 > D3) or (D1 > D3 and D3 > D2 and D2 > D4) or
(D2 > D1 and D1 > D3 and D3 > D4), 4, If(D2 > D1 and D1 > D4 and D4 > D3, 3,
If((D1 > D3 and D3 > D4 and D4 > D2) or ( D1 > D4 and D4 > D2 and D2 > D3)  or
(D2 > D3 and D3 > D1 and D1 > D4)  or (D3 > D1 and D1 > D2 and D2 > D4) , 2,
If((D1 > D4 and D4 > D3 and D3 > D2) or (D3 > D2 and D2 > D1 and D1 > D4), 1, 0)))));
def DCondition2 = If(D4 > D3 and D3 > D2 and D2 > D1, -5,
If((D3 > D4 and D4 > D2 and D2 > D1) or (D4 > D2 and D2 > D3 and D3 > D1) or
(D4 > D3 and D3 > D1 and D1 > D2), -4, If(D3 > D4 and D4 > D1 and D1 > D2, -3,
If((D2 > D4 and D4 > D3 and D3 > D1) or (D3 > D2 and D2 > D4 and D4 > D1)  or
(D4 > D1 and D1 > D3 and D3 > D2)  or (D4 > D2 and D2 > D1 and D1 > D3) , -2,
If((D2 > D3 and D3 > D4 and D4 > D1) or (D4 > D1 and D1 > D2 and D2 > D3), -1, 0)))));
def WCondition1 = If(W1 > W2 and W2 > W3 and W3 > W4, 50,
 If((W1 > W2 and W2 > W4 and W4 > W3) or (W1 > W3 and W3 > W2 and W2 > W4) or
(W2 > W1 and W1 > W3 and W3 > W4), 40, If(W2 > W1 and W1 > W4 and W4 > W3, 30,
If((W1 > W3 and W3 > W4 and W4 > W2) or ( W1 > W4 and W4 > W2 and W2 > W3)  or
(W2 > W3 and W3 > W1 and W1 > W4)  or (W3 > W1 and W1 > W2 and W2 > W4) , 20,
If((W1 > W4 and W4 > W3 and W3 > W2) or (W3 > W2 and W2 > W1 and W1 > W4), 10, 0)))));
def WCondition2 = If(W4 > W3 and W3 > W2 and W2 > W1, -50,
If((W3 > W4 and W4 > W2 and W2 > W1) or (W4 > W2 and W2 > W3 and W3 > W1) or
(W4 > W3 and W3 > W1 and W1 > W2), -40, If(W3 > W4 and W4 > W1 and W1 > W2, -30,
If((W2 > W4 and W4 > W3 and W3 > W1) or (W3 > W2 and W2 > W4 and W4 > W1)  or
(W4 > W1 and W1 > W3 and W3 > W2)  or (W4 > W2 and W2 > W1 and W1 > W3) , -20,
If((W2 > W3 and W3 > W4 and W4 > W1) or (W4 > W1 and W1 > W2 and W2 > W3), -10, 0)))));
def WTrend = (WCondition1+WCondition2);
def TTrend = (WCondition1+WCondition2+ DCondition1+DCondition2);
Def Buy = TTrend > WTrend;
Def Sell = TTrend < WTrend;
Def arrowup = TTrend crosses above WTrend;
Def arrowdown = TTrend crosses Below WTrend;

plot result = if arrowup then 1 else if arrowdown then -1 else 0;
}
def currentPeriod = GetAggregationPeriod();
def s1;
def h1;

if period >= currentPeriod {
    s1 = SymbolHK4emaRank(period = period );
    h1 = SymbolHKArrows(period = period );
} else {
    s1 = Double.NaN;
    h1 = DOuble.Nan;
}

AddLabel(!IsNaN(s1), "4mR:" + (if period == AggregationPeriod.MONTH then "M"
else
if period == AggregationPeriod.WEEK then "W"
else
if period == AggregationPeriod.FOUR_DAYS then "4D"
else
if period == AggregationPeriod.THREE_DAYS then "3D"
else
if period == AggregationPeriod.TWO_DAYS then "2D"
else
if period  == AggregationPeriod.DAY then "D"
else
if period == AggregationPeriod.FOUR_HOURS then "4H"
else
if period == AggregationPeriod.TWO_HOURS then "2H"
else
if period == AggregationPeriod.HOUR then "60m"
else
if period == AggregationPeriod.THIRTY_MIN then "30m"
else
if period == AggregationPeriod.TWENTY_MIN then "20m"
else
if period  == AggregationPeriod.FIFTEEN_MIN then "15m"
else
if period == AggregationPeriod.TEN_MIN then "10m"
else
if period == AggregationPeriod.FIVE_MIN then "5m"
else
if period == AggregationPeriod.FOUR_MIN then "4m"
else
if period  == AggregationPeriod.THREE_MIN then "3m"
else
if period == AggregationPeriod.TWO_MIN then "2m"
else
if period  == AggregationPeriod.MIN then "1m"
else ""), if s1 == 1 then GlobalColor("Long") else if s1 == -1 then GlobalColor("Short") else GlobalColor("Neutral"));
AddLabel(!IsNaN(h1) and h1 != 0, If h1 == 1 then "B" else if h1 == -1 then "S" else "-", if h1 == 1 then GlobalColor("arrow Buy") else if h1 == -1 then GlobalColor("arrow Sell") else color.gray);
 
Last edited by a moderator:
here is the code for a more responsive label
Code:
declare lower;
input period = AggregationPeriod.DAY;

DefineGlobalColor("Long", Color.Green);
DefineGlobalColor("Short", Color.RED);
DefineGlobalColor("Neutral", Color.Yellow);
DefineGlobalColor("arrow Buy", Color.Cyan);
DefineGlobalColor("arrow Sell", Color.Orange);

Script SymbolHK4emaRank{
input period = AggregationPeriod.DAY;
def OP = Open(period= period);
def CP = Close(Period= period);
Def Price = (OP + CP)/2;
input averageType = AverageType.Exponential;
def D1 = MovingAverage(AveragetYPE,Price,3);
def D2 = MovingAverage(AverageType,price,6);
def D3 = MovingAverage(AverageType,price,9);
def D4 = MovingAverage(AverageType,price,12);
def W1 = MovingAverage(AveragetYPE,Price,15);
def W2 = MovingAverage(AverageType,price,30);
def W3 = MovingAverage(AverageType,price,45);
def W4 = MovingAverage(AverageType,price,60);

def DCondition1 = If(D1 > D2 and D2 > D3 and D3 > D4, 5,
If((D1 > D2 and D2 > D4 and D4 > D3) or (D1 > D3 and D3 > D2 and D2 > D4) or
(D2 > D1 and D1 > D3 and D3 > D4), 4, If(D2 > D1 and D1 > D4 and D4 > D3, 3,
If((D1 > D3 and D3 > D4 and D4 > D2) or ( D1 > D4 and D4 > D2 and D2 > D3)  or
(D2 > D3 and D3 > D1 and D1 > D4)  or (D3 > D1 and D1 > D2 and D2 > D4) , 2,
If((D1 > D4 and D4 > D3 and D3 > D2) or (D3 > D2 and D2 > D1 and D1 > D4), 1, 0)))));
def DCondition2 = If(D4 > D3 and D3 > D2 and D2 > D1, -5,
If((D3 > D4 and D4 > D2 and D2 > D1) or (D4 > D2 and D2 > D3 and D3 > D1) or
(D4 > D3 and D3 > D1 and D1 > D2), -4, If(D3 > D4 and D4 > D1 and D1 > D2, -3,
If((D2 > D4 and D4 > D3 and D3 > D1) or (D3 > D2 and D2 > D4 and D4 > D1)  or
(D4 > D1 and D1 > D3 and D3 > D2)  or (D4 > D2 and D2 > D1 and D1 > D3) , -2,
If((D2 > D3 and D3 > D4 and D4 > D1) or (D4 > D1 and D1 > D2 and D2 > D3), -1, 0)))));
def WCondition1 = If(W1 > W2 and W2 > W3 and W3 > W4, 50,
If((W1 > W2 and W2 > W4 and W4 > W3) or (W1 > W3 and W3 > W2 and W2 > W4) or
(W2 > W1 and W1 > W3 and W3 > W4), 40, If(W2 > W1 and W1 > W4 and W4 > W3, 30,
If((W1 > W3 and W3 > W4 and W4 > W2) or ( W1 > W4 and W4 > W2 and W2 > W3)  or
(W2 > W3 and W3 > W1 and W1 > W4)  or (W3 > W1 and W1 > W2 and W2 > W4) , 20,
If((W1 > W4 and W4 > W3 and W3 > W2) or (W3 > W2 and W2 > W1 and W1 > W4), 10, 0)))));
def WCondition2 = If(W4 > W3 and W3 > W2 and W2 > W1, -50,
If((W3 > W4 and W4 > W2 and W2 > W1) or (W4 > W2 and W2 > W3 and W3 > W1) or
(W4 > W3 and W3 > W1 and W1 > W2), -40, If(W3 > W4 and W4 > W1 and W1 > W2, -30,
If((W2 > W4 and W4 > W3 and W3 > W1) or (W3 > W2 and W2 > W4 and W4 > W1)  or
(W4 > W1 and W1 > W3 and W3 > W2)  or (W4 > W2 and W2 > W1 and W1 > W3) , -20,
If((W2 > W3 and W3 > W4 and W4 > W1) or (W4 > W1 and W1 > W2 and W2 > W3), -10, 0)))));
def WTrend = (WCondition1+WCondition2);
def TTrend = (WCondition1+WCondition2+ DCondition1+DCondition2);
Def Buy = TTrend > WTrend;
Def Sell = TTrend < WTrend;
plot result = if buy then 1 else if sell then -1 else 0;
}
Script SymbolHKarrows{
input period = AggregationPeriod.DAY;
def OP = Open(period= period);
def CP = Close(Period= period);
Def Price = (OP + CP)/2;
input averageType = AverageType.Exponential;
def D1 = MovingAverage(AveragetYPE,Price,3);
def D2 = MovingAverage(AverageType,price,6);
def D3 = MovingAverage(AverageType,price,9);
def D4 = MovingAverage(AverageType,price,12);
def W1 = MovingAverage(AveragetYPE,Price,15);
def W2 = MovingAverage(AverageType,price,30);
def W3 = MovingAverage(AverageType,price,45);
def W4 = MovingAverage(AverageType,price,60);

def DCondition1 = If(D1 > D2 and D2 > D3 and D3 > D4, 5,
If((D1 > D2 and D2 > D4 and D4 > D3) or (D1 > D3 and D3 > D2 and D2 > D4) or
(D2 > D1 and D1 > D3 and D3 > D4), 4, If(D2 > D1 and D1 > D4 and D4 > D3, 3,
If((D1 > D3 and D3 > D4 and D4 > D2) or ( D1 > D4 and D4 > D2 and D2 > D3)  or
(D2 > D3 and D3 > D1 and D1 > D4)  or (D3 > D1 and D1 > D2 and D2 > D4) , 2,
If((D1 > D4 and D4 > D3 and D3 > D2) or (D3 > D2 and D2 > D1 and D1 > D4), 1, 0)))));
def DCondition2 = If(D4 > D3 and D3 > D2 and D2 > D1, -5,
If((D3 > D4 and D4 > D2 and D2 > D1) or (D4 > D2 and D2 > D3 and D3 > D1) or
(D4 > D3 and D3 > D1 and D1 > D2), -4, If(D3 > D4 and D4 > D1 and D1 > D2, -3,
If((D2 > D4 and D4 > D3 and D3 > D1) or (D3 > D2 and D2 > D4 and D4 > D1)  or
(D4 > D1 and D1 > D3 and D3 > D2)  or (D4 > D2 and D2 > D1 and D1 > D3) , -2,
If((D2 > D3 and D3 > D4 and D4 > D1) or (D4 > D1 and D1 > D2 and D2 > D3), -1, 0)))));
def WCondition1 = If(W1 > W2 and W2 > W3 and W3 > W4, 50,
If((W1 > W2 and W2 > W4 and W4 > W3) or (W1 > W3 and W3 > W2 and W2 > W4) or
(W2 > W1 and W1 > W3 and W3 > W4), 40, If(W2 > W1 and W1 > W4 and W4 > W3, 30,
If((W1 > W3 and W3 > W4 and W4 > W2) or ( W1 > W4 and W4 > W2 and W2 > W3)  or
(W2 > W3 and W3 > W1 and W1 > W4)  or (W3 > W1 and W1 > W2 and W2 > W4) , 20,
If((W1 > W4 and W4 > W3 and W3 > W2) or (W3 > W2 and W2 > W1 and W1 > W4), 10, 0)))));
def WCondition2 = If(W4 > W3 and W3 > W2 and W2 > W1, -50,
If((W3 > W4 and W4 > W2 and W2 > W1) or (W4 > W2 and W2 > W3 and W3 > W1) or
(W4 > W3 and W3 > W1 and W1 > W2), -40, If(W3 > W4 and W4 > W1 and W1 > W2, -30,
If((W2 > W4 and W4 > W3 and W3 > W1) or (W3 > W2 and W2 > W4 and W4 > W1)  or
(W4 > W1 and W1 > W3 and W3 > W2)  or (W4 > W2 and W2 > W1 and W1 > W3) , -20,
If((W2 > W3 and W3 > W4 and W4 > W1) or (W4 > W1 and W1 > W2 and W2 > W3), -10, 0)))));
def WTrend = (WCondition1+WCondition2);
def TTrend = (WCondition1+WCondition2+ DCondition1+DCondition2);
Def Buy = TTrend > WTrend;
Def Sell = TTrend < WTrend;
Def arrowup = TTrend crosses above WTrend;
Def arrowdown = TTrend crosses Below WTrend;

plot result = if arrowup then 1 else if arrowdown then -1 else 0;
}
def currentPeriod = GetAggregationPeriod();
def s1;
def h1;

if period >= currentPeriod {
    s1 = SymbolHK4emaRank(period = period );
    h1 = SymbolHKArrows(period = period );
} else {
    s1 = Double.NaN;
    h1 = DOuble.Nan;
}

AddLabel(!IsNaN(s1), "4mR:" + (if period == AggregationPeriod.MONTH then "M"
else
if period == AggregationPeriod.WEEK then "W"
else
if period == AggregationPeriod.FOUR_DAYS then "4D"
else
if period == AggregationPeriod.THREE_DAYS then "3D"
else
if period == AggregationPeriod.TWO_DAYS then "2D"
else
if period  == AggregationPeriod.DAY then "D"
else
if period == AggregationPeriod.FOUR_HOURS then "4H"
else
if period == AggregationPeriod.TWO_HOURS then "2H"
else
if period == AggregationPeriod.HOUR then "60m"
else
if period == AggregationPeriod.THIRTY_MIN then "30m"
else
if period == AggregationPeriod.TWENTY_MIN then "20m"
else
if period  == AggregationPeriod.FIFTEEN_MIN then "15m"
else
if period == AggregationPeriod.TEN_MIN then "10m"
else
if period == AggregationPeriod.FIVE_MIN then "5m"
else
if period == AggregationPeriod.FOUR_MIN then "4m"
else
if period  == AggregationPeriod.THREE_MIN then "3m"
else
if period == AggregationPeriod.TWO_MIN then "2m"
else
if period  == AggregationPeriod.MIN then "1m"
else ""), if s1 == 1 then GlobalColor("Long") else if s1 == -1 then GlobalColor("Short") else GlobalColor("Neutral"));
AddLabel(!IsNaN(h1) and h1 != 0, If h1 == 1 then "B" else if h1 == -1 then "S" else "-", if h1 == 1 then GlobalColor("arrow Buy") else if h1 == -1 then GlobalColor("arrow Sell") else color.gray);
Thank you for the info. I loaded your script up and I'm not sure what its supposed to do. I see the "4mR:D" label in the lower study but don't know how to interpret it.
 
Last edited by a moderator:
Thank you for the info. I loaded your script up and I'm not sure what its supposed to do. I see the "4mR:D" label in the lower study but don't know how to interpret it.
I have taken 4 EMA's short term and compared them against 4 longer term EMA's , The labels are for the last day to tell you if the trend is bullish or bearish
 
Here is HK_APC_4_EMA_Rank_w_5_MTF
Code:
Declare Lower;
# GLOBAL DEFINITIONS

DefineGlobalColor("TrendUp", Color.Cyan);
DefineGlobalColor("TrendDown", Color.Magenta);

input agg = AggregationPeriod.FIVE_MIN;
input agg2 = AggregationPeriod.THIRTY_MIN;
input agg3 = AggregationPeriod.HOUR;
input agg4 = AggregationPeriod.TWO_Hours;;
input agg5 = AggregationPeriod.FOUR_HOURs;
Input apc = 7;
input DotSize = 3;
input n = 6;
def n1  = n + 1;
Input Length1 =3;
Input Length2 =6;
Input Length3 =9;
Input Length4 =12;
Input Length5 =15;
Input Length6 =30;
Input Length7 =45;
Input Length8 =60;
def o = Open(Period = AGG);
def C = Close(Period =AGG);
def o2 = Open(Period = AGG2);
def C2 = Close(Period =AGG2);
def o3 = Open(Period = AGG3);
def C3 = Close(Period =AGG3);
def o4 = Open(Period = AGG4);
def C4 = Close(Period =AGG4);
def o5 = Open(Period = AGG5);
def C5 = Close(Period =AGG5);
Def price =(O+C)/2;
Def Bprice =(O2+C2)/2;
Def Cprice =(O3+C3)/2;
Def Dprice =(O4+C4)/2;
Def Eprice =(O5+C5)/2;

input averageType = AverageType.exponential;
def D1 = MovingAverage(AveragetYPE,Price,length1);
def D2 = MovingAverage(AverageType,price,Length2);
def D3 = MovingAverage(AverageType,price,Length3);
def D4 = MovingAverage(AverageType,price,Length4);
def W1 = MovingAverage(AveragetYPE,Price,length5);
def W2 = MovingAverage(AverageType,price,Length6);
def W3 = MovingAverage(AverageType,price,Length7);
def W4 = MovingAverage(AverageType,price,Length8);
def BD1 = MovingAverage(AveragetYPE,BPrice,length1);
def BD2 = MovingAverage(AverageType,Bprice,Length2);
def BD3 = MovingAverage(AverageType,Bprice,Length3);
def BD4 = MovingAverage(AverageType,Bprice,Length4);
def BW1 = MovingAverage(AveragetYPE,BPrice,length5);
def BW2 = MovingAverage(AverageType,Bprice,Length6);
def BW3 = MovingAverage(AverageType,Bprice,Length7);
def BW4 = MovingAverage(AverageType,Bprice,Length8);
def CD1 = MovingAverage(AveragetYPE,CPrice,length1);
def CD2 = MovingAverage(AverageType,Cprice,Length2);
def CD3 = MovingAverage(AverageType,Cprice,Length3);
def CD4 = MovingAverage(AverageType,Cprice,Length4);
def CW1 = MovingAverage(AveragetYPE,CPrice,length5);
def CW2 = MovingAverage(AverageType,Cprice,Length6);
def CW3 = MovingAverage(AverageType,Cprice,Length7);
def CW4 = MovingAverage(AverageType,Cprice,Length8);
def DD1 = MovingAverage(AveragetYPE,DPrice,length1);
def DD2 = MovingAverage(AverageType,Dprice,Length2);
def DD3 = MovingAverage(AverageType,Dprice,Length3);
def DD4 = MovingAverage(AverageType,Dprice,Length4);
def DW1 = MovingAverage(AveragetYPE,DPrice,length5);
def DW2 = MovingAverage(AverageType,Dprice,Length6);
def DW3 = MovingAverage(AverageType,Dprice,Length7);
def DW4 = MovingAverage(AverageType,Dprice,Length8);
def ED1 = MovingAverage(AveragetYPE,EPrice,length1);
def ED2 = MovingAverage(AverageType,Eprice,Length2);
def ED3 = MovingAverage(AverageType,Eprice,Length3);
def ED4 = MovingAverage(AverageType,Eprice,Length4);
def EW1 = MovingAverage(AveragetYPE,EPrice,length5);
def EW2 = MovingAverage(AverageType,Eprice,Length6);
def EW3 = MovingAverage(AverageType,Eprice,Length7);
def EW4 = MovingAverage(AverageType,Eprice,Length8);

def DCondition1 = If(D1 > D2 and D2 > D3 and D3 > D4, 5,
If((D1 > D2 and D2 > D4 and D4 > D3) or (D1 > D3 and D3 > D2 and D2 > D4) or
(D2 > D1 and D1 > D3 and D3 > D4), 4, If(D2 > D1 and D1 > D4 and D4 > D3, 3,
If((D1 > D3 and D3 > D4 and D4 > D2) or ( D1 > D4 and D4 > D2 and D2 > D3)  or
(D2 > D3 and D3 > D1 and D1 > D4)  or (D3 > D1 and D1 > D2 and D2 > D4) , 2,
If((D1 > D4 and D4 > D3 and D3 > D2) or (D3 > D2 and D2 > D1 and D1 > D4), 1, 0)))));
def DCondition2 = If(D4 > D3 and D3 > D2 and D2 > D1, -5,
If((D3 > D4 and D4 > D2 and D2 > D1) or (D4 > D2 and D2 > D3 and D3 > D1) or
(D4 > D3 and D3 > D1 and D1 > D2), -4, If(D3 > D4 and D4 > D1 and D1 > D2, -3,
If((D2 > D4 and D4 > D3 and D3 > D1) or (D3 > D2 and D2 > D4 and D4 > D1)  or
(D4 > D1 and D1 > D3 and D3 > D2)  or (D4 > D2 and D2 > D1 and D1 > D3) , -2,
If((D2 > D3 and D3 > D4 and D4 > D1) or (D4 > D1 and D1 > D2 and D2 > D3), -1, 0)))));
def WCondition1 = If(W1 > W2 and W2 > W3 and W3 > W4, 50,
If((W1 > W2 and W2 > W4 and W4 > W3) or (W1 > W3 and W3 > W2 and W2 > W4) or
(W2 > W1 and W1 > W3 and W3 > W4), 40, If(W2 > W1 and W1 > W4 and W4 > W3, 30,
If((W1 > W3 and W3 > W4 and W4 > W2) or ( W1 > W4 and W4 > W2 and W2 > W3)  or
(W2 > W3 and W3 > W1 and W1 > W4)  or (W3 > W1 and W1 > W2 and W2 > W4) , 20,
If((W1 > W4 and W4 > W3 and W3 > W2) or (W3 > W2 and W2 > W1 and W1 > W4), 10, 0)))));
def WCondition2 = If(W4 > W3 and W3 > W2 and W2 > W1, -50,
If((W3 > W4 and W4 > W2 and W2 > W1) or (W4 > W2 and W2 > W3 and W3 > W1) or
(W4 > W3 and W3 > W1 and W1 > W2), -40, If(W3 > W4 and W4 > W1 and W1 > W2, -30,
If((W2 > W4 and W4 > W3 and W3 > W1) or (W3 > W2 and W2 > W4 and W4 > W1)  or
(W4 > W1 and W1 > W3 and W3 > W2)  or (W4 > W2 and W2 > W1 and W1 > W3) , -20,
If((W2 > W3 and W3 > W4 and W4 > W1) or (W4 > W1 and W1 > W2 and W2 > W3), -10, 0)))));

def BDCondition1 = If(BD1 > BD2 and BD2 > BD3 and BD3 > BD4, 5,
If((BD1 > BD2 and BD2 > BD4 and BD4 > BD3) or (BD1 > BD3 and BD3 > BD2 and BD2 > BD4) or
(BD2 > BD1 and BD1 > BD3 and BD3 > BD4), 4, If(BD2 > BD1 and BD1 > BD4 and BD4 > BD3, 3,
If((BD1 > BD3 and BD3 > BD4 and BD4 > BD2) or ( BD1 > BD4 and BD4 > BD2 and BD2 > BD3)  or
(BD2 > BD3 and BD3 > BD1 and BD1 > BD4)  or (BD3 > BD1 and BD1 > BD2 and BD2 > BD4) , 2,
If((BD1 > BD4 and BD4 > BD3 and BD3 > BD2) or (BD3 > BD2 and BD2 > BD1 and BD1 > BD4), 1, 0)))));
def BDCondition2 = If(BD4 > BD3 and BD3 > BD2 and BD2 > BD1, -5,
If((BD3 > BD4 and BD4 > BD2 and BD2 > BD1) or (BD4 > BD2 and BD2 > BD3 and BD3 > BD1) or
(BD4 > BD3 and BD3 > BD1 and BD1 > BD2), -4, If(BD3 > BD4 and BD4 > BD1 and BD1 > BD2, -3,
If((BD2 > BD4 and BD4 > BD3 and BD3 > BD1) or (BD3 > BD2 and BD2 > BD4 and BD4 > BD1)  or
(BD4 > BD1 and BD1 > BD3 and BD3 > BD2)  or (BD4 > BD2 and BD2 > BD1 and BD1 > BD3) , -2,
If((BD2 > BD3 and BD3 > BD4 and BD4 > BD1) or (BD4 > BD1 and BD1 > BD2 and BD2 > BD3), -1, 0)))));
def BWCondition1 = If(BW1 > BW2 and BW2 > BW3 and BW3 > BW4, 50,
If((BW1 > BW2 and BW2 > BW4 and BW4 > BW3) or (BW1 > BW3 and BW3 > BW2 and BW2 > BW4) or
(BW2 > BW1 and BW1 > BW3 and BW3 > BW4), 40, If(BW2 > BW1 and BW1 > BW4 and BW4 > BW3, 30,
If((BW1 > BW3 and BW3 > BW4 and BW4 > BW2) or ( BW1 > BW4 and BW4 > BW2 and BW2 > BW3)  or
(BW2 > BW3 and BW3 > BW1 and BW1 > BW4)  or (BW3 > BW1 and BW1 > BW2 and BW2 > BW4) , 20,
If((BW1 > BW4 and BW4 > BW3 and BW3 > BW2) or (BW3 > BW2 and BW2 > BW1 and BW1 > BW4), 10, 0)))));
def BWCondition2 = If(BW4 > BW3 and BW3 > BW2 and BW2 > BW1, -50,
If((BW3 > BW4 and BW4 > BW2 and BW2 > BW1) or (BW4 > BW2 and BW2 > BW3 and BW3 > BW1) or
(BW4 > BW3 and BW3 > BW1 and BW1 > BW2), -40, If(BW3 > BW4 and BW4 > BW1 and BW1 > BW2, -30,
If((BW2 > BW4 and BW4 > BW3 and BW3 > BW1) or (BW3 > BW2 and BW2 > BW4 and BW4 > BW1)  or
(BW4 > BW1 and BW1 > BW3 and BW3 > BW2)  or (BW4 > BW2 and BW2 > BW1 and BW1 > BW3) , -20,
If((BW2 > BW3 and BW3 > BW4 and BW4 > BW1) or (BW4 > BW1 and BW1 > BW2 and BW2 > BW3), -10, 0)))));

def CDCondition1 = If(CD1 > CD2 and CD2 > CD3 and CD3 > CD4, 5,
If((CD1 > CD2 and CD2 > CD4 and CD4 > CD3) or (CD1 > CD3 and CD3 > CD2 and CD2 > CD4) or
(CD2 > CD1 and CD1 > CD3 and CD3 > CD4), 4, If(CD2 > CD1 and CD1 > CD4 and CD4 > CD3, 3,
If((CD1 > CD3 and CD3 > CD4 and CD4 > CD2) or ( CD1 > CD4 and CD4 > CD2 and CD2 > CD3)  or
(CD2 > CD3 and CD3 > CD1 and CD1 > CD4)  or (CD3 > CD1 and CD1 > CD2 and CD2 > CD4) , 2,
If((CD1 > CD4 and CD4 > CD3 and CD3 > CD2) or (CD3 > CD2 and CD2 > CD1 and CD1 > CD4), 1, 0)))));
def CDCondition2 = If(CD4 > CD3 and CD3 > CD2 and CD2 > CD1, -5,
If((CD3 > CD4 and CD4 > CD2 and CD2 > CD1) or (CD4 > CD2 and CD2 > CD3 and CD3 > CD1) or
(CD4 > CD3 and CD3 > CD1 and CD1 > CD2), -4, If(CD3 > CD4 and CD4 > CD1 and CD1 > CD2, -3,
If((CD2 > CD4 and CD4 > CD3 and CD3 > CD1) or (CD3 > CD2 and CD2 > CD4 and CD4 > CD1)  or
(CD4 > CD1 and CD1 > CD3 and CD3 > CD2)  or (CD4 > CD2 and CD2 > CD1 and CD1 > CD3) , -2,
If((CD2 > CD3 and CD3 > CD4 and CD4 > CD1) or (CD4 > CD1 and CD1 > CD2 and CD2 > CD3), -1, 0)))));
def CWCondition1 = If(CW1 > CW2 and CW2 > CW3 and CW3 > CW4, 50,
If((CW1 > CW2 and CW2 > CW4 and CW4 > CW3) or (CW1 > CW3 and CW3 > CW2 and CW2 > CW4) or
(CW2 > CW1 and CW1 > CW3 and CW3 > CW4), 40, If(CW2 > CW1 and CW1 > CW4 and CW4 > CW3, 30,
If((CW1 > CW3 and CW3 > CW4 and CW4 > CW2) or ( CW1 > CW4 and CW4 > CW2 and CW2 > CW3)  or
(CW2 > CW3 and CW3 > CW1 and CW1 > CW4)  or (CW3 > CW1 and CW1 > CW2 and CW2 > CW4) , 20,
If((CW1 > CW4 and CW4 > CW3 and CW3 > CW2) or (CW3 > CW2 and CW2 > CW1 and CW1 > CW4), 10, 0)))));
def CWCondition2 = If(CW4 > CW3 and CW3 > CW2 and CW2 > CW1, -50,
If((CW3 > CW4 and CW4 > CW2 and CW2 > CW1) or (CW4 > CW2 and CW2 > CW3 and CW3 > CW1) or
(CW4 > CW3 and CW3 > CW1 and CW1 > CW2), -40, If(CW3 > CW4 and CW4 > CW1 and CW1 > CW2, -30,
If((CW2 > CW4 and CW4 > CW3 and CW3 > CW1) or (CW3 > CW2 and CW2 > CW4 and CW4 > CW1)  or
(CW4 > CW1 and CW1 > CW3 and CW3 > CW2)  or (CW4 > CW2 and CW2 > CW1 and CW1 > CW3) , -20,
If((CW2 > CW3 and CW3 > CW4 and CW4 > CW1) or (CW4 > CW1 and CW1 > W2 and CW2 > CW3), -10, 0)))));

def DDCondition1 = If(DD1 > DD2 and DD2 > DD3 and DD3 > DD4, 5,
If((DD1 > DD2 and DD2 > DD4 and DD4 > DD3) or (DD1 > DD3 and DD3 > DD2 and DD2 > DD4) or
(DD2 > DD1 and DD1 > DD3 and DD3 > DD4), 4, If(DD2 > DD1 and DD1 > DD4 and DD4 > DD3, 3,
If((DD1 > DD3 and DD3 > DD4 and DD4 > DD2) or ( DD1 > DD4 and DD4 > DD2 and DD2 > DD3)  or
(DD2 > DD3 and DD3 > DD1 and DD1 > DD4)  or (DD3 > DD1 and DD1 > DD2 and DD2 > DD4) , 2,
If((DD1 > DD4 and DD4 > DD3 and DD3 > DD2) or (DD3 > DD2 and DD2 > DD1 and DD1 > DD4), 1, 0)))));
def DDCondition2 = If(DD4 > DD3 and DD3 > DD2 and DD2 > DD1, -5,
If((DD3 > DD4 and DD4 > DD2 and DD2 > DD1) or (DD4 > DD2 and DD2 > DD3 and DD3 > DD1) or
(DD4 > DD3 and DD3 > DD1 and DD1 > DD2), -4, If(DD3 > DD4 and DD4 > DD1 and DD1 > DD2, -3,
If((DD2 > DD4 and DD4 > DD3 and DD3 > DD1) or (DD3 > DD2 and DD2 > DD4 and DD4 > DD1)  or
(DD4 > DD1 and DD1 > DD3 and DD3 > DD2)  or (DD4 > DD2 and DD2 > DD1 and DD1 > DD3) , -2,
If((DD2 > DD3 and DD3 > DD4 and DD4 > DD1) or (DD4 > DD1 and DD1 > DD2 and DD2 > DD3), -1, 0)))));
def DWCondition1 = If(DW1 > DW2 and DW2 > DW3 and DW3 > DW4, 50,
If((DW1 > DW2 and DW2 > DW4 and DW4 > DW3) or (DW1 > DW3 and DW3 > DW2 and DW2 > DW4) or
(DW2 > DW1 and DW1 > DW3 and DW3 > DW4), 40, If(DW2 > DW1 and DW1 > DW4 and DW4 > DW3, 30,
If((DW1 > DW3 and DW3 > DW4 and DW4 > DW2) or ( DW1 > DW4 and DW4 > DW2 and DW2 > DW3)  or
(DW2 > DW3 and DW3 > DW1 and DW1 > DW4)  or (DW3 > DW1 and DW1 > DW2 and DW2 > DW4) , 20,
If((DW1 > DW4 and DW4 > DW3 and DW3 > DW2) or (DW3 > DW2 and DW2 > DW1 and DW1 > DW4), 10, 0)))));
def DWCondition2 = If(DW4 > DW3 and DW3 > DW2 and DW2 > DW1, -50,
If((DW3 > DW4 and DW4 > DW2 and DW2 > DW1) or (DW4 > DW2 and DW2 > DW3 and DW3 > DW1) or
(DW4 > DW3 and DW3 > DW1 and DW1 > DW2), -40, If(DW3 > DW4 and DW4 > DW1 and DW1 > DW2, -30,
If((DW2 > DW4 and DW4 > DW3 and DW3 > DW1) or (DW3 > DW2 and DW2 > DW4 and DW4 > DW1)  or
(DW4 > DW1 and DW1 > DW3 and DW3 > DW2)  or (DW4 > DW2 and DW2 > DW1 and DW1 > DW3) , -20,
If((DW2 > DW3 and DW3 > DW4 and DW4 > DW1) or (DW4 > DW1 and DW1 > DW2 and DW2 > DW3), -10, 0)))));

def EDCondition1 = If(ED1 > ED2 and ED2 > ED3 and ED3 > D4, 5,
If((ED1 > ED2 and ED2 > ED4 and ED4 > ED3) or (ED1 > ED3 and ED3 > ED2 and ED2 > ED4) or
(ED2 > ED1 and ED1 > ED3 and ED3 > ED4), 4, If(ED2 > ED1 and ED1 > ED4 and ED4 > ED3, 3,
If((ED1 > ED3 and ED3 > ED4 and ED4 > ED2) or ( ED1 > ED4 and ED4 > ED2 and ED2 > ED3)  or
(ED2 > ED3 and ED3 > ED1 and ED1 > ED4)  or (ED3 > ED1 and ED1 > ED2 and ED2 > ED4) , 2,
If((ED1 > ED4 and ED4 > ED3 and ED3 > ED2) or (ED3 > ED2 and ED2 > ED1 and ED1 > ED4), 1, 0)))));
def EDCondition2 = If(ED4 > ED3 and ED3 > ED2 and ED2 > ED1, -5,
If((ED3 > ED4 and ED4 > ED2 and ED2 > ED1) or (ED4 > ED2 and ED2 > ED3 and ED3 > ED1) or
(ED4 > ED3 and ED3 > ED1 and ED1 > ED2), -4, If(ED3 > ED4 and ED4 > ED1 and ED1 > ED2, -3,
If((ED2 > ED4 and ED4 > ED3 and ED3 > ED1) or (ED3 > ED2 and ED2 > ED4 and ED4 > ED1)  or
(ED4 > ED1 and ED1 > ED3 and ED3 > ED2)  or (ED4 > ED2 and ED2 > ED1 and ED1 > ED3) , -2,
If((ED2 > ED3 and ED3 > ED4 and ED4 > ED1) or (ED4 > ED1 and ED1 > ED2 and ED2 > ED3), -1, 0)))));
def EWCondition1 = If(EW1 > EW2 and EW2 > EW3 and EW3 > EW4, 50,
If((EW1 > EW2 and EW2 > EW4 and EW4 > EW3) or (EW1 > EW3 and EW3 > EW2 and EW2 > EW4) or
(EW2 > EW1 and EW1 > EW3 and EW3 > EW4), 40, If(EW2 > EW1 and EW1 > EW4 and EW4 > EW3, 30,
If((EW1 > EW3 and EW3 > EW4 and EW4 > EW2) or ( EW1 > EW4 and EW4 > EW2 and EW2 > EW3)  or
(EW2 > EW3 and EW3 > EW1 and EW1 > EW4)  or (EW3 > EW1 and EW1 > EW2 and EW2 > EW4) , 20,
If((EW1 > EW4 and EW4 > EW3 and EW3 > EW2) or (EW3 > EW2 and EW2 > EW1 and EW1 > EW4), 10, 0)))));
def EWCondition2 = If(EW4 > EW3 and EW3 > EW2 and EW2 > EW1, -50,
If((EW3 > EW4 and EW4 > EW2 and EW2 > EW1) or (EW4 > EW2 and EW2 > EW3 and EW3 > EW1) or
(EW4 > EW3 and EW3 > EW1 and EW1 > EW2), -40, If(EW3 > EW4 and EW4 > EW1 and EW1 > EW2, -30,
If((EW2 > EW4 and EW4 > EW3 and EW3 > EW1) or (EW3 > EW2 and EW2 > EW4 and EW4 > EW1)  or
(EW4 > EW1 and EW1 > EW3 and EW3 > EW2)  or (EW4 > EW2 and EW2 > EW1 and EW1 > EW3) , -20,
If((EW2 > EW3 and EW3 > EW4 and EW4 > EW1) or (EW4 > EW1 and EW1 > EW2 and EW2 > EW3), -10, 0)))));

def WT = (WCondition1+WCondition2);
def TT = (WCondition1+WCondition2+DCondition1+DCondition2);
def BWT = (BWCondition1+BWCondition2);
def BTT = (BWCondition1+BWCondition2+BDCondition1+BDCondition2);
def CWT = (CWCondition1+CWCondition2);
def CTT = (CWCondition1+CWCondition2+CDCondition1+CDCondition2);
def DWT = (DWCondition1+DWCondition2);
def DTT = (DWCondition1+DWCondition2+DDCondition1+DDCondition2);
def EWT = (EWCondition1+EWCondition2);
def ETT = (EWCondition1+EWCondition2+EDCondition1+EDCondition2);
#####signals
Def AXL = TT crosses above WT;
Def AXS = TT Crosses Below WT;
Def BXL = BTT crosses above BWT;
Def BXS = BTT Crosses Below BWT;
Def CXL = CTT crosses above CWT;
Def CXS = CTT Crosses Below CWT;
Def DXL = DTT crosses above DWT;
Def DXS = DTT Crosses Below DWT;
Def EXL = ETT crosses above EWT;
Def EXS = ETT Crosses Below EWT;

Def Along = TT > WT;
Def Ashort = TT < WT;
Def Blong = BTT > BWT;
Def Bshort = BTT < BWT;
Def Clong = CTT > CWT;
Def Cshort = CTT < CWT;
Def Dlong = DTT > DWT;
Def Dshort = DTT < DWT;
Def Elong = ETT > EWT;
Def Eshort = ETT < EWT;
###########Labels
#AddLabel( ALong, "Agg1 Long " , Color.Green);
#AddLabel( AXL, "Agg 1 Buy" , Color.Light_Green);
#AddLabel( AXS, "Agg 1 Sell" , Color.Light_Red);
#AddLabel( Ashort, "Agg 1 Short" , Color.Red);
#AddLabel( BLong, "Agg 2 Long " , Color.Green);
#AddLabel( BXL, "Agg 2 Buy" , Color.Light_Green);
#AddLabel( BXS, "Agg 2 Sell" , Color.Light_Red);
#AddLabel( Bshort, "Agg 2 Short" , Color.Red);
#AddLabel( CLong, "Agg3 Long " , Color.Green);
#AddLabel( CXL, "Agg 3 Buy" , Color.Light_Green);
#AddLabel( CXS, "Agg 3 Sell" , Color.Light_Red);
#AddLabel( CShort, "Agg 3 Short" , Color.Red);
#AddLabel( DLong, "Agg 4 Long " , Color.Green);
#AddLabel( DXL, "Agg 4 Buy" , Color.Light_Green);
#AddLabel( DXS, "Agg 4 Sell" , Color.Light_Red);
#AddLabel( DShort, "Agg 4 Short" , Color.Red);
#AddLabel( ELong, "Agg 5 Long " , Color.Green);
#AddLabel( EXL, "Agg 5 Buy" , Color.Light_Green);
#AddLabel( EXS, "Agg 5 Sell" , Color.Light_Red);
#AddLabel( EShort, "Agg 5 Short" , Color.Red);
###########Plots

plot R1_Dot = if IsNaN(c) then Double.NaN else 1;
R1_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R1_Dot.SetLineWeight(DotSize);
R1_Dot.AssignValueColor(if TT < WT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R2_Dot = if IsNaN(c2) then Double.NaN else 2;
R2_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R2_Dot.SetLineWeight(DotSize);
R2_Dot.AssignValueColor(if BTT < BWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R3_Dot = if IsNaN(c) then Double.NaN else 3;
R3_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R3_Dot.SetLineWeight(DotSize);
R3_Dot.AssignValueColor(if CTT < CWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R4_Dot = if IsNaN(c4) then Double.NaN else 4;
R4_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R4_Dot.SetLineWeight(DotSize);
R4_Dot.AssignValueColor(if DTT < DWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R5_Dot = if IsNaN(c5) then Double.NaN else 5;
R5_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R5_Dot.SetLineWeight(DotSize);
R5_Dot.AssignValueColor(if ETT < EWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot MTF_Dot = 6;
MTF_Dot.SetPaintingStrategy(PaintingStrategy.SQUARES);
MTF_Dot.SetLineWeight(lineWeight = 4);
MTF_Dot.DefineColor("Buy", Color.Green);
MTF_Dot.DefineColor("Sell", Color.red);
MTF_Dot.AssignValueColor ( if (ALong + BLong + CLong + DLong + ELong) >= 3 then MTF_Dot.Color("Buy") else MTF_Dot.Color("Sell"));
#assignPriceColor
assignPriceColor( if apc == 1 then if TT < WT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 2 then if BTT < BWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 3 then if CTT < CWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 4 then if DTT < DWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 5 then if ETT < EWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 6 then if (ALong + BLong + CLong + DLong + ELong) >= 3 then MTF_Dot.Color("Buy") else MTF_Dot.Color("Sell") else Color.current);

This indicator shows a cyan dot when the Bullishness is greater then Bearishness and a magenta dot when bearish. The lowest time frame is on the bottom, the next higher time frame is line 2,and so forth, a consensus square is on line 6.

You can color the price by changing the APC input to which ever line # you want
 
Here is HK_APC_4_EMA_Rank_w_5_MTF
Code:
Declare Lower;
# GLOBAL DEFINITIONS

DefineGlobalColor("TrendUp", Color.Cyan);
DefineGlobalColor("TrendDown", Color.Magenta);

input agg = AggregationPeriod.FIVE_MIN;
input agg2 = AggregationPeriod.THIRTY_MIN;
input agg3 = AggregationPeriod.HOUR;
input agg4 = AggregationPeriod.TWO_Hours;;
input agg5 = AggregationPeriod.FOUR_HOURs;
Input apc = 7;
input DotSize = 3;
input n = 6;
def n1  = n + 1;
Input Length1 =3;
Input Length2 =6;
Input Length3 =9;
Input Length4 =12;
Input Length5 =15;
Input Length6 =30;
Input Length7 =45;
Input Length8 =60;
def o = Open(Period = AGG);
def C = Close(Period =AGG);
def o2 = Open(Period = AGG2);
def C2 = Close(Period =AGG2);
def o3 = Open(Period = AGG3);
def C3 = Close(Period =AGG3);
def o4 = Open(Period = AGG4);
def C4 = Close(Period =AGG4);
def o5 = Open(Period = AGG5);
def C5 = Close(Period =AGG5);
Def price =(O+C)/2;
Def Bprice =(O2+C2)/2;
Def Cprice =(O3+C3)/2;
Def Dprice =(O4+C4)/2;
Def Eprice =(O5+C5)/2;

input averageType = AverageType.exponential;
def D1 = MovingAverage(AveragetYPE,Price,length1);
def D2 = MovingAverage(AverageType,price,Length2);
def D3 = MovingAverage(AverageType,price,Length3);
def D4 = MovingAverage(AverageType,price,Length4);
def W1 = MovingAverage(AveragetYPE,Price,length5);
def W2 = MovingAverage(AverageType,price,Length6);
def W3 = MovingAverage(AverageType,price,Length7);
def W4 = MovingAverage(AverageType,price,Length8);
def BD1 = MovingAverage(AveragetYPE,BPrice,length1);
def BD2 = MovingAverage(AverageType,Bprice,Length2);
def BD3 = MovingAverage(AverageType,Bprice,Length3);
def BD4 = MovingAverage(AverageType,Bprice,Length4);
def BW1 = MovingAverage(AveragetYPE,BPrice,length5);
def BW2 = MovingAverage(AverageType,Bprice,Length6);
def BW3 = MovingAverage(AverageType,Bprice,Length7);
def BW4 = MovingAverage(AverageType,Bprice,Length8);
def CD1 = MovingAverage(AveragetYPE,CPrice,length1);
def CD2 = MovingAverage(AverageType,Cprice,Length2);
def CD3 = MovingAverage(AverageType,Cprice,Length3);
def CD4 = MovingAverage(AverageType,Cprice,Length4);
def CW1 = MovingAverage(AveragetYPE,CPrice,length5);
def CW2 = MovingAverage(AverageType,Cprice,Length6);
def CW3 = MovingAverage(AverageType,Cprice,Length7);
def CW4 = MovingAverage(AverageType,Cprice,Length8);
def DD1 = MovingAverage(AveragetYPE,DPrice,length1);
def DD2 = MovingAverage(AverageType,Dprice,Length2);
def DD3 = MovingAverage(AverageType,Dprice,Length3);
def DD4 = MovingAverage(AverageType,Dprice,Length4);
def DW1 = MovingAverage(AveragetYPE,DPrice,length5);
def DW2 = MovingAverage(AverageType,Dprice,Length6);
def DW3 = MovingAverage(AverageType,Dprice,Length7);
def DW4 = MovingAverage(AverageType,Dprice,Length8);
def ED1 = MovingAverage(AveragetYPE,EPrice,length1);
def ED2 = MovingAverage(AverageType,Eprice,Length2);
def ED3 = MovingAverage(AverageType,Eprice,Length3);
def ED4 = MovingAverage(AverageType,Eprice,Length4);
def EW1 = MovingAverage(AveragetYPE,EPrice,length5);
def EW2 = MovingAverage(AverageType,Eprice,Length6);
def EW3 = MovingAverage(AverageType,Eprice,Length7);
def EW4 = MovingAverage(AverageType,Eprice,Length8);

def DCondition1 = If(D1 > D2 and D2 > D3 and D3 > D4, 5,
If((D1 > D2 and D2 > D4 and D4 > D3) or (D1 > D3 and D3 > D2 and D2 > D4) or
(D2 > D1 and D1 > D3 and D3 > D4), 4, If(D2 > D1 and D1 > D4 and D4 > D3, 3,
If((D1 > D3 and D3 > D4 and D4 > D2) or ( D1 > D4 and D4 > D2 and D2 > D3)  or
(D2 > D3 and D3 > D1 and D1 > D4)  or (D3 > D1 and D1 > D2 and D2 > D4) , 2,
If((D1 > D4 and D4 > D3 and D3 > D2) or (D3 > D2 and D2 > D1 and D1 > D4), 1, 0)))));
def DCondition2 = If(D4 > D3 and D3 > D2 and D2 > D1, -5,
If((D3 > D4 and D4 > D2 and D2 > D1) or (D4 > D2 and D2 > D3 and D3 > D1) or
(D4 > D3 and D3 > D1 and D1 > D2), -4, If(D3 > D4 and D4 > D1 and D1 > D2, -3,
If((D2 > D4 and D4 > D3 and D3 > D1) or (D3 > D2 and D2 > D4 and D4 > D1)  or
(D4 > D1 and D1 > D3 and D3 > D2)  or (D4 > D2 and D2 > D1 and D1 > D3) , -2,
If((D2 > D3 and D3 > D4 and D4 > D1) or (D4 > D1 and D1 > D2 and D2 > D3), -1, 0)))));
def WCondition1 = If(W1 > W2 and W2 > W3 and W3 > W4, 50,
If((W1 > W2 and W2 > W4 and W4 > W3) or (W1 > W3 and W3 > W2 and W2 > W4) or
(W2 > W1 and W1 > W3 and W3 > W4), 40, If(W2 > W1 and W1 > W4 and W4 > W3, 30,
If((W1 > W3 and W3 > W4 and W4 > W2) or ( W1 > W4 and W4 > W2 and W2 > W3)  or
(W2 > W3 and W3 > W1 and W1 > W4)  or (W3 > W1 and W1 > W2 and W2 > W4) , 20,
If((W1 > W4 and W4 > W3 and W3 > W2) or (W3 > W2 and W2 > W1 and W1 > W4), 10, 0)))));
def WCondition2 = If(W4 > W3 and W3 > W2 and W2 > W1, -50,
If((W3 > W4 and W4 > W2 and W2 > W1) or (W4 > W2 and W2 > W3 and W3 > W1) or
(W4 > W3 and W3 > W1 and W1 > W2), -40, If(W3 > W4 and W4 > W1 and W1 > W2, -30,
If((W2 > W4 and W4 > W3 and W3 > W1) or (W3 > W2 and W2 > W4 and W4 > W1)  or
(W4 > W1 and W1 > W3 and W3 > W2)  or (W4 > W2 and W2 > W1 and W1 > W3) , -20,
If((W2 > W3 and W3 > W4 and W4 > W1) or (W4 > W1 and W1 > W2 and W2 > W3), -10, 0)))));

def BDCondition1 = If(BD1 > BD2 and BD2 > BD3 and BD3 > BD4, 5,
If((BD1 > BD2 and BD2 > BD4 and BD4 > BD3) or (BD1 > BD3 and BD3 > BD2 and BD2 > BD4) or
(BD2 > BD1 and BD1 > BD3 and BD3 > BD4), 4, If(BD2 > BD1 and BD1 > BD4 and BD4 > BD3, 3,
If((BD1 > BD3 and BD3 > BD4 and BD4 > BD2) or ( BD1 > BD4 and BD4 > BD2 and BD2 > BD3)  or
(BD2 > BD3 and BD3 > BD1 and BD1 > BD4)  or (BD3 > BD1 and BD1 > BD2 and BD2 > BD4) , 2,
If((BD1 > BD4 and BD4 > BD3 and BD3 > BD2) or (BD3 > BD2 and BD2 > BD1 and BD1 > BD4), 1, 0)))));
def BDCondition2 = If(BD4 > BD3 and BD3 > BD2 and BD2 > BD1, -5,
If((BD3 > BD4 and BD4 > BD2 and BD2 > BD1) or (BD4 > BD2 and BD2 > BD3 and BD3 > BD1) or
(BD4 > BD3 and BD3 > BD1 and BD1 > BD2), -4, If(BD3 > BD4 and BD4 > BD1 and BD1 > BD2, -3,
If((BD2 > BD4 and BD4 > BD3 and BD3 > BD1) or (BD3 > BD2 and BD2 > BD4 and BD4 > BD1)  or
(BD4 > BD1 and BD1 > BD3 and BD3 > BD2)  or (BD4 > BD2 and BD2 > BD1 and BD1 > BD3) , -2,
If((BD2 > BD3 and BD3 > BD4 and BD4 > BD1) or (BD4 > BD1 and BD1 > BD2 and BD2 > BD3), -1, 0)))));
def BWCondition1 = If(BW1 > BW2 and BW2 > BW3 and BW3 > BW4, 50,
If((BW1 > BW2 and BW2 > BW4 and BW4 > BW3) or (BW1 > BW3 and BW3 > BW2 and BW2 > BW4) or
(BW2 > BW1 and BW1 > BW3 and BW3 > BW4), 40, If(BW2 > BW1 and BW1 > BW4 and BW4 > BW3, 30,
If((BW1 > BW3 and BW3 > BW4 and BW4 > BW2) or ( BW1 > BW4 and BW4 > BW2 and BW2 > BW3)  or
(BW2 > BW3 and BW3 > BW1 and BW1 > BW4)  or (BW3 > BW1 and BW1 > BW2 and BW2 > BW4) , 20,
If((BW1 > BW4 and BW4 > BW3 and BW3 > BW2) or (BW3 > BW2 and BW2 > BW1 and BW1 > BW4), 10, 0)))));
def BWCondition2 = If(BW4 > BW3 and BW3 > BW2 and BW2 > BW1, -50,
If((BW3 > BW4 and BW4 > BW2 and BW2 > BW1) or (BW4 > BW2 and BW2 > BW3 and BW3 > BW1) or
(BW4 > BW3 and BW3 > BW1 and BW1 > BW2), -40, If(BW3 > BW4 and BW4 > BW1 and BW1 > BW2, -30,
If((BW2 > BW4 and BW4 > BW3 and BW3 > BW1) or (BW3 > BW2 and BW2 > BW4 and BW4 > BW1)  or
(BW4 > BW1 and BW1 > BW3 and BW3 > BW2)  or (BW4 > BW2 and BW2 > BW1 and BW1 > BW3) , -20,
If((BW2 > BW3 and BW3 > BW4 and BW4 > BW1) or (BW4 > BW1 and BW1 > BW2 and BW2 > BW3), -10, 0)))));

def CDCondition1 = If(CD1 > CD2 and CD2 > CD3 and CD3 > CD4, 5,
If((CD1 > CD2 and CD2 > CD4 and CD4 > CD3) or (CD1 > CD3 and CD3 > CD2 and CD2 > CD4) or
(CD2 > CD1 and CD1 > CD3 and CD3 > CD4), 4, If(CD2 > CD1 and CD1 > CD4 and CD4 > CD3, 3,
If((CD1 > CD3 and CD3 > CD4 and CD4 > CD2) or ( CD1 > CD4 and CD4 > CD2 and CD2 > CD3)  or
(CD2 > CD3 and CD3 > CD1 and CD1 > CD4)  or (CD3 > CD1 and CD1 > CD2 and CD2 > CD4) , 2,
If((CD1 > CD4 and CD4 > CD3 and CD3 > CD2) or (CD3 > CD2 and CD2 > CD1 and CD1 > CD4), 1, 0)))));
def CDCondition2 = If(CD4 > CD3 and CD3 > CD2 and CD2 > CD1, -5,
If((CD3 > CD4 and CD4 > CD2 and CD2 > CD1) or (CD4 > CD2 and CD2 > CD3 and CD3 > CD1) or
(CD4 > CD3 and CD3 > CD1 and CD1 > CD2), -4, If(CD3 > CD4 and CD4 > CD1 and CD1 > CD2, -3,
If((CD2 > CD4 and CD4 > CD3 and CD3 > CD1) or (CD3 > CD2 and CD2 > CD4 and CD4 > CD1)  or
(CD4 > CD1 and CD1 > CD3 and CD3 > CD2)  or (CD4 > CD2 and CD2 > CD1 and CD1 > CD3) , -2,
If((CD2 > CD3 and CD3 > CD4 and CD4 > CD1) or (CD4 > CD1 and CD1 > CD2 and CD2 > CD3), -1, 0)))));
def CWCondition1 = If(CW1 > CW2 and CW2 > CW3 and CW3 > CW4, 50,
If((CW1 > CW2 and CW2 > CW4 and CW4 > CW3) or (CW1 > CW3 and CW3 > CW2 and CW2 > CW4) or
(CW2 > CW1 and CW1 > CW3 and CW3 > CW4), 40, If(CW2 > CW1 and CW1 > CW4 and CW4 > CW3, 30,
If((CW1 > CW3 and CW3 > CW4 and CW4 > CW2) or ( CW1 > CW4 and CW4 > CW2 and CW2 > CW3)  or
(CW2 > CW3 and CW3 > CW1 and CW1 > CW4)  or (CW3 > CW1 and CW1 > CW2 and CW2 > CW4) , 20,
If((CW1 > CW4 and CW4 > CW3 and CW3 > CW2) or (CW3 > CW2 and CW2 > CW1 and CW1 > CW4), 10, 0)))));
def CWCondition2 = If(CW4 > CW3 and CW3 > CW2 and CW2 > CW1, -50,
If((CW3 > CW4 and CW4 > CW2 and CW2 > CW1) or (CW4 > CW2 and CW2 > CW3 and CW3 > CW1) or
(CW4 > CW3 and CW3 > CW1 and CW1 > CW2), -40, If(CW3 > CW4 and CW4 > CW1 and CW1 > CW2, -30,
If((CW2 > CW4 and CW4 > CW3 and CW3 > CW1) or (CW3 > CW2 and CW2 > CW4 and CW4 > CW1)  or
(CW4 > CW1 and CW1 > CW3 and CW3 > CW2)  or (CW4 > CW2 and CW2 > CW1 and CW1 > CW3) , -20,
If((CW2 > CW3 and CW3 > CW4 and CW4 > CW1) or (CW4 > CW1 and CW1 > W2 and CW2 > CW3), -10, 0)))));

def DDCondition1 = If(DD1 > DD2 and DD2 > DD3 and DD3 > DD4, 5,
If((DD1 > DD2 and DD2 > DD4 and DD4 > DD3) or (DD1 > DD3 and DD3 > DD2 and DD2 > DD4) or
(DD2 > DD1 and DD1 > DD3 and DD3 > DD4), 4, If(DD2 > DD1 and DD1 > DD4 and DD4 > DD3, 3,
If((DD1 > DD3 and DD3 > DD4 and DD4 > DD2) or ( DD1 > DD4 and DD4 > DD2 and DD2 > DD3)  or
(DD2 > DD3 and DD3 > DD1 and DD1 > DD4)  or (DD3 > DD1 and DD1 > DD2 and DD2 > DD4) , 2,
If((DD1 > DD4 and DD4 > DD3 and DD3 > DD2) or (DD3 > DD2 and DD2 > DD1 and DD1 > DD4), 1, 0)))));
def DDCondition2 = If(DD4 > DD3 and DD3 > DD2 and DD2 > DD1, -5,
If((DD3 > DD4 and DD4 > DD2 and DD2 > DD1) or (DD4 > DD2 and DD2 > DD3 and DD3 > DD1) or
(DD4 > DD3 and DD3 > DD1 and DD1 > DD2), -4, If(DD3 > DD4 and DD4 > DD1 and DD1 > DD2, -3,
If((DD2 > DD4 and DD4 > DD3 and DD3 > DD1) or (DD3 > DD2 and DD2 > DD4 and DD4 > DD1)  or
(DD4 > DD1 and DD1 > DD3 and DD3 > DD2)  or (DD4 > DD2 and DD2 > DD1 and DD1 > DD3) , -2,
If((DD2 > DD3 and DD3 > DD4 and DD4 > DD1) or (DD4 > DD1 and DD1 > DD2 and DD2 > DD3), -1, 0)))));
def DWCondition1 = If(DW1 > DW2 and DW2 > DW3 and DW3 > DW4, 50,
If((DW1 > DW2 and DW2 > DW4 and DW4 > DW3) or (DW1 > DW3 and DW3 > DW2 and DW2 > DW4) or
(DW2 > DW1 and DW1 > DW3 and DW3 > DW4), 40, If(DW2 > DW1 and DW1 > DW4 and DW4 > DW3, 30,
If((DW1 > DW3 and DW3 > DW4 and DW4 > DW2) or ( DW1 > DW4 and DW4 > DW2 and DW2 > DW3)  or
(DW2 > DW3 and DW3 > DW1 and DW1 > DW4)  or (DW3 > DW1 and DW1 > DW2 and DW2 > DW4) , 20,
If((DW1 > DW4 and DW4 > DW3 and DW3 > DW2) or (DW3 > DW2 and DW2 > DW1 and DW1 > DW4), 10, 0)))));
def DWCondition2 = If(DW4 > DW3 and DW3 > DW2 and DW2 > DW1, -50,
If((DW3 > DW4 and DW4 > DW2 and DW2 > DW1) or (DW4 > DW2 and DW2 > DW3 and DW3 > DW1) or
(DW4 > DW3 and DW3 > DW1 and DW1 > DW2), -40, If(DW3 > DW4 and DW4 > DW1 and DW1 > DW2, -30,
If((DW2 > DW4 and DW4 > DW3 and DW3 > DW1) or (DW3 > DW2 and DW2 > DW4 and DW4 > DW1)  or
(DW4 > DW1 and DW1 > DW3 and DW3 > DW2)  or (DW4 > DW2 and DW2 > DW1 and DW1 > DW3) , -20,
If((DW2 > DW3 and DW3 > DW4 and DW4 > DW1) or (DW4 > DW1 and DW1 > DW2 and DW2 > DW3), -10, 0)))));

def EDCondition1 = If(ED1 > ED2 and ED2 > ED3 and ED3 > D4, 5,
If((ED1 > ED2 and ED2 > ED4 and ED4 > ED3) or (ED1 > ED3 and ED3 > ED2 and ED2 > ED4) or
(ED2 > ED1 and ED1 > ED3 and ED3 > ED4), 4, If(ED2 > ED1 and ED1 > ED4 and ED4 > ED3, 3,
If((ED1 > ED3 and ED3 > ED4 and ED4 > ED2) or ( ED1 > ED4 and ED4 > ED2 and ED2 > ED3)  or
(ED2 > ED3 and ED3 > ED1 and ED1 > ED4)  or (ED3 > ED1 and ED1 > ED2 and ED2 > ED4) , 2,
If((ED1 > ED4 and ED4 > ED3 and ED3 > ED2) or (ED3 > ED2 and ED2 > ED1 and ED1 > ED4), 1, 0)))));
def EDCondition2 = If(ED4 > ED3 and ED3 > ED2 and ED2 > ED1, -5,
If((ED3 > ED4 and ED4 > ED2 and ED2 > ED1) or (ED4 > ED2 and ED2 > ED3 and ED3 > ED1) or
(ED4 > ED3 and ED3 > ED1 and ED1 > ED2), -4, If(ED3 > ED4 and ED4 > ED1 and ED1 > ED2, -3,
If((ED2 > ED4 and ED4 > ED3 and ED3 > ED1) or (ED3 > ED2 and ED2 > ED4 and ED4 > ED1)  or
(ED4 > ED1 and ED1 > ED3 and ED3 > ED2)  or (ED4 > ED2 and ED2 > ED1 and ED1 > ED3) , -2,
If((ED2 > ED3 and ED3 > ED4 and ED4 > ED1) or (ED4 > ED1 and ED1 > ED2 and ED2 > ED3), -1, 0)))));
def EWCondition1 = If(EW1 > EW2 and EW2 > EW3 and EW3 > EW4, 50,
If((EW1 > EW2 and EW2 > EW4 and EW4 > EW3) or (EW1 > EW3 and EW3 > EW2 and EW2 > EW4) or
(EW2 > EW1 and EW1 > EW3 and EW3 > EW4), 40, If(EW2 > EW1 and EW1 > EW4 and EW4 > EW3, 30,
If((EW1 > EW3 and EW3 > EW4 and EW4 > EW2) or ( EW1 > EW4 and EW4 > EW2 and EW2 > EW3)  or
(EW2 > EW3 and EW3 > EW1 and EW1 > EW4)  or (EW3 > EW1 and EW1 > EW2 and EW2 > EW4) , 20,
If((EW1 > EW4 and EW4 > EW3 and EW3 > EW2) or (EW3 > EW2 and EW2 > EW1 and EW1 > EW4), 10, 0)))));
def EWCondition2 = If(EW4 > EW3 and EW3 > EW2 and EW2 > EW1, -50,
If((EW3 > EW4 and EW4 > EW2 and EW2 > EW1) or (EW4 > EW2 and EW2 > EW3 and EW3 > EW1) or
(EW4 > EW3 and EW3 > EW1 and EW1 > EW2), -40, If(EW3 > EW4 and EW4 > EW1 and EW1 > EW2, -30,
If((EW2 > EW4 and EW4 > EW3 and EW3 > EW1) or (EW3 > EW2 and EW2 > EW4 and EW4 > EW1)  or
(EW4 > EW1 and EW1 > EW3 and EW3 > EW2)  or (EW4 > EW2 and EW2 > EW1 and EW1 > EW3) , -20,
If((EW2 > EW3 and EW3 > EW4 and EW4 > EW1) or (EW4 > EW1 and EW1 > EW2 and EW2 > EW3), -10, 0)))));

def WT = (WCondition1+WCondition2);
def TT = (WCondition1+WCondition2+DCondition1+DCondition2);
def BWT = (BWCondition1+BWCondition2);
def BTT = (BWCondition1+BWCondition2+BDCondition1+BDCondition2);
def CWT = (CWCondition1+CWCondition2);
def CTT = (CWCondition1+CWCondition2+CDCondition1+CDCondition2);
def DWT = (DWCondition1+DWCondition2);
def DTT = (DWCondition1+DWCondition2+DDCondition1+DDCondition2);
def EWT = (EWCondition1+EWCondition2);
def ETT = (EWCondition1+EWCondition2+EDCondition1+EDCondition2);
#####signals
Def AXL = TT crosses above WT;
Def AXS = TT Crosses Below WT;
Def BXL = BTT crosses above BWT;
Def BXS = BTT Crosses Below BWT;
Def CXL = CTT crosses above CWT;
Def CXS = CTT Crosses Below CWT;
Def DXL = DTT crosses above DWT;
Def DXS = DTT Crosses Below DWT;
Def EXL = ETT crosses above EWT;
Def EXS = ETT Crosses Below EWT;

Def Along = TT > WT;
Def Ashort = TT < WT;
Def Blong = BTT > BWT;
Def Bshort = BTT < BWT;
Def Clong = CTT > CWT;
Def Cshort = CTT < CWT;
Def Dlong = DTT > DWT;
Def Dshort = DTT < DWT;
Def Elong = ETT > EWT;
Def Eshort = ETT < EWT;
###########Labels
#AddLabel( ALong, "Agg1 Long " , Color.Green);
#AddLabel( AXL, "Agg 1 Buy" , Color.Light_Green);
#AddLabel( AXS, "Agg 1 Sell" , Color.Light_Red);
#AddLabel( Ashort, "Agg 1 Short" , Color.Red);
#AddLabel( BLong, "Agg 2 Long " , Color.Green);
#AddLabel( BXL, "Agg 2 Buy" , Color.Light_Green);
#AddLabel( BXS, "Agg 2 Sell" , Color.Light_Red);
#AddLabel( Bshort, "Agg 2 Short" , Color.Red);
#AddLabel( CLong, "Agg3 Long " , Color.Green);
#AddLabel( CXL, "Agg 3 Buy" , Color.Light_Green);
#AddLabel( CXS, "Agg 3 Sell" , Color.Light_Red);
#AddLabel( CShort, "Agg 3 Short" , Color.Red);
#AddLabel( DLong, "Agg 4 Long " , Color.Green);
#AddLabel( DXL, "Agg 4 Buy" , Color.Light_Green);
#AddLabel( DXS, "Agg 4 Sell" , Color.Light_Red);
#AddLabel( DShort, "Agg 4 Short" , Color.Red);
#AddLabel( ELong, "Agg 5 Long " , Color.Green);
#AddLabel( EXL, "Agg 5 Buy" , Color.Light_Green);
#AddLabel( EXS, "Agg 5 Sell" , Color.Light_Red);
#AddLabel( EShort, "Agg 5 Short" , Color.Red);
###########Plots

plot R1_Dot = if IsNaN(c) then Double.NaN else 1;
R1_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R1_Dot.SetLineWeight(DotSize);
R1_Dot.AssignValueColor(if TT < WT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R2_Dot = if IsNaN(c2) then Double.NaN else 2;
R2_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R2_Dot.SetLineWeight(DotSize);
R2_Dot.AssignValueColor(if BTT < BWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R3_Dot = if IsNaN(c) then Double.NaN else 3;
R3_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R3_Dot.SetLineWeight(DotSize);
R3_Dot.AssignValueColor(if CTT < CWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R4_Dot = if IsNaN(c4) then Double.NaN else 4;
R4_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R4_Dot.SetLineWeight(DotSize);
R4_Dot.AssignValueColor(if DTT < DWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot R5_Dot = if IsNaN(c5) then Double.NaN else 5;
R5_Dot.SetPaintingStrategy(PaintingStrategy.TRIANGLES);
R5_Dot.SetLineWeight(DotSize);
R5_Dot.AssignValueColor(if ETT < EWT then GlobalColor("TrendDown") else GlobalColor("TrendUp"));

plot MTF_Dot = 6;
MTF_Dot.SetPaintingStrategy(PaintingStrategy.SQUARES);
MTF_Dot.SetLineWeight(lineWeight = 4);
MTF_Dot.DefineColor("Buy", Color.Green);
MTF_Dot.DefineColor("Sell", Color.red);
MTF_Dot.AssignValueColor ( if (ALong + BLong + CLong + DLong + ELong) >= 3 then MTF_Dot.Color("Buy") else MTF_Dot.Color("Sell"));
#assignPriceColor
assignPriceColor( if apc == 1 then if TT < WT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 2 then if BTT < BWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 3 then if CTT < CWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 4 then if DTT < DWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 5 then if ETT < EWT then GlobalColor("TrendDown") else GlobalColor("TrendUp") else if apc == 6 then if (ALong + BLong + CLong + DLong + ELong) >= 3 then MTF_Dot.Color("Buy") else MTF_Dot.Color("Sell") else Color.current);

This indicator shows a cyan dot when the Bullishness is greater then Bearishness and a magenta dot when bearish. The lowest time frame is on the bottom, the next higher time frame is line 2,and so forth, a consensus square is on line 6.

You can color the price by changing the APC input to which ever line # you want
Very cool. That is an intense code. I am more of a swing trader, will it mess up the indicator if I change the time frames to a higher aggragation?
 
Very cool. That is an intense code. I am more of a swing trader, will it mess up the indicator if I change the time frames to a higher aggragation?
what that indicator does ,it takes the short term averages and grades them as they intermingle and compares them against the long term average as they intermingle and plots a binary signal. It does that for 5 time frames. and then it also has a consensus line, it also allows the user to paint the price bars to visually see how it affects the price
 
Here is EMA Stacked script and Vwap script.

Code:
def stackedUp = MovAvgExponential("length" = 9)."AvgExp" is greater than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 50)."AvgExp"
and MovAvgExponential("length" = 75)."AvgExp" is greater than MovAvgExponential("length" = 100)."AvgExp";


def stackedDn = MovAvgExponential("length" = 9)."AvgExp" is less than MovAvgExponential("length" = 21)."AvgExp"
and MovAvgExponential("length" = 21)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 50)."AvgExp"
and MovAvgExponential("length" = 75)."AvgExp" is less than MovAvgExponential("length" = 100)."AvgExp";


AddLabel(yes, " EMA 9-100 Stacked ", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
#END

Color vwap script with label

input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

def cap = getAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = getYyyyMmDd();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((daysFromDate(first(yyyyMmDd)) + getDayOfWeek(first(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = roundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = compoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = compoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = compoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = compoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;
def deviation = Sqrt(Max(volumeVwap2Sum / volumeSum - Sqr(price), 0));

plot VWAP = price;
plot UpperBand = price + numDevUp * deviation;
plot LowerBand = price + numDevDn * deviation;

VWAP.setDefaultColor(getColor(0));
VWAP.AssignValueColor(if close>=VWAP then Color.Green else Color.magenta);
UpperBand.setDefaultColor(getColor(2));
LowerBand.setDefaultColor(getColor(4));

AddLabel(yes, if close > vwap then "Above VWAP(Up Trend,Call's only) " else " Below VWAP(Down Trend,Put's only) ", if close > vwap then Color.GREEN else Color.RED);

#END
 
Last edited by a moderator:
Code:
def EMA8 = ExpAverage(close,8);
def EMA13 = ExpAverage(close,13);
def EMA21 = ExpAverage(close,21);
def EMA34 = ExpAverage(close,34);
def bullish = EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34;
def bearish = EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34;
AddLabel(bullish, “Stacked MAs”, color.black);
AddLabel(bearish, “Stacked MAs”, color.black);
AddLabel(!bullish and !bearish, " ", color.black);
AssignbackgroundColor(if EMA8 > EMA13 and EMA13 > EMA21 and EMA21 > EMA34 then color.green else if EMA8 < EMA13 and EMA13 < EMA21 and EMA21 < EMA34 then color.red else color.black);

Its changing my whole chart Green
 
delete the bottom four lines and replace it with something like this.

AddLabel(yes, “Stacked MAs”, if bullish then color.green else if bearish then color.red else color.black);
 
Hello --

I'm trying to create a watchlist icon that is Dark Green or Dark Red when the following criteria are met:

EMA 5 is greater than EMA 12
EMA 34 is greater than EMA 50
EMA 72 is greater than EMA 89
All of the above is greater than EMA 200

I modified the following script from another thread here, however I'm running into a couple of issues triggering it that are beyond my ability to script here. What I'm noticing is that it is turning green when all 3 things above happen, but not necessarily stacked atop one another.

I would appreciate some edits to my script on the following:

1) The alert is triggered when all 3 things are happening, however I want all 3 to be over each other: 5 > 12 > 34 > 50 > 72 > 89 (bonus points if it is also above 200 EMA, but not a requirement). I'd like the watchlist to be Dark Green if these are all met.

2) I'm not sure what triggers Grey coming into play, but ideally if any part of the 5 > 12 > 34 > 50 > 72 > 89 are NOT occurring but it is over the 200 EMA, it would be Grey.

3) If any part of the 5 > 12 > 34 > 50 > 72 > 89 are NOT occurring but it is under the 200 EMA, it would be Dark Red.

Here is the script so far (I know the 200 EMA stuff is not in there, but there are definitely other problems)

input lookback = 1; # past N minutes
def condition_up =ExpAverage(close[72],89) < ExpAverage(close[50],89)
and
ExpAverage(close[50],89) < ExpAverage(close[34],89)
and
ExpAverage(close[34],89) < ExpAverage(close[12],89)
and
ExpAverage(close[5],89) < ExpAverage(close,89);

plot condition_down =ExpAverage(close[72],89) > ExpAverage(close[50],89)
and
ExpAverage(close[50],89)> ExpAverage(close[34],89)
and
ExpAverage(close[34],89) > ExpAverage(close[12],89)
and
ExpAverage(close[5],89) > ExpAverage(close,89);

AssignBackgroundColor(if condition_up then color.darK_GREEN
else if condition_down then color.dark_RED
else color.gray);
AssignPriceColor(if condition_up then color.darK_GREEN
else if condition_down then color.dark_RED
else color.gray);

# end code

Thank you
 
@abestinkin8 Welcome! I believe you are on the right track!
Most of the stacking logic, that I have seen, need a few more lines, in order to get them stacked. Try adding the bolded logic and see if your script gives you the results you are looking for.
EMA 5 is greater than EMA 12
EMA 12 is greater than EMA 34
EMA 34 is greater than EMA 50
EMA 50 is greater than EMA 72
EMA 72 is greater than EMA 89
 
In searching for something completely different I came across a thread here that broke it down in a way that made more sense to meThat thread is here: https://usethinkscript.com/threads/here-are-all-flavors-of-ema-sma-vwap-stacked-indicators.4719/


My modified code for stacking the EMAs and making it a watchlist column is as follows:

input lookback = 1; # past N minutes

def stackedUp = MovAvgExponential("length" = 5)."AvgExp" is greater than MovAvgExponential("length" = 12)."AvgExp"
and MovAvgExponential("length" = 12)."AvgExp" is greater than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is greater than MovAvgExponential("length" = 50)."AvgExp"
and MovAvgExponential("length" = 50)."AvgExp" is greater than MovAvgExponential("length" = 72)."AvgExp"
and MovAvgExponential("length" = 72)."AvgExp" is greater than MovAvgExponential("length" = 89)."AvgExp";


plot stackedDn = MovAvgExponential("length" = 5)."AvgExp"
and MovAvgExponential("length" = 12)."AvgExp" is less than MovAvgExponential("length" = 34)."AvgExp"
and MovAvgExponential("length" = 34)."AvgExp" is less than MovAvgExponential("length" = 50)."AvgExp"
and MovAvgExponential("length" = 50)."AvgExp" is less than MovAvgExponential("length" = 72)."AvgExp"
and MovAvgExponential("length" = 72)."AvgExp" is less than MovAvgExponential("length" = 89)."AvgExp";


AssignBackgroundColor(if stackedup then color.darK_GREEN
else if stackeddn then color.dark_RED
else color.gray);
AssignPriceColor(if stackedup then color.darK_GREEN
else if stackeddn then color.dark_RED
else color.gray);


EDIT: I have not tested this live yet, but in poking around it SEEMS to work. I will confirm if I remember
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
189 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top