Weighted Advance/Decline For ThinkOrSwim

Firstly, I do agree that the SPX is the weighted $ADSPD. You can verify this by plotting the ADSPD and SPX on the same chart but on different axis. Nevertheless, here be my attempt.
Code:
#declare upper;
declare once_per_bar;
declare real_size;
declare lower;
HidePricePlot(yes);
input Roundness = 2;
input Indicator = "$ADSPD";

# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
def XLK_WT = 26.65;
def XLV_WT = 14.59;
def XLF_WT = 10.98;
def XLY_WT = 10.79;
def XLC_WT = 8.94;
def XLI_WT = 7.93;
def XLP_WT = 6.95;
def XLE_WT = 4.53;
def XLU_WT = 2.94;
def XLRE_WT = 2.77;
def XLB_WT = 2.71;


def aggregationPeriod = AggregationPeriod.DAY; #
plot Metric = close(Indicator);
Metric.SetDefaultColor(CreateColor(255,255,255));

input c = 120; #HINT: Scaling factor
AddLabel(yes, Indicator + " scaling " + c);


def XLKo = open("XLK", period = aggregationPeriod);
def XLKpchg = (close("XLK", period = GetAggregationPeriod()) - XLKo)/XLKo;

def XLVo = open("XLV", period = aggregationPeriod);
def XLVpchg = (close("XLV", period = GetAggregationPeriod()) - XLVo)/XLVo;

def XLYo = open("XLY", period = aggregationPeriod);
def XLYpchg = (close("XLY", period = GetAggregationPeriod()) - XLYo)/XLYo;

def XLFo = open("XLF", period = aggregationPeriod);
def XLFpchg = (close("XLF", period = GetAggregationPeriod()) - XLFo)/XLFo;

def XLCo = open("XLC", period = aggregationPeriod);
def XLCpchg = (close("XLC", period = GetAggregationPeriod()) - XLCo)/XLCo;

def XLIo = open("XLI", period = aggregationPeriod);
def XLIpchg = (close("XLI", period = GetAggregationPeriod()) - XLIo)/XLIo;

def XLPo = open("XLP", period = aggregationPeriod);
def XLPpchg = (close("XLP", period = GetAggregationPeriod()) - XLPo)/XLPo;

def XLEo = open("XLE", period = aggregationPeriod);
def XLEpchg = (close("XLE", period = GetAggregationPeriod()) - XLEo)/XLEo;

def XLUo = open("XLU", period = aggregationPeriod);
def XLUpchg = (close("XLU", period = GetAggregationPeriod()) - XLUo)/XLUo;

def XLBo = open("XLB", period = aggregationPeriod);
def XLBpchg = (close("XLB", period = GetAggregationPeriod()) - XLBo)/XLBo;

def XLREo = open("XLRE", period = aggregationPeriod);
def XLREpchg = (close("XLRE", period = GetAggregationPeriod()) - XLREo)/XLREo;

def SPYo = open("SPY", period = aggregationPeriod);
def SPYchg = (close("SPY", period = GetAggregationPeriod()) - SPYo)/SPYo;

# $ for c in XLK XLV XLY XLF XLC XLI XLP XLE XLU XLB XLRE; do echo "+c * ${c}_WT * sign(${c}pchg)"; done
DefineGlobalColor("MetricUp", CreateColor(128, 0, 64));
DefineGlobalColor("MetricDown", CreateColor(64, 64, 64));

plot Spy = c * SPYchg * 100;
Spy.setLineWeight(2);
Spy.setDefaultColor(Color.Magenta);

plot WeightedMetric =
 c * XLK_WT * XLKpchg
+c * XLV_WT * XLVpchg
+c * XLY_WT * XLYpchg
+c * XLF_WT * XLFpchg
+c * XLC_WT * XLCpchg
+c * XLI_WT * XLIpchg
+c * XLP_WT * XLPpchg
+c * XLE_WT * XLEpchg
+c * XLU_WT * XLUpchg
+c * XLB_WT * XLBpchg
+c * XLRE_WT * XLREpchg;
WeightedMetric.DefineColor("Up", CreateColor(128, 0, 128));
WeightedMetric.DefineColor("Down", CreateColor(64, 64, 128));
WeightedMetric.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
WeightedMetric.AssignValueColor(if WeightedMetric > WeightedMetric[1] then WeightedMetric.Color("Up") else WeightedMetric.Color("Down"));


plot ZeroLine = 0;
ZeroLine.SetDefaultColor(GetColor(9));
ZeroLine.HideTitle();
ZeroLine.HideBubble();
Code:
 

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

Link to the indicator: http://tos.mx/pL7BP15



Code:
# # Sources:
# best with 5 mins timeframe.
# concept from shadowtrader.net
# coded by TWOO
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# sector-breakdown
# modified by CWPARKER23

declare lower;
input open_time = 930;

DEF displace = 0;
input length = 12;
input xlpwt = 6.8; #ConsumerStaples
input xluwt = 2.9; #Utilities
input xlcwt = 8.6; #Communications
input xlfwt = 11.0; #Financials
input xlbwt = 2.8; #Materials
input xliwt = 8.0; #Industrials
input xlewt = 4.2; #Energy
input xlrewt = 2.9; #RealEstate
input xlvwt = 14.2; #HealthCare
input xlkwt = 27.2; #InformationTechnology
input xlywt = 11.5; #ConsumerDiscretionary

