S&P 500 Sector Performance

T

tomsk

Well-known member
VIP
Warehouse
I found an S&P 500 Sector Performance chart from the Yahoo ThinkScript newsgroup, written by Jaimie Pinto that folks might find interesting.
It was written sometime ago but still very applicable today. Please enjoy it!

Code:
# PINTO_SectorsOverlay
# Emulation of the PerfChart: S&P Sector ETFs
# http://stockcharts.com/freecharts/perf.php?[SECT]
# Updated on May/22/2012

declare upper;

input ShowOverlay    = yes;
input pivot          = 180;  ### bars
input length         = 4;
input displace       = 3;
input SnP500         = "SPY";
input Cyclicals      = "XLY";
input Technology     = "XLK";
input Industrials    = "XLI";
input Materials      = "XLB";
input Energy         = "XLE";
input Staples        = "XLP";
input HealthCare     = "XLV";
input Utilities      = "XLU";
input Financials     = "XLF";

def BasePrice        = WildersAverage(open             , length)[-length + displace];
def SnP500Price      = WildersAverage(open(SnP500)     , length)[-length + displace];
def CyclicalsPrice   = WildersAverage(open(Cyclicals)  , length)[-length + displace];
def TechnologyPrice  = WildersAverage(open(Technology) , length)[-length + displace];
def IndustrialsPrice = WildersAverage(open(Industrials), length)[-length + displace];
def MaterialsPrice   = WildersAverage(open(Materials)  , length)[-length + displace];
def EnergyPrice      = WildersAverage(open(Energy)     , length)[-length + displace];
def StaplesPrice     = WildersAverage(open(Staples)    , length)[-length + displace];
def HealthCarePrice  = WildersAverage(open(HealthCare) , length)[-length + displace];
def UtilitiesPrice   = WildersAverage(open(Utilities)  , length)[-length + displace];
def FinancialsPrice  = WildersAverage(open(Financials) , length)[-length + displace];

def barNumber        = if barNumber() > pivot then barNumber() else 1;
def BaseOpen         = if barNumber == 1 then basePrice        else BaseOpen[1];
def SnP500Open       = if barNumber == 1 then SnP500Price      else SnP500Open[1];
def CyclicalsOpen    = if barNumber == 1 then CyclicalsPrice   else CyclicalsOpen[1];
def TechnologyOpen   = if barNumber == 1 then TechnologyPrice  else TechnologyOpen[1];
def IndustrialsOpen  = if barNumber == 1 then IndustrialsPrice else IndustrialsOpen[1];
def MaterialsOpen    = if barNumber == 1 then MaterialsPrice   else MaterialsOpen[1];
def EnergyOpen       = if barNumber == 1 then EnergyPrice      else EnergyOpen[1];
def StaplesOpen      = if barNumber == 1 then StaplesPrice     else StaplesOpen[1];
def HealthCareOpen   = if barNumber == 1 then HealthCarePrice  else HealthCareOpen[1];
def UtilitiesOpen    = if barNumber == 1 then UtilitiesPrice   else UtilitiesOpen[1];
def FinancialsOpen   = if barNumber == 1 then FinancialsPrice  else FinancialsOpen[1];

def Multiplier       = BaseOpen;

plot dot =  if barNumber() == pivot then BaseOpen else Double.NaN;
dot.SetDefaultColor(Color.WHITE);
dot.SetStyle(Curve.POINTS);
dot.SetLineWeight(5);

plot basePriceToPlot        = if ShowOverlay then BasePrice        - (BasePrice       - BaseOpen) + Multiplier * (BasePrice        - BaseOpen)        / BaseOpen        else Double.NaN;
basePriceToPlot.SetDefaultColor(Color.WHITE);
AddLabel(ShowOverlay, " " + GetUnderlyingSymbol() + " ", Color.WHITE);
basePriceToPlot.SetLineWeight(2);

