Alternate Sector Analysis Study for ThinkorSwim

RmS59

Member
Here is a Sector Analysis Study that I created a few years ago and like. It ranks the sectors vs SP500 (or other). Also see the second version below.

Code:
SectorRelativePerformance
################
# Displays relative performance over defined period
# Intended for Sector Analysis
# Created 7/15/18 RmS59

declare lower;

input n1 = "  Analysis Inputs";
input Period = 60;
input price = FundamentalType.CLOSE;
input smoothPeriod = 5;
input DisplayLabel = 1;
input DisplayAltIndex = 1;

input n2 = "  Sector Inputs";
#input Base = "SPX";
input AltIndex1 = "NDX";
input AltIndex2 = "RUT";

input Energy = {default "$SP500#10","XLE"};
input Materials = {default "$SP500#15", "XLB"};
input Industrials = {default "$SP500#20", "XLI"};
input ConsDiscr = {default "$SP500#25", "XLY"};
input ConsStpls = {default "$SP500#30", "XLP"};
input Healthcare = {default "$SP500#35", "XLV"};
input Financials = {default "$SP500#40", "XLF"};
input InfoTech = {default "$SP500#45", "XLK"};
input Telecom = {default "$SP500#50", "IYZ", "XLC"};
input Utilities = {default "$SP500#55", "XLU"};
input RealEstate = "XLRE";
input Retail = "XRT";

# For Reference, the S&P500 Sectors are as follows:
# Energy = "$SP500#10";
# Materials = "$SP500#15";
# Industrials = "$SP500#20";
# ConsDiscr = "$SP500#25";
# ConsStpls = "$SP500#30";
# Healthcare = "$SP500#35";
# Financials = "$SP500#40";
# InfoTech = "$SP500#45"
# Telecom = "$SP500#50";
# Utilities = "$SP500#55";


def priceBase = Fundamental(price);
#def priceBase = Fundamental(price, Base);
def priceAltIndex1 = Fundamental(price, AltIndex1);
def priceAltIndex2 = Fundamental(price, AltIndex2);

def priceEnergy = Fundamental(price, Energy);
def priceMaterials = Fundamental(price, Materials);
def priceIndustrials = Fundamental(price, Industrials);
def priceConsDiscr = Fundamental(price, ConsDiscr);
def priceConsStpls = Fundamental(price, ConsStpls);
def priceHealthcare = Fundamental(price, Healthcare);
def priceFinancials = Fundamental(price, Financials);
def priceInfoTech = Fundamental(price, InfoTech);
def priceTelecom = Fundamental(price, Telecom);
def priceUtilities = Fundamental(price, Utilities);
def priceRealEstate = Fundamental(price, RealEstate);
def priceRetail = Fundamental(price, Retail);

def changeBase = (priceBase - priceBase[Period]) / priceBase[Period];
def changeAltIndex1 = (priceAltIndex1 - priceAltIndex1[Period]) / priceBase[Period];
def changeAltIndex2 = (priceAltIndex2 - priceAltIndex2[Period]) / priceBase[Period];

def changeEnergy = (priceEnergy - priceEnergy[Period]) / priceEnergy[Period];
def changeMaterials = (priceMaterials - priceMaterials[Period]) / priceMaterials[Period];
def changeIndustrials = (priceIndustrials - priceIndustrials[Period]) / priceIndustrials[Period];
def changeConsDiscr = (priceConsDiscr - priceConsDiscr[Period]) / priceConsDiscr[Period];
def changeConsStpls = (priceConsStpls - priceConsStpls[Period]) / priceConsStpls[Period];
def changeHealthcare = (priceHealthcare - priceHealthcare[Period]) / priceHealthcare[Period];
def changeFinancials = (priceFinancials - priceFinancials[Period]) / priceFinancials[Period];
def changeInfoTech = (priceInfoTech - priceInfoTech[Period]) / priceInfoTech[Period];
def changeTelecom = (priceTelecom - priceTelecom[Period]) / priceTelecom[Period];
def changeUtilities = (priceUtilities - priceUtilities[Period]) / priceUtilities[Period];
def changeRealEstate = (priceRealEstate - priceRealEstate[Period]) / priceRealEstate[Period];
def changeRetail = (priceRetail - priceRetail[Period]) / priceRetail[Period];

plot BasePerf = if priceBase == 0 then Double.NaN else round(movAvgExponential(changeBase - changeBase,2),smoothPeriod);
plot AltIndex1Perf = if priceAltIndex1== 0 then Double.NaN else round(100*(changeAltIndex1 - changeBase),1);
plot AltIndex2Perf = if priceAltIndex2== 0 then Double.NaN else round(100*(changeAltIndex2 - changeBase),1);

