percentage change label

us3r001

New member
Hello, the script is fine in regular trading hours.
How can I tweak to let it work outside RTH ? Thanks!


input period_Type = AggregationPeriod.DAY;

def begin = close(period = period_Type)[1];
def end = close(period = period_Type);
def NetChg = end - begin;
def PctChg = (end / begin) - 1;

AddLabel(yes, "" + (NetChg) + " " + AsPercent(PctChg), if NetChg > 0 then CreateColor(100,200,100) else if NetChg < 0 then CreateColor(255,125,125) else color.LIGHT_GRAY);
 
Hello, the script is fine in regular trading hours.
How can I tweak to let it work outside RTH ? Thanks!


input period_Type = AggregationPeriod.DAY;

def begin = close(period = period_Type)[1];
def end = close(period = period_Type);
def NetChg = end - begin;
def PctChg = (end / begin) - 1;

AddLabel(yes, "" + (NetChg) + " " + AsPercent(PctChg), if NetChg > 0 then CreateColor(100,200,100) else if NetChg < 0 then CreateColor(255,125,125) else color.LIGHT_GRAY);

See if this what you want by substituting 'def end = close' for end instead of 'def end = close(period = period_Type)'. Otherwise, please describe the period that defines 'outside RTH.'

Capture.jpg

Code:
input period_Type = AggregationPeriod.DAY;

def begin = close(period = period_Type)[1];
def end = close(period = period_Type);
def NetChg = end - begin;
def PctChg = (end / begin) - 1;

AddLabel(yes, "" + (NetChg) + " " + AsPercent(PctChg), if NetChg > 0 then CreateColor(100,200,100) else if NetChg < 0 then CreateColor(255,125,125) else color.LIGHT_GRAY);

def end1    = close;
def NetChg1 = end1 - begin;
def PctChg1 = (end1 / begin) - 1;

AddLabel(yes, "" + (NetChg1) + " " + AsPercent(PctChg1), if NetChg1 > 0 then CreateColor(100,200,100) else if NetChg1 < 0 then CreateColor(255,125,125) else color.LIGHT_GRAY);

input debug = yes;
addlabel(debug, "begin: " + begin + " end: " + end + " end1: " + close, color.white);
 
What I'd like is current price (after hour + premarket + regular trading hours)
CHG relative to previous day close.
So: always label visible.

Let me check better because weirdly on the weekend I can see label.
I expected black label as it was during the week outside RTH.
I'll write again eventually. Thanks.
Maybe if you have quick time you could check the script to find when it's expected to display black label.
I didn't write that script, I just tweaked colours.
 
What I'd like is current price (after hour + premarket + regular trading hours)
CHG relative to previous day close.
So: always label visible.

Let me check better because weirdly on the weekend I can see label.
I expected black label as it was during the week outside RTH.
I'll write again eventually. Thanks.
Maybe if you have quick time you could check the script to find when it's expected to display black label.
I didn't write that script, I just tweaked colours.

See if this is more of what you were looking to get

Capture.jpg

Ruby:
input showPriorCloseBasis = yes;
input marketopen     = 0930;
input marketclose    = 1600;
input dayend         = 2359;#end of day
input dayopen        = 0000;#beginning of new day
def c                = close;
def closepd          = close(period = AggregationPeriod.DAY)[1];
def closetd          = close(period = aggregationPeriod.DAY);
def closeam          = if SecondsFromTime(marketclose) > 0 and
                          SecondsTillTime(dayend) > 0
                       then c
                       else closeam[1];
def closepm          = if SecondsFromTime(dayopen) > 0 and
                          SecondsTillTime(marketopen) > 0
                       then c
                       else closepm[1];
def closerth         = if SecondsFromTime(marketopen) >= 0 and
                          SecondsTillTime(marketclose) >= 0
                       then c
                       else closerth[1];

AddLabel(1, (if !showPriorCloseBasis
            then ""
            else "Prior " +
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
            then closetd
            else closepd)) +
            " After:  " + closeam + " Chg " + (closeam -
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
             then closetd
             else closepd)) +
            " " + (100 * (closeam /
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
             then closetd
             else closepd) - 1)) +
            "%", if (closeam -
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
             then closetd
             else closepd)) < 0
             then Color.PINK
             else Color.LIGHT_GREEN);

AddLabel(1, " ", Color.BLACK); #Spacer

AddLabel(1, if SecondsFromTime(marketclose) > 0
            then "PreMarket Closed  "
            else (if !showPriorCloseBasis
                  then ""
                  else "Prior " + closepd) + " Pre:  " + closepm +
                       " Chg " + astext(closepm - closepd) +
                       " " + (100 * (closepm / closepd - 1)) + "%",
           if SecondsFromTime(marketclose) > 0
           then Color.WHITE
           else if (closepm - closepd) < 0
           then Color.PINK
           else Color.LIGHT_GREEN);