plot SnP500PriceToPlot      = if ShowOverlay then SnP500Price      - (SnP500Open      - BaseOpen) + Multiplier * (SnP500Price      - SnP500Open)     / SnP500Open      else Double.NaN;
SnP500PriceToPlot.SetDefaultColor(Color.RED);
AddLabel(ShowOverlay, " SnP500 ", Color.RED);

plot CyclicalsPriceToPlot   = if ShowOverlay then CyclicalsPrice   - (CyclicalsOpen   - BaseOpen) + Multiplier * (CyclicalsPrice   - CyclicalsOpen)  / CyclicalsOpen   else Double.NaN;
CyclicalsPriceToPlot.SetDefaultColor(Color.VIOLET);
AddLabel(ShowOverlay, " Cyclicals ", Color.VIOLET);

plot TechologyPriceToPlot   = if ShowOverlay then TechnologyPrice  - (TechnologyOpen  - BaseOpen) + Multiplier * (TechnologyPrice  - TechnologyOpen) / TechnologyOpen  else Double.NaN;
TechologyPriceToPlot.SetDefaultColor(Color.GREEN);
AddLabel(ShowOverlay, " Techology ", Color.GREEN);

plot IndustrialsPriceToPlot = if ShowOverlay then IndustrialsPrice - (IndustrialsOpen - BaseOpen) + Multiplier * (IndustrialsPrice - IndustrialsOpen) / IndustrialsOpen else Double.NaN;
IndustrialsPriceToPlot.SetDefaultColor(Color.MAGENTA);
AddLabel(ShowOverlay, " Industrials ", Color.MAGENTA);

plot MaterialsPriceToPlot   = if ShowOverlay then MaterialsPrice   - (MaterialsOpen   - BaseOpen) + Multiplier * (MaterialsPrice   - MaterialsOpen)  / MaterialsOpen   else Double.NaN;
MaterialsPriceToPlot.SetDefaultColor(Color.CYAN);
AddLabel(ShowOverlay, " Materials ", Color.CYAN);

plot EnergyPriceToPlot      = if ShowOverlay then EnergyPrice      - (EnergyOpen      - BaseOpen) + Multiplier * (EnergyPrice      - EnergyOpen)     / EnergyOpen      else Double.NaN;
EnergyPriceToPlot.SetDefaultColor(Color.YELLOW);
AddLabel(ShowOverlay, " Energy ", Color.YELLOW);

plot StaplesPriceToPlot     = if ShowOverlay then StaplesPrice     - (StaplesOpen     - BaseOpen) + Multiplier * (StaplesPrice     - StaplesOpen)    / StaplesOpen     else Double.NaN;
StaplesPriceToPlot.SetDefaultColor(Color.DARK_GREEN);
AddLabel(ShowOverlay, " Staples ", Color.DARK_GREEN);

plot HealthCarePriceToPlot  = if ShowOverlay then HealthCarePrice  - (HealthCareOpen  - BaseOpen) + Multiplier * (HealthCarePrice  - HealthCareOpen) / HealthCareOpen  else Double.NaN;
HealthCarePriceToPlot.SetDefaultColor(Color.PLUM);
AddLabel(ShowOverlay, " HealthCare ", Color.PLUM);

plot UtilitiesPriceToPlot   = if ShowOverlay then UtilitiesPrice   - (UtilitiesOpen   - BaseOpen) + Multiplier * (UtilitiesPrice   - UtilitiesOpen)  / UtilitiesOpen   else Double.NaN;
UtilitiesPriceToPlot.SetDefaultColor(Color.ORANGE);
AddLabel(ShowOverlay, " Utilities ", Color.ORANGE);

plot FinancialsPriceToPlot  = if ShowOverlay then FinancialsPrice  - (FinancialsOpen  - BaseOpen) + Multiplier * (FinancialsPrice  - FinancialsOpen) / FinancialsOpen  else Double.NaN;
FinancialsPriceToPlot.SetDefaultColor(Color.PINK);
AddLabel(ShowOverlay, " Financials ", Color.PINK);
 
markos

markos

Well-known member
VIP
Warehouse
Hi @tomsk when you get back from vacation could you please review this? Especially with the coloring by @mn88 , I hope this can be worthy of your time. Happy New Year!
 
S

samiranadhikari

New member
Is it OK? See SPY performance (343.74) is different from SnP500 (321.14) in the chart (right Panel) ??
 
T

tomsk

Well-known member
VIP
Warehouse
@markos I took a look at this study, the code looks pretty clean. What I really like about this is that there are bubbles that depict correlated sectors to the SPX which in turn suggests sectors to concentrate in/avoid. However the display of the plots seems to be rather "haphazard" with the use of different painting styles namely LINE_VS_SQUARE, LONG DASH, SHORT DASH, LINE_vs_POINTS and LINE.

Based on my observations, I have made some minor modifications on ed_nn's original code that was posted to TSL in April 2019. My mods standardized the plot display for stoch1 through stoch9 to use LINE so that it no longer has that "haphazard" look. In addition, I appended in the bubble the sector percent correlation so that at one glance one can determine the top 2-3 sectors.

The only downside is that some of the sectors are so close together that the bubbles overlap - I see no easy resolution to that, so perhaps the thing to do is to concentrate on the top/bottom 3 sectors.

Here then is version 1,1 of the code which originally was named Beta Rotation, I have retained that name in the same spirit.

Code:
# Beta Rotation V1.1
# ed_nn, with modifications by tomsk
# 1.4.2020

# V1.0 - 04.10.2019 - ed_nn - Initial release of Beta Rotation from TSL
# V1.1 - 01.04.2020 - tomsk - Standardized plots for stoch1-stoch9 and added sector percent correlation

# ed_nn: Maybe it has some interest. It attempts to code a posted idea
# (using Beta + Stochastics to observe rotation) from last week

# Mobius: To find rotation quickly, use primary ETF's in a watchlist with 2 columns
# first column is Correlation to SPX, second column is a stochastic of Beta
# if Beta is 1 or close to 1 that ETF is moving at the fastest momentum in that range
# and if correlation is with SPX .85 or better it's moving with SPX correlation below 0
# and it's moving the opposite of SPX.

declare lower;

input BetaLength = 21;
input StochLength =34;
input showBeta = No;
input showOverlay = Yes;

input Cyclicals      = "XLY";
input Technology     = "XLK";
input Industrials    = "XLI";
input Materials      = "XLB";
input Energy         = "XLE";
input Staples        = "XLP";
input HealthCare     = "XLV";
input Utilities      = "XLU";
input Financials     = "XLF";

defineglobalColor(“PlumMedium“, createColor(221, 160, 221));
defineglobalColor(“Orchid“, createColor(218, 130, 214));
defineglobalColor(“MediumOrchid“, createColor(186, 85, 211));
defineglobalColor(“MediumPurple“, createColor(147, 112, 219));
defineglobalColor(“DarkOrchid“, createColor(153, 50, 204));

addLabel(1,"Rotation Beta/Stochastic (" + betaLength + "," +stochLength + ") ", color.Light_Gray);

script calcBeta {
  input secondSymbol = "XLF";
  input refSymbol = "SPX";
  input betaLength = 21;
  input returnLength = 1;

  def refPrice = close(refSymbol);
  def primary = if refPrice[returnLength] == 0
                then 0
                else (refPrice - refPrice[returnLength]) /
                      refPrice[returnLength] * 100;
  def secondPrice = close(secondSymbol);
  def secondary = if secondPrice[returnLength] == 0
                  then 0
                  else (secondPrice - secondPrice[returnLength]) /
                        secondPrice[returnLength] * 100;
  plot Beta = covariance(secondary, primary, betaLength) /
                         Sqr(stdev(primary, betaLength));
}

script EhlersESSfilter {
    input price = close;
    input length = 8;
    def ESS_coeff_0 = Exp(-Double.Pi * Sqrt(2) / length);
    def ESS_coeff_2 = 2 * ESS_coeff_0 * Cos(Sqrt(2) * Double.Pi / length);
    def ESS_coeff_3 = - Sqr(ESS_coeff_0);
    def ESS_coeff_1 = 1 - ESS_coeff_2 - ESS_coeff_3;
    def ESS_filter = if IsNaN(price + price[1]) then
                      ESS_filter[1]
                 else ESS_coeff_1 * (price + price[1]) / 2 +
                      ESS_coeff_2 * ESS_filter[1] +
                      ESS_coeff_3 * ESS_filter[2];
    plot Smooth_Filter =
         if barnumber() <  length then
              price
         else if !IsNaN(price) then
              ESS_filter
         else Double.NaN;
}

script calcStoch {
  input data = close;
  input StochLength = 21;
  def stochasticValue = ((data - lowest(data, StochLength)) /
                         (highest(data, StochLength) - lowest(data, StochLength)));
  plot stoch = stochasticValue;
}

plot beta1 = if showBeta then calcBeta(Cyclicals) else Double.NaN;
plot beta2 = if showBeta then calcBeta(Technology) else Double.NaN;

plot stoch1 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Cyclicals,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch1.SetDefaultColor(Color.VIOLET);
stoch1.SetLineWeight(2);
stoch1.SetPaintingStrategy(PaintingStrategy.LINE);
stoch1.HideBubble();
AddLabel(ShowOverlay, " Cyclicals ", Color.VIOLET);
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch1 < 0.15 then 0 else if stoch1 > 0.85 then 1 else stoch1,
"Cyclicals (" + AsPercent(stoch1) + ")", Color.VIOLET, stoch1 > 0.5);

plot stoch2 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Technology,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch2.SetDefaultColor(CreateColor(90, 160, 120));
stoch2.SetLineWeight(5);
stoch2.SetPaintingStrategy(PaintingStrategy.LINE);
stoch2.HideBubble();
AddLabel(ShowOverlay, " Techology ", CreateColor(90, 160, 120));
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch2 < 0.15 then 0 else if stoch2 > 0.85 then 1 else stoch2, "Technology (" + AsPercent(stoch2) + ")", CreateColor(90, 160, 120), stoch2 > 0.5);

plot stoch3 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Industrials,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch3.SetDefaultColor(Color.MAGENTA);
stoch3.SetLineWeight(5);
stoch3.SetPaintingStrategy(PaintingStrategy.LINE);
stoch3.HideBubble();
AddLabel(ShowOverlay, " Industrials ", Color.MAGENTA);
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch3 < 0.15 then 0 else if stoch3 > 0.85 then 1 else stoch3, "Industrials (" + AsPercent(stoch3) + ")", Color.MAGENTA, stoch3 > 0.5);

plot stoch4 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Materials,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch4.SetDefaultColor(Color.CYAN);
stoch4.SetLineWeight(2);
stoch4.SetPaintingStrategy(PaintingStrategy.LINE);
stoch4.HideBubble();
AddLabel(ShowOverlay, " Materials ", Color.CYAN);
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch4 < 0.15 then 0 else if stoch4 > 0.85 then 1 else stoch4, "Materials (" + AsPercent(stoch4) + ")", Color.CYAN, stoch4 > 0.5);

plot stoch5 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Energy,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch5.SetDefaultColor(Color.YELLOW);
stoch5.SetLineWeight(1);
stoch5.SetPaintingStrategy(PaintingStrategy.LINE);
stoch5.HideBubble();
AddLabel(ShowOverlay, " Energy ", Color.YELLOW);
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch5 < 0.15 then 0 else if stoch5 > 0.85 then 1 else stoch5,
"Energy (" + AsPercent(stoch5) + ")", Color.YELLOW, stoch5 > 0.5);

plot stoch6 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Staples,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch6.SetDefaultColor(CreateColor(80, 180, 70));
stoch6.SetLineWeight(2);
stoch6.SetPaintingStrategy(PaintingStrategy.LINE);
stoch6.HideBubble();
AddLabel(ShowOverlay, " Staples ", CreateColor(80, 180, 70));
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch6 < 0.15 then 0 else if stoch6 > 0.85 then 1 else stoch6,
"Staples (" + AsPercent(stoch6) + ")", CreateColor(80, 180, 70), stoch6 > close);

plot stoch7 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = HealthCare,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch7.SetDefaultColor(CreateColor(180, 80, 180));
stoch7.SetLineWeight(4);
stoch7.SetPaintingStrategy(PaintingStrategy.LINE);
stoch7.HideBubble();
AddLabel(ShowOverlay, " HealthCare ", CreateColor(180, 80, 180));
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch7 < 0.15 then 0 else if stoch7 > 0.85 then 1 else stoch7, "HealthCare (" + AsPercent(stoch7) + ")", CreateColor(180, 80, 180), stoch7 > 0.5);

plot stoch8 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Utilities,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch8.SetDefaultColor(Color.ORANGE);
stoch8.SetLineWeight(2);
stoch8.SetPaintingStrategy(PaintingStrategy.LINE);
stoch8.HideBubble();
AddLabel(ShowOverlay, " Utilities ", Color.ORANGE);
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close[0]), if stoch8 < 0.15 then 0 else if stoch8 > 0.85 then 1 else stoch8, "Utilities (" + AsPercent(stoch8) + ")", Color.ORANGE, stoch8 > 0.5);

plot stoch9 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Financials,
                               betaLength = BetaLength)),
                                     stochLength = StochLength);
stoch9.SetDefaultColor(Color.PINK);
stoch9.SetLineWeight(2);
stoch9.SetPaintingStrategy(PaintingStrategy.LINE);
stoch9.HideBubble();
AddLabel(ShowOverlay, " Financials ", Color.PINK);
AddChartBubble(ShowOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch9 < 0.15 then 0 else if stoch9 > 0.85 then 1 else stoch9, "Financials (" + AsPercent(stoch9) + ")", Color.PINK, stoch9 > 0.5);

def barNumber = BarNumber();
def endBar = if !IsNaN(close) and IsNaN(close[-1]) then barNumber else endBar[1];
def lastBar = HighestAll(endBar);

input flowLabelStep = 40;

DefineGlobalColor("YellowGreen", CreateColor(90, 140, 5));
AddChartBubble(barNumber == (lastBar - flowLabelStep), 1.01, "M O N E Y   F L O W S   I N", globalColor("YellowGreen"), 1);
AddChartBubble(barNumber == (lastBar - 2*flowLabelStep), 1.01, "M O N E Y   F L O W S   I N", globalColor("YellowGreen"), 1);
AddChartBubble(barNumber == (lastBar - 3*flowLabelStep), 1.01, "M O N E Y   F L O W S   I N", globalColor("YellowGreen"), 1);

DefineGlobalColor("Cinamon", CreateColor(200, 10, 40));
AddChartBubble(barNumber == (lastBar - flowLabelStep), -0.01, "M O N E Y   F L O W S   O U T", globalColor("Cinamon"), 0);
AddChartBubble(barNumber == (lastBar - 2*flowLabelStep), -0.01, "M O N E Y   F L O W S   O U T", globalColor("Cinamon"), 0);
AddChartBubble(barNumber == (lastBar - 3*flowLabelStep), -0.01, "M O N E Y   F L O W S   O U T", globalColor("Cinamon"), 0);

plot zero = if barNumber > (lastBar + 7) then double.nan else 0;
zero.SetDefaultColor(createColor(90, 20, 20));
zero.SetStyle(Curve.Long_Dash);
zero.SetLineWeight(5);
zero.HideBubble();

plot one = if barNumber > (lastBar + 7) then double.nan else 1;
one.SetDefaultColor(createColor(20, 70, 20));
one.SetStyle(Curve.Long_Dash);
one.SetLineWeight(5);
one.HideBubble();
# End Beta Rotation V1.1
 
Last edited:
R

RmS59

New member
Forgive me if I mess up this post - it is my first.

I agree with your concern about difficulty seeing all the bubbles. I added the value of the stoch to the labels.

Here is my revised code:

Code:
# From https://usethinkscript.com/threads/s-p-500-sector-performance.1152/

# Beta Rotation V1.1
# ed_nn, with modifications by tomsk
# 1.4.2020

# V1.0 - 04.10.2019 - ed_nn - Initial release of Beta Rotation from TSL
# V1.1 - 01.04.2020 - tomsk - Standardized plots for stoch1-stoch9 and added sector percent correlation
# V1.2 -01.11.2020 - RmS59 - added Stoch values to the labels

# ed_nn: Maybe it has some interest. It attempts to code a posted idea
# (using Beta + Stochastics to observe rotation) from last week

# Mobius: To find rotation quickly, use primary ETF's in a watchlist with 2 columns
# first column is Correlation to SPX, second column is a stochastic of Beta
# if Beta is 1 or close to 1 that ETF is moving at the fastest momentum in that range
# and if correlation is with SPX .85 or better it's moving with SPX correlation below 0
# and it's moving the opposite of SPX.

# RmS59: To facilitate legibility, the value of the various sector stochs are displayed in the Labels


declare lower;

input BetaLength = 21;
input StochLength = 34;
input showBeta = No;
input showOverlay = Yes;

input Discretionary  = "XLY";
input Technology     = "XLK";
input Industrials    = "XLI";
input Materials      = "XLB";
input Energy         = "XLE";
input Staples        = "XLP";
input HealthCare     = "XLV";
input Utilities      = "XLU";
input Financials     = "XLF";

DefineGlobalColor(“PlumMedium“, CreateColor(221, 160, 221));
DefineGlobalColor(“Orchid“, CreateColor(218, 130, 214));
DefineGlobalColor(“MediumOrchid“, CreateColor(186, 85, 211));
DefineGlobalColor(“MediumPurple“, CreateColor(147, 112, 219));
DefineGlobalColor(“DarkOrchid“, CreateColor(153, 50, 204));

AddLabel(1, "Rotation Beta/Stochastic (" + BetaLength + "," + StochLength + ") ", Color.LIGHT_GRAY);

script calcBeta {
    input secondSymbol = "XLF";
    input refSymbol = "SPX";
    input betaLength = 21;
    input returnLength = 1;

    def refPrice = close(refSymbol);
    def primary = if refPrice[returnLength] == 0
                then 0
                else (refPrice - refPrice[returnLength]) /
                      refPrice[returnLength] * 100;
    def secondPrice = close(secondSymbol);
    def secondary = if secondPrice[returnLength] == 0
                  then 0
                  else (secondPrice - secondPrice[returnLength]) /
                        secondPrice[returnLength] * 100;
    plot Beta = Covariance(secondary, primary, betaLength) /
                         Sqr(StDev(primary, betaLength));
}

script EhlersESSfilter {
    input price = close;
    input length = 8;
    def ESS_coeff_0 = Exp(-Double.Pi * Sqrt(2) / length);
    def ESS_coeff_2 = 2 * ESS_coeff_0 * Cos(Sqrt(2) * Double.Pi / length);
    def ESS_coeff_3 = - Sqr(ESS_coeff_0);
    def ESS_coeff_1 = 1 - ESS_coeff_2 - ESS_coeff_3;
    def ESS_filter = if IsNaN(price + price[1]) then
                      ESS_filter[1]
                 else ESS_coeff_1 * (price + price[1]) / 2 +
                      ESS_coeff_2 * ESS_filter[1] +
                      ESS_coeff_3 * ESS_filter[2];
    plot Smooth_Filter =
         if BarNumber() <  length then
              price
         else if !IsNaN(price) then
              ESS_filter
         else Double.NaN;
}

script calcStoch {
    input data = close;
    input StochLength = 21;
    def stochasticValue = ((data - Lowest(data, StochLength)) /
                         (Highest(data, StochLength) - Lowest(data, StochLength)));
    plot stoch = stochasticValue;
}