plot EnergyPerf = if priceEnergy == 0 then Double.NaN else round(expAverage(100*(changeEnergy - changeBase),smoothPeriod),1);
plot MaterialsPerf = if priceMaterials == 0 then Double.NaN else round(expAverage(100*(changeMaterials - changeBase),smoothPeriod),1);
plot IndustrialsPerf = if priceIndustrials== 0 then Double.NaN else round(expAverage(100*(changeIndustrials - changeBase),smoothPeriod),1);
plot ConsDiscrPerf = if priceConsDiscr== 0 then Double.NaN else round(expAverage(100*(changeConsDiscr - changeBase),smoothPeriod) ,1);
plot ConsStplsPerf = if priceConsStpls== 0 then Double.NaN else round(expAverage(100*(changeConsStpls - changeBase),smoothPeriod),1);
plot HealthcarePerf = if priceHealthcare== 0 then Double.NaN else round(expAverage(100*(changeHealthcare - changeBase),smoothPeriod),1);
plot FinancialsPerf = if priceFinancials== 0 then Double.NaN else round(expAverage(100*(changeFinancials - changeBase),smoothPeriod),1);
plot InfoTechPerf = if priceInfoTech== 0 then Double.NaN else round(expAverage(100*(changeInfoTech - changeBase),smoothPeriod),1);
plot TelecomPerf = if priceTelecom== 0 then Double.NaN else round(expAverage(100*(changeTelecom - changeBase),smoothPeriod),1);
plot UtilitiesPerf = if priceUtilities== 0 then Double.NaN else round(expAverage(100*(changeUtilities - changeBase),smoothPeriod),1);
plot RealEstatePerf = if priceRealEstate== 0 then Double.NaN else round(expAverage(100*(changeRealEstate - changeBase),smoothPeriod),1);
plot RetailPerf = if priceRetail== 0 then Double.NaN else round(expAverage(100*(changeRetail - changeBase),smoothPeriod),1);

plot Zero = if !IsNaN(close) then 0 else Double.NaN;

AltIndex1Perf.SetDefaultColor(Color.LIGHT_RED);
AltIndex2Perf.SetDefaultColor(Color.LIME);

EnergyPerf.SetDefaultColor(Color.LIGHT_GRAY);
MaterialsPerf.SetDefaultColor(Color.CYAN);
IndustrialsPerf.SetDefaultColor(Color.MAGENTA);
ConsDiscrPerf.SetDefaultColor(Color.VIOLET);
ConsStplsPerf.SetDefaultColor(Color.DARK_GREEN);
HealthcarePerf.SetDefaultColor(Color.PLUM);
FinancialsPerf.SetDefaultColor(Color.DARK_RED);
InfoTechPerf.SetDefaultColor(Color.GREEN);
TelecomPerf.SetDefaultColor(Color.LIGHT_GREEN);
UtilitiesPerf.SetDefaultColor(Color.ORANGE);
RealEstatePerf.SetDefaultColor(Color.White);

AddLabel(DisplayLabel, period +" Period RS/"+ GetUnderlyingSymbol() + " " + smoothPeriod + " Sm",  Color.WHITE);
AddLabel(DisplayLabel, "  ",Color.DARK_GRAY);
AddLabel(DisplayLabel && DisplayAltIndex, " "+ AltIndex1 + " " + AltIndex1Perf + "%",AltIndex1Perf.TakeValueColor());
AddLabel(DisplayLabel && DisplayAltIndex, " "+ AltIndex2 + " " + AltIndex2Perf + "%",AltIndex2Perf.TakeValueColor());

AddLabel(DisplayLabel, " Energy "+ Energy + " " + EnergyPerf + "%",EnergyPerf.TakeValueColor());
AddLabel(DisplayLabel, " Materials "+ Materials + " " + MaterialsPerf + "%",MaterialsPerf.TakeValueColor());
AddLabel(DisplayLabel, " Industrials "+ Industrials + " " + IndustrialsPerf + "%",IndustrialsPerf.TakeValueColor());
AddLabel(DisplayLabel, " ConsDiscr "+ ConsDiscr + " " + ConsDiscrPerf + "%",ConsDiscrPerf.TakeValueColor());
AddLabel(DisplayLabel, " ConsStpls "+ ConsStpls + " " + ConsStplsPerf + "%",ConsStplsPerf.TakeValueColor());
AddLabel(DisplayLabel, " Healthcare "+ Healthcare + " " + HealthcarePerf + "%",HealthcarePerf.TakeValueColor());
AddLabel(DisplayLabel, " Financials "+ Financials + " " + FinancialsPerf + "%",FinancialsPerf.TakeValueColor());
AddLabel(DisplayLabel, " InfoTech "+ InfoTech + " " + InfoTechPerf + "%",InfoTechPerf.TakeValueColor());
AddLabel(DisplayLabel, " CommSvc "+ Telecom + " " + TelecomPerf + "%",TelecomPerf.TakeValueColor());
AddLabel(DisplayLabel, " Utilities "+ Utilities + " " + UtilitiesPerf + "%",UtilitiesPerf.TakeValueColor());
AddLabel(DisplayLabel, " RealEstate "+ RealEstate + " " + RealEstatePerf + "%",RealEstatePerf.TakeValueColor());
AddLabel(DisplayLabel, " Retail "+ Retail + " " + RetailPerf + "%",RetailPerf.TakeValueColor());
#### End

