Custom Thinkscript - Volume Stats for ThinkOrSwim

3AMBH

Member
2019 Donor
This custom Thinkscript indicator shows you important volume data on your ThinkOrSwim charts. This includes Current Volume, Average 30 Day Volume, Percentage of Current Volume to Average Daily Volume, Average 30 Bar Volume and Current Bar Volume. Volume stats change colors to indicate when the volume reaches the average daily volume level as well as a customizable unusual volume level. This allows you to easily see critical changes in volume levels which are important for all types of trading.

The Script:

Code:
# Box Volume Stats
# Version 1.0
# Created by: Enigma
# Created: 05/18/17

declare lower;

#Inputs
input Show30DayAvg = yes;
input ShowTodayVolume =  yes;
input ShowPercentOf30DayAvg = yes;
input UnusualVolumePercent = 200;
input Show30BarAvg = yes;
input ShowCurrentBar = yes;


#Volume Data
def volLast30DayAvg = (volume(period = "DAY")[1] + volume(period = "DAY")[2] + volume(period = "DAY")[3] + volume(period = "DAY")[4] + volume(period = "DAY")[5] + volume(period = "DAY")[6] + volume(period = "DAY")[7] + volume(period = "DAY")[8] + volume(period = "DAY")[9] + volume(period = "DAY")[10] + volume(period = "DAY")[11] + volume(period = "DAY")[12] + volume(period = "DAY")[13] + volume(period = "DAY")[14] + volume(period = "DAY")[15] + volume(period = "DAY")[16] + volume(period = "DAY")[17] + volume(period = "DAY")[18] + volume(period = "DAY")[19] + volume(period = "DAY")[20] + volume(period = "DAY")[21] + volume(period = "DAY")[22] + volume(period = "DAY")[23] + volume(period = "DAY")[24] + volume(period = "DAY")[25] + volume(period = "DAY")[26] + volume(period = "DAY")[27] + volume(period = "DAY")[28] + volume(period = "DAY")[29] + volume(period = "DAY")[30]) / 30;
def today = volume(period = "DAY");
def percentOf30Day = Round((today / volLast30DayAvg) * 100, 0);
#def avg30Bars = VolumeAvg(30).VolAvg;
def avg30Bars = (volume[1] + volume[2] + volume[3] + volume[4] + volume[5] + volume[6] + volume[7] + volume[8] + volume[9] + volume[10] + volume[11] + volume[12] + volume[13] + volume[14] + volume[15] + volume[16] + volume[17] + volume[18] + volume[19] + volume[20] + volume[21] + volume[22] + volume[23] + volume[24] + volume[25] + volume[26] + volume[27] + volume[28] + volume[29] + volume[30]) / 30;
def curVolume = volume;


# Labels
AddLabel(Show30DayAvg, "Daily Avg: " + Round(volLast30DayAvg, 0), Color.LIGHT_GRAY);
AddLabel(ShowTodayVolume, "Today: " + today, (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.LIGHT_GRAY));
AddLabel(ShowPercentOf30DayAvg, percentOf30Day + "%", (if percentOf30Day >= UnusualVolumePercent then Color.GREEN else if percentOf30Day >= 100 then Color.ORANGE else Color.WHITE) );
AddLabel(Show30BarAvg, "Avg 30 Bars: " + Round(avg30Bars, 0), Color.LIGHT_GRAY);
AddLabel(ShowCurrentBar, "Cur Bar: " + curVolume, (if curVolume >= avg30Bars then Color.GREEN else Color.ORANGE));

Shared link: https://tos.mx/rSGwbW

YouTube

 
Last edited by a moderator:

przinho

New member
Hi, im newbie with coding.
Can somebody help me with label that shows first 30 min volume of today session?
Thank you for your time.
 

tomsk

Well-known member
VIP
@przinho Here's a label for the first 30 mins of volume as requested. Run this on an intraday chart

Code:
# Volume RTH First 30 Mins
# tomsk
# 11.5.2019

declare hide_on_daily;

input startTime = 0930;
input endTime = 1000;

def Active = SecondsFromTime(startTime) >= 0 and SecondsTillTime(endTime) >= 0;
def Vol30Mins = if Active and !Active[1] then volume
                else if Active then Vol30Mins[1] + volume
                else Vol30Mins[1];
AddLabel(1, "Volume First 30 Mins = " + Vol30Mins, Color.YELLOW);
# End Volume RTH First 30 Mins
 
Last edited:

tenacity11

Active member
2019 Donor
@tomsk Is there a way to have the Volume Stats on the top left corner of the chart without having the actual volume box?
TIA
 

