Sorting AddChartBubbles

Whistler

New member
I have taken sections of code discussed in this thread
https://usethinkscript.com/threads/sector-rotation-indicator-for-thinkorswim.845/
and modified it in an attempt to rank and sort the percentage change of each sector since the open of the trading day.

But the ranking is not working in the way I had intended. I'd like it to be sorted from highest change to lowest. I would appreciate any feedback that might help me fix this. Thanx!

Intraday Sector Ranking.png


Code:
# Displays sector weightings and percentage change from the beginning
# of the trading day.

# SECTOR WEIGHTINGS SHOULD BE UPDATED EVERY FIRST DAY OF THE MONTH
# BEST SOURCE https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# Use Sector Breakdown section towards the bottom of page.

# OTHER SOURCES
# https://eresearch.fidelity.com/eresearch/markets_sectors/sectors/si_weighting_recommendations.jhtml?tab=sirecommendations
# https://www.sectorspdr.com/sectorspdr/sector/xli
# https://siblisresearch.com/data/us-sector-weightings/
# https://seekingalpha.com/etfs-and-funds/etf-tables/sectors
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# https://www.sectorspdr.com/sectorspdr/sectors

declare lower;

input XLK = 29.6 ; #info tech XLK
input XLF = 14.7 ; #financial XLF
input XLV = 11.2 ;  #healthcare XLV
input XLY = 10.3 ;  #cons.discetion XLY
input XLC = 9.2 ;   #communications XLC
input XLI = 8.5 ;   #industrials XLI
input XLP = 6.1 ;   #staples XLP
input XLE = 3.7;   #energy  XLE
input XLU = 2.5;   #utilities XLU
input XLRE = 2.3 ;  #real estate XLRE
input XLB = 2.0;   #materials XLB


script na {
    plot na = Double.NaN;
}
script date {
    plot date = GetYYYYMMDD();
}
script rthStart {
    plot rthStart = RegularTradingStart(date()) / AggregationPeriod.MIN;
}
script rthEnd {
    plot rthEnd = RegularTradingEnd(date())  / AggregationPeriod.MIN;
}
script firstBar {
    plot firstBar = BarNumber() == 1;
}
script rth {
    plot rth;
    plot tod = (GetTime() / AggregationPeriod.MIN);
    rth = tod between rthStart() and rthEnd();
}
script globex {
    plot globex = !rth();
}
script intraDay {
    plot intraDay = GetAggregationPeriod() < AggregationPeriod.DAY;
}
script datechanged {
    plot datechanged = date() != date()[1];
}
script rthchanged {
    plot rthchanged = rth() != rth()[1];
}
script rthstarted {
    plot rthstarted;
    rthstarted = rth() and (rthchanged() or datechanged());
}
script rthended {
    plot rthended;
    plot rth = rth();
    rthended = rthStarted() or (!rth() and rthchanged());
}

# End KW_RTH

AddLabel(!intraDay(), "Not available on this time frame", Color.GRAY);
def show = IntraDay();

DefineGlobalColor("2", Color.Pink);
DefineGlobalColor("1",  Color.RED);
DefineGlobalColor("3", Color.LIGHT_GREEN);
DefineGlobalColor("4", Color.GREEN);

script opn {
    plot r;
    input cur = 0;
    input prev = 0;
    r = If(
    !intraDay(), Double.NaN,
    If(
      firstBar(),
      If(rth(), cur, Double.NaN),
      If(rthStarted() and !IsNaN(cur), cur, prev)
    )
);
}

script cls {
    input cur = 0;
    input prev = 0;

    plot cls;
    plot rth = rth();

    cls = If(
    !intraDay(), Double.NaN,
    If(
      firstBar(),
      If(rth, cur, Double.NaN),
      If(rth and !IsNaN(cur), cur, prev)
    )
  );
}

script pct {
    plot r;
    input op = 0;
    input cl = 0;

    plot diff = cl - op;
    r = If(IsNaN(op) or IsNaN(cl), 0, Round((diff / op) * 100, 2));
}

input Open_Time = 930;

def ko;
ko = opn(open("XLK"), ko[1]);
def kc;
kc = cls(close("XLK"), kc[1]);
def kp = pct(ko, kc);

def fo;
fo = opn(open("XLF"), fo[1]);
def fc;
fc = cls(close("XLF"), fc[1]);
def fp = pct(fo, fc);

def vo;
vo = opn(open("XLV"), vo[1]);
def vc;
vc = cls(close("XLV"), vc[1]);
def vp = pct(vo, vc);

def yo;
yo = opn(open("XLY"), yo[1]);
def yc;
yc = cls(close("XLY"), yc[1]);
def yp = pct(yo, yc);

def co;
co = opn(open("XLC"), co[1]);
def cc;
cc = cls(close("XLC"), cc[1]);
def cp = pct(co, cc);

def io;
io = opn(open("XLI"), io[1]);
def ic;
ic = cls(close("XLI"), ic[1]);
def ip = pct(io, ic);

def po;
po = opn(open("XLP"), po[1]);
def pc;
pc = cls(close("XLP"), pc[1]);
def pp = pct(po, pc);

def eo;
eo = opn(open("XLE"), eo[1]);
def ec;
ec = cls(close("XLE"), ec[1]);
def ep = pct(eo, ec);