AddLabel(1, " ", Color.BLACK); #Spacer

AddLabel(1,  if !(SecondsFromTime(marketopen) >= 0 and
                  SecondsTillTime(marketclose) >= 0)
             then "RTH Closed  "
             else if !showPriorCloseBasis
             then ""
             else "Prior " + closepd + " RTH:  " + closerth +
                  " Chg " + astext(closerth - closepd) +
                  " " + (100 * (closerth / closepd - 1)) + "%",
             if !(SecondsFromTime(marketopen) >= 0 and
                  SecondsTillTime(marketclose) >= 0)
             then Color.WHITE
             else if (closerth - closepd) < 0
             then Color.PINK
             else Color.LIGHT_GREEN);
 
Last edited:
Well thanks ! Yours looks like a brand new script.
Labels are visible in pre-market, I was hoping for a tweak of the script I provided
because I very much need small labels , ideally one.
So I guess I'll try to tweak your ideas in the script I provided ... let's see if I can, thanks !
 
Last edited:
Well pre-market I can see label so I'd assume you solved my issue ..and thanks :)

That version used the previous day's close for 'After Market' (AM). If you want 'AM' to be based upon the close 'Today' rather than the 'Prior Day', then this should work. [Edited code 7.22 Code needs fixed]
 
Last edited:
That version used the previous day's close for 'After Market' (AM). If you want 'AM' to be based upon the close 'Today' rather than the 'Prior Day', then this should work. [Edited code 7.22 Code needs fixed]

Here is the revised code. Hopefully, this now captures the correct prior closes to determine the percentage change for Aftermarket, Premarket and Regular Trading Hours. When the market closes, the Aftermarket label will show using the prior day value as that day's RTHours close.. Thereafter, the Premarket will show and use the same prior day close as the Aftemarket label. Finallly, the Regular Trading Hours label will show. When the Aftermarket starts again, the Premarket and RTHours labels will appear 'closed'.

You can input whether you want to display the Prior Day's value basis in the labels.

Ruby:
input showPriorCloseBasis = yes;
input marketopen     = 0930;
input marketclose    = 1600;
input dayend         = 2359;#end of day
input dayopen        = 0000;#beginning of new day
def c                = close;
def closepd          = close(period = AggregationPeriod.DAY)[1];
def closetd          = close(period = aggregationPeriod.DAY);
def closeam          = if SecondsFromTime(marketclose) > 0 and
                          SecondsTillTime(dayend) > 0
                       then c
                       else closeam[1];
def closepm          = if SecondsFromTime(dayopen) > 0 and
                          SecondsTillTime(marketopen) > 0
                       then c
                       else closepm[1];
def closerth         = if SecondsFromTime(marketopen) >= 0 and
                          SecondsTillTime(marketclose) >= 0
                       then c
                       else closerth[1];

AddLabel(1, (if !showPriorCloseBasis
            then ""
            else "Prior " +
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
            then closetd
            else closepd)) +
            " After:  " + closeam + " Chg " + (closeam -
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
             then closetd
             else closepd)) +
            " " + (100 * (closeam /
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
             then closetd
             else closepd) - 1)) +
            "%", if (closeam -
            (if GetYYYYMMDD() and SecondsFromTime(marketclose) > 0 and SecondsTillTime(dayend) > 0
             then closetd
             else closepd)) < 0
             then Color.PINK
             else Color.LIGHT_GREEN);

AddLabel(1, " ", Color.BLACK); #Spacer

AddLabel(1, if SecondsFromTime(marketclose) > 0
            then "PreMarket Closed  "
            else (if !showPriorCloseBasis
                  then ""
                  else "Prior " + closepd) + " Pre:  " + closepm +
                       " Chg " + astext(closepm - closepd) +
                       " " + (100 * (closepm / closepd - 1)) + "%",
           if SecondsFromTime(marketclose) > 0
           then Color.WHITE
           else if (closepm - closepd) < 0
           then Color.PINK
           else Color.LIGHT_GREEN);

AddLabel(1, " ", Color.BLACK); #Spacer

AddLabel(1,  if !(SecondsFromTime(marketopen) >= 0 and
                  SecondsTillTime(marketclose) >= 0)
             then "RTH Closed  "
             else if !showPriorCloseBasis
             then ""
             else "Prior " + closepd + " RTH:  " + closerth +
                  " Chg " + astext(closerth - closepd) +
                  " " + (100 * (closerth / closepd - 1)) + "%",
             if !(SecondsFromTime(marketopen) >= 0 and
                  SecondsTillTime(marketclose) >= 0)
             then Color.WHITE
             else if (closerth - closepd) < 0
             then Color.PINK
             else Color.LIGHT_GREEN);
 
Last edited:

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
332 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