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"));

Slippage

Active member
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

scott69

Member
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:

Slippage

Active member
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.
 

scott69

Member
@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:

C4men

Member
@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?
 

Slippage

Active member
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.
 

XeoNoX

Well-known member
VIP
ThinkorSwim's data is sometimes slightly off in those terms when comparing intra data with total daily data, this includes both price and volume.
 

mrsaver11

New member
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
 

XeoNoX

Well-known member
VIP
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
 

mrsaver11

New member
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
 

om4

New member
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?
 

Pensar

Active member
VIP
@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

New member
@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
 

Pensar

Active member
VIP
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.
 

Similar threads

Top