tomsk

Well-known member
VIP
Your request is incomplete. What volume stats are you looking for? Not sure I understand what you mean by "actual volume box". Like I always tell people - you'll need to be real clear and explicit with your requests or it won't be easily understood
 

tenacity11

Active member
2019 Donor
Your request is incomplete. What volume stats are you looking for? Not sure I understand what you mean by "actual volume box". Like I always tell people - you'll need to be real clear and explicit with your requests or it won't be easily understood
Apologies. As it stands now the volume stats populate the same box as volume in the lower part of the chart. What I want is to have the volume stats at the top of the chart under the area where the chart name, date, O-H_L_C is. I don't want to have the volume box on. Hope that helps explain.
 

tomsk

Well-known member
VIP
Gotcha, from your decription, you'd like to turn off the volume subgraph on your chart and display the volume as a label. This is real simple. To disable the volume subgraph on your chart, go to Chart Settings > Equities. Then unclick the checkbox "Show volume subgraoh" Here is a very simple label that displays the volume as a chart label

Code:
addLabel(1, "Volume = " + volume, color.Yellow);
 
Last edited:

tenacity11

Active member
2019 Donor
Gotcha, from your decription, you'd like to turn off the volume subgraph on your chart and display the volume as a label. This is real simple. To disable the volume subgraph on your chart, go to Chart Settings > Equities. Then unclick the checkbox "Show bolume subgraoh" Here is a very simple label that displays the volume as a chart label

Code:
addLabel(1, "Volume = " + volume, color.Yellow);
Once again thanks. Perfect
 

SuryaKiranC

Well-known member
Hi All,

Been here and checking out some scripts for few months. Though about time I start contributing.

My first ever post ever. I am trying to improve this script and include, rVolume as well in the labels. Intended changes are listed.
  • Change the number of days for average calculation to be an input, instead of fixed 30 days.
  • Include rVolume Label.
I think I got the calculations right, however when I give 30 Days as an input for the modified script, it's slightly off in 30DayAvg Calculations.

Was wondering, if somebody here can take a look at this and see what am I missing. Please see the attached screenshot, the one on the left is using original code and the one on the right is using the modified code.

gGUYCGO.png


Code:
# Box Volume Stats
# Version 1.0
# Created by: Enigma
# Created: 05/18/17
# Modified by: Surya Kiran C ## Included rVolume label and Changed length as input.

declare on_volume;

input length = 30;
input ShowDayAvg = yes;
input ShowTodayVolume = yes;
input ShowPercentOfDayAvg = yes;
input UnusualVolumePercent = 200;
input ShowBarAvg = yes;
input ShowCurrentBar = yes;

def VolDayAvg = (fold index = 1 to length with Avg do Avg + (volume(period = "DAY")[index])) / length ;
def Today = volume(period =  "DAY");
def PercentOfDayAvg = Round((Today / VolDayAvg) * 100, 0);
def AvgBars = (fold index2 = 1 to length with Bar do Bar + (volume[index2])) / length;
def CurVol = volume;

def offset = 1;
def ADV = Average(volume, length)[offset];
def rVol = volume / ADV;

# Labels
AddLabel(yes, Round(rVol, 2));
AddLabel(yes, "ADV :" + ADV);
#AddLabel(yes, asPercent(rVol)); # remove "#" infront of Addlabels to select prefer choice
#AssignPriceColor(if rVol >= 1 then color.dark_red else if rVol <=.5 then Color.black else color.Gray);
AddLabel(ShowDayAvg, "Daily Avg: " + Round(VolDayAvg, 0), Color.LIGHT_GRAY);
AddLabel(ShowTodayVolume, "Today: " + Today + " ", (if PercentOfDayAvg >= UnusualVolumePercent then Color.GREEN else if PercentOfDayAvg >= 100 then Color.ORANGE else Color.LIGHT_GRAY));
AddLabel(ShowPercentOfDayAvg, PercentOfDayAvg + "%", (if PercentOfDayAvg >= UnusualVolumePercent then Color.GREEN else if PercentOfDayAvg >= 100 then Color.ORANGE else Color.WHITE) );
AddLabel(ShowBarAvg, "Avg 30 Bars: " + Round(AvgBars, 0) + " ", Color.LIGHT_GRAY);
AddLabel(ShowCurrentBar, "Cur Bar: " + CurVol + " ", (if CurVol >= AvgBars then Color.GREEN else Color.ORANGE));
 

SuryaKiranC

Well-known member
I think I fixed it. While at it tried to fold Pre-Market, 1H Market and Post-Market volumes added to the labels, must work Equity, if not with futures.

