Show SPY current value and gain as labels

stormy77

Member
I know how to add a label for the current ticker on chart, but am having trouble adding a label for a different ticker.
For example, I want to see the following labels :
SPX value %changetoday
$DJI value %changetoday

and so on... can someone please point me in the right direction?
 
Solution
I know how to add a label for the current ticker on chart, but am having trouble adding a label for a different ticker.
For example, I want to see the following labels :
SPX value %changetoday
$DJI value %changetoday

and so on... can someone please point me in the right direction?
Example
Ruby:
def S = close("SPX", AggregationPeriod.DAY);
AddLabel(1, "SPX: " + S + "  " + (S[1] - S) / S[1] * 100 + "%");
I know how to add a label for the current ticker on chart, but am having trouble adding a label for a different ticker.
For example, I want to see the following labels :
SPX value %changetoday
$DJI value %changetoday

and so on... can someone please point me in the right direction?
Example
Ruby:
def S = close("SPX", AggregationPeriod.DAY);
AddLabel(1, "SPX: " + S + "  " + (S[1] - S) / S[1] * 100 + "%");
 
Solution
Thanks! That was very helpful. It allowed me to add others as well
ZAecWPT.png
 
That is pretty awesome! Is there a way to define the previous day's close regardless of what time frame your chart is on? Also it is showing a "-" before the percentage so it looks like the SPY is actually down 1.5% today as opposed to up 1.5%.
 
That is pretty awesome! Is there a way to define the previous day's close regardless of what time frame your chart is on? Also it is showing a "-" before the percentage so it looks like the SPY is actually down 1.5% today as opposed to up 1.5%.
yeah, good catch. I think we just need to reverse S - S[1] values

so instead of (S[1] - S) / S[1] * 100
use: (S - S[1]) / S[1] * 100
 
It's actually showing the "-" when the market is green today because the code needs to be changed to this (EDIT we must have posted at the same time!):

Code:
def S = close("SPX", AggregationPeriod.DAY);
AddLabel(1, "SPX: " + S + "  " + (S - S[1]) / S[1] * 100 + "%");

But how can you format the label so it shows up green when the SPY is green and red when the SPY is red? I thought something like this would work but i'm getting errors (i'm really not good at thinkscript, please don't laugh if this is way off):

Code:
def S = close("SPX", AggregationPeriod.DAY);
AddLabel(1, "SPX " +  S + " " + (S - S[1]) / S[1] * 100 + "%"), if (S - S[1]) / S[1] * 100 >0 then Color.Green else Color.Red);
 
It's actually showing the "-" when the market is green today because the code needs to be changed to this (EDIT we must have posted at the same time!):

Code:
def S = close("SPX", AggregationPeriod.DAY);
AddLabel(1, "SPX: " + S + "  " + (S - S[1]) / S[1] * 100 + "%");

But how can you format the label so it shows up green when the SPY is green and red when the SPY is red? I thought something like this would work but i'm getting errors (i'm really not good at thinkscript, please don't laugh if this is way off):

Code:
def S = close("SPX", AggregationPeriod.DAY);
AddLabel(1, "SPX " +  S + " " + (S - S[1]) / S[1] * 100 + "%"), if (S - S[1]) / S[1] * 100 >0 then Color.Green else Color.Red);
Try removing the ) after the %

SO i defined the percentage as :
def SS = (S - S[1]) / S[1] * 100 ;
and in my label ccode:
AddLabel(1, "S&P: " + S + " " + " " + SS + "%" , IF SS > 0 then Color.GREEN else Color.RED);

Makes it a bit cleaner
 
Last edited:
I think it should work because we are specifying "AggregationPeriod.DAY" in the code itself, so it should not matter what time period you have on the chart. I work on the 3m and daily charts and it seemed to show up fine in both... I may have to double check to be sure, but someone more experienced can respond
 
Here is the full code that will label the SPX percentage change (even on a non-daily chart). The only issue i see is it doesn't seem to work during premarket or on stocks that have had zero volume traded during the day (a stock like BNOX or ELSE). It was mentioned on thinkscript chat that #Prefetch Kludge may be needed for it to display correctly on stocks with very minimal volume. I need to research "Prefetch Kludge" now...

Code:
def S = close("SPX",period = AggregationPeriod.DAY);
def percentchange = round((close("SPX",period = AggregationPeriod.DAY) - close("SPX",period = AggregationPeriod.DAY)[1]) / close("SPX",period = AggregationPeriod.DAY)[1] * 100,2);

AddLabel(1, "SPX " +  S + " " + percentchange + "%", if percentchange >0 then Color.Green else Color.Red);
 

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