Useful Intraday Stats for ThinkorSwim

Can someone add the 52 Week high and low stats in a label and percentage that price is away from high and low. Thanks

Here is the chart study code for what you asked:

#52 week high and low and percent differences

def HY = highest(HIGH (period = AggregationPeriod.DAY), 252);
def LY = lowest(LOW (period = AggregationPeriod.DAY), 252);
def H2 = Round(100*(close/HY),1);
def L2 = Round(100*(close/LY),1);

AddLabel(1,"HY: " + HY +"%", createcolor(239,222,205));
Addlabel(1,"LY: " + LY +"%", createcolor(155,166,192));
Addlabel(1,"Hd: " + H2 + "%", createcolor(239,222,205));
Addlabel(1,"Ld: " + L2 + "%", createcolor(155,166,192));
 
Last edited:
@soary

The hours are eastern standard time. Premarket from 4am to 9:30am and Market hours from 9:30am to 4pm.
Here is the code for what you asked:

Code:
#Premarket High

def h = high;
def bar = BarNumber();
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

def ONhigh = if GlobeX and !GlobeX[1] then h else if GlobeX and h > ONhigh[1] then h else ONhigh[1];
def ONhighBar = if GlobeX and h == ONhigh then bar else Double.NaN;
def OverNightHigh = if BarNumber() == HighestAll(ONhighBar) then ONhigh else OverNightHigh[1];
AddLabel(1, "PMH: " + Round(OverNightHigh,2), color.CYAN);


#Current Price
Addlabel (1, "Price: " + close, createcolor(254,216,177));


#52 week high and low and percent differences from yesterday close

def HY = highest(HIGH (period = AggregationPeriod.DAY), 252);
def LY = lowest(LOW (period = AggregationPeriod.DAY), 252);
def YC = close(period = AggregationPeriod.DAY)[1];

def H2 = Round(100*(YC/HY),1);
def L2 = Round(100*(YC/LY),1);

AddLabel(1,"HY: " + HY, createcolor(239,222,205));
Addlabel(1,"LY: " + LY, createcolor(155,166,192));
Addlabel(1,"Hd: " + H2 + "%", createcolor(239,222,205));
Addlabel(1,"Ld: " + L2 + "%", createcolor(155,166,192));
 
Last edited:
Just want to make sure I give you the right info. Do you want VWAP value as a chart label or difference between VWAP and current price as a label? I already submitted a chart study that shows an alert when price crosses above VWAP. I could add code to show when it goes below VWAP as well if you want. Please let me know which of these codes you are looking for and I will try to do it.

Thanks!


no not VWAP value idont know if i am explaining it right but you know when price is above VWAP its seen as bullish and when its below its seen as bearish. I just want the label to say front side or backside depending on where price is if its above or below
 
If want to know the difference between today's volume and yesterday's volume, you could add the following code:

def difvol = today-YV;
Addlabel(1,"Difvol: " + difvol, color.PINK);

Make sure you add the code for yesterday's volume which I gave above before adding this code for difvol.

........................................................................................

@Sonny Thank you for your time and please forgive me for the length of this message. Most of the code works great the problem is when I piece portions from this thread together as posted below. The volume parts that read

= volume (period = AP); def today

def difvol = today-YV;

are red with the error codes that say:

39.29 No such variable AP

42.22 No such variable YV

And red also is the premarket line that says,

Addlabel(pmopen, "PMGD: " + PMGD, color.PINK);

Has error code that say:

64:10 No such variable pmopen

If it's not too much trouble could you add to the volume part the average volume of the last 100 days and the % difference between current volume and volume average of the last 100 days. Then, to the premarket part is where you would piece in the premarket open and difference between yesterdays close and the premarket open, also label for premarket low. Will that current price portion of the label show premarket price bec Tos charts don't show price in premarket? This is going to save me time from looking around for stats and doing calculations.

I read that you modified the labels to disappear but for my trading style I want them to remain visible all day and I couldn’t figure out what to change so the labels remain. Could you fix that please. You said,

