Stacked Moving Averages

henry1224

Active member
VIP
Dear JIMIandJAY, 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);
 

Jimiandjaymason

New member
Dear JIMIandJAY, 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.
 

henry1224

Active member
VIP
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
 

henry1224

Active member
VIP
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
 

Jimiandjaymason

New member
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?
 

henry1224

Active member
VIP
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
 

mike meta

New member
@rad14733 Thank you. Can you help to format this code for scanning?

Thank you. Some times moving averages doesn't actually line up/down, but you will see a big move when they are very close to each other. Please see if you can build a code with some percentage of distance between the moving averages. Thank you.
did you manage to get a code for this? its exactly what im looking for. thanks
 

s1111

Active member
@VenB @mike meta .This script tells you percentage between whatever ema distance you choose , hope this helps.
input price = close;
input length_1 = 9;
input length_2 = 20;
input AverageType = AverageType.EXPONENTIAL;

def MA_1 = MovingAverage(AverageType, price, length_1);
def MA_2 = MovingAverage(AverageType, price, length_2);

def MA_delta = Round(MA_1 - MA_2, 2);

AddLabel(yes, MA_delta, if MA_delta > 0 then Color.GREEN else Color.RED);
 

Partner

New member
VIP
@VenB @mike meta .This script tells you percentage between whatever ema distance you choose , hope this helps.
input price = close;
input length_1 = 9;
input length_2 = 20;
input AverageType = AverageType.EXPONENTIAL;

def MA_1 = MovingAverage(AverageType, price, length_1);
def MA_2 = MovingAverage(AverageType, price, length_2);

def MA_delta = Round(MA_1 - MA_2, 2);

AddLabel(yes, MA_delta, if MA_delta > 0 then Color.GREEN else Color.RED);
Thank you S1111. What you have provided is somewhat similar to what we are looking for.
Can you help to create a script to identify the ticker when stacking VWAP, 200 SMA, 9 EMA, 21 SMA, 34 EMA & 89 EMA are together - The percentage between all the MA & VWAP distance is less than 0.25
What I have observed when they merged together we normally see a big move in a ticker on either side.
 

s1111

Active member
Thank you S1111. What you have provided is somewhat similar to what we are looking for.
Can you help to create a script to identify the ticker when stacking VWAP, 200 SMA, 9 EMA, 21 SMA, 34 EMA & 89 EMA are together - The percentage between all the MA & VWAP distance is less than 0.25
What I have observed when they merged together we normally see a big move in a ticker on either side.
I'm sorry I'm not a coder, wish I could. I also notice that if above/below vwap and EMA stacked Tsla moves 15-20 bucks eithere side. All the script I use is from here , I just mod it few things here and there to make it the way I like to see .

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:

henry1224

Active member
VIP
Is there an updated version? I have tried to make it work and nothing shows up when I copy/paste. Not a programmer but do not see any errors. TY
If using intraday time frames, make sure that you have enough data bars loaded
HdcKAhh.png
 
Last edited:

abestinkin8

New member
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
 

MerryDay

Administrative
Staff member
Staff
VIP
@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
 

abestinkin8

New member
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:

MerryDay

Administrative
Staff member
Staff
VIP
@abestinkin8 That 'looks' exactly right. That is the exact logic that I explained to you. To make a stacked EMA. The moving averages have to be daisy-chained together. Your original post was missing the averages that connected the last one w/ the next one.

The logic seems right. I am not in front of my app so I can't check for syntax errors. Give it a roll. Congrats!
 

abestinkin8

New member
I did some more work and came up with a second watchlist column that would be green/red depending on whether the EMA5 and EMA89 were within a certain percentage of one another. (Because the scripts would be stacked, I think that those would be the only 2 that matter).

The following script lights up Dark Green if EMA5 and EMA 89 are within .1 of each other. It lights up Dark Red if they are more than 1 of teacher other. It's Gray the rest of the time. I'm not exactly sure what the .1 / 1 measure, but it seems to track where the closer the number is to 0, the more consolidated the trend lines become. I'm posting this in the hopes that other people might shed light on what the numbers mean, and if there are better metrics to use than .1 / 1.

Code:
input lookback = 5; # past N minutes

Def a = MovAvgExponential(“length” = 5);
Def b = MovAvgExponential(“length” = 89);
Def d = a-b;
Plot p = (d / b) * 100;

Def Good = p <= .1 && p >= -.1;
Def Bad1 = p >= 1;
Def Bad2 = p <= -1;

AssignBackgroundColor(if Good then color.darK_GREEN
else if Bad1 then color.dark_RED else if Bad2 then color.dark_RED
else color.gray);
 
Last edited by a moderator:

rad14733

Well-known member
VIP
It was the study you referenced on this thread’s first page - combination of EMA stack & TTM.