def uo;
uo = opn(open("XLU"), uo[1]);
def uc;
uc = cls(close("XLU"), uc[1]);
def up = pct(uo, uc);

def reo;
reo = opn(open("XLRE"), reo[1]);
def recl;
recl = cls(close("XLRE"), recl[1]);
def rep = pct(reo, recl);

def bo;
bo = opn(open("XLB"), bo[1]);
def bc;
bc = cls(close("XLB"), bc[1]);
def bp = pct(bo, bc);


def st1 = kp;
def st2 = fp;
def st3 = vp;
def st4 = yp;
def st5 = cp;
def st6 = ip;
def st7 = pp;
def st8 = ep;
def st9 = up;
def st10 = rep;
def st11 = bp;


# determine interger sequence (ranking)
def c1 = (st1>st2)+(st1>st3)+(st1>st4)+(st1>st5)+(st1>st6)+(st1>st7)+(st1>st8)+(st1>st9)+(st1>st10)+(st1>st11)+1;
def c2 = (st2>st1)+(st2>st3)+(st2>st4)+(st2>st5)+(st2>st6)+(st2>st7)+(st2>st8)+(st2>st9)+(st2>st10)+(st2>st11)+1;
def c3 = (st3>st1)+(st3>st2)+(st3>st4)+(st3>st5)+(st3>st6)+(st3>st7)+(st3>st8)+(st3>st9)+(st3>st10)+(st3>st11)+1;
def c4 = (st4>st1)+(st4>st2)+(st4>st3)+(st4>st5)+(st4>st6)+(st4>st7)+(st4>st8)+(st4>st9)+(st4>st10)+(st4>st11)+1;
def c5 = (st5>st1)+(st5>st2)+(st5>st3)+(st5>st4)+(st5>st6)+(st5>st7)+(st5>st8)+(st5>st9)+(st5>st10)+(st5>st11)+1;
def c6 = (st6>st1)+(st6>st2)+(st6>st3)+(st6>st4)+(st6>st5)+(st6>st7)+(st6>st8)+(st6>st9)+(st6>st10)+(st6>st11)+1;
def c7 = (st7>st1)+(st7>st2)+(st7>st3)+(st7>st4)+(st7>st5)+(st7>st6)+(st7>st8)+(st7>st9)+(st7>st10)+(st7>st11)+1;
def c8 = (st8>st1)+(st8>st2)+(st8>st3)+(st8>st4)+(st8>st5)+(st8>st6)+(st8>st7)+(st8>st9)+(st8>st10)+(st8>st11)+1;
def c9 = (st9>st1)+(st9>st2)+(st9>st3)+(st9>st4)+(st9>st5)+(st9>st6)+(st9>st7)+(st9>st8)+(st9>st10)+(st9>st11)+1;
def c10 = (st10>st1)+(st10>st2)+(st10>st3)+(st10>st4)+(st10>st5)+(st10>st6)+(st10>st7)+(st10>st8)+(st10>st9)+(st10>st11)+1;
def c11 = (st11>st1)+(st11>st2)+(st11>st3)+(st11>st4)+(st11>st5)+(st11>st6)+(st11>st7)+(st11>st8)+(st11>st9)+(st11>st10)+1;


# remove duplicate sequence numbers
def d1 = c1;
def d2 = c2+(c2==c1);
def d3 = c3+(c3==c1)+(c3==c2);
def d4 = c4+(c4==c1)+(c4==c2)+(c4==c3);
def d5 = c5+(c5==c1)+(c5==c2)+(c5==c3)+(c5==c4);
def d6 = c6+(c6==c1)+(c6==c2)+(c6==c3)+(c6==c4)+(c6==c5);
def d7 = c7+(c7==c1)+(c7==c2)+(c7==c3)+(c7==c4)+(c7==c5)+(c7==c6);
def d8 = c8+(c8==c1)+(c8==c2)+(c8==c3)+(c8==c4)+(c8==c5)+(c8==c6)+(c8==c7);
def d9 = c9+(c9==c1)+(c9==c2)+(c9==c3)+(c9==c4)+(c9==c5)+(c9==c6)+(c9==c7)+(c9==c8);
def d10 = c10+(c10==c1)+(c10==c2)+(c10==c3)+(c10==c4)+(c10==c5)+(c10==c6)+(c10==c7)+(c10==c8)+(c10==c9);
def d11 = c11+(c11==c1)+(c11==c2)+(c11==c3)+(c11==c4)+(c11==c5)+(c11==c6)+(c11==c7)+(c11==c8)+(c11==c9)+(c11==c10);


def rank1 = d1;
def rank2 = d2;
def rank3 = d3;
def rank4 = d4;
def rank5 = d5;
def rank6 = d6;
def rank7 = d7;
def rank8 = d8;
def rank9 = d9;
def rank10 = d10;
def rank11 = d11;

