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"));
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"));
 
Last edited:
Solution
Price "change throughout the day is calculated as CURRENT PRICE OR LAST PRICE minus PREVIOUS CLOSE, except for futures. The above coded formula only works if you want to know what the change has been since the open, which is OK, but not used in most cases

So, the bigger problem is that because you use /ES in your example, then neither your open price nor the PREV CLOSE is used for this calculation. The previous day "settlement" price is used. If you look at your TOS platform, the change is not the "current price" minus the previous day 4:00PM CT close price nor the 5:00PM CT open price, it is the current price (last price) minus the settlement price (as calculated by the CME).
 
Last edited:
Regarding open vs close in the percent calculation, fixed. Just a typo while creating that quickly. Thanks for catching it.

Regarding price type, I believe the close() function returns LAST by default though the docs don't state the default. The docs do state the others (ASK, BID, MARK) "are only supported on intraday charts with time interval not greater than 15 days" and this code works on daily charts. https://tlc.thinkorswim.com/center/reference/thinkScript/Functions/Fundamentals/close Or is your concern that it's referencing the daily timeframe to get the close? For most symbols this is what I expect most people would want once the session ends rather than getting after hours data for stocks and seeing different prices/percents depending on whether or not your chart has extended hours enabled. It was my intention to make this work with any symbol. This also avoids issues with getting data from the next session.

Regarding settlement price, there is no such price type available to ThinkScript. There are many things in TOS not available to our scripts. https://tlc.thinkorswim.com/center/reference/thinkScript/Constants/PriceType. Again, it was my intention to make this work with any symbol anyway. I expect the daily close should return whatever the current/last official price is/was for the calendar day regardless of various session opening and closing times.

Lastly, it's free and open source code. If you want something different you're free to modify it for your own purposes and if the requestor wants something different he's/she's free to ask for it. I don't trade futures and I'm not particularly interested in the specifics.
 
@Slippage I only commented because you used a futures contract in your example. And, the open - close difference is not the correct price move. You need to use the last price (close) and the previous close for calculations: thus C - C[1]. That will take opening gaps into consideration. Your method does not. If a stock previously closed at 100 and opens at 120, your method would show the 1st price quote at zero change when in fact the price is up 20 at open. If this doesn't interest you, I apologize. I won't comment or read on any further.
 
Last edited:
@Slippage I only commented because you used a futures contract in your example. And, the open - close difference is not the correct price move. You need to use the last price (close) and the previous close for calculations: thus C - C[1]. That will take opening gaps into consideration. Your method does not. If a stock previously closed at 100 and opens at 120, your method would show the 1st price quote at zero change when in fact the price is up 20 at open. If this doesn't interest you, I apologize. I won't comment or read on any further.

Thank you both for the quick reply!

I suppose SPY would would just as well for these purposes. When I replace "/ES" with "SPY", the price is correct but the 'change' seems to be off (-7.76 vs -5.56).

Any thoughts?
 
On daily chart...
Looking at TOS's chart header I see the following for SPY
O: 375.31 C: 370.07 for a change of -5.24
The data TOS gives ThinkScript for open and close are
O: 375.63 C: 370.07 for a change of -5.56
Note that there are no other price types to request on a daily chart.
It returns the same values whether or not I include period = AggregationPeriod.DAY.

On 1m and 15m first candle open and last candle close, with or without ext hours...
Looking at TOS's chart header I see the following for SPY
O: 375.61 C: 370.19 for a change of -5.24
The data TOS gives ThinkScript for open and close are
O: 375.63 C: 370.07 for a change of -5.56

Note that even in TOS's UI the opening price for the day is different between the daily chart and the intraday chart. I noticed this same problem some time ago when I was calculating gap sizes. There's clearly an issue with TOS's data. It gives us one thing on daily, another intraday and a third thing to ThinkScript. There's nothing we can do in code to fix that.
 
ThinkorSwim's data is sometimes slightly off in those terms when comparing intra data with total daily data, this includes both price and volume.
 
How to add price labels like the one in stockchar
ts.
like when you check the Full qoute and price labels it will show the high and low in a given period. I dont know how to show the creen shot but going to stockcharts.com and clicking the price labels will show high/low thank you
 
