Help Recreating My Label Scripts

Mike

Member
VIP
I Lost my label scripts

1) AddLabel(yes, "MA20: " + Average(close, 20), if close > Average(close, 20) then Color.DARK_GREEN else Color.DARK_RED);
Question: How do I Round the label to 2 decimal places?

2) I'd like to recreate a label that displays the percent the close is above or below MA20. If the percent change is positive, I'd like to color the label DARK_GREEN. If the percent change is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

3) I'd like to recreate a label that displays the percent change in the close from the last trading day of 2023 to the current close. The Label would look like this: Label "YoY%: 4.94%". If the percent change is positive, I'd like to color the label DARK_GREEN. If the percent change is negative, I'd like the color the label DARK_RED. I'd like to Round the label to 2 decimal places.

4) I'd like to recreate a label that displays the VIX close on an Intraday Chart. If the close from the previous bar is positive, I'd like to color the label DARK_GREEN. If the close from the previous bar is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

5) I'd like to recreate a label that displays the VIX close on a Daily Chart. If the close from the previous bar is positive, I'd like to color the label DARK_GREEN. If the close from the previous bar is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

Again, help would be greatly appreciated.


UPDATE:
I have read many label scripts...plus more...this weekend, and came up with and/or wrote the following:

Label_MA200:

AddLabel(yes, "MA200: " + Average(close, 200) , if close > Average(close, 200) then Color.DARK_GREEN else Color.DARK_RED);

Still unable to round 2 decimal places successfully.


Label_MA200_Pct_PAB (Percent Price is Above/Below Moving Average)

def value = (Average(close, 200) - close) / Average(close, 200) * -100;
def showlabel = value;

AddLabel(showLabel,"% A/B MA: " + Round(value,2),
if value >0 then Color.DARK_GREEN else
if value <0 then Color.DARK_RED else Color.WHITE);

The Label Values are correct. The color callouts are unconventional but work. I tried to rewrite the script (a number of times) conventionally but wasn't successful.


Label_PctYoY (Percent Price Change YoY)

#SPX 2024 Close: 4769.83

def value = (close - 4769.83) / (4769.83) * 100;
def showlabel = value;

AddLabel(showLabel,"SPX YoY % G/L: " + Round(value,2),
if value >0 then Color.DARK_GREEN else
if value <0 then Color.DARK_RED else Color.WHITE);


The Label Values are correct. The color callouts are unconventional but work. I tried to rewrite the script (a number of times) conventionally but wasn't successful. It would be more beneficial if the TOS manual would provide simpler examples in MHO.

Would greatly appreciate your inputs/help!

Mike
 
Last edited by a moderator:
Solution
I Lost my label scripts

1) AddLabel(yes, "MA20: " + Average(close, 20), if close > Average(close, 20) then Color.DARK_GREEN else Color.DARK_RED);
Question: How do I Round the label to 2 decimal places?

2) I'd like to recreate a label that displays the percent the close is above or below MA20. If the percent change is positive, I'd like to color the label DARK_GREEN. If the percent change is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

3) I'd like to recreate a label that displays the percent change in the close from the last trading day of 2023 to the current close. The Label would look like this: Label "YoY%: 4.94%". If the percent change is positive, I'd like to color the...
I Lost my label scripts

1) AddLabel(yes, "MA20: " + Average(close, 20), if close > Average(close, 20) then Color.DARK_GREEN else Color.DARK_RED);
Question: How do I Round the label to 2 decimal places?

2) I'd like to recreate a label that displays the percent the close is above or below MA20. If the percent change is positive, I'd like to color the label DARK_GREEN. If the percent change is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

3) I'd like to recreate a label that displays the percent change in the close from the last trading day of 2023 to the current close. The Label would look like this: Label "YoY%: 4.94%". If the percent change is positive, I'd like to color the label DARK_GREEN. If the percent change is negative, I'd like the color the label DARK_RED. I'd like to Round the label to 2 decimal places.

4) I'd like to recreate a label that displays the VIX close on an Intraday Chart. If the close from the previous bar is positive, I'd like to color the label DARK_GREEN. If the close from the previous bar is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

5) I'd like to recreate a label that displays the VIX close on a Daily Chart. If the close from the previous bar is positive, I'd like to color the label DARK_GREEN. If the close from the previous bar is negative, I'd like to color the label DARK_RED. I'd like to Round the label to 2 decimal places.

Again, help would be greatly appreciated.


UPDATE:
I have read many label scripts...plus more...this weekend, and came up with and/or wrote the following:

Label_MA200:

AddLabel(yes, "MA200: " + Average(close, 200) , if close > Average(close, 200) then Color.DARK_GREEN else Color.DARK_RED);

Still unable to round 2 decimal places successfully.


Label_MA200_Pct_PAB (Percent Price is Above/Below Moving Average)

def value = (Average(close, 200) - close) / Average(close, 200) * -100;
def showlabel = value;