plot one = 1;

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank1, "Consumer: " + AsText(XLY) + "% | " + AsText(yp) + "%", if (yp < -.5) then GlobalColor("1")
    else if yp < 0 then GlobalColor("2")
    else if yp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank2, "Technology: " + AsText(XLK) + "% | " + AsText(kp) + "%", if (kp < -.5) then GlobalColor("1")
    else if kp < 0 then GlobalColor("2")
    else if kp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank3, "Industrials: " + AsText(XLK) + "% | " + AsText(ip) + "%", if (ip < -.5) then GlobalColor("1")
    else if ip < 0 then GlobalColor("2")
    else if ip > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank4, "Materials: " + AsText(XLB) + "% | " + AsText(bp) + "%", if (bp < -.5) then GlobalColor("1")
    else if bp < 0 then GlobalColor("2")
    else if bp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank5, "Energy: " + AsText(XLE) + "% | " + AsText(ep) + "%", if (ep < -.5) then GlobalColor("1")
    else if ep < 0 then GlobalColor("2")
    else if ep > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank6, "Staples: " + AsText(XLP) + "% | " + AsText(pp) + "%", if (pp < -.5) then GlobalColor("1")
    else if pp < 0 then GlobalColor("2")
    else if pp > .5 then GlobalColor("4")
    else GlobalColor("3"));


AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank7, "HealthCare: " + AsText(XLV) + "% | " + AsText(vp) + "%", if (vp < -.5) then GlobalColor("1")
    else if vp < 0 then GlobalColor("2")
    else if vp > .5 then GlobalColor("4")
    else GlobalColor("3"));

# rank8.SetDefaultColor(Color.VIOLET);

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank8, "Utilities: " + AsText(XLU) + "% | " + AsText(up) + "%", if (up < -.5) then GlobalColor("1")
    else if up < 0 then GlobalColor("2")
    else if up > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank9, "Financials: " + AsText(XLF) + "% | " + AsText(fp) + "%", if (fp < -.5) then GlobalColor("1")
    else if fp < 0 then GlobalColor("2")
    else if fp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank10, "Communications: " + AsText(XLC) + "% | " + AsText(cp) + "%", if (cp < -.5) then GlobalColor("1")
    else if cp < 0 then GlobalColor("2")
    else if cp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank11, "RealEstate: " + AsText(XLRE) + "% | " + AsText(rep) + "%", if (rep < -.5) then GlobalColor("1")
    else if rep < 0 then GlobalColor("2")
    else if rep > .5 then GlobalColor("4")
    else GlobalColor("3"));

#EOC
 

Attachments

  • Intraday Sector Ranking.png
    Intraday Sector Ranking.png
    43.2 KB · Views: 17
Last edited by a moderator:
Solution
I have taken sections of code discussed in this thread
https://usethinkscript.com/threads/sector-rotation-indicator-for-thinkorswim.845/
and modified it in an attempt to rank and sort the percentage change of each sector since the open of the trading day.

But the ranking is not working in the way I had intended. I'd like it to be sorted from highest change to lowest. I would appreciate any feedback that might help me fix this. Thanx!

View attachment 24555

Code:
# Displays sector weightings and percentage change from the beginning
# of the trading day.

# SECTOR WEIGHTINGS SHOULD BE UPDATED EVERY FIRST DAY OF THE MONTH
# BEST SOURCE https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# Use Sector Breakdown section towards the...
I have taken sections of code discussed in this thread
https://usethinkscript.com/threads/sector-rotation-indicator-for-thinkorswim.845/
and modified it in an attempt to rank and sort the percentage change of each sector since the open of the trading day.

But the ranking is not working in the way I had intended. I'd like it to be sorted from highest change to lowest. I would appreciate any feedback that might help me fix this. Thanx!

View attachment 24555

Code:
# Displays sector weightings and percentage change from the beginning
# of the trading day.

# SECTOR WEIGHTINGS SHOULD BE UPDATED EVERY FIRST DAY OF THE MONTH
# BEST SOURCE https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# Use Sector Breakdown section towards the bottom of page.

# OTHER SOURCES
# https://eresearch.fidelity.com/eresearch/markets_sectors/sectors/si_weighting_recommendations.jhtml?tab=sirecommendations
# https://www.sectorspdr.com/sectorspdr/sector/xli
# https://siblisresearch.com/data/us-sector-weightings/
# https://seekingalpha.com/etfs-and-funds/etf-tables/sectors
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# https://www.sectorspdr.com/sectorspdr/sectors

declare lower;

input XLK = 29.6 ; #info tech XLK
input XLF = 14.7 ; #financial XLF
input XLV = 11.2 ;  #healthcare XLV
input XLY = 10.3 ;  #cons.discetion XLY
input XLC = 9.2 ;   #communications XLC
input XLI = 8.5 ;   #industrials XLI
input XLP = 6.1 ;   #staples XLP
input XLE = 3.7;   #energy  XLE
input XLU = 2.5;   #utilities XLU
input XLRE = 2.3 ;  #real estate XLRE
input XLB = 2.0;   #materials XLB


script na {
    plot na = Double.NaN;
}
script date {
    plot date = GetYYYYMMDD();
}
script rthStart {
    plot rthStart = RegularTradingStart(date()) / AggregationPeriod.MIN;
}
script rthEnd {
    plot rthEnd = RegularTradingEnd(date())  / AggregationPeriod.MIN;
}
script firstBar {
    plot firstBar = BarNumber() == 1;
}
script rth {
    plot rth;
    plot tod = (GetTime() / AggregationPeriod.MIN);
    rth = tod between rthStart() and rthEnd();
}
script globex {
    plot globex = !rth();
}
script intraDay {
    plot intraDay = GetAggregationPeriod() < AggregationPeriod.DAY;
}
script datechanged {
    plot datechanged = date() != date()[1];
}
script rthchanged {
    plot rthchanged = rth() != rth()[1];
}
script rthstarted {
    plot rthstarted;
    rthstarted = rth() and (rthchanged() or datechanged());
}
script rthended {
    plot rthended;
    plot rth = rth();
    rthended = rthStarted() or (!rth() and rthchanged());
}

