Sector Ranked Labels

Thinker

Member
VIP
I've created a code that labels sectors and indexes that shows the current % change. I tried to get it to rank order them, but I've succeeded only in getting the labels at the top with the percentages but not rank ordering them. Can someone improve on the code so it does the rank ordering?

Here's a chart link: http://tos.mx/!V1CshPSK

Here's a screenshot of the chart:
1744216407599.png


Here's the code:

declare upper;

# Sectors
def XLK = (close("XLK") - close("XLK")[1]) / close("XLK")[1] * 100;
def XLV = (close("XLV") - close("XLV")[1]) / close("XLV")[1] * 100;
def XLF = (close("XLF") - close("XLF")[1]) / close("XLF")[1] * 100;
def XLI = (close("XLI") - close("XLI")[1]) / close("XLI")[1] * 100;
def XLE = (close("XLE") - close("XLE")[1]) / close("XLE")[1] * 100;
def XLU = (close("XLU") - close("XLU")[1]) / close("XLU")[1] * 100;
def XLB = (close("XLB") - close("XLB")[1]) / close("XLB")[1] * 100;
def XLY = (close("XLY") - close("XLY")[1]) / close("XLY")[1] * 100;
def XLP = (close("XLP") - close("XLP")[1]) / close("XLP")[1] * 100;
def XLRE = (close("XLRE") - close("XLRE")[1]) / close("XLRE")[1] * 100;
def XLC = (close("XLC") - close("XLC")[1]) / close("XLC")[1] * 100;

# Index ETFs
def SPY = (close("SPY") - close("SPY")[1]) / close("SPY")[1] * 100;
def QQQ = (close("QQQ") - close("QQQ")[1]) / close("QQQ")[1] * 100;
def IWM = (close("IWM") - close("IWM")[1]) / close("IWM")[1] * 100;

# Labels
AddLabel(yes, "XLK (Tech): " + Round(XLK, 2) + "%", if XLK >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLV (Health): " + Round(XLV, 2) + "%", if XLV >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLF (Financials): " + Round(XLF, 2) + "%", if XLF >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLI (Industrials): " + Round(XLI, 2) + "%", if XLI >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLE (Energy): " + Round(XLE, 2) + "%", if XLE >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLU (Utilities): " + Round(XLU, 2) + "%", if XLU >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLB (Materials): " + Round(XLB, 2) + "%", if XLB >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLY (Cons Disc): " + Round(XLY, 2) + "%", if XLY >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLP (Cons Stap): " + Round(XLP, 2) + "%", if XLP >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLRE (Real Estate): " + Round(XLRE, 2) + "%", if XLRE >= 0 then Color.GREEN else Color.RED);
AddLabel(yes, "XLC (Comm Services): " + Round(XLC, 2) + "%", if XLC >= 0 then Color.GREEN else Color.RED);

# Indexes
AddLabel(yes, "SPY (S&P 500): " + Round(SPY, 2) + "%", if SPY >= 0 then Color.CYAN else Color.MAGENTA);
AddLabel(yes, "QQQ (Nasdaq): " + Round(QQQ, 2) + "%", if QQQ >= 0 then Color.CYAN else Color.MAGENTA);
AddLabel(yes, "IWM (Russell 2000): " + Round(IWM, 2) + "%", if IWM >= 0 then Color.CYAN else Color.MAGENTA);
 
Last edited by a moderator:
@Thinker What you have described, as I am interpreting it would be very difficult to do, if possible at all in the Thinkorswim platform, because Thinkscript doesn't allow arrays nor does it have any sorting functions... Using roundabout methods, again if possible at all, would require a lot of code and would impact overall performance... You are talking about sorting data prior to painting labels from what I'm reading and we cannot do that within the platform...
 
@Thinker What you have described, as I am interpreting it would be very difficult to do, if possible at all in the Thinkorswim platform, because Thinkscript doesn't allow arrays nor does it have any sorting functions... Using roundabout methods, again if possible at all, would require a lot of code and would impact overall performance... You are talking about sorting data prior to painting labels from what I'm reading and we cannot do that within the platform...
Thanks for the quick reply. Sorry to learn that it can't be done.
 

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
273 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