```
## OneNote Archive Name: Sequence Counter _Dilbert
## Archive Section:
## Suggested Tos Name: SequenceCounter_Dilbert
## Archive Date: 5.14.2018
## Archive Notes:
## "##" indicates an addition or adjustment by the OneNote Archivist
## Original Code Follows
#Dilbert_SC
# V1.5 - 021117 - Dilbert - Inputs to move arrows and bubbles
# V1.4 - 121316 - Dilbert - Make Length2 bubble color.cyan
# V1.3 - 121316 - Dilbert - Add 9 count support resistance line, and 13 count risk line. Use same colors as vertical lines.
# V1.2 - 121016 - Dilbert - Use Length1/Length2 all the time instead of hardcode.
# V1.1 - 121016 - Dilbert - UT13Bar/DT13Bar only if UT13/DT13 == 1
# V1.0 - 121016 - Dilbert - 1st code cut, beta version still
# Posted by Amalia on 03.02.2018
#Hint Length1: Number of bars in setup
input Length1 = 9;
#Hint Length2: Number of bars in countdown
input Length2 = 13;
#Hint HorizontalLines: 1 == show, 0 == hide
input HorizontalLines = 1;
#Hint Bubbles: 1 == show, 0 == hide
input Bubbles = 1;
def H = high;
def L = low;
def C = close;
def BN = BarNumber();
def TS = TickSize();
input BubbleMult = 10; # Move Bubble up/down
input ArrowMult = 50; # Move Arrow up/down
#######################################################################################
# Count to 9 with down trend
def DT9 = if C < C[4] then 1 else 0;
def DT9Sum = if DT9 == 0
then 0
else if DT9Sum[1] == Length1 and DT9 == 1 # if we reach 9 count to 9 again
then 1
else DT9Sum[1] + DT9;
#plot DT9Sum2 = DT9Sum;
def DT9Sum2 = DT9Sum;
plot DT9SumP = if DT9Sum != 0
and DT9Sum < Length1 + 1
then DT9Sum
else Double.NaN;
DT9SumP.SetPaintingStrategy(paintingStrategy = PaintingStrategy.VALUES_BELOW);
DT9SumP.AssignValueColor(Color.WHITE);
######################################################################################
#
# Count to 9 with up trend
def UT9X = if C > C[4] then 1 else 0;
def UT9 = if UT9X == 1
then 1
else if UT9X == 1
then 1
else 0;
def UT9Sum = if UT9 == 0
then 0
else if UT9Sum[1] == Length1 and UT9 == 1 # if we reach 9 count to 9 again
then 1
else UT9Sum[1] + UT9;
def UT9Sum2 = UT9Sum;
plot UT9SumP = if UT9Sum != 0
and UT9Sum < Length1 + 1
then UT9Sum
else Double.NaN;
UT9SumP.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
UT9SumP.AssignValueColor(Color.WHITE);
#####################################################################################
# UT9Bar/DT9BAR logic
def DT9Bar = if DT9Sum == Length1 then BN else DT9Bar[1];
def UT9Bar = if UT9Sum == Length1 then BN else UT9Bar[1];
# Cancel the countdown if the opposite pattern is found
def DT9Active = if DT9Bar == BN then 1
else if UT9Bar == BN then 0
else DT9Active[1];
def UT9Active = if UT9Bar == BN then 1
else if DT9Bar == BN then 0
else UT9Active[1];
#####################################################################################
# 9 count trend lines
def UTH9 = if UT9Bar == BN then Highest(H,9) else UTH9[1];
def DTH9 = if DT9Bar == BN then Highest(H,9) else DTH9[1];
def UTL9 = if UT9Bar == BN then Lowest(L,9) else UTL9[1];
def DTL9 = if DT9Bar == BN then Lowest(L,9) else DTL9[1];
plot UTH9P = if HorizontalLines then UTH9 else Double.NaN;
plot DTH9P = if HorizontalLines then DTH9 else Double.NaN;
plot UTL9P = if HorizontalLines then UTL9 else Double.NaN;
plot DTL9P = if HorizontalLines then DTL9 else Double.NaN;
UTH9P.SetDefaultColor(Color.Cyan);
DTH9P.SetDefaultColor(Color.Orange);
UTL9P.SetDefaultColor(Color.Cyan);
DTL9P.SetDefaultColor(Color.Orange);
#####################################################################################
# Count to 13 in down trend
def DT13 = if BN >= DT9Bar and DT9Active == 1 and C <= L[2] then 1 else 0;
def DT13Sum = if DT9Bar == BN
then DT13
else DT13Sum[1] + DT13;
def DT13SumP = if DT13Sum != 0
and DT13Sum < Length2 + 1
and DT13Sum != DT13Sum[1]
then DT13Sum
else Double.NaN;
AddChartBubble(DT13Sum != 0 and Bubbles == 1
and DT13Sum < Length2 + 1
and DT13Sum != DT13Sum[1], L - TS * BubbleMult, DT13Sum, if DT13Sum == Length2 then Color.Cyan else Color.GREEN, no);
#####################################################################################
# Down Trend 13 risk line
def DT13LL;
def DT13LLBarRange;
if DT9Bar == BN
then {
DT13LL = L;
DT13LLBarRange = H - L;
}
else if DT9Active and DT13Sum <= 13 and DT13 == 1 and L < DT13LL[1]
then {
DT13LL = L;
DT13LLBarRange = H - L;
}
else {
DT13LL = DT13LL[1];
DT13LLBarRange = DT13LLBarRange[1];
};
plot DT13Risk = if HorizontalLines then DT13LL - DT13LLBarRange else Double.NaN;
DT13Risk.SetDefaultColor(Color.Green);
#####################################################################################
# Count to 13 in up trend
def UT13 = if BN >= UT9Bar and UT9Active == 1 and C >= H[2] then 1 else 0;
def UT13Sum = if UT9Bar == BN
then UT13
else UT13Sum[1] + UT13;
def UT13SumP = if UT13Sum != 0
and UT13Sum < Length2 + 1
and UT13Sum != UT13Sum[1]
then UT13Sum
else Double.NaN;
AddChartBubble(UT13Sum != 0 and Bubbles == 1
and UT13Sum < Length2 + 1
and UT13Sum != UT13Sum[1], H + TS * BubbleMult, UT13Sum, if UT13Sum == Length2 then Color.Cyan else Color.RED, yes);
#####################################################################################
# Up Trend 13 risk line
def UT13HH;
def UT13HHBarRange;
if UT9Bar == BN
then {
UT13HH = H;
UT13HHBarRange = H - L;
}
else if UT9Active and UT13Sum <= Length2 and UT13 == 1 and H > UT13HH[1]
then {
UT13HH = H;
UT13HHBarRange = H - L;
}
else {
UT13HH = UT13HH[1];
UT13HHBarRange = UT13HHBarRange[1];
};
plot UT13Risk = if HorizontalLines then UT13HH + UT13HHBarRange else Double.NaN;
UT13Risk.SetDefaultColor(Color.Red);
#####################################################################################
# UT13Bar/DT13BAR logic
def DT13Bar = if DT13Sum == Length2 and DT13 == 1 then BN else DT13Bar[1];
def UT13Bar = if UT13Sum == Length2 and UT13 == 1 then BN else UT13Bar[1];
# Cancel the countdown if the opposite pattern is found
#def DT13Active = if DT9Bar == BN then 1
# else if UT9Bar == BN then 0
# else DT13Active[1];
#def UT13Active = if UT9Bar == BN then 1
# else if DT9Bar == BN then 0
# else UT13Active[1];
######################################################################################
#Perfect 9 down trend logic
plot P9Buy = if DT9Bar == BN and (L < L[2] and L < L[3] or L[1] < L[2] and L[1] < L[3])
then L - TS * ArrowMult else Double.NaN;
P9Buy.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
P9Buy.AssignValueColor(Color.WHITE);
######################################################################################
######################################################################################
#Perfect 9 up trend logic
plot P9Sell = if UT9Bar == BN and (H > H[2] and high > high[3] or H[1] > H[2] and H[1] > high[3]) then H + TS * ArrowMult else Double.NaN;
P9Sell.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
P9Sell.AssignValueColor(Color.WHITE);
######################################################################################
#Perfect 13 down trend logic
plot P13Buy = if DT13Bar == BN and L <= C[5] then L - TS * ArrowMult else Double.NaN;
P13Buy.SetPaintingStrategy(PaintingStrategy.ARROW_Up);
P13Buy.AssignValueColor(Color.RED);
######################################################################################
#Perfect 13 up trend logic
plot P13Sell = if UT13Bar == BN and H >= C[5]then H + TS * ArrowMult else Double.NaN;
P13Sell.SetPaintingStrategy(PaintingStrategy.ARROW_Down);
P13Sell.AssignValueColor(Color.RED);
AddVerticalLine(DT9Bar == BN, "", Color.ORANGE, curve.SHORT_DASH);
AddVerticalLine(UT9Bar == BN, "", Color.Cyan, curve.SHORT_DASH);
AddVerticalLine(DT13Bar == BN, "", Color.Green, curve.SHORT_DASH);
AddVerticalLine(UT13Bar == BN, "", Color.Red, curve.SHORT_DASH);
```