# End KW_RTH

AddLabel(!intraDay(), "Not available on this time frame", Color.GRAY);
def show = IntraDay();

DefineGlobalColor("2", Color.Pink);
DefineGlobalColor("1",  Color.RED);
DefineGlobalColor("3", Color.LIGHT_GREEN);
DefineGlobalColor("4", Color.GREEN);

script opn {
    plot r;
    input cur = 0;
    input prev = 0;
    r = If(
    !intraDay(), Double.NaN,
    If(
      firstBar(),
      If(rth(), cur, Double.NaN),
      If(rthStarted() and !IsNaN(cur), cur, prev)
    )
);
}

script cls {
    input cur = 0;
    input prev = 0;

    plot cls;
    plot rth = rth();

    cls = If(
    !intraDay(), Double.NaN,
    If(
      firstBar(),
      If(rth, cur, Double.NaN),
      If(rth and !IsNaN(cur), cur, prev)
    )
  );
}

script pct {
    plot r;
    input op = 0;
    input cl = 0;

    plot diff = cl - op;
    r = If(IsNaN(op) or IsNaN(cl), 0, Round((diff / op) * 100, 2));
}

input Open_Time = 930;

def ko;
ko = opn(open("XLK"), ko[1]);
def kc;
kc = cls(close("XLK"), kc[1]);
def kp = pct(ko, kc);

def fo;
fo = opn(open("XLF"), fo[1]);
def fc;
fc = cls(close("XLF"), fc[1]);
def fp = pct(fo, fc);

def vo;
vo = opn(open("XLV"), vo[1]);
def vc;
vc = cls(close("XLV"), vc[1]);
def vp = pct(vo, vc);

def yo;
yo = opn(open("XLY"), yo[1]);
def yc;
yc = cls(close("XLY"), yc[1]);
def yp = pct(yo, yc);

def co;
co = opn(open("XLC"), co[1]);
def cc;
cc = cls(close("XLC"), cc[1]);
def cp = pct(co, cc);

def io;
io = opn(open("XLI"), io[1]);
def ic;
ic = cls(close("XLI"), ic[1]);
def ip = pct(io, ic);

def po;
po = opn(open("XLP"), po[1]);
def pc;
pc = cls(close("XLP"), pc[1]);
def pp = pct(po, pc);

def eo;
eo = opn(open("XLE"), eo[1]);
def ec;
ec = cls(close("XLE"), ec[1]);
def ep = pct(eo, ec);

def uo;
uo = opn(open("XLU"), uo[1]);
def uc;
uc = cls(close("XLU"), uc[1]);
def up = pct(uo, uc);

def reo;
reo = opn(open("XLRE"), reo[1]);
def recl;
recl = cls(close("XLRE"), recl[1]);
def rep = pct(reo, recl);

def bo;
bo = opn(open("XLB"), bo[1]);
def bc;
bc = cls(close("XLB"), bc[1]);
def bp = pct(bo, bc);


def st1 = kp;
def st2 = fp;
def st3 = vp;
def st4 = yp;
def st5 = cp;
def st6 = ip;
def st7 = pp;
def st8 = ep;
def st9 = up;
def st10 = rep;
def st11 = bp;


# determine interger sequence (ranking)
def c1 = (st1>st2)+(st1>st3)+(st1>st4)+(st1>st5)+(st1>st6)+(st1>st7)+(st1>st8)+(st1>st9)+(st1>st10)+(st1>st11)+1;
def c2 = (st2>st1)+(st2>st3)+(st2>st4)+(st2>st5)+(st2>st6)+(st2>st7)+(st2>st8)+(st2>st9)+(st2>st10)+(st2>st11)+1;
def c3 = (st3>st1)+(st3>st2)+(st3>st4)+(st3>st5)+(st3>st6)+(st3>st7)+(st3>st8)+(st3>st9)+(st3>st10)+(st3>st11)+1;
def c4 = (st4>st1)+(st4>st2)+(st4>st3)+(st4>st5)+(st4>st6)+(st4>st7)+(st4>st8)+(st4>st9)+(st4>st10)+(st4>st11)+1;
def c5 = (st5>st1)+(st5>st2)+(st5>st3)+(st5>st4)+(st5>st6)+(st5>st7)+(st5>st8)+(st5>st9)+(st5>st10)+(st5>st11)+1;
def c6 = (st6>st1)+(st6>st2)+(st6>st3)+(st6>st4)+(st6>st5)+(st6>st7)+(st6>st8)+(st6>st9)+(st6>st10)+(st6>st11)+1;
def c7 = (st7>st1)+(st7>st2)+(st7>st3)+(st7>st4)+(st7>st5)+(st7>st6)+(st7>st8)+(st7>st9)+(st7>st10)+(st7>st11)+1;
def c8 = (st8>st1)+(st8>st2)+(st8>st3)+(st8>st4)+(st8>st5)+(st8>st6)+(st8>st7)+(st8>st9)+(st8>st10)+(st8>st11)+1;
def c9 = (st9>st1)+(st9>st2)+(st9>st3)+(st9>st4)+(st9>st5)+(st9>st6)+(st9>st7)+(st9>st8)+(st9>st10)+(st9>st11)+1;
def c10 = (st10>st1)+(st10>st2)+(st10>st3)+(st10>st4)+(st10>st5)+(st10>st6)+(st10>st7)+(st10>st8)+(st10>st9)+(st10>st11)+1;
def c11 = (st11>st1)+(st11>st2)+(st11>st3)+(st11>st4)+(st11>st5)+(st11>st6)+(st11>st7)+(st11>st8)+(st11>st9)+(st11>st10)+1;