def SectorCount = 11;
def Scale = 5000;

#SP500 ETF sectors percent change
script PC {
    input Symbol = "SPX";
    def isFirstBar = GetTime() == RegularTradingStart(GetYYYYMMDD()) + 1;
    def O = if isFirstBar then close(Symbol) else O[1];
    def C = close(Symbol);
    plot PctChg = (C - O) / O;
}
def xlkPctChg = PC("XLK");
def xlyPctChg = PC("XLY");
def xlvPctChg = PC("XLV");
def xlfPctChg = PC("XLF");
def xlcPctChg = PC("XLC");
def xliPctChg = PC("XLI");
def xlpPctChg = PC("XLP");
def xlrePctChg = PC("XLRE");
def xlePctChg = PC("XLE");
def xlbPctChg = PC("XLB");
def xluPctChg = PC("XLU");


def xlkSizing = xlkPctChg * xlkwt;
def xlvSizing = xlvPctChg * xlvwt;
def xlySizing = xlyPctChg * xlywt;
def xlfSizing = xlfPctChg * xlfwt;
def xlcSizing = xlcPctChg * xlcwt;
def xliSizing = xliPctChg * xliwt;
def xlpSizing = xlpPctChg * xlpwt;
def xleSizing = xlePctChg * xlewt;
def xlreSizing = xlrePctChg * xlrewt;
def xlbSizing = xlbPctChg * xlbwt;
def xluSizing = xluPctChg * xluwt;
def combinedSizing = Scale * (
xlkSizing +
xlvSizing +
xlySizing +
xlfSizing +
xlcSizing +
xliSizing +
xlpSizing +
xleSizing +
xlreSizing +
xlbSizing +
xluSizing
) / SectorCount;

# Weighted_AD
DEF Weighted_AD = if !IsNaN(combinedSizing) then combinedSizing else Double.NaN;
#Weighted_AD.DefineColor("Lower", Color.YELLOW);
#Weighted_AD.DefineColor("Higher", Color.MAGENTA);
#Weighted_AD.AssignValueColor(if Weighted_AD < Weighted_AD[1] then Weighted_AD.Color("Lower") else  Weighted_AD.Color("Higher"));
#Weighted_AD.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

plot LowerBand = Lowest(Weighted_AD[-displace + 1], length);
LowerBand.SetDefaultColor(GetColor(8));

plot UpperBand = Highest(Weighted_AD[-displace + 1], length);
UpperBand.SetDefaultColor(GetColor(1));

PLOT MID = (UPPERBand + LOWErBand)/2;


plot Diff_Weighted_AD = Weighted_AD;

Diff_Weighted_AD.SetDefaultColor(GetColor(5));
Diff_Weighted_AD.SetPaintingStrategy(PaintingStrategy.sQUARED_HISTOGRAM);
Diff_Weighted_AD.SetLineWeight(3);
Diff_Weighted_AD.DefineColor("Positive and Up", Color.GREEN);
Diff_Weighted_AD.DefineColor("Positive and Down", Color.DARK_GREEN);
Diff_Weighted_AD.DefineColor("Negative and Down", Color.RED);
Diff_Weighted_AD.DefineColor("Negative and Up", Color.pink);
Diff_Weighted_AD.AssignValueColor(if Diff_Weighted_AD >= 0 then if Diff_Weighted_AD > Diff_Weighted_AD[1] then Diff_Weighted_AD.color("Positive and Up") else Diff_Weighted_AD.color("Positive and Down") else if Diff_Weighted_AD < Diff_Weighted_AD[1] then Diff_Weighted_AD.color("Negative and Down") else Diff_Weighted_AD.color("Negative and Up"));

#Zeroline
plot zero = 0;
zero.SetDefaultColor(Color.GRAY);
zero.HideBubble();
zero.HideTitle();

# SPX Non_Weighted_AD
def spxcombinedSizing = Scale *
(xlkPctChg +
xlvPctChg +
xlyPctChg +
xlfPctChg +
xlcPctChg +
xliPctChg +
xlpPctChg +
xlePctChg +
xlrePctChg +
xlbPctChg +
xluPctChg);
plot Non_Weighted_AD =  spxcombinedSizing ;
Non_Weighted_AD.SetDefaultColor(Color.WHITE);
Non_Weighted_AD.SetPaintingStrategy(PaintingStrategy.LINE);
DefineGlobalColor("Bullish", Color.cyan);
DefineGlobalColor("Bearish", Color.pink);
AddCloud(Non_Weighted_AD, zero , globalColor("Bullish"), globalColor("Bearish"));
# NDX Non_Weighted_AD
def NDXPctChg = PC("NDX") * 35000;
plot Non_Weighted_NDX = NDXPctChg   ;
Non_Weighted_NDX.SetDefaultColor(Color.CYAN);
Non_Weighted_NDX.SetLineWeight(1);
Non_Weighted_NDX.Hide();
Non_Weighted_NDX.HideBubble();
Non_Weighted_NDX.HideTitle();