How to add price labels like the one in stockchar
ts.
like when you check the Full qoute and price labels it will show the high and low in a given period. I dont know how to show the creen shot but going to stockcharts.com and clicking the price labels will show high/low thank you
you can add the data box
 
I am more on visual, i like to use it when i go thru my watchlist quick, i can see if it bottom down and possible started to reverse and use that price label as my stop lost. As my understanding on stockcharts that the last price labels signal either possible reversals
 
I want to see the high and low price for the last closed candlestick along with the difference on the top right size of the chart. can anyone tell me how to create a script for it?
 
@om4 The easiest way to do that is with labels. There is a catch, though, as ThinkScript only plots labels at the upper left corner of the chart. The way to move them to the upper right corner is to create empty "dummy" labels and string them across the top of your chart, pushing the actual labels over to the right. Here are the values you are looking for, as well as a empty label that you can copy and paste to shift the desired labels across. Just delete the dummy label if you prefer to not use that method.

Code:
# dummy label
AddLabel(1,"                    ",color.black);

AddLabel(1,"High: " + High[1],color.green);
AddLabel(1,"Low: " + Low[1], color.red);
AddLabel(1,"High - Low: " + (High[1] - Low[1]),color.white);
 
Last edited:
@om4 The easiest way to do that is with labels. There is a catch, though, as ThinkScript only plots labels at the upper left corner of the chart. The way to move them to the upper right corner is to create empty "dummy" labels and string them across the top of your chart, pushing the actual labels over to the right. Here are the values you are looking for, as well as a empty label that you can copy and paste to shift the desired labels across. Just delete the dummy label if you prefer to not use that method.

Code:
# dummy label
AddLabel(1,"                    ",color.black);

AddLabel(1,"High: " + High,color.green);
AddLabel(1,"Low: " + Low, color.red);
AddLabel(1,"HL2: " + hl2,color.white);
its giving the current bar price, can we make it the previous closed bar. also i want the difference in terms of high - low = ....as example high is 1 and low is .50 = should display = .50
 
its giving the current bar price, can we make it the previous closed bar. also i want the difference in terms of high - low = ....as example high is 1 and low is .50 = should display = .50
@om4 Sure! The code above is updated, guess I did'nt pay close enough attention to what you asked for earlier.
 
Hoping to get labels depending on study value of previous candle/bar , that will show green or red.

I will call current candle #0
first closed candle #1 Label green if study value at #1 close was > #2 at close... red if <
second closed candle #2 Label green if study value at #2 close was > #3 at close... red if <
third closed candle #3 Label green if study value at #3 close was > #4 at close... red if <

going to use on cci average using cci avg length =9

do not need labels for cci value ONLY CCI AVG

Code:
AddLabel(3,  "           ",color.black);
AddLabel(2,  "           ",color.black);
AddLabel(1,  "           ",color.black);

PLOT CCIAverage;

def VAL1 = CCIAverage[1];
def VAL2  = CCIAverage[2];
def VAL3   = CCIAverage[3];
def VAL4   = CCIAverage[4];

AddLabel (3,"BAR 3: " +  VAL4 < VAL3, color.green else color.red);
AddLabel(2,"BAR 2 : " + val3 < val2, color.green else color.red);
AddLabel(1,"BAR 1: " + val2 < val1 ,color.green else color.red);

This is what I tried, but as you know does not work!
Thank you so much!
 
Last edited:
I have been using this Label posted by Slippage above for a label. 2 questions.

1. Does anyone know how to make the label switch to the current symbol, when the symbol is added from a watchlist, rather than changing the symbol via the the Label study input?
2. Does anyone know how to also make the label change colors in relation to positive or negative for the day? I know how to do this but I can't seem to incorporate it into the existing code.

Thanks!!!
 
I have been using this Label posted by Slippage above for a label. 2 questions.

1. Does anyone know how to make the label switch to the current symbol, when the symbol is added from a watchlist, rather than changing the symbol via the the Label study input?
2. Does anyone know how to also make the label change colors in relation to positive or negative for the day? I know how to do this but I can't seem to incorporate it into the existing code.

Thanks!!!

This will allow you choose to either display the symbol from a watchlist or manually entered. The label will change colors between green/red for UP/DN

Ruby:
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 open(symbol, period = AggregationPeriod.DAY)
        else open(period = AggregationPeriod.DAY);
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"));
 

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