# remove duplicate sequence numbers
def d1 = c1;
def d2 = c2+(c2==c1);
def d3 = c3+(c3==c1)+(c3==c2);
def d4 = c4+(c4==c1)+(c4==c2)+(c4==c3);
def d5 = c5+(c5==c1)+(c5==c2)+(c5==c3)+(c5==c4);
def d6 = c6+(c6==c1)+(c6==c2)+(c6==c3)+(c6==c4)+(c6==c5);
def d7 = c7+(c7==c1)+(c7==c2)+(c7==c3)+(c7==c4)+(c7==c5)+(c7==c6);
def d8 = c8+(c8==c1)+(c8==c2)+(c8==c3)+(c8==c4)+(c8==c5)+(c8==c6)+(c8==c7);
def d9 = c9+(c9==c1)+(c9==c2)+(c9==c3)+(c9==c4)+(c9==c5)+(c9==c6)+(c9==c7)+(c9==c8);
def d10 = c10+(c10==c1)+(c10==c2)+(c10==c3)+(c10==c4)+(c10==c5)+(c10==c6)+(c10==c7)+(c10==c8)+(c10==c9);
def d11 = c11+(c11==c1)+(c11==c2)+(c11==c3)+(c11==c4)+(c11==c5)+(c11==c6)+(c11==c7)+(c11==c8)+(c11==c9)+(c11==c10);


def rank1 = d1;
def rank2 = d2;
def rank3 = d3;
def rank4 = d4;
def rank5 = d5;
def rank6 = d6;
def rank7 = d7;
def rank8 = d8;
def rank9 = d9;
def rank10 = d10;
def rank11 = d11;

plot one = 1;

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank1, "Consumer: " + AsText(XLY) + "% | " + AsText(yp) + "%", if (yp < -.5) then GlobalColor("1")
    else if yp < 0 then GlobalColor("2")
    else if yp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank2, "Technology: " + AsText(XLK) + "% | " + AsText(kp) + "%", if (kp < -.5) then GlobalColor("1")
    else if kp < 0 then GlobalColor("2")
    else if kp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank3, "Industrials: " + AsText(XLK) + "% | " + AsText(ip) + "%", if (ip < -.5) then GlobalColor("1")
    else if ip < 0 then GlobalColor("2")
    else if ip > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank4, "Materials: " + AsText(XLB) + "% | " + AsText(bp) + "%", if (bp < -.5) then GlobalColor("1")
    else if bp < 0 then GlobalColor("2")
    else if bp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank5, "Energy: " + AsText(XLE) + "% | " + AsText(ep) + "%", if (ep < -.5) then GlobalColor("1")
    else if ep < 0 then GlobalColor("2")
    else if ep > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank6, "Staples: " + AsText(XLP) + "% | " + AsText(pp) + "%", if (pp < -.5) then GlobalColor("1")
    else if pp < 0 then GlobalColor("2")
    else if pp > .5 then GlobalColor("4")
    else GlobalColor("3"));


AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank7, "HealthCare: " + AsText(XLV) + "% | " + AsText(vp) + "%", if (vp < -.5) then GlobalColor("1")
    else if vp < 0 then GlobalColor("2")
    else if vp > .5 then GlobalColor("4")
    else GlobalColor("3"));

# rank8.SetDefaultColor(Color.VIOLET);

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank8, "Utilities: " + AsText(XLU) + "% | " + AsText(up) + "%", if (up < -.5) then GlobalColor("1")
    else if up < 0 then GlobalColor("2")
    else if up > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank9, "Financials: " + AsText(XLF) + "% | " + AsText(fp) + "%", if (fp < -.5) then GlobalColor("1")
    else if fp < 0 then GlobalColor("2")
    else if fp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank10, "Communications: " + AsText(XLC) + "% | " + AsText(cp) + "%", if (cp < -.5) then GlobalColor("1")
    else if cp < 0 then GlobalColor("2")
    else if cp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank11, "RealEstate: " + AsText(XLRE) + "% | " + AsText(rep) + "%", if (rep < -.5) then GlobalColor("1")
    else if rep < 0 then GlobalColor("2")
    else if rep > .5 then GlobalColor("4")
    else GlobalColor("3"));

#EOC

that is a clever use of my code from post #64, using ranking for the Y in bubble placement.
https://usethinkscript.com/threads/sector-rotation-indicator-for-thinkorswim.845/page-4#post-123711

you didn't use the correct variables in the bubbles, that is why the bubbles aren't sorted.

if things don't work, make a new, simplified bubble and display some values.

i made a test bubble that displays all 11 values, symbols, desc.
it looked like valid data, so then i copied it and made 11 separate bubbles, and used rank for the Y.
they seem to be sorted , big to small.


Code:
#sectors_sort_bubbles

