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;
def na = double.nan;
def bn = barnumber();
def lastbarbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbarbn) then 1 else 0;
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;
def close_line2 = if bn < (lastbarbn+1) then na else close_line;
def DMIndx_up = (DMIndx > DMIndx[1]);
def DMIndx_dwn = (DMIndx < DMIndx[1]);
def DMIndx_flat = ( !DMIndx_up and !DMIndx_dwn);
def DMIndx_crossup = (DMIndx crosses above ZeroLine);
def DMIndx_crossdwn = (DMIndx crosses below ZeroLine);
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;
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);
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;
def bnindex = (bn % 4) + 1;
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 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;
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));
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);