def na = double.NaN ;
def isbulldiv = if (weighted_AD - weighted_AD[1] )>0 and (LOW[1] - LOW)>0 then 1 else 0;
def isbeardiv = if (weighted_AD[1] - weighted_AD )>0  and (HIGH - HIGH[1])>0  then 1 else 0;

DEF PositveDivergence = if
       
         isbulldiv > 0 then
        non_Weighted_AD else na;
   
plot PD = if PositveDivergence IS TRUE then non_Weighted_AD else Double.NaN;
PD.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PD.SetDefaultColor(Color.WHITE);
PD.HideBubble();
PD.HideTitle();

DEF NegativeDivergence = if
       
         isbeardiv > 0 then
        non_Weighted_AD else na;
   
plot ND = if NEGativeDivergence  IS TRUE then non_Weighted_AD else Double.NaN;
ND.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ND.SetDefaultColor(Color.YELLOW);
ND.HideBubble();
ND.HideTitle();





AddVerticalLine(( GetDay() <> GetDay()[1]), "", Color.YeLLOW, Curve.SHORT_DASH);

#SP500 ETF sectors BUBBLE
Script pct {
input Symbol = "SPX";
def aggregationPeriod = AggregationPeriod.DAY;
def price = open(Symbol, period = aggregationPeriod);
def diff = close (Symbol) - open(Symbol, period = aggregationPeriod);
 plot d_pct = 100 * diff / price;
}

def XLK_d_pct = pct(“XLK”)  ;
 def XLV_d_pct = pct(“XLV”)  ;
def XLY_d_pct = pct(“XLY”)  ;
def XLC_d_pct = pct(“XLC”)  ;
 def XLF_d_pct = pct(“XLF”)  ;
 def XLI_d_pct = pct(“XLI”)  ;
 def XLP_d_pct = pct(“XLP”)  ;
 def XLRE_d_pct = pct(“XLRE”)  ;
 def XLB_d_pct = pct(“XLB”)  ;
 def XLU_d_pct = pct(“XLU”)  ;
def XLE_d_pct = pct(“XLE”)  ;