#https://usethinkscript.com/threads/sorting-addchartbubbles.20960/
#Sorting AddChartBubbles

# SECTOR WEIGHTINGS SHOULD BE UPDATED EVERY FIRST DAY OF THE MONTH
# BEST SOURCE https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# Use Sector Breakdown section towards the bottom of page.

# OTHER SOURCES
# https://eresearch.fidelity.com/eresearch/markets_sectors/sectors/si_weighting_recommendations.jhtml?tab=sirecommendations
# https://www.sectorspdr.com/sectorspdr/sector/xli
# https://siblisresearch.com/data/us-sector-weightings/
# https://seekingalpha.com/etfs-and-funds/etf-tables/sectors
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# https://www.sectorspdr.com/sectorspdr/sectors

declare lower;

input XLK = 29.6;  # XLK  info tech
input XLF = 14.7;  # XLF  financial
input XLV = 11.2;  # XLV  healthcare
input XLY = 10.3;  # XLY  consumer discetion
input XLC = 9.2;   # XLC  communications
input XLI = 8.5;   # XLI  industrials
input XLP = 6.1;   # XLP  staples
input XLE = 3.7;   # XLE  energy
input XLU = 2.5;   # XLU  utilities
input XLRE = 2.3;  # XLRE real estate
input XLB = 2.0;   # XLB  materials


script na {
    plot na = Double.NaN;
}
script date {
    plot date = GetYYYYMMDD();
}
script rthStart {
    plot rthStart = RegularTradingStart(date()) / AggregationPeriod.MIN;
}
script rthEnd {
    plot rthEnd = RegularTradingEnd(date())  / AggregationPeriod.MIN;
}
script firstBar {
    plot firstBar = BarNumber() == 1;
}
script rth {
    plot rth;
    plot tod = (GetTime() / AggregationPeriod.MIN);
    rth = tod between rthStart() and rthEnd();
}
script globex {
    plot globex = !rth();
}
script intraDay {
    plot intraDay = GetAggregationPeriod() < AggregationPeriod.DAY;
}
script datechanged {
    plot datechanged = date() != date()[1];
}
script rthchanged {
    plot rthchanged = rth() != rth()[1];
}
script rthstarted {
    plot rthstarted;
    rthstarted = rth() and (rthchanged() or datechanged());
}
script rthended {
    plot rthended;
    plot rth = rth();
    rthended = rthStarted() or (!rth() and rthchanged());
}

# End KW_RTH

AddLabel(!intraDay(), "Not available on this time frame", Color.GRAY);
def show = IntraDay();

DefineGlobalColor("2", Color.Pink);
DefineGlobalColor("1",  Color.RED);
DefineGlobalColor("3", Color.LIGHT_GREEN);
DefineGlobalColor("4", Color.GREEN);

script opn {
    plot r;
    input cur = 0;
    input prev = 0;
    r = If(
    !intraDay(), Double.NaN,
    If(
      firstBar(),
      If(rth(), cur, Double.NaN),
      If(rthStarted() and !IsNaN(cur), cur, prev)
    )
);
}

script cls {
    input cur = 0;
    input prev = 0;

    plot cls;
    plot rth = rth();

    cls = If(
    !intraDay(), Double.NaN,
    If(
      firstBar(),
      If(rth, cur, Double.NaN),
      If(rth and !IsNaN(cur), cur, prev)
    )
  );
}

script pct {
    plot r;
    input op = 0;
    input cl = 0;

    plot diff = cl - op;
    r = If(IsNaN(op) or IsNaN(cl), 0, Round((diff / op) * 100, 2));
}

input Open_Time = 930;

def ko;
ko = opn(open("XLK"), ko[1]);
def kc;
kc = cls(close("XLK"), kc[1]);
def kp = pct(ko, kc);

def fo;
fo = opn(open("XLF"), fo[1]);
def fc;
fc = cls(close("XLF"), fc[1]);
def fp = pct(fo, fc);

def vo;
vo = opn(open("XLV"), vo[1]);
def vc;
vc = cls(close("XLV"), vc[1]);
def vp = pct(vo, vc);

def yo;
yo = opn(open("XLY"), yo[1]);
def yc;
yc = cls(close("XLY"), yc[1]);
def yp = pct(yo, yc);

def co;
co = opn(open("XLC"), co[1]);
def cc;
cc = cls(close("XLC"), cc[1]);
def cp = pct(co, cc);

def io;
io = opn(open("XLI"), io[1]);
def ic;
ic = cls(close("XLI"), ic[1]);
def ip = pct(io, ic);

def po;
po = opn(open("XLP"), po[1]);
def pc;
pc = cls(close("XLP"), pc[1]);
def pp = pct(po, pc);

def eo;
eo = opn(open("XLE"), eo[1]);
def ec;
ec = cls(close("XLE"), ec[1]);
def ep = pct(eo, ec);

def uo;
uo = opn(open("XLU"), uo[1]);
def uc;
uc = cls(close("XLU"), uc[1]);
def up = pct(uo, uc);

def reo;
reo = opn(open("XLRE"), reo[1]);
def recl;
recl = cls(close("XLRE"), recl[1]);
def rep = pct(reo, recl);

def bo;
bo = opn(open("XLB"), bo[1]);
def bc;
bc = cls(close("XLB"), bc[1]);
def bp = pct(bo, bc);


