Sequential Indicators for ThinkorSwim

Srt4ever

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

Administrative
Staff member
Staff
VIP
Lifetime
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);
 

Srt4ever

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

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

BenTen

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

Srt4ever

New member
2019 Donor
@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

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

febreezus

New member
I'm trying to implement a simplified version of the logic behind this strategy, but I'm having trouble. Below is the code I've written so far. The code is a mess, I know - I can't figure out the order in which to assign the variables because each line requires another variable.

The idea is to trace trends. We are in an uptrend until we cross below the open of the bar 4 bars before the latest high... whereupon we switch to a downtrend, and remain so for as long as we make lower lows from this point... until we cross back above the open 4 bars before the latest low... etc.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def firstBarOfDay = if (beforeStart[5] == 1 and beforeStart[4] == 0) or (isRollover[4] and beforeStart[4] == 0) then 1 else 0;


def hi =  if firstBarOfDay then if close > open[4] then close else open[4] else
                 if dir[1] == -1 and close > brk or dir[1] == 11 and close > hi[1] then close else hi[1];
def lo =  if firstBarOfDay then if close < open[4] then close else open[4] else
                 if dir[1] == 1 and close < brk or dir[1] == -1 and close < lo[1] then close else lo[1];
def dir = if firstBarOfDay then (if close < open[4] then -1 else if close > open[4] then 1 else 0) else
                 if close>brk then 1 else if close<brk then -1 else dir[1];
def inc = if firstBarOfDay then 4 else
                 if brk==brk[1] then inc[1]+1 else 4;
def brk = if firstBarOfDay then open[4] else
                 if close>hi[1] or close<lo[1] then open[4] else GetValue(close,inc);


Anyone have any suggestions?
 

febreezus

New member
I think I mostly figured it out, although it still needs a little tweaking.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def bar2 = if (beforeStart[2] == 1 and beforeStart[1] == 0) or (isRollover[1] and beforeStart[1] == 0) then 1 else 0;
def bar3 = if (beforeStart[3] == 1 and beforeStart[2] == 0) or (isRollover[2] and beforeStart[2] == 0) then 1 else 0;
def bar4 = if (beforeStart[4] == 1 and beforeStart[3] == 0) or (isRollover[3] and beforeStart[3] == 0) then 1 else 0;

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
                 if (close>brk[1] and close>close[1]) or (close<brk[1] and close<close[1]) then open[4] else brk[1];
def hi =  if bar1 then if close > open then close else open else if bar2 or bar3 or bar4 then hi[1] else
                 if open[1] <= brk[1] and close > brk[1] then open else hi[1];
def lo =  if bar1 then if close > open then open else open else if bar2 or bar3 or bar4 then lo[1] else
                 if open[1] >= brk[1] and close < brk[1] then close else lo[1];
def dir = if bar1 or bar2 or bar3 then 0 else if bar4 then (if close < open[4] then -1 else if close > open[4] then 1 else 0) else
                 if close>brk then 1 else if close<brk then -1 else dir[1];


plot dir_plot = round(dir,0);
dir_plot.SetPaintingStrategy(PaintingStrategy.VALUES_ABOVE);
dir_plot.SetDefaultColor(Color.white);
 

febreezus

New member
The main problem I can see with the code right now is that the "def brk" line should ideally read

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[4] else brk[1];

instead of

def brk = if bar1 then open else if bar2 or bar3 or bar4 then open[1] else
if (close>brk[1] and close>close[1]) or (close<brk[1] and close<close[1]) then open[4] else brk[1];

But obviously I can't reference "hi" and "lo" before defining them, and I need to reference "brk" when defining "hi" and "lo"... hence my conundrum. Any help would be appreciated.
 

febreezus

New member
Robert Payne helped me to figure out the variable issue. The code still needs work, but here it is as I've got it now.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk;
def hi;
def lo;
def dir;
def brk_lookback = 4;

brk = if bar1 then open else
                 if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[brk_lookback] else brk[1];
hi =  if bar1 then if close > open then close else open else
                 if dir[1] == -1 and close > brk[1] or (dir == 1 and close > hi[1]) then close else hi[1];
lo =  if bar1 then if close > open then open else open else
                 if dir[1] == 1 and close < brk[1] or dir == -1 and close < lo[1] then close else lo[1];
dir = if bar1 then (if close < open[brk_lookback] then -1 else if close > open[brk_lookback] then 1 else 0) else
                 if close>brk[1] then 1 else if close<brk[1] then -1 else dir[1];


AssignPriceColor(if dir==-1
                then Color.RED
                 else Color.GREEN);
 

febreezus

New member
Here's a version that, rather than painting the trend, places a buy arrow 9 bars into a downtrend and a sell arrow 9 bars into an uptrend, as per the instructions.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk;
def hi;
def lo;
def dir;
def brk_lookback = 4;

brk = if bar1 then open else
                 if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[brk_lookback] else brk[1];
hi =  if bar1 then if close > open then close else open else
                 if dir[1] == -1 and close > brk[1] or (dir == 1 and close > hi[1]) then close else hi[1];