This study shows sector Rate of Change performance over time. It allows best performing sectors to be easily identified.

Code:
Sector_ROC
############

# Sector_ROC
# Displays rate of changeAltIndex1 (%) over defined period
# Intended for Sector Analysis
# Created 9/10/18 RmS59

declare lower;

input n1 = "  Analysis Inputs";
input lookback = 13;
input smoothingPeriod = 19;
input DisplayLabel = 1;
input DisplayIndexes = 1;
input price = FundamentalType.CLOSE;



input n2 = "  Sector Inputs";
input SandP = {default "SPY","SPX"};
input Nasdaq = {default "QQQ","COMP"};
input Russell = {default "IWM","RUT"};

input Energy = {default "XLE","$SP500#10"};
input Materials = {default "XLB", "$SP500#15" };
input Industrials = {default "XLI", "$SP500#20"};
input ConsDiscr = {default "XLY","$SP500#25"};
input ConsStpls = {default "XLP","$SP500#30"};
input Healthcare = {default "XLV","$SP500#35"};
input Financials = {default "XLF","$SP500#40"};
input InfoTech = {default "XLK","$SP500#45"};
input Telecom = {default "IYZ","XLC","$SP500#50"};
input Utilities = {default "XLU","$SP500#55"};
input RealEstate = "XLRE";




def priceSandP = Fundamental(price,SandP);
def priceNasdaq = Fundamental(price, Nasdaq);
def priceRussell = Fundamental(price, Russell);

def priceEnergy = Fundamental(price, Energy);
def priceMaterials = Fundamental(price, Materials);
def priceIndustrials = Fundamental(price, Industrials);
def priceConsDiscr = Fundamental(price, ConsDiscr);
def priceConsStpls = Fundamental(price, ConsStpls);
def priceHealthcare = Fundamental(price, Healthcare);
def priceFinancials = Fundamental(price, Financials);
def priceInfoTech = Fundamental(price, InfoTech);
def priceTelecom = Fundamental(price, Telecom);
def priceUtilities = Fundamental(price, Utilities);
def priceRealEstate = Fundamental(price, RealEstate);




def SandPROC1 = round(expAverage(100*((priceSandP - priceSandP[lookback])/priceSandP[lookback]),smoothingPeriod),1) ;
def NasdaqROC1 = round(expAverage(100*((priceNasdaq - priceNasdaq[lookback])/priceNasdaq[lookback]),smoothingPeriod),1);
def RussellROC1 = round(expAverage(100*((priceRussell - priceRussell[lookback])/priceRussell[lookback]),smoothingPeriod),1) ;

plot SandPROC = if DisplayIndexes then SandPROC1 else Double.NaN;;
plot NasdaqROC = if DisplayIndexes then NasdaqROC1 else Double.NaN;
plot RussellROC = if DisplayIndexes then RussellROC1 else Double.NaN;