“I modified my original code so that the intraday labels will show up only at 9:30am. From 4am to 9:30am, you will get Current price, Percent Change, Yesterday Close, and Premarket High (PMH). The PMH will disappear at 10am. The key to the labels appearing is the first variable in Addlabel. The label will show if it is 1 and not show if it is 0.”

Here’s what I pieced tighter and it includes the errors mentioned above:

Code:
#Intraday Stats

#Sonny @ usethinkscript.com

#modified by Pensar to add user-adjustable symbol input


input symbol = "/ES";

def c = close(symbol, aggregationperiod.DAY);

def o = open(symbol, aggregationperiod.DAY);

def h = high(symbol, aggregationperiod.DAY);

def l = low(symbol, aggregationperiod.DAY);

def v = volume(symbol, aggregationperiod.DAY);


#Symbol Label

AddLabel(1,symbol,color.white);


#Current Price

Addlabel (1, "Price: " + c, color.CYAN);


#Percent Change from yesterday close

def Priorclose = c[1];

def PctChange = (c - Priorclose) / Priorclose;

def PC1 = Round(PctChange,3);

Addlabel(1, "YC Ch: " + AsPercent(PC1), if PctChange > 0 then Createcolor(238,210,238) else Color.LIGHT_RED);


#Prior Close

Addlabel (1, "YC: " + Round(Priorclose,2), color.LIGHT_GREEN);


#Open price

Addlabel (1, "O: " + Round(o,2), createcolor(254,216,177));


#High of Day

def high1 = Highest(h,1);

Addlabel (1, "Hi: " + Round(high1,2), color.CYAN);


#Low of Day

def low1 = Lowest(l,1);

Addlabel (1, "Lo: " + Round(low1,2), createcolor(178,255,102));


#Volume Today

def today = volume(period = AP);

Addlabel(1, "Vol: " + today, createcolor(206,177,128));


def difvol = today-YV;

Addlabel(1,"Difvol: " + difvol, color.PINK);


#Change From Open

def CFO = (c / o)-1;

def CFO1 = Round(CFO,3);

Addlabel(1, "CFO: " + AsPercent(CFO1), if CFO >0 then Createcolor(0,255,128) else createcolor(255,153,153));

#…………………………………………………………………………………………..

#Premarket High

def bar = BarNumber();

def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());


def ONhigh = if GlobeX and !GlobeX[1] then h else if GlobeX and h > ONhigh[1] then h else ONhigh[1];

def ONhighBar = if GlobeX and h == ONhigh then bar else Double.NaN;

def OverNightHigh = if BarNumber() == HighestAll(ONhighBar) then ONhigh else OverNightHigh[1];

AddLabel(1, "PMH: " + Round(OverNightHigh,2), color.CYAN);


#The PM gap down (from PM high) would show up with this code:
def PMGD = close - OverNightHigh;
Addlabel(pmopen, "PMGD: " + PMGD, color.PINK);

#…………………………………………………………………………………………..

#52 week high and low and percent differences from yesterday close

def HY = highest(HIGH (period = AggregationPeriod.DAY), 252);

def LY = lowest(LOW (period = AggregationPeriod.DAY), 252);

def YC = close(period = AggregationPeriod.DAY)[1];


def H2 = Round(100*(YC/HY),1);

def L2 = Round(100*(YC/LY),1);


AddLabel(1,"HY: " + HY, createcolor(239,222,205));

Addlabel(1,"LY: " + LY, createcolor(155,166,192));

Addlabel(1,"Hd: " + H2 + "%", createcolor(239,222,205));

Addlabel(1,"Ld: " + L2 + "%", createcolor(155,166,192));


#end code
 
Last edited:
@soary

I have put the code for the other things you asked about. Keep in mind that if you take out code pieces from the whole codes, it will not work. If you look at page 1 of this thread, I put in time codes so that the intraday stats show up only starting at 9:30am. If you want something to appear for the whole day, you have to put a 1 as the first character in Addlabel parentheses.

Code:
#Premarket Open and difference between yesterday close and premarket open

def newDay = GetDay() <> GetDay()[1];
def start = newDay or SecondsTillTime(400) == 0;
rec premarketOpen = if start then open else premarketOpen[1];
def x = premarketOpen;
Addlabel(1, “PMO: “ + x, createcolor(212,197,178));

