Heat Map Grid - > 4 sectors - experimental

# ─────────────────────────────────────────────
# │ MarketFragments.com | DNA & Market │
# │ info@marketfragments.com │
# │ www.marketfragments.com │
# ─────────────────────────────────────────────
# Time →
# │
# █ █ █│ █
# █ █ █ │ █ █
# █ █ █ │ █ █ █ ╭─╮
# █ █ █ │ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ █ █╭─╯ ╰─╮
# █ █ █ │ █ █ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ █ ╰─╮ ╭─╯
# █ █ █ │ █ ╰─────╯
# ──────┴──────────────────────────────────────────────────────────────
# T1 T2 T3 T4 T5 T6
#
# grid -> heatmat 1 minute chart
script Rev {
input a = 0.0 ;
input b = 0.0;
def n = CompoundValue(1, n[1] + 1, 1);
def highestn = HighestAll(if IsNaN(close) then 0 else n);
def forward = CompoundValue(1, forward[1] + 2, n - highestn);
def getfor = GetValue(a, forward);
def fcnt = CompoundValue(1, if GetValue(getfor, 1) then 1 else fcnt[1] + 1, 1);
def bforward = GetValue(fcnt, forward);
plot xrevx1 = GetValue(b, -bforward );
}
DefineGlobalColor ( "heat1" ,CreateColor ( 255,160,122 ) ); # lightsalmon
DefineGlobalColor ( "heat2" ,CreateColor ( 205,92,92 ) ); #IndianRed"
DefineGlobalColor ( "heat3" , CreateColor ( 220,20,60 ) ); #crimson
DefineGlobalColor ( "heat4" ,CreateColor ( 255,0,0 ) ); #red
input price = close;
Input gridlength =100;
def h = high;
def l = low;
def c = close;
def O = open;
def V = volume;
def tcnt = tick_count ;
def tickS= if(!IsNaN(TickSize()),TickSize(),.01);
def bn = BarNumber();
def lbar = HighestAll(if IsNaN(close) then 0 else bn);
def nan = Double.NaN;
#5x5
def startbn = if bn==1 then 1 else 0;
def endbn = if bn>=lbar then 1 else 0;
def areaACtive = if (lbar-bn)==gridlength then 1 else 0;
def gridstartbn = if startbn then rev(areaACtive,bn) else gridstartbn [1];
def gridendbn = if startbn then rev(endbn,bn) else gridendbn[1];
def cnt = if bn==gridstartbn then 1 else cnt[1]+1;
def gridendcnt= if startbn then rev(endbn,cnt) else gridendcnt[1];
def abn = if(cnt==1,bn,abn[1]);
def bbn = if(cnt==floor(gridendcnt*.25),bn,bbn[1]);
def cbn = if(cnt==floor(gridendcnt*.50),bn,cbn[1]);
def dbn = if(cnt==floor(gridendcnt*.75),bn,dbn[1]);
def ebn = if(bn==lbar,bn,ebn[1]);
def xha= if bn==gridstartbn then h else if h>xha[1] then h else xha[1];
def xla =if bn==gridstartbn then l else if l<xla[1] then l else xla[1];
def xhb = if bn==lbar then xha else xhb[1];
def xlb = if bn==lbar then xla else xlb[1];
def xh = if bn==gridstartbn then rev(endbn,xhb ) else xh[1];
def xl = if bn==gridstartbn then rev(endbn,xlb ) else xl[1];
def xhl =xh-xl;
def q0 =xl;
def q1 =(xh-(xhl*.75));
def q2 =(xh-(xhl*.50));
def q3 =(xh-(xhl*.25));
def q4 =xh;
def qx1x;
def qx2x;
def qx3x;
def qx4x;
def qx5x;
if bn==1 {
qx1x = nan;
qx2x= nan;
qx3x= nan;
qx4x= nan;
qx5x= nan;
} else if bn==gridstartbn {
qx1x = rev(endbn,q0) ;
qx2x= rev(endbn,q1) ;
qx3x= rev(endbn,q2) ;
qx4x= rev(endbn,q3) ;
qx5x= rev(endbn,q4) ;
} else{
qx1x = qx1x[1];
qx2x= qx2x[1];
qx3x= qx3x[1];
qx4x= qx4x[1];
qx5x=qx5x[1];
}
plot qx1 = qx1x;
Plot qx2 = qx2x;
plot qx3 = qx3x;
plot qx4 = qx4x;
plot qx5 = qx5x;
def total = (qx5 -qx1)/100;
## heat map * only levels not tru 5x5 yet
def heat1;
def heat2;
def heat3;
def heat4;
if bn>=gridstartbn{
heat1 = CompoundValue(1,if C<=qx2 then heat1[1]+1 else heat1[1],0);
heat2 = CompoundValue(1,if c>=qx2 && C<=qx3 then heat2[1]+1 else heat2[1],0);
heat3 =CompoundValue(1, if c>=qx3 && C<=qx4 then heat3[1]+1 else heat3[1],0);
heat4 = CompoundValue(1,if c>=qx4 then heat4[1]+1 else heat4[1],0);
} else{
heat1 = heat1[1];
heat2 = heat2[1];
heat3 = heat3[1];
heat4 = heat4[1];
}
def isheat1 = if bn==1 then rev(endbn,heat1) else isheat1[1];
def isheat2 = if bn==1 then rev(endbn,heat2) else isheat2[1];
def isheat3 = if bn==1 then rev(endbn,heat3) else isheat3[1];
def isheat4 = if bn==1 then rev(endbn,heat4) else isheat4[1];
# rank
def rank1 =Max(isheat1, Max(isheat2, Max(isheat3, isheat4 )));
def rank4 =Min(isheat1, Min(isheat2, Min(isheat3, isheat4 )));
def rank2;
if rank1==isheat1{
rank2= Max(isheat2, Max(isheat3, isheat4 ));
} else if rank1==isheat2{
rank2= Max(isheat1, Max(isheat3, isheat4 ));
} else if rank1==isheat3{
rank2= Max(isheat1, Max(isheat2, isheat4 ));
} else{
rank2= Max(isheat1, Max(isheat2, isheat3 ));
}
def rank3;
if rank1==isheat1 and rank2==isheat2{
rank3= Max(isheat3, isheat4 );
} else if rank1==isheat1 and rank2==isheat3{
rank3= Max(isheat2, isheat4 );
} else if rank1==isheat1 and rank2==isheat4{
rank3= Max(isheat2, isheat3 );
} else if rank1==isheat2 and rank2==isheat3{
rank3= Max(isheat1, isheat4 );
} else if rank1==isheat2 and rank2==isheat4{
rank3= Max(isheat1, isheat3 );
} else{
rank3= Max(isheat1, isheat2);
}
def area1bot =
if rank1==isheat1 then qx1 else
if rank1==isheat2 then qx2 else
if rank1==isheat3 then qx3 else
qx4 ;
def area1top =
if rank1==isheat1 then qx2 else
if rank1==isheat2 then qx3 else
if rank1==isheat3 then qx4 else qx5;
def area2bot=
if rank2==isheat1 then qx1 else
if rank2==isheat2 then qx2 else
if rank2==isheat3 then qx3 else qx4;
def area2top=
if rank2==isheat1 then qx2 else
if rank2==isheat2 then qx3 else
if rank2==isheat3 then qx4 else qx5;
def area3bot=
if rank3==isheat1 then qx1 else
if rank3==isheat2 then qx2 else
if rank3==isheat3 then qx3 else qx4;
def area3top=
if rank3==isheat1 then qx2 else
if rank3==isheat2 then qx3 else
if rank3==isheat3 then qx4 else qx5;
def area4bot=
if rank4==isheat1 then qx1 else
if rank4==isheat2 then qx2 else
if rank4==isheat3 then qx3 else qx4;
def area4top =
if rank4==isheat1 then qx2 else
if rank4==isheat2 then qx3 else
if rank4==isheat3 then qx4 else qx5;
plot vlbot = if bn== bbn then qx1 else nan;
plot vltop = if bn== bbn then qx5 else nan;
addCloud(vlbot,vltop ,color.cyan,color.cyan);
AddCloud(area4top ,area4bot,GlobalColor("Heat1"), GlobalColor("Heat1"));
AddCloud(area3top ,area3bot, GlobalColor("Heat2"), GlobalColor("Heat2"));
AddCloud(area2top ,area2bot, GlobalColor("Heat3"), GlobalColor("Heat3"));
AddCloud(area1top ,area1bot, GlobalColor("Heat4"), GlobalColor("Heat4"));