lo =  if bar1 then if close > open then open else open else
                 if dir[1] == 1 and close < brk[1] or dir == -1 and close < lo[1] then close else lo[1];
dir = if bar1 then (if close < open[brk_lookback] then -1 else if close > open[brk_lookback] then 1 else 0) else
                 if close>brk[1] then 1 else if close<brk[1] then -1 else dir[1];


def sum = sum(dir,9);
def sell_cond = close>close[1] and close[1]>close[2] and close[2]>close[3];
def buy_cond = close<close[1] and close[1]<close[2] and close[2]<close[3];

plot Buy = if buy_cond and sum==-9 and sum[1]!=-9 then low else Double.NaN;
Buy.AssignValueColor(color.green);
Buy.SetPaintingStrategy(PaintingStrategy.arrow_up);
Buy.SetLineWeight(5);
plot Sell = if sell_cond and sum==9 and sum[1]!=9 then high else Double.NaN;
Sell.AssignValueColor(color.red);
Sell.SetPaintingStrategy(PaintingStrategy.arrow_down);
Sell.SetLineWeight(5);

#AssignPriceColor(if dir==-1
#                then Color.RED
#                 else Color.GREEN);
 

Trading51

Active member
2019 Donor
Here's a version that, rather than painting the trend, places a buy arrow 9 bars into a downtrend and a sell arrow 9 bars into an uptrend, as per the instructions.

Code:
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def bar1 = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;

def brk;
def hi;
def lo;
def dir;
def brk_lookback = 4;

brk = if bar1 then open else
                 if (close>brk[1] and close>hi[1]) or (close<brk[1] and close<lo[1]) then open[brk_lookback] else brk[1];
hi =  if bar1 then if close > open then close else open else
                 if dir[1] == -1 and close > brk[1] or (dir == 1 and close > hi[1]) then close else hi[1];
lo =  if bar1 then if close > open then open else open else
                 if dir[1] == 1 and close < brk[1] or dir == -1 and close < lo[1] then close else lo[1];
dir = if bar1 then (if close < open[brk_lookback] then -1 else if close > open[brk_lookback] then 1 else 0) else
                 if close>brk[1] then 1 else if close<brk[1] then -1 else dir[1];


def sum = sum(dir,9);
def sell_cond = close>close[1] and close[1]>close[2] and close[2]>close[3];
def buy_cond = close<close[1] and close[1]<close[2] and close[2]<close[3];

plot Buy = if buy_cond and sum==-9 and sum[1]!=-9 then low else Double.NaN;
Buy.AssignValueColor(color.green);
Buy.SetPaintingStrategy(PaintingStrategy.arrow_up);
Buy.SetLineWeight(5);
plot Sell = if sell_cond and sum==9 and sum[1]!=9 then high else Double.NaN;
Sell.AssignValueColor(color.red);
Sell.SetPaintingStrategy(PaintingStrategy.arrow_down);
Sell.SetLineWeight(5);

#AssignPriceColor(if dir==-1
#                then Color.RED
#                 else Color.GREEN);
nothing came up on the 3 min or 15 time frame
 

febreezus

New member
That's not an issue with the code. I don't know what symbol you're looking at, but you might need to expand the time frame to get a signal. I get signals on any symbol and aggregation period. I haven't done a close examination to see at which aggregation period these signals seem the most useful, but I've noticed I get far fewer the larger the aggregation period, 1 min giving the most.
 

Apharaohh

New member
Hey, im looking for a script which can scan for Demark signals or sequence counter 9 and 13 both buy and sell signals on certain time frames. I want to have it with alerts and want to be able to select what time frame to scan on. Looking to make a watchlist with it Thanks in advance!
 

FateOwnzYou

Active member
VIP
VIP Enthusiast
The traditional TD Sequence 1-9 also adds in what Tom Demark called TDST lines which i believe stands for Support Trend. So on a Sell setup when the sequence is counting 1 through 9 on an ascending price, you place a horizontal line on the lowest wick out of those 9 (which is most the time the 1, but not always). Same with a buy setup when the sequence is counting 1 thru 9 on a descending price, you would place a horizontal line on the highest wick out of those 9. These lines provide great support and resistance lines. When the price is above the line and drops through it though and the support failed, the line is support to delete. I find these lines great for swing trading. Is there anyone that could help out with a script with this? I have gotten the sequence numbers to work and to place a line on the bottom of the 1 wick, but the 1 isnt always the lowest wick, just most of the time. I wish to make this accurate. Anyone have anything already doing this? or maybe help script it?

Example here

Untitled.png
 

BenTen

Administrative
Staff member
Staff
VIP
Lifetime
I looked into this. Unfortunately, ThinkorSwim doesn't disclose the source code for its SequenceCounter indicator. Without the code, we can't add the high and low to each sequence count.
 

FateOwnzYou

Active member
VIP
VIP Enthusiast
Here is a snippet that will plot the numbers of the sequence. Im not sure how to get rid of the the numbers if the sequence is incomplete though. So it will show each sequence on countup and just keep the number even if the sequence is incomplete

Code:
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);
     Count.Setdefaultcolor(color.cyan);
 
Last edited:

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.
Top