# Indicator for TOS
#// © LuxAlgo
#indicator("Liquidity Levels [LuxAlgo]", "LuxAlgo - Liquidity Levels"
#Hint NumberOfLevels: Min = 1, Max = 10.
# Converted by Sam4Cok@Samer800 - 08/20204
input extendLevelsToRight = no;
input extendLevelsToLeft = no;
input extendLevelsTo = 300;
input lookbackLength = 20;
input NumberOfLevels = 5; #,'Number Of Levels', minval = 1)
input showHistogramCloud = yes;
input HistogramWindow = 200; #, 'Histogram Window', maxval = 500, group = 'Histogram')
input showVolumePivotPoints = no;
def na = Double.NaN;
def last = IsNaN(close);
def r = extendLevelsToRight;
def l = extendLevelsToLeft;
def x = extendLevelsTo;
def distwin = HistogramWindow;
def show = Min(Max(NumberOfLevels, 1), 10);
#-- Scripts
script ranking {
input src = 0;
input s0 = 1;
input s1 = 2;
input s2 = 3;
input s3 = 4;
input s4 = 5;
input s5 = 6;
input s6 = 7;
input s7 = 8;
input s8 = 9;
input s9 = 10;
def v0 = if IsNaN(s0) then 0 else s0;
def v1 = if IsNaN(s1) then 0 else s1;
def v2 = if IsNaN(s2) then 0 else s2;
def v3 = if IsNaN(s3) then 0 else s3;
def v4 = if IsNaN(s4) then 0 else s4;
def v5 = if IsNaN(s5) then 0 else s5;
def v6 = if IsNaN(s6) then 0 else s6;
def v7 = if IsNaN(s7) then 0 else s7;
def v8 = if IsNaN(s8) then 0 else s8;
def v9 = if IsNaN(s9) then 0 else s9;
def r0 = src < v0;
def r1 = src < v1;
def r2 = src < v2;
def r3 = src < v3;
def r4 = src < v4;
def r5 = src < v5;
def r6 = src < v6;
def r7 = src < v7;
def r8 = src < v8;
def r9 = src < v9;
def tot = r0 + r1 + r2 + r3 + r4 +
r5 + r6 + r7 + r8 + r9;
plot result = if isNaN(src) then Double.NaN else tot;
}
script Pivot {
input series = close;
input leftBars = 10;
input rightBars = 10;
input isHigh = yes;
def na = Double.NaN;
def HH = series == Highest(series, leftBars + 1);
def LL = series == Lowest(series, leftBars + 1);
def pivotRange = (leftBars + rightBars + 1);
def leftEdgeValue = if series[pivotRange] == 0 then na else series[pivotRange];
def pvtCond = !IsNaN(series) and leftBars > 0 and rightBars > 0 and !IsNaN(leftEdgeValue);
def barIndexH = if pvtCond then
fold i = 1 to rightBars + 1 with p=1 while p do
series > GetValue(series, - i) else na;
def barIndexL = if pvtCond then
fold j = 1 to rightBars + 1 with q=1 while q do
series < GetValue(series, - j) else na;
def PivotPoint;
if isHigh {
PivotPoint = if HH and barIndexH then series else na;
} else {
PivotPoint = if LL and barIndexL then series else na;
}
plot pvt = PivotPoint;
}
def phv = pivot(volume[lookbackLength], lookbackLength, lookbackLength, yes);
def ph0;
def ph1;
def ph2;
def ph3;
def ph4;
def ph5;
def ph6;
def ph7;
def ph8;
def ph9;
def vh0;
def vh1;
def vh2;
def vh3;
def vh4;
def vh5;
def vh6;
def vh7;
def vh8;
def vh9;
def ch0;
def ch1;
def ch2;
def ch3;
def ch4;
def ch5;
def ch6;
def ch7;
def ch8;
def ch9;
if !IsNaN(phv) {
ph9 = ph8[1];
ph8 = ph7[1];
ph7 = ph6[1];
ph6 = ph5[1];
ph5 = ph4[1];
ph4 = ph3[1];
ph3 = ph2[1];
ph2 = ph1[1];
ph1 = ph0[1];
ph0 = close[lookbackLength];
vh9 = vh8[1];
vh8 = vh7[1];
vh7 = vh6[1];
vh6 = vh5[1];
vh5 = vh4[1];
vh4 = vh3[1];
vh3 = vh2[1];
vh2 = vh1[1];
vh1 = vh0[1];
vh0 = volume[lookbackLength];
ch9 = ch8[1];
ch8 = ch7[1];
ch7 = ch6[1];
ch6 = ch5[1];
ch5 = ch4[1];
ch4 = ch3[1];
ch3 = ch2[1];
ch2 = ch1[1];
ch1 = ch0[1];
ch0 = if close > open then 1 else -1;
} else {
ph9 = ph9[1];
ph8 = ph8[1];
ph7 = ph7[1];
ph6 = ph6[1];
ph5 = ph5[1];
ph4 = ph4[1];
ph3 = ph3[1];
ph2 = ph2[1];
ph1 = ph1[1];
ph0 = ph0[1];
vh9 = vh9[1];
vh8 = vh8[1];
vh7 = vh7[1];
vh6 = vh6[1];
vh5 = vh5[1];
vh4 = vh4[1];
vh3 = vh3[1];
vh2 = vh2[1];
vh1 = vh1[1];
vh0 = vh0[1];
ch9 = ch9[1];
ch8 = ch8[1];
ch7 = ch7[1];
ch6 = ch6[1];
ch5 = ch5[1];
ch4 = ch4[1];
ch3 = ch3[1];
ch2 = ch2[1];
ch1 = ch1[1];
ch0 = ch0[1];
}
def lv0 = HighestAll(InertiaAll(ph0, 2));
def lv1 = HighestAll(InertiaAll(ph1, 2));
def lv2 = HighestAll(InertiaAll(ph2, 2));
def lv3 = HighestAll(InertiaAll(ph3, 2));
def lv4 = HighestAll(InertiaAll(ph4, 2));
def lv5 = HighestAll(InertiaAll(ph5, 2));
def lv6 = HighestAll(InertiaAll(ph6, 2));
def lv7 = HighestAll(InertiaAll(ph7, 2));
def lv8 = HighestAll(InertiaAll(ph8, 2));
def lv9 = HighestAll(InertiaAll(ph9, 2));
def l0 = InertiaAll(lv0, x, extendToLeft = l, extendToRight = r);
def l1 = InertiaAll(lv1, x, extendToLeft = l, extendToRight = r);
def l2 = InertiaAll(lv2, x, extendToLeft = l, extendToRight = r);
def l3 = InertiaAll(lv3, x, extendToLeft = l, extendToRight = r);
def l4 = InertiaAll(lv4, x, extendToLeft = l, extendToRight = r);
def l5 = InertiaAll(lv5, x, extendToLeft = l, extendToRight = r);
def l6 = InertiaAll(lv6, x, extendToLeft = l, extendToRight = r);
def l7 = InertiaAll(lv7, x, extendToLeft = l, extendToRight = r);
def l8 = InertiaAll(lv8, x, extendToLeft = l, extendToRight = r);
def l9 = InertiaAll(lv9, x, extendToLeft = l, extendToRight = r);
def c0 = ch0;
def c1 = ch1;
def c2 = ch2;
def c3 = ch3;
def c4 = ch4;
def c5 = ch5;
def c6 = ch6;
def c7 = ch7;
def c8 = ch8;
def c9 = ch9;
plot pv0 = l0;
plot pv1 = if show < 2 then na else l1;
plot pv2 = if show < 3 then na else l2;
plot pv3 = if show < 4 then na else l3;
plot pv4 = if show < 5 then na else l4;
plot pv5 = if show < 6 then na else l5;
plot pv6 = if show < 7 then na else l6;
plot pv7 = if show < 8 then na else l7;
plot pv8 = if show < 9 then na else l8;
plot pv9 = if show < 10 then na else l9;
pv0.AssignValueColor(if c0>0 then Color.GREEN else Color.RED);
pv1.AssignValueColor(if c1>0 then Color.GREEN else Color.RED);
pv2.AssignValueColor(if c2>0 then Color.GREEN else Color.RED);
pv3.AssignValueColor(if c3>0 then Color.GREEN else Color.RED);
pv4.AssignValueColor(if c4>0 then Color.GREEN else Color.RED);
pv5.AssignValueColor(if c5>0 then Color.GREEN else Color.RED);
pv6.AssignValueColor(if c6>0 then Color.GREEN else Color.RED);
pv7.AssignValueColor(if c7>0 then Color.GREEN else Color.RED);
pv8.AssignValueColor(if c8>0 then Color.GREEN else Color.RED);
pv9.AssignValueColor(if c9>0 then Color.GREEN else Color.RED);
def sort0 = show - ranking(pv0, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort1 = show - ranking(pv1, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort2 = show - ranking(pv2, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort3 = show - ranking(pv3, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort4 = show - ranking(pv4, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort5 = show - ranking(pv5, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort6 = show - ranking(pv6, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort7 = show - ranking(pv7, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort8 = show - ranking(pv8, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def sort9 = show - ranking(pv9, pv0, pv1, pv2, pv3, pv4, pv5, pv6, pv7, pv8, pv9);
def lvl0 = if sort0==1 then pv0 else
if sort1==1 then pv1 else
if sort2==1 then pv2 else
if sort3==1 then pv3 else
if sort4==1 then pv4 else
if sort5==1 then pv5 else
if sort6==1 then pv6 else
if sort7==1 then pv7 else
if sort8==1 then pv8 else pv9;
def lvl1 = if sort0==2 then pv0 else
if sort1==2 then pv1 else
if sort2==2 then pv2 else
if sort3==2 then pv3 else
if sort4==2 then pv4 else
if sort5==2 then pv5 else
if sort6==2 then pv6 else
if sort7==2 then pv7 else
if sort8==2 then pv8 else pv9;
def lvl2 = if sort0==3 then pv0 else
if sort1==3 then pv1 else
if sort2==3 then pv2 else
if sort3==3 then pv3 else
if sort4==3 then pv4 else
if sort5==3 then pv5 else
if sort6==3 then pv6 else
if sort7==3 then pv7 else
if sort8==3 then pv8 else pv9;
def lvl3 = if sort0==4 then pv0 else
if sort1==4 then pv1 else
if sort2==4 then pv2 else
if sort3==4 then pv3 else
if sort4==4 then pv4 else
if sort5==4 then pv5 else
if sort6==4 then pv6 else
if sort7==4 then pv7 else
if sort8==4 then pv8 else pv9;
def lvl4 = if sort0==5 then pv0 else
if sort1==5 then pv1 else
if sort2==5 then pv2 else
if sort3==5 then pv3 else
if sort4==5 then pv4 else
if sort5==5 then pv5 else
if sort6==5 then pv6 else
if sort7==5 then pv7 else
if sort8==5 then pv8 else pv9;
def lvl5 = if sort0==6 then pv0 else
if sort1==6 then pv1 else
if sort2==6 then pv2 else
if sort3==6 then pv3 else
if sort4==6 then pv4 else
if sort5==6 then pv5 else
if sort6==6 then pv6 else
if sort7==6 then pv7 else
if sort8==6 then pv8 else pv9;
def lvl6 = if sort0==7 then pv0 else
if sort1==7 then pv1 else
if sort2==7 then pv2 else
if sort3==7 then pv3 else
if sort4==7 then pv4 else
if sort5==7 then pv5 else
if sort6==7 then pv6 else
if sort7==7 then pv7 else
if sort8==7 then pv8 else pv9;
def lvl7 = if sort0==8 then pv0 else
if sort1==8 then pv1 else
if sort2==8 then pv2 else
if sort3==8 then pv3 else
if sort4==8 then pv4 else
if sort5==8 then pv5 else
if sort6==8 then pv6 else
if sort7==8 then pv7 else
if sort8==8 then pv8 else pv9;
def lvl8 = if sort0==9 then pv0 else
if sort1==9 then pv1 else
if sort2==9 then pv2 else
if sort3==9 then pv3 else
if sort4==9 then pv4 else
if sort5==9 then pv5 else
if sort6==9 then pv6 else
if sort7==9 then pv7 else
if sort8==9 then pv8 else pv9;
def lvl9 = if sort0==10 then pv0 else
if sort1==10 then pv1 else
if sort2==10 then pv2 else
if sort3==10 then pv3 else
if sort4==10 then pv4 else
if sort5==10 then pv5 else
if sort6==10 then pv6 else
if sort7==10 then pv7 else
if sort8==10 then pv8 else pv9;
def bull1 = fold i = 0 to distwin with p do
if close[i] >= lvl0 and close[i] <= lvl1 then
if close[i] > open[i] then p + 1 else p else p;
def bull2 = fold i1 = 0 to distwin with p1 do
if close[i1] >= lvl1 and close[i1] <= lvl2 then
if close[i1] > open[i1] then p1 + 1 else p1 else p1;
def bull3 = fold i2 = 0 to distwin with p2 do
if close[i2] >= lvl2 and close[i2] <= lvl3 then
if close[i2] > open[i2] then p2 + 1 else p2 else p2;
def bull4 = fold i3 = 0 to distwin with p3 do
if close[i3] >= lvl3 and close[i3] <= lvl4 then
if close[i3] > open[i3] then p3 + 1 else p3 else p3;
def bull5 = fold i4 = 0 to distwin with p4 do
if close[i4] >= lvl4 and close[i4] <= lvl5 then
if close[i4] > open[i4] then p4 + 1 else p4 else p4;
def bull6 = fold i5 = 0 to distwin with p5 do
if close[i5] >= lvl5 and close[i5] <= lvl6 then
if close[i5] > open[i5] then p5 + 1 else p5 else p5;
def bull7 = fold i6 = 0 to distwin with p6 do
if close[i6] >= lvl6 and close[i6] <= lvl7 then
if close[i6] > open[i6] then p6 + 1 else p6 else p6;
def bull8 = fold i7 = 0 to distwin with p7 do
if close[i7] >= lvl7 and close[i7] <= lvl8 then
if close[i7] > open[i7] then p7 + 1 else p7 else p7;
def bull9 = fold i8 = 0 to distwin with p8 do
if close[i8] >= lvl8 and close[i8] <= lvl9 then
if close[i8] > open[i8] then p8 + 1 else p8 else p8;
def bear1 = fold j = 0 to distwin with q do
if close[j] >= lvl0 and close[j] <= lvl1 then
if close[j] > open[j] then q else q + 1 else q;
def bear2 = fold j1 = 0 to distwin with q1 do
if close[j1] >= lvl1 and close[j1] <= lvl2 then
if close[j1] > open[j1] then q1 else q1 + 1 else q1;
def bear3 = fold j2 = 0 to distwin with q2 do
if close[j2] >= lvl2 and close[j2] <= lvl3 then
if close[j2] > open[j2] then q2 else q2 + 1 else q2;
def bear4 = fold j3 = 0 to distwin with q3 do
if close[j3] >= lvl3 and close[j3] <= lvl4 then
if close[j3] > open[j3] then q3 else q3 + 1 else q3;
def bear5 = fold j4 = 0 to distwin with q4 do
if close[j4] >= lvl4 and close[j4] <= lvl5 then
if close[j4] > open[j4] then q4 else q4 + 1 else q4;
def bear6 = fold j5 = 0 to distwin with q5 do
if close[j5] >= lvl5 and close[j5] <= lvl6 then
if close[j5] > open[j5] then q5 else q5 + 1 else q5;
def bear7 = fold j6 = 0 to distwin with q6 do
if close[j6] >= lvl6 and close[j6] <= lvl7 then
if close[j6] > open[j6] then q6 else q6 + 1 else q6;
def bear8 = fold j7 = 0 to distwin with q7 do
if close[j7] >= lvl7 and close[j7] <= lvl8 then
if close[j7] > open[j7] then q7 else q7 + 1 else q7;
def bear9 = fold j8 = 0 to distwin with q8 do
if close[j8] >= lvl8 and close[j8] <= lvl9 then
if close[j8] > open[j8] then q8 else q8 + 1 else q8;
def tot1 = bull1 + bear1;
def tot2 = bull2 + bear2;
def tot3 = bull3 + bear3;
def tot4 = bull4 + bear4;
def tot5 = bull5 + bear5;
def tot6 = bull6 + bear6;
def tot7 = bull7 + bear7;
def tot8 = bull8 + bear8;
def tot9 = bull9 + bear9;
def difLvl1H = if !showHistogramCloud then na else lvl0 + (lvl1 - lvl0) * (bull1 / tot1);
def difLvl2H = if !showHistogramCloud then na else lvl1 + (lvl2 - lvl1) * (bull2 / tot2);
def difLvl3H = if !showHistogramCloud then na else lvl2 + (lvl3 - lvl2) * (bull3 / tot3);
def difLvl4H = if !showHistogramCloud then na else lvl3 + (lvl4 - lvl3) * (bull4 / tot4);
def difLvl5H = if !showHistogramCloud then na else lvl4 + (lvl5 - lvl4) * (bull5 / tot5);
def difLvl6H = if !showHistogramCloud then na else lvl5 + (lvl6 - lvl5) * (bull6 / tot6);
def difLvl7H = if !showHistogramCloud then na else lvl6 + (lvl7 - lvl6) * (bull7 / tot7);
def difLvl8H = if !showHistogramCloud then na else lvl7 + (lvl8 - lvl7) * (bull8 / tot8);
def difLvl9H = if !showHistogramCloud then na else lvl8 + (lvl9 - lvl8) * (bull9 / tot9);
AddCloud(difLvl1H, lvl0, Color.DARK_GREEN);
AddCloud(difLvl2H, lvl1, Color.DARK_GREEN);
AddCloud(difLvl3H, lvl2, Color.DARK_GREEN);
AddCloud(difLvl4H, lvl3, Color.DARK_GREEN);
AddCloud(difLvl5H, lvl4, Color.DARK_GREEN);
AddCloud(difLvl6H, lvl5, Color.DARK_GREEN);
AddCloud(difLvl7H, lvl6, Color.DARK_GREEN);
AddCloud(difLvl8H, lvl7, Color.DARK_GREEN);
AddCloud(difLvl9H, lvl8, Color.DARK_GREEN);
AddCloud(lvl1, difLvl1H, Color.DARK_RED);
AddCloud(lvl2, difLvl2H, Color.DARK_RED);
AddCloud(lvl3, difLvl3H, Color.DARK_RED);
AddCloud(lvl4, difLvl4H, Color.DARK_RED);
AddCloud(lvl5, difLvl5H, Color.DARK_RED);
AddCloud(lvl6, difLvl6H, Color.DARK_RED);
AddCloud(lvl7, difLvl7H, Color.DARK_RED);
AddCloud(lvl8, difLvl8H, Color.DARK_RED);
AddCloud(lvl9, difLvl9H, Color.DARK_RED);
#-- Bubbles
def pvtV = if phv[-lookbackLength] then high else na; #lookbackLength
AddChartBubble(showVolumePivotPoints and pvtV, pvtV, AsText(phv[-lookbackLength]/1000000) + "M", color.CYAN);
#-- END of CODS