plot EnergyROC = round(expAverage(100*((priceEnergy - priceEnergy[lookback])/priceEnergy[lookback]),smoothingPeriod),1);;
plot MaterialsROC = round(expAverage(100*((priceMaterials - priceMaterials[lookback])/priceMaterials[lookback]),smoothingPeriod),1);;
plot IndustrialsROC = round(expAverage(100*((priceIndustrials - priceIndustrials[lookback])/priceIndustrials[lookback]),smoothingPeriod),1);;
plot ConsDiscrROC = round(expAverage(100*((priceConsDiscr - priceConsDiscr[lookback])/priceConsDiscr[lookback]),smoothingPeriod),1);;
plot ConsStplsROC = round(expAverage(100*((priceConsStpls - priceConsStpls[lookback])/priceConsStpls[lookback]),smoothingPeriod),1);;
plot HealthcareROC = round(expAverage(100*((priceHealthcare - priceHealthcare[lookback])/priceHealthcare[lookback]),smoothingPeriod),1);;
plot FinancialsROC = round(expAverage(100*((priceFinancials - priceFinancials[lookback])/priceFinancials[lookback]),smoothingPeriod),1);;
plot InfoTechROC = round(expAverage(100*((priceInfoTech - priceInfoTech[lookback])/priceInfoTech[lookback]),smoothingPeriod),1);;
plot TelecomROC = round(expAverage(100*((priceTelecom - priceTelecom[lookback])/priceTelecom[lookback]),smoothingPeriod),1);;
plot UtilitiesROC = round(expAverage(100*((priceUtilities - priceUtilities[lookback])/priceUtilities[lookback]),smoothingPeriod),1);;
plot RealEstateROC = round(expAverage(100*((priceRealEstate - priceRealEstate[lookback])/priceRealEstate[lookback]),smoothingPeriod),1);;

plot Zero = if !IsNaN(close) then 1 else Double.NaN;
Zero.setDefaultColor(Color.White);
Zero.SetLineWeight(3);

SandPROC.SetDefaultColor(Color.WHITE);
NasdaqROC.SetDefaultColor(Color.LIGHT_RED);
RussellROC.SetDefaultColor(Color.LIME);
SandPROC.SetLineWeight(2);
NasdaqROC.SetLineWeight(2);
RussellROC.SetLineWeight(2);
SandPROC.SetStyle(Curve.SHORT_DASH);
NasdaqROC.SetStyle(Curve.SHORT_DASH);
RussellROC.SetStyle(Curve.SHORT_DASH);

EnergyROC.SetDefaultColor(Color.LIGHT_GRAY);
MaterialsROC.SetDefaultColor(Color.CYAN);
IndustrialsROC.SetDefaultColor(Color.MAGENTA);
ConsDiscrROC.SetDefaultColor(Color.VIOLET);
ConsStplsROC.SetDefaultColor(Color.DARK_GREEN);
HealthcareROC.SetDefaultColor(Color.PLUM);
FinancialsROC.SetDefaultColor(Color.DARK_RED);
InfoTechROC.SetDefaultColor(Color.GREEN);
TelecomROC.SetDefaultColor(Color.LIGHT_GREEN);
UtilitiesROC.SetDefaultColor(Color.ORANGE);
RealEstateROC.SetDefaultColor(Color.White);

AddLabel(DisplayLabel, lookback +" Period ROC " + smoothingPeriod + " Sm",  Color.WHITE); # Smoothing
AddLabel(DisplayLabel, "  ",Color.DARK_GRAY);

AddLabel(DisplayLabel , " S&P " +  SandPROC1 + "%",SandPROC.TakeValueColor());
AddLabel(DisplayLabel , " Nasdaq " + NasdaqROC1 + "%",NasdaqROC.TakeValueColor());
AddLabel(DisplayLabel , " Russell " + RussellROC1 + "%",RussellROC.TakeValueColor());

AddLabel(DisplayLabel, " Energy "+ Energy + " " + EnergyROC + "%",EnergyROC.TakeValueColor());
AddLabel(DisplayLabel, " Materials "+ Materials + " " + MaterialsROC + "%",MaterialsROC.TakeValueColor());
AddLabel(DisplayLabel, " Industrials "+ Industrials + " " + IndustrialsROC + "%",IndustrialsROC.TakeValueColor());
AddLabel(DisplayLabel, " ConsDiscr "+ ConsDiscr + " " + ConsDiscrROC + "%",ConsDiscrROC.TakeValueColor());
AddLabel(DisplayLabel, " ConsStpls "+ ConsStpls + " " + ConsStplsROC + "%",ConsStplsROC.TakeValueColor());
AddLabel(DisplayLabel, " Healthcare "+ Healthcare + " " + HealthcareROC + "%",HealthcareROC.TakeValueColor());
AddLabel(DisplayLabel, " Financials "+ Financials + " " + FinancialsROC + "%",FinancialsROC.TakeValueColor());
AddLabel(DisplayLabel, " InfoTech "+ InfoTech + " " + InfoTechROC + "%",InfoTechROC.TakeValueColor());
AddLabel(DisplayLabel, " Communications "+ Telecom + " " + TelecomROC + "%",TelecomROC.TakeValueColor());
AddLabel(DisplayLabel, " Utilities "+ Utilities + " " + UtilitiesROC + "%",UtilitiesROC.TakeValueColor());
AddLabel(DisplayLabel, " RealEstate "+ RealEstate + " " + RealEstateROC + "%",RealEstateROC.TakeValueColor());