def YC = close(period=AggregationPeriod.DAY)[1];
def dif1 = x-YC;
Addlabel(1, “Dif1: “ + dif1, createcolor(197,239,161));

#Premarket High and Low

def h = high;
def l = low;
def bar = BarNumber();
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());
def ONhigh = if GlobeX and !GlobeX[1] then h else if GlobeX and h > ONhigh[1] then h else ONhigh[1];
def ONhighBar = if GlobeX and h == ONhigh then bar else Double.NaN;
def OverNightHigh = if BarNumber() == HighestAll(ONhighBar) then ONhigh else OverNightHigh[1];
AddLabel(pastten, "PMH: " + Round(OverNightHigh,2), createcolor(178,152,122));

def ONlow = if GlobeX and !GlobeX[1] then l else if GlobeX and l < ONlow[1] then l else ONlow[1];
def ONlowBar = if GlobeX and l == ONlow then bar else Double.NaN;
def OverNightLow = if BarNumber() == HighestAll(ONlowBar) then ONlow else OverNightLow[1];
Addlabel(1, “PML: “ + Round(OverNightLow,2), createcolor(225,146,171));
 
@soary

Here is Average Volume code you wanted:

Code:
#Average Volume (100d) and difference between AV and today's volume
def AV = AggregationPeriod.Day;
def x = Average(volume(period=AV)[1],100);
def L = RoundDown(lg(x),0);
def a = Round((x/1000),1);
def b = Round((x/1000000),1);
def y = if L<6 then a else b;
Addlabel(L<6, “AV: “+y+"K", createcolor(227,170,124));
Addlabel(L>5, "AV: "+y+"M", createcolor(227,170,124));

def v = volume(period=AV);
def z = v-x;
def L2 = RoundDown(lg(z),0);
def a1 = Round((z/1000),1);
def b1 = Round((z/1000000),1);
def y1 = if L2<6 then a1 else b1;
Addlabel(L2<6, "Diff: "+y1+"K", createcolor(208,209,238));
Addlabel(L2>5, "Diff: "+y1+"M", createcolor(208,209,238));
 
@convertiblejay

Here is the code for VWAP difference. It shows "AV" for above VWAP and "BV" for below it.

Code:
#VWAP Diff

input numDevDn = -2.0;
input numDevUp = 2.0;
input timeFrame = {default DAY, WEEK, MONTH};

def cap = GetAggregationPeriod();
def errorInAggregation =
    timeFrame == timeFrame.DAY and cap >= AggregationPeriod.WEEK or
    timeFrame == timeFrame.WEEK and cap >= AggregationPeriod.MONTH;
Assert(!errorInAggregation, "timeFrame should be not less than current chart aggregation period");

def yyyyMmDd = GetYYYYMMDD();
def periodIndx;
switch (timeFrame) {
case DAY:
    periodIndx = yyyyMmDd;
case WEEK:
    periodIndx = Floor((DaysFromDate(First(yyyyMmDd)) + GetDayOfWeek(First(yyyyMmDd))) / 7);
case MONTH:
    periodIndx = RoundDown(yyyyMmDd / 100, 0);
}
def isPeriodRolled = CompoundValue(1, periodIndx != periodIndx[1], yes);

def volumeSum;
def volumeVwapSum;
def volumeVwap2Sum;

if (isPeriodRolled) {
    volumeSum = volume;
    volumeVwapSum = volume * vwap;
    volumeVwap2Sum = volume * Sqr(vwap);
} else {
    volumeSum = CompoundValue(1, volumeSum[1] + volume, volume);
    volumeVwapSum = CompoundValue(1, volumeVwapSum[1] + volume * vwap, volume * vwap);
    volumeVwap2Sum = CompoundValue(1, volumeVwap2Sum[1] + volume * Sqr(vwap), volume * Sqr(vwap));
}
def price = volumeVwapSum / volumeSum;

Addlabel(close>price, "AV", color.CYAN);
Addlabel(price>close, "BV", createcolor(255,235,209));
 