Code:
# Box Volume Stats
# Version 1.2
# Created by: Enigma
# Created: 05/18/17
# Modified by: Surya Kiran C ## Included rVolume label and Changed length as input. ## Additionally Pre-Market, 1Hr Volume, AfterHour Volume labels are added. 


declare on_volume;

input length = 30;
input ShowDayAvg = yes;
input ShowTodayVolume = yes;
input ShowPercentOfDayAvg = yes;
input UnusualVolumePercent = 200;
input ShowBarAvg = yes;
input ShowCurrentBar = yes;

input PreMktVol = yes;
input RTH1HrVol = yes; 
input PostMktVol = yes; 


def VolDayAvg = (fold index = 1 to length + 1  with Avg = 0 do (Avg + volume(period = "DAY")[index])) / length; 
def AvgBars = (fold index2 = 1 to length + 1 with Bar = 0  do (Bar + volume[index2])) / length;

def Today = volume(period =  "DAY");
def PercentOfDayAvg = Round((Today / VolDayAvg) * 100, 0);

def CurVol = volume;


def offset = 1;
def ADV = Average(volume, length)[offset];
def rVol = volume / ADV;

# Labels

#if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if 1HrRTHVol then 1 else 0,

AddLabel(ShowDayAvg, length + "Day Avg: " + Round(VolDayAvg, 0) + "  ", Color.LIGHT_GRAY);
AddLabel(ShowTodayVolume, "Today: " + Today + " ", (if PercentOfDayAvg >= UnusualVolumePercent then Color.GREEN else if PercentOfDayAvg >= 100 then Color.ORANGE else Color.LIGHT_GRAY));
AddLabel(ShowPercentOfDayAvg, PercentOfDayAvg + "%", (if PercentOfDayAvg >= UnusualVolumePercent then Color.GREEN else if PercentOfDayAvg >= 100 then Color.ORANGE else Color.WHITE) );
AddLabel(ShowBarAvg, "Avg" + length  + "Bars: " + Round(AvgBars, 0) + " ", Color.LIGHT_GRAY);
AddLabel(ShowCurrentBar, "Cur Bar: " + CurVol + " ", (if CurVol >= AvgBars then Color.GREEN else Color.ORANGE));

AddLabel(yes, "rVol :" + Round(rVol, 2));
#AddLabel(yes, "ADV :" + ADV); 
#AddLabel(yes, asPercent(rVol)); # remove "#" infront of Addlabels to select prefer choice
#AssignPriceColor(if rVol >= 1 then color.dark_red else if rVol <=.5 then Color.black else color.Gray);

#Pre, 1Hr RTH, AfterHours Volumes. 
##if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if PreMktVol then 1 else 0

input PrestartTime = 0400;
input PreendTime = 0929;

def PreMkt = SecondsFromTime(PrestartTime) >= 0 and SecondsTillTime(PreendTime) >= 0;
def PreVolMins = if PreMkt and !PreMkt[1] then volume
                else if PreMkt then PreVolMins[1] + volume
                else PreVolMins[1];
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if PreMktVol then 1 else 0, "PreMktVol = " + PreVolMins + "  ", Color.YELLOW);
# End Volume PreMarket

input RTH1HrstartTime = 0930;
input RTH1HrendTime = 1029;

def RTH1Hr = SecondsFromTime(RTH1HrstartTime) >= 0 and SecondsTillTime(RTH1HrendTime) >= 0;
def RTH1HrMins = if RTH1Hr and !RTH1Hr[1] then volume
                else if RTH1Hr then RTH1HrMins[1] + volume
                else RTH1HrMins[1];
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if RTH1HrVol then 1 else 0, "RTH1HrVol = " + RTH1HrMins + "  ", Color.YELLOW);
#End Volume RTH First 60 Mins

input PoststartTime = 1600;
input PostendTime = 1959;

def PostMkt = SecondsFromTime(PoststartTime) >= 0 and SecondsTillTime(PostendTime) >= 0;
def PostVolMins = if PostMkt and !PostMkt[1] then volume
                else if PostMkt then PostVolMins[1] + volume
                else PostVolMins[1];
AddLabel(if GetAggregationPeriod() >= AggregationPeriod.DAY then 0 else if PostMktVol then 1 else 0, "PostMktVol = " + PostVolMins + "  ", Color.YELLOW);
# End Volume PostMarket
 
Last edited:

C4men

Member
Hey @SuryaKiranC - A couple quick asks if you don't mind.

- On the PreMkt label, it seems to be adding the premarket volume to the 1st bar volume.
-- I can get it to show the correct PreMkt volume if I change the end time to 929. But the moment I turn off premarket in the settings, the label then shows 0. I want it to stay there permanently, regardless of whether or not Premarket is selected.

- I like how you have the 1st Hour label.
-- Would there be a way to do two labels for: Candle/Bar 1 Volume, Candle/Bar 2 Volume (based on current timeframe)?

I am very willing to help with this and be a tester, but I am not much of a thinkscripter. Any help would be terrific!!!

Essentially, I am looking for three labels:

1. Premarket Volume (Sum of volume from 4am to 930am, but NOT inclusive of the 930 bar
2. Bar 1 Volume - Displays the volume of the 1st candle, based on timeframe (if 15m, then the volume of the first 15 min bar)
3. Bar 2 Volume - Same as bar 1, except for the second bar based on timeframe selected

The one other piece I'd love to accomplish would be to compare those three volumes to the Average Volume over the last 30 days, as a percentage. So if Daily Avg over the last 30 days is 1,000,000 and Bar 1 is 300,000, then the % would be 30%. I would add it to the label so it's easy to see.

I have been working on a very solid strategy, but it is reliant on this information.

Please help so I can share the strategy lol !!!
 
Last edited by a moderator:

SuryaKiranC

Well-known member
Hey @SuryaKiranC - A couple quick asks if you don't mind.

- On the PreMkt label, it seems to be adding the premarket volume to the 1st bar volume.
-- I can get it to show the correct PreMkt volume if I change the end time to 929. But the moment I turn off premarket in the settings, the label then shows 0. I want it to stay there permanently, regardless of whether or not Premarket is selected.

Are you using the updated code? if you turn off pre-market Volume, label itself will disappear. If I understand your Bar 1 Volume and Bar 2 Volume requirement correct, both of them should be based on the timeframe selected, Am I right?

% if the 1st Bar, based on the time frame can be added should be a problem. Just like average total volume. Let me know a time and date we can work out the requirement and further customize the script.

-S
 

C4men

Member
Are you using the updated code? if you turn off pre-market Volume, label itself will disappear. If I understand your Bar 1 Volume and Bar 2 Volume requirement correct, both of them should be based on the timeframe selected, Am I right?

% if the 1st Bar, based on the time frame can be added should be a problem. Just like average total volume. Let me know a time and date we can work out the requirement and further customize the script.

-S

Yep, using the updated code. But when I manually total the volume premarket and compare to label, it still looks like it then adds the 930am volume. Would love to find a way for it to "stop counting" at 9:29:59 and then the label stay visible throughout the day.

In tinkering, I also realized the Bar % would be hard. I think a good compromise would be to just hard-code it.
  • Input a start time and end time: Example would be Bar 1 0930-0945 and Bar 2 0945-1000
  • Then compare each total against the average daily volume
  • End up with a label for each like:

[PreMkt = 456,789 :: 10% of Daily Avg.] [First 15 = 1,234,567 :: 20% of Daily Avg.] [Second 15 = 2,345,678 :: 40% of Daily Avg.]

Having these as a visual guide to placing day trades will be extremely helpful.
 

C4men

Member
The setting is in 'Chart Settings' under the gear icon. I go to the 'Equities' tab in there and check or uncheck 'Show Extended Hours Trading Session' to turn off premarket. I usually do that at the start of the day.

As for the Pre-Market Volume going to zero, it's a result of me tinkering to try and get it to work.

  • In your code, I change the PreMarket Stop time to 0929
  • This gives me a perfectly accurate label of PreMkt Volume and does NOT include volume from regular hours
  • But, once I turn off premarket when the day starts, the label shows zero.

Using MRNA today as the example...

Extended Hours ON, Code set to 0929 - Label shows 510,917 (which is correct)
Extended Hours OFF, Code set to 0929 - Label shows 0 (which is incorrect)

Any thoughts?

PS - I am having trouble again with screenshots...
 

SuryaKiranC

Well-known member
I was of the opinion, you were referring to turning off Pre/Post Volume at the indicator level, Looks like you are taking away Extended Hours all together from the chart settings. What we do with the indicator is calculate we the data what we see on the chart. Turning this off, I would expect the calculations to return 0. @C4men
 

C4men

Member
I was of the opinion, you were referring to turning off Pre/Post Volume at the indicator level, Looks like you are taking away Extended Hours all together from the chart settings. What we do with the indicator is calculate we the data what we see on the chart. Turning this off, I would expect the calculations to return 0. @C4men

Darn - I need to figure out how to keep that PreMkt label populated with extended off/unchecked.
 

Similar threads

Top