Tom Demark Sequential 9 (TDS 9) is a technical indicator that helps identify a possible trend change. Specifically, if 9 consecutive candles close higher than the previous four, a reversal or correction in a stock's trend is possible.
Ruby:
# demark9_01
#addlabel(1, "demark9_0", color.white);
# https://usethinkscript.com/threads/convert-tradingview-demark-9.11212/
#Convert Tradingview DeMark 9
#//DeMark_9_Indicator_Wealth_Insider
#study("DeMark9 Indicator",overlay=true)
#TD = close > close[4] ?nz(TD[1])+1:0
#TS = close < close[4] ?nz(TS[1])+1:0
#TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
#TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
#plotshape(TDUp==7?true:na,style=shape.triangledown,text="7",color=green,location=location.abovebar)
#plotshape(TDUp==8?true:na,style=shape.triangledown,text="8",color=green,location=location.abovebar)
#plotshape(TDUp==9?true:na,style=shape.triangledown,text="??",color=green,location=location.abovebar)
#plotshape(TDDn==7?true:na,style=shape.triangleup,text="7",color=red,location=location.belowbar)
#plotshape(TDDn==8?true:na,style=shape.triangleup,text="8",color=red,location=location.belowbar)
#plotshape(TDDn==9?true:na,style=shape.triangleup,text="?",color=red,location=location.belowbar)
#------------------------------
input bubbles_on = yes;
# ------------------------------
script ValueWhen {
# look for a previous occurance of a value, and return the value of a diff var
# valuewhen(condition, return_var, nth occurrence)
# nth occurrence, original pine code starts n at 0, so 1 is the 2nd
input cond = 0;
input pr = 0;
input n2 = 0;
def n = n2 + 1;
# start at 0 so it looks at current bar
def offset2 = fold j = 0 to 200
with p
while p < n + 1
do p + ( if p == n then j-n else if getvalue(cond, j) then 1 else 0 );
# def bnz = bn - offset2 + 1;
plot price = getvalue(pr, offset2-1);
plot offset = offset2;
}
#-------------------------------
script nz {
# test if number is valid
input a = 0;
def b = if isnan(a) then 0 else a;
plot c = b;
};
#-------------------------------
def bn = barnumber();
input prev_n_cond = 1;
addlabel(1, "prev n cond " + prev_n_cond, color.yellow);
#TD = close > close[4] ?nz(TD[1])+1:0
#TS = close < close[4] ?nz(TS[1])+1:0
def td_cls4 = close > close[4];
def ts_cls4 = close < close[4];
def TD = if close > close[4] then nz(TD[1])+1 else 0;
def TS = if close < close[4] then nz(TS[1])+1 else 0;
# valuewhen(condition, return_price, nth occurrence)
# n starts at 0, so 1 would be the 2nd prev occurance
#TDUp = TD - valuewhen(TD < TD[1], TD , 1 )
#TDDn = TS - valuewhen(TS < TS[1], TS , 1 )
def td1 = TD < TD[1];
def ts1 = TS < TS[1];
def td_vw = valuewhen(TD1, TD , prev_n_cond).price;
def ts_vw = valuewhen(TS1, TS , prev_n_cond).price;
#def TDUp = TD - valuewhen(TD1, TD , prev_n_cond);
#def TDDn = TS - valuewhen(TS1, TS , prev_n_cond);
def TDUp = TD - td_vw;
def TDDn = TS - ts_vw;
#plotshape(TDUp==7?true:na,style=shape.triangledown,text="7",color=green,location=location.abovebar)
addchartbubble( (bubbles_on and tdup >= 7 and tdup <= 9), high, tdup, color.green, yes);
#plotshape(TDDn==7?true:na,style=shape.triangleup,text="7",color=red,location=location.belowbar)
addchartbubble( (bubbles_on and tddn >= 7 and tddn <= 9), low, tddn, color.red, no);
#------------------------------------
def pr = valuewhen(TD1, TD , prev_n_cond).price;
def off = valuewhen(TD1, TD , prev_n_cond).offset;
#def pr2 = getvalue(td, off-1);
#def td_vw = valuewhen(TD1, TD , prev_n_cond);
input test1 = no;
addchartbubble(test1, (if td1 or td_cls4 then high*1.004 else low*0.996),
td1 + " td1\n" +
bn + " bn\n" +
pr + " pr\n" +
off + " off\n" +
(bn - off + 1) + " prev\n" +
" " + "\n" +
td_cls4 + " cls4\n" +
td + " td\n" +
td1 + " td1\n" +
td_vw + "\n" +
tdup + " tdup\n"
#pr2 + " pr2\n"
#close + " cls\n" +
#close[4] + " cls4"
, (if td1 then color.cyan else color.gray)
, (if td1 or td_cls4 then yes else no));
#
Last edited by a moderator: