# 11 ETF sector lines, difference from close of last bar yesterday For ThinkOrSwim

#### halcyonguy

##### Well-known member
VIP
11 ETF sector lines, difference from close of last bar yesterday

this study is a response to this post

looking over your posts, and post#4, i guessed at what you are after.
you want to see lines,
for the 11 ETF sectors,
that show the difference from the current bar,
to the last bar close of yesterday.

i copied some code from some studies i have and came up with this.

plot 11 lines, of the difference is each sector price.
the lines can be turned on or off.
i didn't set any colors for the lines, they seem to be assigned in the order of getcolor() numbers, starting with 1.
https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Look---Feel/GetColor
i did copy the line colors and used them in the labels, so the labels are the same color as the lines.

there are 3 sets of labels. each can be turned on or off.
labels_name , ETF name , line color
labels_values , the difference from yesterday , line color
labels_values_updown_colors , color decided by value, + = green , - = red

i try to use def on all formulas. then have an option to plot the data or not. i thought in this case, maybe a user would want to see just the labels, without the lines.

i added a plot at 0, as a reference line. it extends 3 bars past the last bar, to make it easier to identify.

you would think that the close of the last bar and the close of the day would be the same, but sometimes they aren't.
i don't remember why, maybe someone else can elaborate on it.
i left some code in, but disabled, where i calculated the day close.

Ruby:
``````# prevdaylastclose_00d_sectors

# sector diff, prev day to current

# REF - #this will find prices from the last bar of the day

declare lower;

def na = double.nan;
def bn = BarNumber();
#---------------------
# i left this in for future experimenting
#def agg = aggregationPeriod.day;
#def close_agg = close(period = agg);
#def prevdayclose = close_agg[1];
#----------------------

# sector symbols
input s01_Energy         = "XLE";
input s02_Materials      = "XLB";
input s03_Industrials    = "XLI";
input s04_descretionary  = "XLY";
input s05_Staples        = "XLP";
input s06_Health_Care    = "XLV";
input s07_Financials     = "XLF";
input s08_Technology     = "XLK";
input s09_services       = "XLC";
input s10_Utilities      = "XLU";
input s11_Real_Estate    = "XLRE";

# -----------------------------------
# get prices for sector symbols, using chart timeframe
def sec01 = close(s01_Energy); # XLE
def sec02 = close(s02_Materials); # XLB
def sec03 = close(s03_Industrials); # XLI
def sec04 = close(s04_descretionary); # XLY
def sec05 = close(s05_Staples); # XLP
def sec06 = close(s06_Health_Care); # XLV
def sec07 = close(s07_Financials); # XLF
def sec08 = close(s08_Technology); # XLK
def sec09 = close(s09_services); # XLC
def sec10 = close(s10_Utilities); # XLU
def sec11 = close(s11_Real_Estate); # XLRE

#-----------------------

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

#----------------------

# find the close of the last bar of the day
def s1lastclose = if lastbar then sec01 else s1lastclose[1];
def s2lastclose = if lastbar then sec02 else s2lastclose[1];
def s3lastclose = if lastbar then sec03 else s3lastclose[1];
def s4lastclose = if lastbar then sec04 else s4lastclose[1];
def s5lastclose = if lastbar then sec05 else s5lastclose[1];
def s6lastclose = if lastbar then sec06 else s6lastclose[1];
def s7lastclose = if lastbar then sec07 else s7lastclose[1];
def s8lastclose = if lastbar then sec08 else s8lastclose[1];
def s9lastclose = if lastbar then sec09 else s9lastclose[1];
def s10lastclose = if lastbar then sec10 else s10lastclose[1];
def s11lastclose = if lastbar then sec11 else s11lastclose[1];

#------------------------

# calc the diff, from current bar , from the last bar yesterday
def s1diff = sec01 - (if lastbar then s1lastclose[1] else s1lastclose);
def s2diff = sec02 - (if lastbar then s2lastclose[1] else s2lastclose);
def s3diff = sec03 - (if lastbar then s3lastclose[1] else s3lastclose);
def s4diff = sec04 - (if lastbar then s4lastclose[1] else s4lastclose);
def s5diff = sec05 - (if lastbar then s5lastclose[1] else s5lastclose);
def s6diff = sec06 - (if lastbar then s6lastclose[1] else s6lastclose);
def s7diff = sec07 - (if lastbar then s7lastclose[1] else s7lastclose);
def s8diff = sec08 - (if lastbar then s8lastclose[1] else s8lastclose);
def s9diff = sec09 - (if lastbar then s9lastclose[1] else s9lastclose);
def s10diff = sec10 - (if lastbar then s10lastclose[1] else s10lastclose);
def s11diff = sec11 - (if lastbar then s11lastclose[1] else s11lastclose);

input show_lines = yes;
plot zs1 = if show_lines then s1diff else na;
plot zs2 = if show_lines then s2diff else na;
plot zs3 = if show_lines then s3diff else na;
plot zs4 = if show_lines then s4diff else na;
plot zs5 = if show_lines then s5diff else na;
plot zs6 = if show_lines then s6diff else na;
plot zs7 = if show_lines then s7diff else na;
plot zs8 = if show_lines then s8diff else na;
plot zs9 = if show_lines then s9diff else na;
plot zs10 = if show_lines then s10diff else na;
plot zs11 = if show_lines then s11diff else na;

zs1.hidebubble();
zs2.hidebubble();
zs3.hidebubble();
zs4.hidebubble();
zs5.hidebubble();
zs6.hidebubble();
zs7.hidebubble();
zs8.hidebubble();
zs9.hidebubble();
zs10.hidebubble();
zs11.hidebubble();

# plot a 0 line, 3 bars after the last bar
plot z = if !show_lines then na else if !isnan(close[3]) then 0 else na;
z.setdefaultcolor(color.light_gray);

input labels_name = yes;
# values , with line colors
input labels_values = yes;
# values , color from + or -
input labels_values_updown_colors = yes;

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s1diff, (if s1diff > 0 then color.green else if s1diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s2diff, (if s2diff > 0 then color.green else if s2diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s3diff, (if s3diff > 0 then color.green else if s3diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s4diff, (if s4diff > 0 then color.green else if s4diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s5diff, (if s5diff > 0 then color.green else if s5diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s6diff, (if s6diff > 0 then color.green else if s6diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s7diff, (if s7diff > 0 then color.green else if s7diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s8diff, (if s8diff > 0 then color.green else if s8diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s9diff, (if s9diff > 0 then color.green else if s9diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s10diff, (if s10diff > 0 then color.green else if s10diff < 0 then color.red else color.gray));

addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);
addlabel(labels_values_updown_colors, s11diff, (if s11diff > 0 then color.green else if s11diff < 0 then color.red else color.gray));
addlabel(labels_name or labels_values or labels_values_updown_colors, " ", color.black);

#--------------------------
#, (if lastbar then high*1.01 else low*0.99),
#lastclose + " last bar\n" +
#"\n" +
#close_agg + " day close\n" +
#prevdayclose + " prev day close\n"
#, (if lastbar then Color.yellow else color.gray)
#, (if lastbar then yes else no));

#AddChartBubble( 0 and lastBar, close,
#"Last Bar",
#Color.GREEN, no);
#``````

plot of the difference of,
(current bar of an ETF sector) - (close from the last bar yesterday)
this is a 15 min chart

Last edited:

#### halcyonguy

##### Well-known member
VIP
added a 4th label,
with ETF name and colored based of price change + or -

Ruby:
``````# prevdaylastclose_00e_sectors

# sector diff, prev day to current

# REF - #this will find prices from the last bar of the day

declare lower;

def na = Double.NaN;
def bn = BarNumber();
#---------------------
# i left this in for future experimenting
#def agg = aggregationPeriod.day;
#def close_agg = close(period = agg);
#def prevdayclose = close_agg[1];
#----------------------

# sector symbols
input s01_Energy         = "XLE";
input s02_Materials      = "XLB";
input s03_Industrials    = "XLI";
input s04_descretionary  = "XLY";
input s05_Staples        = "XLP";
input s06_Health_Care    = "XLV";
input s07_Financials     = "XLF";
input s08_Technology     = "XLK";
input s09_services       = "XLC";
input s10_Utilities      = "XLU";
input s11_Real_Estate    = "XLRE";

# -----------------------------------
# get prices for sector symbols, using chart timeframe
def sec01 = close(s01_Energy); # XLE
def sec02 = close(s02_Materials); # XLB
def sec03 = close(s03_Industrials); # XLI
def sec04 = close(s04_descretionary); # XLY
def sec05 = close(s05_Staples); # XLP
def sec06 = close(s06_Health_Care); # XLV
def sec07 = close(s07_Financials); # XLF
def sec08 = close(s08_Technology); # XLK
def sec09 = close(s09_services); # XLC
def sec10 = close(s10_Utilities); # XLU
def sec11 = close(s11_Real_Estate); # XLRE

#-----------------------

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def afterEnd = GetTime() > RegularTradingEnd(GetYYYYMMDD());

def firstBar = if (beforeStart[1] == 1 and beforeStart == 0) or (isRollover and beforeStart == 0) then 1 else 0;
def lastBar = if (afterEnd[-1] == 1 and afterEnd == 0) or (isRollover[-1] and firstBar[-1]) then 1 else 0;

#----------------------

# find the close of the last bar of the day
def s1lastclose = if lastBar then sec01 else s1lastclose[1];
def s2lastclose = if lastBar then sec02 else s2lastclose[1];
def s3lastclose = if lastBar then sec03 else s3lastclose[1];
def s4lastclose = if lastBar then sec04 else s4lastclose[1];
def s5lastclose = if lastBar then sec05 else s5lastclose[1];
def s6lastclose = if lastBar then sec06 else s6lastclose[1];
def s7lastclose = if lastBar then sec07 else s7lastclose[1];
def s8lastclose = if lastBar then sec08 else s8lastclose[1];
def s9lastclose = if lastBar then sec09 else s9lastclose[1];
def s10lastclose = if lastBar then sec10 else s10lastclose[1];
def s11lastclose = if lastBar then sec11 else s11lastclose[1];

#------------------------

# calc the diff, from current bar , from the last bar yesterday
def s1diff = sec01 - (if lastBar then s1lastclose[1] else s1lastclose);
def s2diff = sec02 - (if lastBar then s2lastclose[1] else s2lastclose);
def s3diff = sec03 - (if lastBar then s3lastclose[1] else s3lastclose);
def s4diff = sec04 - (if lastBar then s4lastclose[1] else s4lastclose);
def s5diff = sec05 - (if lastBar then s5lastclose[1] else s5lastclose);
def s6diff = sec06 - (if lastBar then s6lastclose[1] else s6lastclose);
def s7diff = sec07 - (if lastBar then s7lastclose[1] else s7lastclose);
def s8diff = sec08 - (if lastBar then s8lastclose[1] else s8lastclose);
def s9diff = sec09 - (if lastBar then s9lastclose[1] else s9lastclose);
def s10diff = sec10 - (if lastBar then s10lastclose[1] else s10lastclose);
def s11diff = sec11 - (if lastBar then s11lastclose[1] else s11lastclose);

input show_lines = yes;
plot zs1 = if show_lines then s1diff else na;
plot zs2 = if show_lines then s2diff else na;
plot zs3 = if show_lines then s3diff else na;
plot zs4 = if show_lines then s4diff else na;
plot zs5 = if show_lines then s5diff else na;
plot zs6 = if show_lines then s6diff else na;
plot zs7 = if show_lines then s7diff else na;
plot zs8 = if show_lines then s8diff else na;
plot zs9 = if show_lines then s9diff else na;
plot zs10 = if show_lines then s10diff else na;
plot zs11 = if show_lines then s11diff else na;

zs1.HideBubble();
zs2.HideBubble();
zs3.HideBubble();
zs4.HideBubble();
zs5.HideBubble();
zs6.HideBubble();
zs7.HideBubble();
zs8.HideBubble();
zs9.HideBubble();
zs10.HideBubble();
zs11.HideBubble();

# plot a 0 line, 3 bars after the last bar
plot z = if !show_lines then na else if !IsNaN(close[3]) then 0 else na;
z.SetDefaultColor(Color.LIGHT_GRAY);

input labels_name = yes;
# values , with line colors
input labels_values = yes;
# values , color from + or -
input labels_values_updown_colors = yes;
input labels_names_updown_colors = yes;

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s1diff, (if s1diff > 0 then Color.GREEN else if s1diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s01_Energy, (if s1diff > 0 then Color.GREEN else if s1diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s2diff, (if s2diff > 0 then Color.GREEN else if s2diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s02_Materials, (if s2diff > 0 then Color.GREEN else if s2diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s3diff, (if s3diff > 0 then Color.GREEN else if s3diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s03_Industrials, (if s3diff > 0 then Color.GREEN else if s3diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s4diff, (if s4diff > 0 then Color.GREEN else if s4diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s04_descretionary, (if s4diff > 0 then Color.GREEN else if s4diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s5diff, (if s5diff > 0 then Color.GREEN else if s5diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s05_Staples, (if s5diff > 0 then Color.GREEN else if s5diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s6diff, (if s6diff > 0 then Color.GREEN else if s6diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s06_Health_Care, (if s6diff > 0 then Color.GREEN else if s6diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s7diff, (if s7diff > 0 then Color.GREEN else if s7diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s07_Financials, (if s7diff > 0 then Color.GREEN else if s7diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s8diff, (if s8diff > 0 then Color.GREEN else if s8diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s08_Technology, (if s8diff > 0 then Color.GREEN else if s8diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s9diff, (if s9diff > 0 then Color.GREEN else if s9diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s09_services, (if s9diff > 0 then Color.GREEN else if s9diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s10diff, (if s10diff > 0 then Color.GREEN else if s10diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_names_updown_colors, s10_Utilities, (if s10diff > 0 then Color.GREEN else if s10diff < 0 then Color.RED else Color.GRAY));

AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_values_updown_colors, s11diff, (if s11diff > 0 then Color.GREEN else if s11diff < 0 then Color.RED else Color.GRAY));
AddLabel(labels_name or labels_values or labels_values_updown_colors, " ", Color.BLACK);
AddLabel(labels_names_updown_colors, s11_Real_Estate, (if s11diff > 0 then Color.GREEN else if s11diff < 0 then Color.RED else Color.GRAY));

#--------------------------
#, (if lastbar then high*1.01 else low*0.99),
#lastclose + " last bar\n" +
#"\n" +
#close_agg + " day close\n" +
#prevdayclose + " prev day close\n"
#, (if lastbar then Color.yellow else color.gray)
#, (if lastbar then yes else no));

#AddChartBubble( 0 and lastBar, close,
#"Last Bar",
#Color.GREEN, no);
#``````

## The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
• Exclusive indicators
• Proven strategies & setups
• Private Discord community
• 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?