Just FYI:
You can create your own colors for these labels by going to the website rgbcolorcode.com and making your own colors. You just put in createcolor(A,B,C), {A,B,C are numbers} wherever you need to insert the color.
 
Thanks for the code @Sonny once again. Reason why i asked is because i get faked out by using my own judgement when a candle goes below VWAP and ramps back up so with this code i can wait now for the label to change instead of guessing
 
if market opens at 9;30, Will i see the volume green color with high number right away, if it is very high for that particular stock in my watchlist?
same for red too?
The reason I ask because it is difficult to find a stock in early morning showing high volume in my scan. It does not show early in morning.
Thanks.
 
The volume label will show at 9:30. However, you can see premarket volume by adding Volume as a watchlist column in your scan.

@Sonny good to know, I was wondering about that but figured it's for RTH but also, I found a script on here for premarket volume that I pieced in: https://usethinkscript.com/threads/thinkorswim-pre-market-volume-indicator.2517/

Thanks to you I have some great labels and no more hunting around for those stats. Since market is closed I placed them on a daily chart of the /ES and they show up good and accurately. I'll see how they work out in premarket and RTH
 
I have revised the original code and now has more features, including premarket stats as well.

Please use this code:

Code:
#LABELS
#
#Current Price
AddLabel (1, "P: " + Round(close,2), Color.CYAN);

#Percent Change from yesterday close
def AP = AggregationPeriod.DAY;
def Priorclose = close(period = AP)[1];
def PctChange = (close - Priorclose) / Priorclose;
def PC1 = Round(PctChange, 3);
AddLabel(1, "Ch: " + AsPercent(PC1), if PctChange > 0 then CreateColor(238, 210, 238) else createcolor(255,153,153));

#Check if time is premarket
def pmopen = if secondsFromTime(0400)>=0 and secondstillTime(0930)>=0 then 1 else 0;
def pmclosed = if secondsFromTime(0400)>=0 and secondstillTime(0930)>=0 then 0 else 1;
def pastten = if secondsFromTime(0400)>=0 and secondstillTime(1000)>=0 then 1 else 0;

#Prior Close
AddLabel (1, "YC: " + Round(Priorclose, 2), Color.LIGHT_GREEN);

#Open price
def open1 = open(period = AP);
AddLabel (pmclosed, "O: " + Round(open1, 2), CreateColor(254, 216, 177));

#High of Day
def high1 = Highest(high(period = AP), 1);
AddLabel (pmclosed, "H: " + Round(high1, 2), Color.CYAN);

#Low of Day
def low1 = Lowest(low(period = AP), 1);
AddLabel (pmclosed, "L: " + Round(low1, 2), CreateColor(178, 255, 102));

#Volume Today
def today = volume(period = AP);
def L1 = RoundDown(Lg(today), 0);
AddLabel (L1 < 5, "V: " + today, CreateColor(206, 177, 128));
AddLabel (L1 between 4.5 and 5.5, "V: " + Round((today / 1000), 0) + "K", CreateColor(206, 177, 128));
AddLabel (L1 > 5, "V: " + Round((today / 1000000), 1) + "M", CreateColor(206, 177, 128));

#Change From Open
def CFO = (close / open1) - 1;
def CFO1 = Round(CFO, 3);
AddLabel(pmclosed, "C: " + AsPercent(CFO1), if CFO > 0 then CreateColor(0, 255, 128) else CreateColor(255, 153, 153));

#Relative Volume
def AV = AggregationPeriod.Day;
def x1 = Average(volume(period=AV)[1],60);
def v1 = volume(period=AV);
def z1 = v1/x1;
Addlabel(pmclosed, "R: "+ Round(z1,1), createcolor(208,209,238));

#Premarket Open
def newDay = GetDay() <> GetDay()[1];
def start = newDay or SecondsTillTime(400) == 0;
rec premarketOpen = if start then open else premarketOpen[1];
def x = premarketOpen;
Addlabel(pmopen, "PMO: " + Round(x,2), createcolor(110,160,208));

def h = high;
def bar = BarNumber();
def GlobeX = GetTime() < RegularTradingStart(GetYYYYMMDD());