plot beta1 = if showBeta then calcBeta(Discretionary) else Double.NaN;
plot beta2 = if showBeta then calcBeta(Technology) else Double.NaN;

plot stoch1 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Discretionary,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch1.SetDefaultColor(Color.VIOLET);
stoch1.SetLineWeight(2);
stoch1.SetPaintingStrategy(PaintingStrategy.LINE);
stoch1.HideBubble();
AddLabel(showOverlay, " Discretionary " + 100 * Round(stoch1, 2), Color.VIOLET);
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch1 < 0.15 then 0 else if stoch1 > 0.85 then 1 else stoch1,
"Discretionary (" + AsPercent(stoch1) + ")", Color.VIOLET, stoch1 > 0.5);

plot stoch2 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Technology,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch2.SetDefaultColor(CreateColor(90, 160, 120));
stoch2.SetLineWeight(5);
stoch2.SetPaintingStrategy(PaintingStrategy.LINE);
stoch2.HideBubble();
AddLabel(showOverlay, " Techology " + 100 * Round(stoch2, 2), CreateColor(90, 160, 120));
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch2 < 0.15 then 0 else if stoch2 > 0.85 then 1 else stoch2, "Technology (" + AsPercent(stoch2) + ")", CreateColor(90, 160, 120), stoch2 > 0.5);

plot stoch3 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Industrials,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch3.SetDefaultColor(Color.MAGENTA);
stoch3.SetLineWeight(5);
stoch3.SetPaintingStrategy(PaintingStrategy.LINE);
stoch3.HideBubble();
AddLabel(showOverlay, " Industrials " + 100 * Round(stoch3, 2), Color.MAGENTA);
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch3 < 0.15 then 0 else if stoch3 > 0.85 then 1 else stoch3, "Industrials (" + AsPercent(stoch3) + ")", Color.MAGENTA, stoch3 > 0.5);

plot stoch4 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Materials,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch4.SetDefaultColor(Color.CYAN);
stoch4.SetLineWeight(2);
stoch4.SetPaintingStrategy(PaintingStrategy.LINE);
stoch4.HideBubble();
AddLabel(showOverlay, " Materials " + 100 * Round(stoch4, 2), Color.CYAN);
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch4 < 0.15 then 0 else if stoch4 > 0.85 then 1 else stoch4, "Materials (" + AsPercent(stoch4) + ")", Color.CYAN, stoch4 > 0.5);

plot stoch5 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Energy,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch5.SetDefaultColor(Color.YELLOW);
stoch5.SetLineWeight(1);
stoch5.SetPaintingStrategy(PaintingStrategy.LINE);
stoch5.HideBubble();
AddLabel(showOverlay, " Energy " + 100 * Round(stoch5, 2), Color.YELLOW);
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch5 < 0.15 then 0 else if stoch5 > 0.85 then 1 else stoch5,
"Energy (" + AsPercent(stoch5) + ")", Color.YELLOW, stoch5 > 0.5);

plot stoch6 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Staples,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch6.SetDefaultColor(CreateColor(80, 180, 70));
stoch6.SetLineWeight(2);
stoch6.SetPaintingStrategy(PaintingStrategy.LINE);
stoch6.HideBubble();
AddLabel(showOverlay, " Staples " + 100 * Round(stoch6, 2), CreateColor(80, 180, 70));
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch6 < 0.15 then 0 else if stoch6 > 0.85 then 1 else stoch6,
"Staples (" + AsPercent(stoch6) + ")", CreateColor(80, 180, 70), stoch6 > close);

plot stoch7 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = HealthCare,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch7.SetDefaultColor(CreateColor(180, 80, 180));
stoch7.SetLineWeight(4);
stoch7.SetPaintingStrategy(PaintingStrategy.LINE);
stoch7.HideBubble();
AddLabel(showOverlay, " HealthCare " + 100 * Round(stoch7, 2), CreateColor(180, 80, 180));
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch7 < 0.15 then 0 else if stoch7 > 0.85 then 1 else stoch7, "HealthCare (" + AsPercent(stoch7) + ")", CreateColor(180, 80, 180), stoch7 > 0.5);