AddLabel(showLabel,"% A/B MA: " + Round(value,2),
if value >0 then Color.DARK_GREEN else
if value <0 then Color.DARK_RED else Color.WHITE);

The Label Values are correct. The color callouts are unconventional but work. I tried to rewrite the script (a number of times) conventionally but wasn't successful.


Label_PctYoY (Percent Price Change YoY)

#SPX 2024 Close: 4769.83

def value = (close - 4769.83) / (4769.83) * 100;
def showlabel = value;

AddLabel(showLabel,"SPX YoY % G/L: " + Round(value,2),
if value >0 then Color.DARK_GREEN else
if value <0 then Color.DARK_RED else Color.WHITE);


The Label Values are correct. The color callouts are unconventional but work. I tried to rewrite the script (a number of times) conventionally but wasn't successful. It would be more beneficial if the TOS manual would provide simpler examples in MHO.

Would greatly appreciate your inputs/help!

Mike

you listed the rules well, making it easy to figure out what you wanted.
you codes at the end didn't match your words, so i ignored most of it.
i did add in a color of white if a value is 0.

for rule 3, if there isn't data from last year, a different label is shown, that says 'no data'
i added cyan labels as separators between all the labels


Code:
#labels_recreate

#https://usethinkscript.com/threads/help-recreating-my-label-scripts.18889/
#Help Recreating My Label Scripts


# use round() to limit decimal places
# create variables for possible values, calc something once, then reference the variable.


def na = double.nan;
def bn = barnumber();

addlabel(1, " - ", color.cyan);
#-------------------------------------
# 1)
# how do I Round the label to 2 decimal places?
#
def data = close;
#input avg1_type = AverageType.exponential;
input avg1_type = AverageType.Simple;
input avg1_length = 20;
def avg1 = round(MovingAverage(avg1_type, data, avg1_length ),2);
#
input show_avg1 = no;
plot zavg1 = if show_avg1 then avg1 else na;
#
AddLabel(yes, "MA" + avg1_length + ": " + avg1, (if close > avg1 then Color.DARK_GREEN else if close < avg1 then Color.DARK_RED else Color.WHITE));
addlabel(1, " - ", color.cyan);



#-------------------------------------
# 2)
# a label that displays the percent the close is above or below MA20.
#  If the percent change is positive, I'd like to color the label DARK_GREEN.
#  If the percent change is negative, I'd like to color the label DARK_RED.
# Round the label to 2 decimal places.

def diff1 = close - avg1;
def diff1per = round(100 * diff1/avg1, 2);
#
AddLabel(yes, "MA diff %" + diff1per, (if diff1per > 0 then Color.DARK_GREEN else if diff1per < 0 then Color.DARK_RED else Color.WHITE));
addlabel(1, " - ", color.cyan);



#-------------------------------------
# 3)
# a label that displays the percent change in the close from the last trading day of 2023 to the current close.
# The Label would look like this:
#  Label "YoY%: 4.94%".
#   If the percent change is positive, DARK_GREEN.
#   If the percent change is negative, DARK_RED.
#  Round the label to 2 decimal places.

def yr = getyear();
def prevyrlast = yr != yr[-1];
def prevyrlastcls = if bn == 1 or isnan(close[1]) then na
  else if prevyrlast then close
  else prevyrlastcls[1];
def isprev = !isnan(prevyrlastcls);

def diff2 = close - prevyrlastcls;
def diff2per = round(100 * diff2/prevyrlastcls, 2);
#
addLabel(isprev, "YoY%: " + diff2per, (if diff2per > 0 then Color.DARK_GREEN else if diff2per < 0 then Color.DARK_RED else Color.WHITE));
addLabel(!isprev, "    no data from last year    ", color.cyan);
#
input show_last_year_line = yes;
plot z = if show_last_year_line then prevyrlastcls else na;
addlabel(1, " - ", color.cyan);
#addlabel(1, yr, color.yellow);



#-------------------------------------
# 4)
# a label that displays the VIX close on an Intraday Chart.
#  If the close from the previous bar is positive, DARK_GREEN.
#  If the close from the previous bar is negative, DARK_RED.
# Round the label to 2 decimal places.
#
input sym = "VIX";
def v = round(close(sym), 2);
#
addLabel(1, "VIX: " + v, (if v > 0 then Color.DARK_GREEN else if v < 0 then Color.DARK_RED else Color.WHITE));
#
addlabel(1, " - ", color.cyan);



#-------------------------------------
# 5)
# a label that displays the VIX close on a Daily Chart.
#  If the close from the previous bar is positive, DARK_GREEN.
#  If the close from the previous bar is negative, DARK_RED.
# Round the label to 2 decimal places.
#
#      use  #4 code on a daily chart
#
addlabel(1, " - ", color.cyan);
#
 

Attachments

  • Capture.JPG
    Capture.JPG
    9.8 KB · Views: 78
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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