Display current stock price as label on chart

C4men

Member
Would there be a way to turn an index into a basic label to place on my chart?

Something like: [ /ES $3690.50, Up $80 (2.5%) ]

Just wondering if possible so it can be a small reference point on my charts.
 
Solution
As requested:
Ruby:
input symbol = "/ES";

def c = close(symbol, period = AggregationPeriod.DAY);
def o = open(symbol, period = AggregationPeriod.DAY);
def diff = c - o;
def pct = diff / o;

DefineGlobalColor("LabelColor", Color.LIGHT_GRAY);

AddLabel(yes, symbol + " " + AsDollars(c) + if diff >= 0 then " Up " else " Down " + AsPrice(AbsValue(diff)) + " (" + AsPercent(pct) + ")", GlobalColor("LabelColor"));

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

Ooops,

This code is comparing the open to close, for change and percentage change. I'm trying to get the calculation from Previous close to current or Last Price. Also when loading a stock it does not calculate the percentage change. I'm setting the chart to not show extended hours. I tried changing code to prev day close, but still not getting it right.

It appears to work if the asset is negative for the period.


Code:
input findsymbol = {default watchlist, manual};
input symbol     = "/ES";

def c = if findsymbol == findsymbol.manual
        then close(symbol, period = AggregationPeriod.DAY)
        else close(period = AggregationPeriod.DAY);
def o = if findsymbol == findsymbol.manual
        then close(symbol, period = AggregationPeriod.DAY)[1]
        else close(period = AggregationPeriod.DAY)[1];
def diff = c - o;
def pct = diff / o;

DefineGlobalColor("UP", Color.LIGHT_GREEN);
DefineGlobalColor("DN", Color.LIGHT_RED);

AddLabel(yes,  (if findsymbol == findsymbol.manual then symbol else GetSymbol()) + "  " + AsDollars(c) + if diff >= 0 then " Up " else " Down " + AsPrice(AbsValue(diff)) + " (" + AsPercent(pct) + ")", if diff >= 0 then GlobalColor("UP") else GlobalColor("DN"));
 
Last edited:
I found this code elsewhere on this site. The calculation is accurate. Might combine this with some of th code from. Slippage.
Code:
#This study plots a chart label for Net Change and Percent Change from prior close (regardless of time period of the chart.

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, "Change:  " + AsDollars(NetChg) + "  " + AsPercent(PctChg), if NetChg > 0 then CreateColor(100,200,100) else if NetChg < 0 then Color.Dark_Orange else color.LIGHT_GRAY);
 
https://tos.mx/H7CebzI

I need to create a new label for this shared chart that will show the percentage gain of each symbol on this comparison chart. There are some labels on the chart already but not what I need.

I need a label that has inputs for symbol and color displaying % gain over the charted period. I will adjust the label color to match the symbols color as designated in the comparison in the comparison study.

The label will show the symbol and % gain. My current label shows % gain for the current day only and changes color according to the days price.

The most efficient way to code would be to add to the comparison study but that is a stock code from TOS.

Any help would be appreciated, I have very little coding experience and ChatGPT was no help for this coding.

Here is the code I use for intraday charts which shows %change today for stocks:
Code:
#HINT: This study plots a chart label for Net Change and Percent Change from prior close (regardless of time period of the chart.

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, "Change:  " + AsDollars(NetChg) + "  " + AsPercent(PctChg), if NetChg > 0 then Color.LIGHT_GREEN else if NetChg < 0 then Color.LIGHT_RED else Color.LIGHT_GRAY);

This code is for day change on an indicator like $ADD:
Code:
input symbol = "$ADD" ;

def C_now = close(symbol);
def c = close(symbol, period = AggregationPeriod.DAY)[1];

def Price = close(symbol);

AddLabel( 1, symbol + " = " + Price, if Price < 1.00 then Color.Light_RED else if Price > 1.00 then Color.Light_GREEN else Color.GRAY );
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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