plot stoch8 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Utilities,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch8.SetDefaultColor(Color.ORANGE);
stoch8.SetLineWeight(2);
stoch8.SetPaintingStrategy(PaintingStrategy.LINE);
stoch8.HideBubble();
AddLabel(showOverlay, " Utilities " + 100 * Round(stoch8, 2), Color.ORANGE);
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close[0]), if stoch8 < 0.15 then 0 else if stoch8 > 0.85 then 1 else stoch8, "Utilities (" + AsPercent(stoch8) + ")", Color.ORANGE, stoch8 > 0.5);

plot stoch9 = calcStoch(
                        data = EhlersESSfilter(
                               calcBeta(secondSymbol = Financials,
                               betaLength = betaLength)),
                                     stochLength = StochLength);
stoch9.SetDefaultColor(Color.PINK);
stoch9.SetLineWeight(2);
stoch9.SetPaintingStrategy(PaintingStrategy.LINE);
stoch9.HideBubble();
AddLabel(showOverlay, " Financials " + 100 * Round(stoch9, 2), Color.PINK);
AddChartBubble(showOverlay and IsNaN(close[-1]) and !IsNaN(close), if stoch9 < 0.15 then 0 else if stoch9 > 0.85 then 1 else stoch9, "Financials (" + AsPercent(stoch9) + ")", Color.PINK, stoch9 > 0.5);

def barNumber = BarNumber();
def endBar = if !IsNaN(close) and IsNaN(close[-1]) then barNumber else endBar[1];
def lastBar = HighestAll(endBar);

input flowLabelStep = 40;

DefineGlobalColor("YellowGreen", CreateColor(90, 140, 5));
AddChartBubble(barNumber == (lastBar - flowLabelStep), 1.01, "M O N E Y   F L O W S   I N", GlobalColor("YellowGreen"), 1);
AddChartBubble(barNumber == (lastBar - 2 * flowLabelStep), 1.01, "M O N E Y   F L O W S   I N", GlobalColor("YellowGreen"), 1);
AddChartBubble(barNumber == (lastBar - 3 * flowLabelStep), 1.01, "M O N E Y   F L O W S   I N", GlobalColor("YellowGreen"), 1);

DefineGlobalColor("Cinamon", CreateColor(200, 10, 40));
AddChartBubble(barNumber == (lastBar - flowLabelStep), -0.01, "M O N E Y   F L O W S   O U T", GlobalColor("Cinamon"), 0);
AddChartBubble(barNumber == (lastBar - 2 * flowLabelStep), -0.01, "M O N E Y   F L O W S   O U T", GlobalColor("Cinamon"), 0);
AddChartBubble(barNumber == (lastBar - 3 * flowLabelStep), -0.01, "M O N E Y   F L O W S   O U T", GlobalColor("Cinamon"), 0);

plot zero = if barNumber > (lastBar + 7) then Double.NaN else 0;
zero.SetDefaultColor(CreateColor(90, 20, 20));
zero.SetStyle(Curve.LONG_DASH);
zero.SetLineWeight(5);
zero.HideBubble();

plot one = if barNumber > (lastBar + 7) then Double.NaN else 1;
one.SetDefaultColor(CreateColor(20, 70, 20));
one.SetStyle(Curve.LONG_DASH);
one.SetLineWeight(5);
one.HideBubble();
# End Beta Rotation V1.1
 
Last edited by a moderator:
T

tomsk

Well-known member
VIP
Warehouse
When I was reviewing this for @markos I did think about adding the percentages to the labels but felt that it might be better to focus on the top/bottom 3 sectors rather than the entire range. Another approach might be to ditch all the rather haphazard looking plots and just represent the percentages via labels. However in so doing there is no way to rank/sequence these labels dynamically. Hence I left it as version 1.1.
 

Top