def st1 = kp;
def st2 = fp;
def st3 = vp;
def st4 = yp;
def st5 = cp;
def st6 = ip;
def st7 = pp;
def st8 = ep;
def st9 = up;
def st10 = rep;
def st11 = bp;


# determine interger sequence (ranking)
def c1 = (st1>st2)+(st1>st3)+(st1>st4)+(st1>st5)+(st1>st6)+(st1>st7)+(st1>st8)+(st1>st9)+(st1>st10)+(st1>st11)+1;
def c2 = (st2>st1)+(st2>st3)+(st2>st4)+(st2>st5)+(st2>st6)+(st2>st7)+(st2>st8)+(st2>st9)+(st2>st10)+(st2>st11)+1;
def c3 = (st3>st1)+(st3>st2)+(st3>st4)+(st3>st5)+(st3>st6)+(st3>st7)+(st3>st8)+(st3>st9)+(st3>st10)+(st3>st11)+1;
def c4 = (st4>st1)+(st4>st2)+(st4>st3)+(st4>st5)+(st4>st6)+(st4>st7)+(st4>st8)+(st4>st9)+(st4>st10)+(st4>st11)+1;
def c5 = (st5>st1)+(st5>st2)+(st5>st3)+(st5>st4)+(st5>st6)+(st5>st7)+(st5>st8)+(st5>st9)+(st5>st10)+(st5>st11)+1;
def c6 = (st6>st1)+(st6>st2)+(st6>st3)+(st6>st4)+(st6>st5)+(st6>st7)+(st6>st8)+(st6>st9)+(st6>st10)+(st6>st11)+1;
def c7 = (st7>st1)+(st7>st2)+(st7>st3)+(st7>st4)+(st7>st5)+(st7>st6)+(st7>st8)+(st7>st9)+(st7>st10)+(st7>st11)+1;
def c8 = (st8>st1)+(st8>st2)+(st8>st3)+(st8>st4)+(st8>st5)+(st8>st6)+(st8>st7)+(st8>st9)+(st8>st10)+(st8>st11)+1;
def c9 = (st9>st1)+(st9>st2)+(st9>st3)+(st9>st4)+(st9>st5)+(st9>st6)+(st9>st7)+(st9>st8)+(st9>st10)+(st9>st11)+1;
def c10 = (st10>st1)+(st10>st2)+(st10>st3)+(st10>st4)+(st10>st5)+(st10>st6)+(st10>st7)+(st10>st8)+(st10>st9)+(st10>st11)+1;
def c11 = (st11>st1)+(st11>st2)+(st11>st3)+(st11>st4)+(st11>st5)+(st11>st6)+(st11>st7)+(st11>st8)+(st11>st9)+(st11>st10)+1;


# remove duplicate sequence numbers
def d1 = c1;
def d2 = c2+(c2==c1);
def d3 = c3+(c3==c1)+(c3==c2);
def d4 = c4+(c4==c1)+(c4==c2)+(c4==c3);
def d5 = c5+(c5==c1)+(c5==c2)+(c5==c3)+(c5==c4);
def d6 = c6+(c6==c1)+(c6==c2)+(c6==c3)+(c6==c4)+(c6==c5);
def d7 = c7+(c7==c1)+(c7==c2)+(c7==c3)+(c7==c4)+(c7==c5)+(c7==c6);
def d8 = c8+(c8==c1)+(c8==c2)+(c8==c3)+(c8==c4)+(c8==c5)+(c8==c6)+(c8==c7);
def d9 = c9+(c9==c1)+(c9==c2)+(c9==c3)+(c9==c4)+(c9==c5)+(c9==c6)+(c9==c7)+(c9==c8);
def d10 = c10+(c10==c1)+(c10==c2)+(c10==c3)+(c10==c4)+(c10==c5)+(c10==c6)+(c10==c7)+(c10==c8)+(c10==c9);
def d11 = c11+(c11==c1)+(c11==c2)+(c11==c3)+(c11==c4)+(c11==c5)+(c11==c6)+(c11==c7)+(c11==c8)+(c11==c9)+(c11==c10);


def rank1 = d1;
def rank2 = d2;
def rank3 = d3;
def rank4 = d4;
def rank5 = d5;
def rank6 = d6;
def rank7 = d7;
def rank8 = d8;
def rank9 = d9;
def rank10 = d10;
def rank11 = d11;

plot one = 1;

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank1, "Consumer: " + AsText(XLY) + "% | " + AsText(yp) + "%", if (yp < -.5) then GlobalColor("1")
    else if yp < 0 then GlobalColor("2")
    else if yp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank2, "Technology: " + AsText(XLK) + "% | " + AsText(kp) + "%", if (kp < -.5) then GlobalColor("1")
    else if kp < 0 then GlobalColor("2")
    else if kp > .5 then GlobalColor("4")
    else GlobalColor("3"));

# fix symbol
AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank3, "Industrials: " + AsText(XLI) + "% | " + AsText(ip) + "%", if (ip < -.5) then GlobalColor("1")
    else if ip < 0 then GlobalColor("2")
    else if ip > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank4, "Materials: " + AsText(XLB) + "% | " + AsText(bp) + "%", if (bp < -.5) then GlobalColor("1")
    else if bp < 0 then GlobalColor("2")
    else if bp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank5, "Energy: " + AsText(XLE) + "% | " + AsText(ep) + "%", if (ep < -.5) then GlobalColor("1")
    else if ep < 0 then GlobalColor("2")
    else if ep > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank6, "Staples: " + AsText(XLP) + "% | " + AsText(pp) + "%", if (pp < -.5) then GlobalColor("1")
    else if pp < 0 then GlobalColor("2")
    else if pp > .5 then GlobalColor("4")
    else GlobalColor("3"));


AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank7, "HealthCare: " + AsText(XLV) + "% | " + AsText(vp) + "%", if (vp < -.5) then GlobalColor("1")
    else if vp < 0 then GlobalColor("2")
    else if vp > .5 then GlobalColor("4")
    else GlobalColor("3"));

# rank8.SetDefaultColor(Color.VIOLET);

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank8, "Utilities: " + AsText(XLU) + "% | " + AsText(up) + "%", if (up < -.5) then GlobalColor("1")
    else if up < 0 then GlobalColor("2")
    else if up > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank9, "Financials: " + AsText(XLF) + "% | " + AsText(fp) + "%", if (fp < -.5) then GlobalColor("1")
    else if fp < 0 then GlobalColor("2")
    else if fp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank10, "Communications: " + AsText(XLC) + "% | " + AsText(cp) + "%", if (cp < -.5) then GlobalColor("1")
    else if cp < 0 then GlobalColor("2")
    else if cp > .5 then GlobalColor("4")
    else GlobalColor("3"));

AddChartBubble(IsNaN(close[-1]) and !IsNaN(close), rank11, "RealEstate: " + AsText(XLRE) + "% | " + AsText(rep) + "%", if (rep < -.5) then GlobalColor("1")
    else if rep < 0 then GlobalColor("2")
    else if rep > .5 then GlobalColor("4")
    else GlobalColor("3"));

#EOC

input test1_bubbles = yes;
def f = 8;
def x = (!isnan(close[f]) and isnan(close[f-1]));
addchartbubble(x and test1_bubbles, 1,
"rank   sym   desc   % chg\n\n" +
rank1[f] + "  XLK  info tech  " + st1[f] + "\n" +
rank2[f] + "  XLF  financial  " + st2[f] + "\n" +
rank3[f] + "  XLV  health  " + st3[f] + "\n" +
rank4[f] + "  XLY  consumer  " + st4[f] + "\n" +
rank5[f] + "  XLC  commun  " + st5[f] + "\n" +
rank6[f] + "  XLI  indus  " + st6[f] + "\n" +
rank7[f] + "  XLP  staples  " + st7[f] + "\n" +
rank8[f] + "  XLE  energy  " + st8[f] + "\n" +
rank9[f] + "  XLU  utilities  " + st9[f] + "\n" +
rank10[f] + "  XLRE real estate  " + st10[f] + "\n" +
rank11[f] + "  XLB  materials  " + st11[f] + "\n"
, color.yellow, yes);



# copy prev bubble code and separate it
def g = 15;
def u = (!isnan(close[g]) and isnan(close[g-1]));
input test2_sorted = yes;

addchartbubble(u and test2_sorted, rank1[g],
rank1[g] + "  XLK  info tech  " + st1[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank2[g],
rank2[g] + "  XLF  financial  " + st2[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank3[g],
rank3[g] + "  XLV  health  " + st3[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank4[g],
rank4[g] + "  XLY  consumer  " + st4[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank5[g],
rank5[g] + "  XLC  commun  " + st5[g] + "\n"
 , color.yellow, yes);
addchartbubble(u and test2_sorted, rank6[g],
rank6[g] + "  XLI  indus  " + st6[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank7[g],
rank7[g] + "  XLP  staples  " + st7[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank8[g],
rank8[g] + "  XLE  energy  " + st8[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank9[g],
rank9[g] + "  XLU  utilities  " + st9[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank10[g],
rank10[g] + "  XLRE real estate  " + st10[g] + "\n"
, color.yellow, yes);
addchartbubble(u and test2_sorted, rank11[g],
rank11[g] + "  XLB  materials  " + st11[g] + "\n"
, color.yellow, yes);



#input XLK = 29.6;  # XLK  info tech
#input XLF = 14.7;  # XLF  financial
#input XLV = 11.2;  # XLV  healthcare
#input XLY = 10.3;  # XLY  consumer discetion
#input XLC = 9.2;   # XLC  communications
#input XLI = 8.5;   # XLI  industrials
#input XLP = 6.1;   # XLP  staples
#input XLE = 3.7;   # XLE  energy
#input XLU = 2.5;   # XLU  utilities
#input XLRE = 2.3;  # XLRE real estate
#input XLB = 2.0;   # XLB  materials



# "XLK"  kp = pct(ko, kc);
# "XLF"  fp = pct(fo, fc);
# "XLV"  vp = pct(vo, vc);
# "XLY"  yp = pct(yo, yc);
# "XLC"  cp = pct(co, cc);
# "XLI"  ip = pct(io, ic);
# "XLP"  pp = pct(po, pc);
# "XLE"  ep = pct(eo, ec);
# "XLU"  up = pct(uo, uc);
# "XLRE" rep = pct(reo, recl);
# "XLB"  bp = pct(bo, bc);

#
 

Attachments

  • img1.JPG
    img1.JPG
    100.9 KB · Views: 39
Solution

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
365 Online
Create Post

Similar threads

Similar threads

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top