Looking for Tom DeMark’s TD Sequential in ThinkorSwim

S

Srt4ever

New member
VIP
Looking for someone who can help create a TD Sequential Indicator. I would like it to have the arrows right above or below the candle as well as having the numbers displayed on the candles followed by the upcoming play as seen in the link to the website that explains how the TD works.


Website- https://tradetrekker.wordpress.com/tdsequential/
 
Last edited:
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
Did you check out the default SequenceCounter indicator in ThinkorSwim? If not, you should.

Here are a few custom DeMark Sequence indicators I found in the thinkScript lounge.

Mobius' Version

Code:
# TD Buy Setup and Countdown Sequence
# Mobius
# V02.  Fixed 0 plot error

# Once TD Setup is complete, TD Countdown can begin, from the close
# of bar nine of TD Setup (inclusive), onward.
# • TD Setup compares the current close with the corresponding close
# four bars earlier,
# Whereas
# • TD Countdown compares the current close with the low two bars
# earlier for a potential buy, and compares the current close with the
# high two bars earlier for a prospective sell. This price relationship is
# an important distinction from TD Setup, because the market must
# be trending for TD Countdown to objectively identify the likely
# exhaustion point for a trend reversal.
# The first bar of a TD Buy Countdown occurs when a TD Buy Setup is in place.
# ? To Initiate TD Buy Countdown
# After
# TD Buy Setup is in place, initiation of a TD Buy Countdown.
# If
# Bar nine of a TD Buy Setup also has a close less than, or equal to, the low two bars
# earlier.
# Then,
# Bar nine of a TD Buy Setup becomes bar one of a TD Buy Countdown.
# If
# That condition is not met,
# Then
# TD Buy Countdown bar one is postponed until it does, and the TD Buy Countdown
# continues until there are a total of thirteen closes, each one less than, or equal to, # the low two bars earlier.
# ? To Complete a TD Buy Countdown
# 1. The low of TD Buy Countdown bar thirteen must be less than, or equal to, the
# close of TD Buy Countdown bar eight, and
# 2. The close of TD Buy Countdown bar thirteen must be less than, or equal to, the
# low two bars earlier.
# TD Buy is Broken if Price does not exceed 1.5 times 9 bar ATR or
# price breaks down below entry Bar low.

def SetUpCount = CompoundValue(1, if close < close[4]
                 then SetUpCount[1] + 1
                 else if close > close[4]
                 then 0
                 else SetUpCount[1], 1);

plot Count = if SetUpCount == 0 or
                SetUpCount == SetUPCount[1] or
                SetUpCount > 9
             then double.nan
             else SetUpCount;
     Count.SetPaintingStrategy(PaintingStrategy.Values_Above);
def Setup1 = if SetUpCount == 9 and
                close <= low[2]
             then 1
             else if SetUpCount < 9
                  then 0
             else if SetUpCount > 9 and
                     close <= low[2]
                  then Setup1[1] + 1
             else Setup1[1];

def BuyPoint1 = if Setup1 >= 13
           then high
           else BuyPoint1[1];
plot BuyPoint1Line = if BuyPoint1 > 0 then BuyPoint1 else Double.NaN;
     BuyPoint1Line.SetPaintingStrategy(PaintingStrategy.Dashes);  
def Bar8 = if SetUpCount == 8
           then BarNumber()
           else Bar8[1];
def Bar8close = if barNumber() == Bar8
              then close
              else Bar8close[1];
def Bar9 = if SetUpCount == 9
           then BarNumber()
           else Bar9[1];

def SetUp2 = if BarNumber() > Bar9 and
                low <= bar8close and
                close < low[2]
             then high
             else SetUp2[1];
plot BuyPoint2Line = highestAll(if isNaN(close[-1])
                             then SetUp2
                             else Double.NaN);
     BuyPoint2Line.SetPaintingStrategy(PaintingStrategy.Dashes);
plot BuyCross1 = close crosses above BuyPoint1;
     BuyCross1.SetPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
     BuyCross1.SetDefaultColor(Color.Plum);
plot BuyCross2 = close crosses above SetUp2;
     BuyCross2.SetPaintingStrategy(PaintingStrategy.Boolean_Arrow_Up);
     BuyCross2.SetDefaultColor(Color.Green);
   
# End Code TD Buy Setup and Countdown Sequence
Here is another one, this is to be added as a Strategy.

Code:
# DeMark Sequence
# Mobius
# V003 012012

input count = 9;
input contracts = 2;

def o = open;
def h = high;
def l = low;
def c = close;

rec BuySwitch = if c[1] > c[5]
                then 1
                else if c[1] > c[4]
                then 0
                else BuySwitch[1];


rec BuyCountdown = fold i = 0 to 100
                   with p = if buyswitch == 1 then 1 else double.nan
                   while if BuySwitch[1] == 1 then 1 else double.nan
                   do if c[1] <= c[4]
                         then BuyCountdown[1] + 1
                         else if c[1] > c[4]
                         then double.nan
                         else BuyCountdown[1];

plot BuySequence = BuyCountdown;
BuySequence.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);

def BuyPoint = if BuySequence == count then c[1] else double.nan;

#AddOrder(type = OrderType.BUY_AUTO, condition = c > BuyPoint, price = open, tradeSize = contracts, tickcolor = Color.GREEN, arrowcolor = Color.GREEN, name = concat("", o));
                 
rec SellSwitch = if c[1] < c[5]
                then 1
                else if c[1] < c[4]
                then 0
                else SellSwitch[1];


rec SellCountdown = fold j = 0 to 100
                   with jp = if Sellswitch == 1 then 1 else double.nan
                   while if SellSwitch[1] == 1 then 1 else double.nan
                   do if c[1] <= c[4]
                         then SellCountdown[1] + 1
                         else if c[1] > c[4]
                         then double.nan
                         else SellCountdown[1];

plot SellSequence = SellCountdown;
SellSequence.SetPaintingStrategy(PaintingStrategy.VALUES_Below);

def SellPoint = if SellSequence == count then l[1] else double.nan;

#AddOrder(type = OrderType.Sell_AUTO, condition = c < SellPoint, price = open, tradeSize = contracts, tickcolor = Color.red, arrowcolor = Color.red, name = concat("", o));

# AddLabel(yes, concat("BuySwitch ", BuySwitch), color.white);
# AddLabel(yes, concat("BuyCountdown ", BuyCountdown), color.white);
# AddLabel(yes, concat("SellSwitch ", SellSwitch), color.white);
# AddLabel(yes, concat("SellCountdown ", SellCountdown), color.white);

Dilbert's Version

Code:
## 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);
 
S

Srt4ever

New member
VIP
Thanks Ben, the first script you posted I notice it only has green arrows on the bottom, will red arrows come on top to list a sell or no ?
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
I have no idea. Never tested it. You should look through different tickers and timeframes to see if they show up. Looking at the source code, though, there is only buy signals. I don't see anything for sell signals.
 
S

Srt4ever

New member
VIP
Could sell signals be incorporated into the one you posted ?
 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
@Srt4ever The first script was only a Buy setup. However, the one after that appears to have sell signals as well. See if that is similar to what you're looking for.
 
S

Srt4ever

New member
VIP
@Srt4ever The first script was only a Buy setup. However, the one after that appears to have sell signals as well. See if that is similar to what you're looking for.
Thats to be posted into strategy not studies, can I post it into studies ? here is an example...

 
BenTen

BenTen

Administrative
Staff
VIP
Warehouse
As far as I know, the default SequenceCounter in ThinkorSwim is the closest thing to TD Sequential. Even some of the comments on that page say the same.
 

Top