#Premarket High
def ONhigh = if GlobeX and !GlobeX[1] then h else if GlobeX and h > ONhigh[1] then h else ONhigh[1];
def ONhighBar = if GlobeX and h == ONhigh then bar else Double.NaN;
def OverNightHigh = if BarNumber() == HighestAll(ONhighBar) then ONhigh else OverNightHigh[1];
AddLabel(pastten, "PMH: " + Round(OverNightHigh,2), color.CYAN);

#Pct difference between price and premarket open
Def z=Round(100*((close/x)-1),1);
Addlabel(pmopen, "PMG: " + z + "%", if z <0 then createcolor(255,153,153) else createcolor(197,239,161));
 
Last edited by a moderator:
@Sonny it's possible to write the code for the volume on a current bar in any timeframe?

Here is the code for volume tracking. V4 is the current volume, V1 is volume 1 min ago, V2 is volume 2min ago, and V3 is volume 3 min ago. You can customize it if you want.

#Volume tracker
def AP = AggregationPeriod.MIN;
def V1 = volume(period = AP)[1];
def V2 = volume(period = AP)[2];
def V3 = volume(period = AP)[3];
def V4 = volume;
Addlabel (1,"V3: "+V3,color.LIGHT_ORANGE);
Addlabel (1,"V2: "+V2,color.PINK);
Addlabel (1,"V1: "+V1,color.LIGHT_GREEN);
Addlabel (1,"VN: "+V4,color.CYAN);
 
This code is only for chart labels. The watchlist columns already have most of this data. You just have to add it to your columns.
 
It is getting better and better. Thanks for all the hard work. I don't want to abuse of your generosity, but is there a label that I can add to tell percentage change of today's volume to yesterday volume
 
@Johnny Cash

Just add this code to the bottom of the big code I posted above this reply:

#Pct diff between today and yest volume
def x2 = volume(period=AP)[1];
def x3 = Round(100*((today/x2)-1),1);
Addlabel(pmclosed, "Vc: "+x3+"%", if x3>0 then createcolor(97,217,244) else createcolor(255,153,153));
 
Last edited:
Hi @Sonny it is getting better and better, thanks for your work. All the parts of the code worked except for the volume codes. It did not show up on Thursday during RTH and has no errors in TOS. I see you changed some things so I updated the scripts with the new changes. i'll see if they show up on Monday during RTH and let you know. Here's the volume portion of the scripts throughout the thread that I need:

Code:
#Sonny @ usethinkscript.com

input v = volume;
input aggregationPeriod = AggregationPeriod.DAY;
def AP = AggregationPeriod.DAY;

#Yest Vol
def YV = v[1];
def YVL = RoundDown(Lg(YV), 0);
Addlabel(1, “YV” + V[1], CreateColor(206, 177, 128));

#Volume today
def VT = volume(period = AP) ;
AddLabel(1, "VT: " + V, CreateColor(206, 177, 128));

#Dif bet yest volume and today
def difvol = YV - V;
AddLabel(1, "2D VOLD: " + difvol, Color.PINK);

#Relative Volume 2d
def volD2d = volume(period = volD2d);
def z = Average(volume(period = volD2d)[1], 2);
def volD2dR = v/z;
Addlabel(volD2dR, " volD2dR: "+ Round(volD2dR,1), createcolor(208,209,238));

#Relative Volume 100d
#Average Volume in (100d)
def AV = volume(period = AV);
def x = Average(volume(period = AV)[1], 100);
def ARV = v/x;
Addlabel(ARV, "ARV: "+ Round(ARV,1), createcolor(208,209,238));

#Difference between AV and today's volume
def AVD = x-v;
Addlabel(AVD, "AVD: "+ Round(AVD,1), createcolor(208,209,238));
 
The volume label will show at 9:30. However, you can see premarket volume by adding Volume as a watchlist column in your scan.
Are you saying this script I can use for premarket as well? I tried but it works only when market opens. I m new and learning so apologize. Explain ti me how can i add this for premarket under watchlist or i use as a scan? this script works great during when market opens.
 

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