AddLabel(yes, "InfoTech 27.2%: " + XLK_d_pct,  (if xlk_d_pct > 0 then Color.GREEN else if xlk_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Healthcare 14.2%: " + XLV_d_pct, (if xlv_d_pct > 0 then Color.GREEN else if xlv_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "ConsDisc 11.5: " + XLY_d_pct, (if xly_d_pct > 0 then Color.GREEN else if xly_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Comms 8.6%: " + XLC_d_pct, (if xlc_d_pct > 0 then Color.GREEN else if xlc_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Financials 11.0%: " + XLF_d_pct, (if xlf_d_pct > 0 then Color.GREEN else if xlf_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Industrials 8.0%: " + XLI_d_pct,(if xli_d_pct > 0 then Color.GREEN else if xli_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "ConsStaples 6.8%: " + XLP_d_pct, (if xlp_d_pct > 0 then Color.GREEN else if xlp_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "RealEstate 2.9%: " + XLRE_d_pct,(if xlre_d_pct > 0 then Color.GREEN else if xlre_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Materials 2.8%: " + XLB_d_pct, (if xlb_d_pct > 0 then Color.GREEN else if xlb_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Utilities 2.9%: " + XLU_d_pct, (if xlu_d_pct > 0 then Color.GREEN else if xlu_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Energy 4.2%: " + XLE_d_pct,(if xle_d_pct > 0 then Color.GREEN else if xle_d_pct < 0 then color.RED else Color.gray));
Thanks for your work and video explanation on this. Great job.
 
Information Technology
27.1%
Health Care
14.4%
Financials
11.2%
Consumer Discretionary
10.9%
Communication Services
8.8%
Industrials
7.8%
Consumer Staples
6.5%
Energy
4.8%
Utilities
3.0%
Materials
2.8%
Real Estate
2.8%
The weightings for each sector of the index are rounded to the nearest tenth of a percent; therefore, the aggregate weights for the index may not equal 100%.

Based on GICS® sectors


As of May 31, 2022
 
Information Technology
27.1%
Health Care
14.4%
Financials
11.2%
Consumer Discretionary
10.9%
Communication Services
8.8%
Industrials
7.8%
Consumer Staples
6.5%
Energy
4.8%
Utilities
3.0%
Materials
2.8%
Real Estate
2.8%
The weightings for each sector of the index are rounded to the nearest tenth of a percent; therefore, the aggregate weights for the index may not equal 100%.

Based on GICS® sectors


As of May 31, 2022
So, is this changing now that it is a new month? Is it significant enough to change the %'s in the indicator every month or is it "close enough" do you think?

Thanks,


Joe
 
Thanks for your work and video explanation on this. Great job.
Anyone, is there a version that does NOT post the different sectors on the chart? 2 problems with that 1. Takes up too much room on the screen, and 2. more importantly the indicator seems to take a ton of memory and wondering if that was removed if this would improve...? Thanks.
 
When Mobius mentioned "adding a weight adjusted by the closing value of AD might be useful", what does he mean?

Does he mean apply a weighted moving average to the closing value of AD (i.e. close(Advance)-close(Decline))?
 
@pk1729
Code:
# # Sources:
# best with 5 mins timeframe.
# concept from shadowtrader.net
# coded by TWOO
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# sector-breakdown

declare lower;
input open_time = 930;


input xlpwt = 6.2; #ConsumerStaples
input xluwt = 2.6; #Utilities
input xlcwt = 9.6; #Communications
input xlfwt = 11.5; #Financials
input xlbwt = 2.6; #Materials
input xliwt = 8.0; #Industrials
input xlewt = 3.7; #Energy
input xlrewt = 2.6; #RealEstate
input xlvwt = 13.3; #HealthCare
input xlkwt = 28.1; #InformationTechnology
input xlywt = 11.8; #ConsumerDiscretionary

def SectorCount = 11;
def Scale = 5000;

#SP500 ETF sectors percent change
script PC {
    input Symbol = "SPX";
    def isFirstBar = GetTime() == RegularTradingStart(GetYYYYMMDD()) + 1;
    def O = if isFirstBar then close(Symbol) else O[1];
    def C = close(Symbol);
    plot PctChg = (C - O) / O;
}
def xlkPctChg = PC("XLK");
def xlyPctChg = PC("XLY");
def xlvPctChg = PC("XLV");
def xlfPctChg = PC("XLF");
def xlcPctChg = PC("XLC");
def xliPctChg = PC("XLI");
def xlpPctChg = PC("XLP");
def xlrePctChg = PC("XLRE");
def xlePctChg = PC("XLE");
def xlbPctChg = PC("XLB");
def xluPctChg = PC("XLU");


def xlkSizing = xlkPctChg * xlkwt;
def xlvSizing = xlvPctChg * xlvwt;
def xlySizing = xlyPctChg * xlywt;
def xlfSizing = xlfPctChg * xlfwt;
def xlcSizing = xlcPctChg * xlcwt;
def xliSizing = xliPctChg * xliwt;
def xlpSizing = xlpPctChg * xlpwt;
def xleSizing = xlePctChg * xlewt;
def xlreSizing = xlrePctChg * xlrewt;
def xlbSizing = xlbPctChg * xlbwt;
def xluSizing = xluPctChg * xluwt;
def combinedSizing = Scale * (
xlkSizing +
xlvSizing +
xlySizing +
xlfSizing +
xlcSizing +
xliSizing +
xlpSizing +
xleSizing +
xlreSizing +
xlbSizing +
xluSizing
) / SectorCount;

# Weighted_AD
plot Weighted_AD = if !IsNaN(combinedSizing) then combinedSizing else Double.NaN;
Weighted_AD.DefineColor("Lower", Color.YELLOW);
Weighted_AD.DefineColor("Higher", Color.MAGENTA);
Weighted_AD.AssignValueColor(if Weighted_AD < Weighted_AD[1] then Weighted_AD.Color("Lower") else  Weighted_AD.Color("Higher"));
Weighted_AD.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

#Zeroline
plot zero = 0;
zero.SetDefaultColor(Color.GRAY);
zero.HideBubble();
zero.HideTitle();

# SPX Non_Weighted_AD
def spxcombinedSizing = Scale *
(xlkPctChg +
xlvPctChg +
xlyPctChg +
xlfPctChg +
xlcPctChg +
xliPctChg +
xlpPctChg +
xlePctChg +
xlrePctChg +
xlbPctChg +
xluPctChg);
plot Non_Weighted_AD =  spxcombinedSizing ;
Non_Weighted_AD.SetDefaultColor(Color.WHITE);
Non_Weighted_AD.SetPaintingStrategy(PaintingStrategy.LINE);

# NDX Non_Weighted_AD
def NDXPctChg = PC("NDX") * 35000;
plot Non_Weighted_NDX = NDXPctChg   ;
Non_Weighted_NDX.SetDefaultColor(Color.CYAN);
Non_Weighted_NDX.SetLineWeight(1);
Non_Weighted_NDX.Hide();
Non_Weighted_NDX.HideBubble();
Non_Weighted_NDX.HideTitle();

AddVerticalLine(( GetDay() <> GetDay()[1]), "", Color.YeLLOW, Curve.SHORT_DASH);

#SP500 ETF sectors BUBBLE
Script pct {
input Symbol = "SPX";
def aggregationPeriod = AggregationPeriod.DAY;
def price = open(Symbol, period = aggregationPeriod);
def diff = close (Symbol) - open(Symbol, period = aggregationPeriod);
 plot d_pct = 100 * diff / price;
}

def XLK_d_pct = pct(“XLK”)  ;
 def XLV_d_pct = pct(“XLV”)  ;
def XLY_d_pct = pct(“XLY”)  ;
def XLC_d_pct = pct(“XLC”)  ;
 def XLF_d_pct = pct(“XLF”)  ;
 def XLI_d_pct = pct(“XLI”)  ;
 def XLP_d_pct = pct(“XLP”)  ;
 def XLRE_d_pct = pct(“XLRE”)  ;
 def XLB_d_pct = pct(“XLB”)  ;
 def XLU_d_pct = pct(“XLU”)  ;
def XLE_d_pct = pct(“XLE”)  ;

AddLabel(yes, "InfoTech 29.2%: " + XLK_d_pct,  (if xlk_d_pct > 0 then Color.GREEN else if xlk_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Healthcare 13.3%: " + XLV_d_pct, (if xlv_d_pct > 0 then Color.GREEN else if xlv_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "ConsDisc 12.5%: " + XLY_d_pct, (if xly_d_pct > 0 then Color.GREEN else if xly_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Comms 10.2%: " + XLC_d_pct, (if xlc_d_pct > 0 then Color.GREEN else if xlc_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Financials 10.7%: " + XLF_d_pct, (if xlf_d_pct > 0 then Color.GREEN else if xlf_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Industrials 7.8%: " + XLI_d_pct,(if xli_d_pct > 0 then Color.GREEN else if xli_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "ConsStaples 5.9%: " + XLP_d_pct, (if xlp_d_pct > 0 then Color.GREEN else if xlp_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "RealEstate 2.8%: " + XLRE_d_pct,(if xlre_d_pct > 0 then Color.GREEN else if xlre_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Materials 2.6%: " + XLB_d_pct, (if xlb_d_pct > 0 then Color.GREEN else if xlb_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Utilities 2.5%: " + XLU_d_pct, (if xlu_d_pct > 0 then Color.GREEN else if xlu_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "Energy 2.7%: " + XLE_d_pct,(if xle_d_pct > 0 then Color.GREEN else if xle_d_pct < 0 then color.RED else Color.gray));plot Data = close
Strange, question for you.....first, thanks for tweaking the indicator. For some reason, it doesn't show up on TOS's mobile platform. Do you know why?
 
So let me get this straight, what this does is weights it with all the ETFs?
def xlkPctChg = PC("XLK");
def xlyPctChg = PC("XLY");
def xlvPctChg = PC("XLV");
def xlfPctChg = PC("XLF");
def xlcPctChg = PC("XLC");
def xliPctChg = PC("XLI");
def xlpPctChg = PC("XLP");
def xlrePctChg = PC("XLRE");
def xlePctChg = PC("XLE");
def xlbPctChg = PC("XLB");
def xluPctChg = PC("XLU");

Is this exactly what the Shadow Trader codes weight it to as well?
 
New weightings
Information Technology
26.8%
Health Care
15.1%
Financials
10.8%
Consumer Discretionary
10.5%
Communication Services
8.9%
Industrials
7.8%
Consumer Staples
7.0%
Energy
4.4%
Utilities
3.1%
Real Estate
2.9%
Materials
2.6%
 
cosmetics changes for better visuals. options for arrow/cloud and made the labels to be dynamic.

# # Sources:
# best with 5 mins timeframe.
# concept from shadowtrader.net
# coded by TWOO
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# sector-breakdown
# modified by CWPARKER23
# modifiedTrueRange by Sam4COK @ 07/2022

declare lower;
input open_time = 930;
input length = 12;
input ShowLabel = yes;
input PriceChannel = yes;
input ShowArrow = no;
input ShowCloud = no;

input InformationTechnology = 26.8; # xlkwt
input HealthCare = 15.1; # xlvwt
input Financials = 10.8; # xlfwt
input ConsumerDiscretionary = 10.5; # xlywt
input Communications = 8.9; # xlcwt
input Industrials = 7.8; # xliwt
input ConsumerStaples = 7.0; # xlpwt
input Energy = 4.4; # xlewt
input Utilities = 3.1; # xluwt
input RealEstate = 2.9; # xlrewt
input Materials = 2.6; # xlbwt

def na = double.NaN ;
def SectorCount = 11;
def Scale = 5000;
def displace = 0;

#### COLORS
DefineGlobalColor("Bullish", Color.cyan);
DefineGlobalColor("Bearish", Color.pink);
DefineGlobalColor("Channel", CreateColor(100, 181, 246));

#SP500 ETF sectors percent change
script PC {
input Symbol = "SPX";
def isFirstBar = GetTime() == RegularTradingStart(GetYYYYMMDD()) + 1;
def O = if isFirstBar then close(Symbol) else O[1];
def C = close(Symbol);
plot PctChg = (C - O) / O;
}
def xlkPctChg = PC("XLK");
def xlyPctChg = PC("XLY");
def xlvPctChg = PC("XLV");
def xlfPctChg = PC("XLF");
def xlcPctChg = PC("XLC");
def xliPctChg = PC("XLI");
def xlpPctChg = PC("XLP");
def xlrePctChg = PC("XLRE");
def xlePctChg = PC("XLE");
def xlbPctChg = PC("XLB");
def xluPctChg = PC("XLU");

def xlkSizing = xlkPctChg * InformationTechnology;
def xlvSizing = xlvPctChg * HealthCare;
def xlySizing = xlyPctChg * ConsumerDiscretionary;
def xlfSizing = xlfPctChg * Financials;
def xlcSizing = xlcPctChg * Communications;
def xliSizing = xliPctChg * Industrials;
def xlpSizing = xlpPctChg * ConsumerStaples;
def xleSizing = xlePctChg * Energy;
def xlreSizing = xlrePctChg * RealEstate;
def xlbSizing = xlbPctChg * Materials;
def xluSizing = xluPctChg * Utilities;

def combinedSizing = Scale * (
xlkSizing +
xlvSizing +
xlySizing +
xlfSizing +
xlcSizing +
xliSizing +
xlpSizing +
xleSizing +
xlreSizing +
xlbSizing +
xluSizing
) / SectorCount;

# Weighted_AD
def Weighted_AD = if !IsNaN(combinedSizing) then combinedSizing else na;
#Zeroline
plot zero = 0;
zero.SetDefaultColor(Color.DARK_GRAY);
zero.HideBubble();
zero.HideTitle();

# SPX Non_Weighted_AD
def spxcombinedSizing = Scale *
(xlkPctChg +
xlvPctChg +
xlyPctChg +
xlfPctChg +
xlcPctChg +
xliPctChg +
xlpPctChg +
xlePctChg +
xlrePctChg +
xlbPctChg +
xluPctChg);

plot Non_Weighted_AD = if !IsNaN(spxcombinedSizing) then spxcombinedSizing else na;
Non_Weighted_AD.SetDefaultColor(Color.WHITE);
Non_Weighted_AD.SetLineWeight(2);
Non_Weighted_AD.HideBubble();
Non_Weighted_AD.HideTitle();

AddCloud(if ShowCloud then Non_Weighted_AD else na, zero , globalColor("Bullish"), globalColor("Bearish"));

#####Price Channel###########
plot LowerBand = if PriceChannel then Lowest(Weighted_AD[-displace + 1], length) else na;
LowerBand.SetDefaultColor(GlobalColor("Channel"));
LowerBand.HideBubble();
LowerBand.HideTitle();

plot UpperBand = if PriceChannel then Highest(Weighted_AD[-displace + 1], length) else na;
UpperBand.SetDefaultColor(GlobalColor("Channel"));
UpperBand.HideBubble();
UpperBand.HideTitle();

plot MID = (UPPERBand + LOWErBand)/2;
MID.SetDefaultColor(GlobalColor("Channel"));
MID.SetStyle(Curve.SHORT_DASH);
MID.HideBubble();
MID.HideTitle();

#######################################
plot Diff_Weighted_AD = Weighted_AD;
Diff_Weighted_AD.SetDefaultColor(GetColor(5));
Diff_Weighted_AD.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Diff_Weighted_AD.SetLineWeight(1);
Diff_Weighted_AD.AssignValueColor(
if Diff_Weighted_AD >= 0 then
if Diff_Weighted_AD > Diff_Weighted_AD[1] then Color.GREEN else Color.DARK_GREEN else
if Diff_Weighted_AD < Diff_Weighted_AD[1] then Color.RED else Color.PINK);

# divergence Weighted_AD
def isbulldiv = (weighted_AD - weighted_AD[1] )>0 and (LOW[1] - LOW)>0;
def isbeardiv = (weighted_AD[1] - weighted_AD )>0 and (HIGH - HIGH[1])>0;

def PositveDivergence = if isbulldiv then non_Weighted_AD else na;

plot PD = if ShowArrow then PositveDivergence else na;
PD.SetPaintingStrategy(PaintingStrategy.ARROW_UP);
PD.SetDefaultColor(Color.WHITE);
PD.HideBubble();
PD.HideTitle();

def NegativeDivergence = if isbeardiv then non_Weighted_AD else na;

plot ND = if ShowArrow then NEGativeDivergence else na;
ND.SetPaintingStrategy(PaintingStrategy.ARROW_DOWN);
ND.SetDefaultColor(Color.YELLOW);
ND.HideBubble();
ND.HideTitle();

AddVerticalLine(( GetDay() <> GetDay()[1]), "", Color.YeLLOW, Curve.SHORT_DASH);

#SP500 ETF sectors BUBBLE
Script pct {
input Symbol = "SPX";
def aggregationPeriod = AggregationPeriod.DAY;
def price = open(Symbol, period = aggregationPeriod);
def diff = close (Symbol) - open(Symbol, period = aggregationPeriod);
plot d_pct = 100 * diff / price;
}

def XLK_d_pct = pct(“XLK”) ;
def XLV_d_pct = pct(“XLV”) ;
def XLY_d_pct = pct(“XLY”) ;
def XLC_d_pct = pct(“XLC”) ;
def XLF_d_pct = pct(“XLF”) ;
def XLI_d_pct = pct(“XLI”) ;
def XLP_d_pct = pct(“XLP”) ;
def XLRE_d_pct = pct(“XLRE”) ;
def XLB_d_pct = pct(“XLB”) ;
def XLU_d_pct = pct(“XLU”) ;
def XLE_d_pct = pct(“XLE”) ;

def Label1 = if ShowLabel and xlk_d_pct !=0 then yes else na;
def Label2 = if ShowLabel and XLV_d_pct !=0 then yes else na;
def Label3 = if ShowLabel and XLY_d_pct !=0 then yes else na;
def Label4 = if ShowLabel and XLC_d_pct !=0 then yes else na;
def Label5 = if ShowLabel and XLF_d_pct !=0 then yes else na;
def Label6 = if ShowLabel and XLI_d_pct !=0 then yes else na;
def Label7 = if ShowLabel and XLP_d_pct !=0 then yes else na;
def Label8 = if ShowLabel and XLRE_d_pct !=0 then yes else na;
def Label9 = if ShowLabel and XLB_d_pct !=0 then yes else na;
def Label10 = if ShowLabel and XLU_d_pct !=0 then yes else na;
def Label11 = if ShowLabel and XLE_d_pct !=0 then yes else na;

AddLabel(Label1, "IT " + InformationTechnology + "%: " + ROUND(XLK_d_pct, 2), (if xlk_d_pct > 0 then Color.GREEN else if xlk_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label2, "HC " + Healthcare + "%: " + ROUND(XLV_d_pct, 2), (if xlv_d_pct > 0 then Color.GREEN else if xlv_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label3, "CD " + ConsumerDiscretionary + "%: " + ROUND(XLY_d_pct, 2), (if xly_d_pct > 0 then Color.GREEN else if xly_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label4, "CO " + Communications + "%: " + ROUND(XLC_d_pct, 2), (if xlc_d_pct > 0 then Color.GREEN else if xlc_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label5, "FN " + Financials + "%: " + ROUND(XLF_d_pct, 2), (if xlf_d_pct > 0 then Color.GREEN else if xlf_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label6, "ID " + Industrials + "%: " + ROUND(XLI_d_pct, 2),(if xli_d_pct > 0 then Color.GREEN else if xli_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label7, "CS " + ConsumerStaples + "%: " + ROUND(XLP_d_pct, 2), (if xlp_d_pct > 0 then Color.GREEN else if xlp_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label8, "RE " + RealEstate + "%: " + ROUND(XLRE_d_pct, 2),(if xlre_d_pct > 0 then Color.GREEN else if xlre_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label9, "MT " + Materials + "%: " + ROUND(XLB_d_pct, 2), (if xlb_d_pct > 0 then Color.GREEN else if xlb_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label10, "UT " + Utilities + "%: " + ROUND(XLU_d_pct, 2), (if xlu_d_pct > 0 then Color.GREEN else if xlu_d_pct < 0 then color.RED else Color.gray));

AddLabel(Label11, "EG " + Energy + "%: " + ROUND(XLE_d_pct, 2),(if xle_d_pct > 0 then Color.GREEN else if xle_d_pct < 0 then color.RED else Color.gray));
 
cosmetics changes for better visuals. options for arrow/cloud and made the labels to be dynamic.
Thanks to @samer800 @Cwparker23 and others who have contributed to this fabulous rendition of an already amazing indicator from Shadow Trader.

If anyone scalps ES futures (like I do), this indicator is the closest one will get to trading real time with real market insights... without the Fibonacci, Moving Avg, channels, support / res fluff... and believe me there is a ton of fluff all around...
Market internals are key to trading ES and this is the best of the lot!!!
 
Information Technology
27.9%
Health Care
14.3%
Consumer Discretionary
11.5%
Financials
10.6%
Communication Services
8.4%
Industrials
7.8%
Consumer Staples
6.6%
Energy
4.4%
Utilities
3.0%
Real Estate
2.9%
Materials
2.5%

The weightings for each sector of the index are rounded to the nearest tenth of a percent; therefore, the aggregate weights for the index may not equal 100%.

Based on GICS® sectors

As of Jul 29, 2022
 
NEED HELP CHANGING HISTOGRAM COLORS LIKE THAT OF TTM SQUEEZE
https://usethinkscript.com/threads/weighted-advance-decline.9487/page-4#post-102799
# # Sources:
# best with 5 mins timeframe.
# concept from shadowtrader.net
# coded by TWOO
# https://www.spglobal.com/spdji/en/indices/equity/sp-500/#data
# sector-breakdown

declare lower;
input open_time = 930;


input xlpwt = 6.2; #ConsumerStaples
input xluwt = 2.6; #Utilities
input xlcwt = 9.6; #Communications
input xlfwt = 11.5; #Financials
input xlbwt = 2.6; #Materials
input xliwt = 8.0; #Industrials
input xlewt = 3.7; #Energy
input xlrewt = 2.6; #RealEstate
input xlvwt = 13.3; #HealthCare
input xlkwt = 28.1; #InformationTechnology
input xlywt = 11.8; #ConsumerDiscretionary

def SectorCount = 11;
def Scale = 5000;

#SP500 ETF sectors percent change
script PC {
input Symbol = "SPX";
def isFirstBar = GetTime() == RegularTradingStart(GetYYYYMMDD()) + 1;
def O = if isFirstBar then close(Symbol) else O[1];
def C = close(Symbol);
plot PctChg = (C - O) / O;
}
def xlkPctChg = PC("XLK");
def xlyPctChg = PC("XLY");
def xlvPctChg = PC("XLV");
def xlfPctChg = PC("XLF");
def xlcPctChg = PC("XLC");
def xliPctChg = PC("XLI");
def xlpPctChg = PC("XLP");
def xlrePctChg = PC("XLRE");
def xlePctChg = PC("XLE");
def xlbPctChg = PC("XLB");
def xluPctChg = PC("XLU");


def xlkSizing = xlkPctChg * xlkwt;
def xlvSizing = xlvPctChg * xlvwt;
def xlySizing = xlyPctChg * xlywt;
def xlfSizing = xlfPctChg * xlfwt;
def xlcSizing = xlcPctChg * xlcwt;
def xliSizing = xliPctChg * xliwt;
def xlpSizing = xlpPctChg * xlpwt;
def xleSizing = xlePctChg * xlewt;
def xlreSizing = xlrePctChg * xlrewt;
def xlbSizing = xlbPctChg * xlbwt;
def xluSizing = xluPctChg * xluwt;
def combinedSizing = Scale * (
xlkSizing +
xlvSizing +
xlySizing +
xlfSizing +
xlcSizing +
xliSizing +
xlpSizing +
xleSizing +
xlreSizing +
xlbSizing +
xluSizing
) / SectorCount;

# Weighted_AD
plot Weighted_AD = if !IsNaN(combinedSizing) then combinedSizing else Double.NaN;
Weighted_AD.DefineColor("Lower", Color.RED);
Weighted_AD.DefineColor("Higher", Color.CYAN);
Weighted_AD.AssignValueColor(if Weighted_AD < Weighted_AD[1] then Weighted_AD.Color("Lower") else Weighted_AD.Color("Higher"));
Weighted_AD.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);

#AddLabel(WEIGHted_AD > 0, if WEIGHted_AD>WEIGHTed_AD[1] then "BULLISH" else "BULLISH PULLBACK", if WEIghted_AD>WEIGHted_AD[1] then WEIGHted_AD.color("PosAndUp") else WEIGHted_AD.color("PosAndDN"));

#Zeroline
plot zero = 0;
zero.SetDefaultColor(Color.GRAY);
zero.HideBubble();
zero.HideTitle();

# SPX Non_Weighted_AD
def spxcombinedSizing = Scale *
(xlkPctChg +
xlvPctChg +
xlyPctChg +
xlfPctChg +
xlcPctChg +
xliPctChg +
xlpPctChg +
xlePctChg +
xlrePctChg +
xlbPctChg +
xluPctChg);
plot Non_Weighted_AD = spxcombinedSizing ;
Non_Weighted_AD.SetDefaultColor(Color.WHITE);
Non_Weighted_AD.SetPaintingStrategy(PaintingStrategy.LINE);

# NDX Non_Weighted_AD
def NDXPctChg = PC("NDX") * 35000;
plot Non_Weighted_NDX = NDXPctChg ;
Non_Weighted_NDX.SetDefaultColor(Color.CYAN);
Non_Weighted_NDX.SetLineWeight(1);
Non_Weighted_NDX.Hide();
Non_Weighted_NDX.HideBubble();
Non_Weighted_NDX.HideTitle();

AddVerticalLine(( GetDay() <> GetDay()[1]), "", Color.YeLLOW, Curve.SHORT_DASH);

#SP500 ETF sectors BUBBLE
Script pct {
input Symbol = "SPX";
def aggregationPeriod = AggregationPeriod.DAY;
def price = open(Symbol, period = aggregationPeriod);
def diff = close (Symbol) - open(Symbol, period = aggregationPeriod);
plot d_pct = 100 * diff / price;
}

def XLK_d_pct = pct(“XLK”) ;
def XLV_d_pct = pct(“XLV”) ;
def XLY_d_pct = pct(“XLY”) ;
def XLC_d_pct = pct(“XLC”) ;
def XLF_d_pct = pct(“XLF”) ;
def XLI_d_pct = pct(“XLI”) ;
def XLP_d_pct = pct(“XLP”) ;
def XLRE_d_pct = pct(“XLRE”) ;
def XLB_d_pct = pct(“XLB”) ;
def XLU_d_pct = pct(“XLU”) ;
def XLE_d_pct = pct(“XLE”) ;

AddLabel(yes, "TECH 29.2%: " + XLK_d_pct, (if xlk_d_pct > 0 then Color.GREEN else if xlk_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "HEALTHCARE 13.3%: " + XLV_d_pct, (if xlv_d_pct > 0 then Color.GREEN else if xlv_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "CONSUMERDIS 12.5%: " + XLY_d_pct, (if xly_d_pct > 0 then Color.GREEN else if xly_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "COMMUNICATIONS 10.2%: " + XLC_d_pct, (if xlc_d_pct > 0 then Color.GREEN else if xlc_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "FINANACIALS 10.7%: " + XLF_d_pct, (if xlf_d_pct > 0 then Color.GREEN else if xlf_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "INDUSTRIALS 7.8%: " + XLI_d_pct,(if xli_d_pct > 0 then Color.GREEN else if xli_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "CONSUMERSTAPLES 5.9%: " + XLP_d_pct, (if xlp_d_pct > 0 then Color.GREEN else if xlp_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "REALSTATE 2.8%: " + XLRE_d_pct,(if xlre_d_pct > 0 then Color.GREEN else if xlre_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "MATERIALS 2.6%: " + XLB_d_pct, (if xlb_d_pct > 0 then Color.GREEN else if xlb_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "UTILITIES 2.5%: " + XLU_d_pct, (if xlu_d_pct > 0 then Color.GREEN else if xlu_d_pct < 0 then color.RED else Color.gray));

AddLabel(yes, "ENERGY 2.7%: " + XLE_d_pct,(if xle_d_pct > 0 then Color.GREEN else if xle_d_pct < 0 then color.RED else Color.gray));plot Data = close;
 
Last edited by a moderator:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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