### END
 
Last edited by a moderator:

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

@RmS59 Thanks for sharing. May i know if which of these scripts are most effective using on which time frame? Do we need to use both of the at the same time? Lastly, may i request based on the attached picture, how do we interpret it the sector analysis, do we look for higher value to suggest that funds are potentially moving to that sector? What the difference btw the two scripts...Much appreciated

xOd8HUj.jpg
 
@Nick
Thanks for your questions.

I developed the Relative Performance study first. The Relative Strength compares the sector performance vs the SPX. This also gives you a good idea of performance, but be wary when the SPX is down. A sector's performance may be better than the SPX, but still not be positive.

So, I look at the Relative Strength first to see how the sectors compare to SPX, then check the RoC to see if change is actually positive.

Since I mostly trade daily charts, I look at these indicators on a daily chart looking back 13 periods. When I trade weekly charts, I look at the same indicators on a weekly chart.

Don't know if this answers your questions, but I hope it helps
 
@RmS59 Thank you for your prompt reply. I am clear about the sector performance relative strength but don't quite understand the interpretation of the ROC. For example, in the attached pic, Sector Performance for XLI is -23.7% and the ROC is -0.1% and InfoTech XLK sector performance is -10.7 % and ROC is +3.2%. How can i correctly utilise this info to generate some trade ideas? Does XLK ROC of 3.2% mean that the sector will likely to be strong for the next few weeks? thanks for taking time to answer these queries
 
@Nick That gets to my earlier point. If a security is outperforming the SPX as illustrated by the Relative Strength, it doesn't mean that it is going up. It just means that it is performing better than the SPX. The Rate of Change study tells you price direction.

Hope that helps
 
Hi @RmS59 , I like your studies. I was playing with them for the past few days. I have a question. would it be possible to create a label study that shows the sector of selected stocks we're viewing on a chart and whether that sector is up or down?
 
@Nick That gets to my earlier point. If a security is outperforming the SPX as illustrated by the Relative Strength, it doesn't mean that it is going up. It just means that it is performing better than the SPX. The Rate of Change study tells you price direction.

Hope that helps
@RmS59 Got it..thank you.
 
@Art
I really wish there was. I don't know of any fundamental data that contains a stock's sector that can be referenced in a study.

If anyone knows of a "hidden variable", please let me know :)
 
Last edited:
@RmS59 I found an alternative way of doing it. I kept only the sector labels on my chart and got the sectors added to my watchlist.
 
It says that the indicator was set to 60 periods and it is showing the Relative Strength (RS) compared to UNH. So, you are comparing whatever security is on your chart to United Healthcare.
 
Sorry it has taken so long to reply.

I use these studies to determine where I look for trading opportunities. I am a trend trader, so look for momentum. When one or two sectors are strongly outperforming, I filter all of my system's trading signals to focus on these sectors.

If you trade intraday, you may want to use a short time period.
 
Does anyone know if this study could be adapted to use TD's sub-industries instead of the parent sectors? If not, could it use tickers you input? It's not perfect but I know the Dow Jones Industry tickers for maybe 100 or so industries.. I'm looking at a way to track their performance on excel and in TOS, and an relative strength indicator would be great, even if it wouldn't be necessarily very readable, but the information you can get from using something like that in scan hacker for example might make it very valuable.
 
unknown.png

I've been trying to find these indicators for 1 year and I'm so lucky that I found this grateful community here can help me!
Please
 
Those symbols are in a built-in watchlist, and you can simply add the description, change, and percent change columns to the watchlist. You'll also be able to sort it, and do other things too, like add additional data easier without messing with code. Have you tried this yet? To me it is a much better option than filling up a chart with labels.

$SP500#10 S&P 500 Energy (Sector) 0 0.00%
$SP500#15 S&P 500 Materials (Sector) 0 0.00%
$SP500#20 S&P 500 Industrials (Sector) 0 0.00%
$SP500#25 S&P 500 Consumer Discretionary (Sector) 0 0.00%
$SP500#30 S&P 500 Consumer Staples (Sector) 0 0.00%
$SP500#35 S&P 500 Health Care (Sector) 0 0.00%
$SP500#40 S&P 500 Financials (Sector) 0 0.00%
$SP500#45 S&P 500 Information Technology (Sector) 0 0.00%
$SP500#50 S&P 500 Communication Services (Sector) 0 0.00%
$SP500#55 S&P 500 Utilities (Sector) 0 0.00%
$SP500#60 S&P 500 Real Estate (Sector) 0 0.00%
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
484 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