# demand_horz_lines_2
# ----------------------------------------
# copy the original demandindex study
# TD Ameritrade IP Company, Inc. (c) 2010-2021
#declare lower;
input length = 5;
def wClose = (high + low + 2 * close) * 0.25;
def wCRatio = (wClose - wClose[1]) / Min(wClose, wClose[1]);
def closeRatio = 3 * wClose / Average(Highest(High, 2) - Lowest(Low, 2), length) * AbsValue(wCRatio);
def volumeRatio = Volume / Average(Volume, length);
def volumePerClose = volumeRatio / exp(Min(88, closeRatio));
def buyP;
def sellP;
if (wCRatio > 0) {
buyP = volumeRatio;
sellP = volumePerClose;
} else {
buyP = volumePerClose;
sellP = volumeRatio;
}
def buyPres = if IsNaN(buyPres[1]) then 0 else ((buyPres[1] * (length - 1)) + buyP) / length;
def sellPres = if IsNaN(sellPres[1]) then 0 else ((sellPres[1] * (length - 1)) + sellP) / length;
def tempDI;
if ((((sellPres[1] * (length - 1)) + sellP) / length - ((buyPres[1] * (length - 1)) + buyP) / length) > 0) {
tempDI = - if (sellPres != 0) then buyPres / sellPres else 1;
} else {
tempDI = if (buyPres != 0) then sellPres / buyPres else 1;
}
def DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
def ZeroLine = 0;
#plot DMIndx = if IsNaN(close) then Double.NaN else if tempDI < 0 then -1 - tempDI else 1 - tempDI;
#plot ZeroLine = 0;
#DMIndx.setDefaultColor(GetColor(1));
#ZeroLine.SetDefaultColor(GetColor(5));
# --------------------------------------
def na = double.nan;
def bn = barnumber();
def lastbarbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbarbn) then 1 else 0;
# ------------------------------
# create a priceline level, at the last bar close
input barsBack = 1000;
def c = if !IsNaN(close) and IsNaN(close[-1])
then close
else c[1];
def close_line = if isNaN(close[-barsBack])
then c[-barsBack]
else Double.NaN;
# line only after last bar. +1 , line starts 1 bar after lastbar
def close_line2 = if bn < (lastbarbn+1) then na else close_line;
# ------------------------------
# Draws a green horizontal line , when the demand Index > prev bar demand index
def DMIndx_up = (DMIndx > DMIndx[1]);
# Draws a red horizontal line , when the demand Index < prev bar demand index
def DMIndx_dwn = (DMIndx < DMIndx[1]);
def DMIndx_flat = ( !DMIndx_up and !DMIndx_dwn);
# Draws a cyan horizontal line , when the demand Index crosses above zero
def DMIndx_crossup = (DMIndx crosses above ZeroLine);
# Draws a yellow horizontal line , when the demand Index crosses below zero
def DMIndx_crossdwn = (DMIndx crosses below ZeroLine);
# ----------------------
# keep values after last bar
def DMIndx_up2 = if lastbar then DMIndx_up else DMIndx_up2[1];
def DMIndx_dwn2 = if lastbar then DMIndx_dwn else DMIndx_dwn2[1];
def DMIndx_flat2 = if lastbar then DMIndx_flat else DMIndx_flat2[1];
def DMIndx_crossup2 = if lastbar then DMIndx_crossup else DMIndx_crossup2[1];
def DMIndx_crossdwn2 = if lastbar then DMIndx_crossdwn else DMIndx_crossdwn2[1];
# --------------------------------------
input show_many_lines = yes;
# yes = 1 line on each bar
# no = only 1 line after last bar
# ------------------------------
plot z = if show_many_lines then na else close_line2;
z.DefineColor("Up", color.green);
z.DefineColor("dwn", color.red);
z.DefineColor("xup", color.cyan);
z.DefineColor("xdwn", color.yellow);
z.definecolor("flat", color.gray);
z.AssignValueColor( if DMIndx_crossup2 then z.color("xup")
else if DMIndx_crossdwn2 then z.color("xdwn")
else if DMIndx_up2 then z.color("up")
else if DMIndx_dwn2 then z.color("dwn")
else z.color("flat"));
z.SetStyle(Curve.MEDIUM_DASH);
# x.SetDefaultColor(Color.red);
#z.setlineweight(2);
# x.hidebubble();
# ------------------------------------
# create 1 var, with 5 values, to determine signal type
# crossing = 2, -2
# up/dwn = 1 , -1
# flat = 0
def dm_num = if DMIndx_crossup then 2
else if DMIndx_crossdwn then -2
else if DMIndx_up then 1
else if DMIndx_dwn then -1
else 0;
# ----------------------
# plot line after each bar , spanning 3 bars
# need a counter , 1,2,3,4,1,2,3,4,1,..
def bnindex = (bn % 4) + 1;
# addchartbubble(1, low*0.99, bn + "\n" + bnindex, color.cyan, no);
# start line 1 bar after the data bar
input lines_offset = 0;
def p1;
def p2;
def p3;
def p4;
def p1y;
def p2y;
def p3y;
def p4y;
if bn == 1 then {
p1 = na;
p2 = na;
p3 = na;
p4 = na;
p1y = na;
p2y = na;
p3y = na;
p4y = na;
} else if bnindex == 1 then {
p1 = dm_num[lines_offset];
p2 = p2[1];
p3 = p3[1];
p4 = p4[1];
p1y = close[lines_offset];
p2y = na;
p3y = p3y[1];
p4y = p4y[1];
} else if bnindex == 2 then {
p1 = p1[1];
p2 = dm_num[lines_offset];
p3 = p3[1];
p4 = p4[1];
p1y = p1y[1];
p2y = close[lines_offset];
p3y = na;
p4y = p4y[1];
} else if bnindex == 3 then {
p1 = p1[1];
p2 = p2[1];
p3 = dm_num[lines_offset];
p4 = p4[1];
p1y = p1y[1];
p2y = p2y[1];
p3y = close[lines_offset];
p4y = na;
} else if bnindex == 4 then {
p1 = p1[1];
p2 = p2[1];
p3 = p3[1];
p4 = dm_num[lines_offset];
p1y = na;
p2y = p2y[1];
p3y = p3y[1];
p4y = close[lines_offset];
} else {
p1 = na;
p2 = na;
p3 = na;
p4 = na;
p1y = na;
p2y = na;
p3y = na;
p4y = na;
}
input test_bubbles1 = no;
input test_bubbles2 = no;
addchartbubble(test_bubbles2, low*0.99,
bn
+ "\n" + bnindex
+ "\n" + dm_num
+ "\n" + "---"
+ "\n" + p1
+ "\n" + p2
+ "\n" + p3
+ "\n" + p4
+ "\n" + p1y
+ "\n" + p2y
+ "\n" + p3y
+ "\n" + p4y
, color.cyan, no);
# --------------------------
# plot 3 lines
plot v1 = if show_many_lines then p1y else na;
plot v2 = if show_many_lines then p2y else na;
plot v3 = if show_many_lines then p3y else na;
plot v4 = if show_many_lines then p4y else na;
# crossing = 2, -2
# up/dwn = 1 , -1
# flat = 0
v1.AssignValueColor( if p1 == 2 then color.cyan
else if p1 == 1 then color.green
else if p1 == -1 then color.red
else if p1 == -2 then color.yellow
else color.gray);
v2.AssignValueColor( if p2 == 2 then color.cyan
else if p2 == 1 then color.green
else if p2 == -1 then color.red
else if p2 == -2 then color.yellow
else color.gray);
v3.AssignValueColor( if p3 == 2 then color.cyan
else if p3 == 1 then color.green
else if p3 == -1 then color.red
else if p3 == -2 then color.yellow
else color.gray);
v4.AssignValueColor( if p4 == 2 then color.cyan
else if p4 == 1 then color.green
else if p4 == -1 then color.red
else if p4 == -2 then color.yellow
else color.gray);
# ------------------------------------
input test_labels = no;
addlabel( test_labels, "DMIndx, prev " + dmindx[1], color.magenta);
addlabel( test_labels, "DMIndx " + DMIndx , color.magenta);
addlabel( test_labels, "DMIndx_up " + DMIndx_up2 , (if DMIndx_up2 then color.green else color.gray));
addlabel( test_labels, "DMIndx_dwn " + DMIndx_dwn2 , (if DMIndx_dwn then color.red else color.gray));
addlabel( test_labels, "DMIndx_flat " + DMIndx_flat2 , ( if DMIndx_flat2 then color.light_gray else color.gray));
addlabel( test_labels, "DMIndx_crossup " + DMIndx_crossup2 , (if DMIndx_crossup2 then color.cyan else color.gray));
addlabel( test_labels, "DMIndx_crossdwn " + DMIndx_crossdwn2 , (if DMIndx_crossdwn2 then color.yellow else color.gray));
#input test_bubbles1 = no;
addchartbubble(test_bubbles1, low*0.98,
bn + "\n" +
"P " + dmindx[1] + "\n" +
" " + DMIndx + "\n" +
"up " + DMIndx_up2 + "\n" +
"dwn " + DMIndx_dwn2 + "\n" +
"F " + DMIndx_flat2 + "\n" +
"Xup " + DMIndx_crossup2 + "\n" +
"Xd " + DMIndx_crossdwn2
, color.yellow, no);
#