Thanks

I never had a combined indicator but here is my EMA_x5_Stack script... I could add a bunch of modifications but don't use this indicator much these days...

My TTM_Squeeze_Clouds indicator is here in the forums... I had a hard time finding it as several similar topics were merged some time ago... I have other TTM_Squeeze and TTM_Squeeze_Pro related code that I have written yet are not submitted to the forums...

Ruby:
#EMA_x5_Stack
#Created by rad14733 for usethinkscript.com
#v1.0 2021-01-02
#NOTE: EMA crossovers can be set to only paint based on long term trends.
#      When configured, EMA crossovers will only paint when filtered by longer term trends (ema1 <> ema5).
# EMA Length's
input ema1Length = 8;
input ema2Length = 21;
input ema3Length = 34;
input ema4Length = 55;
input ema5Length = 89;
input showXovers = yes;

# Display EMA's Stacked Label
input show_stacked_label = yes;
input show_ema_label = yes;

# Conditionally Plot EMA's
plot ema1 = ExpAverage(close, ema1Length);
ema1.SetPaintingStrategy(PaintingStrategy.LINE);
ema1.SetStyle(Curve.FIRM);
ema1.SetLineWeight(1);
ema1.SetDefaultColor(Color.GREEN);
plot ema2 = ExpAverage(close, ema2Length);
ema2.SetPaintingStrategy(PaintingStrategy.LINE);
ema2.SetStyle(Curve.FIRM);
ema2.SetLineWeight(1);
ema2.SetDefaultColor(Color.YELLOW);
plot ema3 = ExpAverage(close, ema3Length);
ema3.SetPaintingStrategy(PaintingStrategy.LINE);
ema3.SetStyle(Curve.FIRM);
ema3.SetLineWeight(1);
ema3.SetDefaultColor(Color.RED);
plot ema4 = ExpAverage(close, ema4Length);
ema4.SetPaintingStrategy(PaintingStrategy.LINE);
ema4.SetStyle(Curve.FIRM);
ema4.SetLineWeight(1);
ema4.SetDefaultColor(Color.MAGENTA);
plot ema5 = ExpAverage(close, ema5Length);
ema5.SetPaintingStrategy(PaintingStrategy.LINE);
ema5.SetStyle(Curve.FIRM);
ema5.SetLineWeight(1);
ema5.SetDefaultColor(Color.BLUE);

# Calculate EMA Crossovers
plot x1a2 = showXovers and ema1 crosses above ema2;
x1a2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
x1a2.SetStyle(Curve.FIRM);
x1a2.SetLineWeight(3);
x1a2.SetDefaultColor(Color.YELLOW);
plot x1b2 = showXovers and ema1 crosses below ema2;
x1b2.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_DOWN);
x1b2.SetStyle(Curve.FIRM);
x1b2.SetLineWeight(3);
x1b2.SetDefaultColor(Color.DARK_ORANGE);
# Define Global Colors For Clouds
# Crosses Above
DefineGlobalCOlor("x1a2", Color.GREEN);
DefineGlobalCOlor("x2a3", Color.DARK_GREEN);
DefineGlobalCOlor("x3a4", Color.YELLOW);
DefineGlobalCOlor("x4a5", Color.WHITE);
# Crosses Below
DefineGlobalCOlor("x1b2", Color.RED);
DefineGlobalCOlor("x2b3", Color.DARK_RED);
DefineGlobalCOlor("x3b4", Color.MAGENTA);
DefineGlobalCOlor("x4b5", Color.BLUE);

# Paint Clouds
input showClouds = yes;
AddCloud(if showClouds then ema1 else Double.NaN, if showClouds then ema2 else Double.NaN, GlobalColor("x1a2"), GlobalColor("x1b2"));
AddCloud(ema2, ema3, GlobalColor("x2a3"), GlobalColor("x2b3"));
AddCloud(ema3, ema4, GlobalColor("x3a4"), GlobalColor("x3b4"));
AddCloud(ema4, ema5, GlobalColor("x4a5"), GlobalColor("x4b5"));

# Create Chart Label to signify that EMA's are Stacked
def stackedUp = ema1 > ema2 and ema2 > ema3 and ema3 > ema4 and ema4 > ema5;
def stackedDn = ema1 < ema2 and ema2 < ema3 and ema3 < ema4 and ema4 < ema5;
AddLabel(show_ema_label, "" + ema1Length + "|" + ema2Length + "|" + ema3Length + "|" + ema4Length + "|" + ema5Length + "|" + "EMAs", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
AddLabel(show_stacked_label, "EMAs Stacked", if stackedUp then Color.GREEN else if stackedDn then Color.RED else Color.GRAY);
# END EMA_5_Stack
 

Similar threads

Top