• Get $30 off VIP with code SUMMER30. Ends July 27. Happy trading!

ThinkorSwim Total Volume Traded Indicator

markos

Well-known member
VIP
ThinkorSwim label to show the current total volume traded and the total volume of the previous trading day.

Code:
declare upper;

input aggregationPeriod = AggregationPeriod.DAY;
def volume = volume(period = aggregationPeriod);
def old_volume = volume(period = aggregationPeriod)[1];
AddLabel(yes, Concat("Today's Total Volume = ", volume), color.orange);
AddLabel(yes, Concat("Previous Day Traded Volume = ", old_volume), color.red);

GGKA8dZ.png


Compre Yesterday's Volume vs Today's Volume at Same Time

Volume comparison indicator for ThinkorSwim. :)

Code:
# 09:30 Mobius: Good Morning - Don't have time to stay but wanted to post a volume study that plots a comparison of yesterdays total volume at the same bar and compares an average volume to the same time yesterday.
# Volume Comparison
# Plots Yesterdays Total Volume At Same Bar and Average Volume At Same Bar
# Mobius
# V02.06.2018 Posted to Chat Room 07.13.2018

declare on_volume;

input avgLength = 10;

def v = volume;
def vD = volume(period = AggregationPeriod.Day);
def c = close;
def x = BarNumber();
def nan = double.nan;
def RTHbar1 = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
              then x
              else RTHbar1[1];
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
          GetTime() <= RegularTradingEnd(GetYYYYMMDD());
def PrevRTHbar1 = if RTHbar1 != RTHbar1[1]
                  then RTHbar1[1]
                  else PrevRTHbar1[1];
def indexBar = RTHbar1 - PrevRTHbar1;
plot prevVol = if IsNaN(c)
               then nan
               else GetValue(v, indexBar);
prevVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
prevVol.SetDefaultColor(CreateColor(75, 75, 75));
prevVol.SetLineWeight(1);
plot Vol = v;
Vol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Vol.AssignValueColor(if close > open then CreateColor(5, 250, 12) else CreateColor(250, 5, 25));
AssignPriceColor(if close > open then CreateColor(5, 250, 12) else CreateColor(250, 5, 25));
def avgPrev = Average(prevVol, avgLength);
def avgCurr = Average(Vol, avgLength);
def prevDailyVol = if RTH and !RTH[1]
                   then getValue(v, indexBar)
                   else if RTH
                        then compoundValue(1, prevDailyVol[1] + GetValue(v, indexBar), GetValue(v, indexBar))
                   else prevDailyVol[1];
AddLabel(1, "Prev D Vol = " + prevDailyVol +
          "  Prev Vol avg(" + avgLength + ") = " + Round(avgPrev, 0), prevVol.TakeValueColor());
AddLabel(1, "Current D Vol = " + vD +
          "  Curr Vol avg(" + avgLength + ") = " + Round(avgCurr, 0), if vD > prevDailyVol
                                                                      then color.green
                                                                      else color.red);
# End Code Volume Comparison

6kHC0KO.png
 
Last edited by a moderator:

WIZARD

New member
What about putting a comparison between total volume today compared to yesterday up to the point of time in the day as a label? I'll take a look at your initial volume, but I don't want to have to change the time, just want a cumulative.
 

unoeit

New member
This is a neat script to compare current volume to previous day's volume at same bar. I noticed that the after hours bars throw off the indexbar. When I turn off after hours on the chart, the script doesn't work (due NaNs).

I found this on thinkscript learning website:

Code:
declare lower;
input symbol = "IBM";
def closeSymbol = close(symbol);
def closeSymbolWithoutNaN = CompoundValue(1, if IsNaN(closeSymbol) then closeSymbolWithOutNaN[1] else closeSymbol, closeSymbol);
plot Data = closeSymbolWithOutNaN;

This code plots the close price of an input symbol across the current chart. Any gap in the input symbol price data (NaN value) is replaced with the last-known close price value before the gap. But I couldn't integrate it to the thinkscript. Any ideas on how to get this script to ignore the after hours bars?
 

Cheytac

New member
First time poster here. Love the site btw. This is a really cool indicator by Mobius.

I'm not sure if this is possible, but is there any way to show this indicator even when charting option chains? For instance, get this to show in the lower i.e. AMD stock volume when looking at an options chart for AMD. I wasn't sure if there was anything that could be done using GetUnderlyingSymbol(). I'm guessing that the BarNumber() messes that up trying to display the underlying stock volume on an options chart.

If anyone knows of a workaround please share.

Thanks!
 

greges2109

New member
I'm trying to add another condition to my scanner with no success, I want to add condition to scan all stocks that the current volume is equal or greater than 1milion for example... not total volume, the current volume... any ideas?
 

markos

Well-known member
VIP
@Cheytac no, I do not believe that it is possible. Try reviewing the code & the manuals in the first entry of our Tutorial section.

@greges2109 please review the script. I noticed that there is an input for the study. Change that to 14.

@J007RMC Glad it could be brought to you!
 

tomsk

Well-known member
VIP
A closer examination of the code in post #1 shows that it is written for intraday, i,e, aggregation periods less than DAILY. Hence you'll need to restate your request precisely what you're looking for so that @markos (whom you directed your response to) or one of the coders here can follow up with the specifics.
 

greges2109

New member
Thank you @tomsk and @markos ! I am new here but will be very much excited to learn from you.

I am looking for a script that will help me to calculate relative volume especially during trading the open.

The script above does almost what i have been looking for! The only difference that i would love to learn how to implement is to take a volume of each bar from the past 14 sessions (instead of a previous day only; for example a volume at9:31,9:32,9:33 etc each from previous 14 days) and calculate an average. Then use this value to compare to a volume at a current bar. I will be grateful so very much for your help! Thanks
 

tomsk

Well-known member
VIP
From a technical standpoint there are a few issues you'll need to be aware of. If you want to keep track of bar volumes at each of the previous 14 days, you will need to maintain 14 separate variables. The other BIGGER problem is that in ThinkScript there's a midnight rollover that is somewhat a pain to code. These were the exact issues that were previously discussed in the Thinkscript lounge when similar requests were made, and that was the general consensus from the experts there.
 

magicfon

New member
This code is great, and i try to use it for a scanner to find out the stock which volume is higher than previous day at same time.
But it shows "Secondary period not allowed: Day"
How can I solve this problem? Thanks!
Code:
declare on_volume;

input avgLength = 10;
input unusualpercent = 200;

def v = volume;
def vD = volume(period = AggregationPeriod.Day);
def c = close;
def x = BarNumber();
def nan = double.nan;
def RTHbar1 = if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
              then x
              else RTHbar1[1];
def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
          GetTime() <= RegularTradingEnd(GetYYYYMMDD());
def PrevRTHbar1 = if RTHbar1 != RTHbar1[1]
                  then RTHbar1[1]
                  else PrevRTHbar1[1];
def indexBar = RTHbar1 - PrevRTHbar1;
def prevVol = if IsNaN(c)
               then nan
               else GetValue(v, indexBar);

def Vol = v;

AssignPriceColor(if close > open then CreateColor(5, 250, 12) else CreateColor(250, 5, 25));
def avgPrev = Average(prevVol, avgLength);
def avgCurr = Average(Vol, avgLength);
def prevDailyVol = if RTH and !RTH[1]
                   then getValue(v, indexBar)
                   else if RTH
                        then compoundValue(1, prevDailyVol[1] + GetValue(v, indexBar), GetValue(v, indexBar))
                   else prevDailyVol[1];
def TodaycumVol = if RTH and !RTH[1]
                   then v
                   else if RTH
                        then compoundValue(1,  TodaycumVol[1] + v, v)
                   else TodaycumVol[1];
def todaybar=RTHbar1-x;
#plot data=TodaycumVol;
#plot data2= PrevRTHbar1;

def compvolume=vD/prevDailyVol*100;

plot comp= compvolume > unusualpercent;
 

invsto

New member
VIP
This is a masterpiece. Thank you for this amazing piece of work. Mobius for creating this and @markos for sharing. Here's the updated version thanks to @skynetgen

Code:
# 09:30 Mobius: Good Morning - Don't have time to stay but wanted to post a volume study that plots a comparison of yesterdays total volume at the same bar and compares an average volume to the same time yesterday.
# Volume Comparison
# Plots Yesterdays Total Volume At Same Bar and Average Volume At Same Bar
# Mobius
# V02.06.2018 Posted to Chat Room 07.13.2018

#declare on_volume;
declare lower;

input avgLength = 10;

def v = volume;
def vD = volume(period = AggregationPeriod.Day);
def c = close;
def x = BarNumber();
def nan = double.nan;

def RTHbar1 =  if getday()<>getday()[1]
              then x
              else RTHbar1[1];
#Original code: if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())

def RTH = GetTime() >= RegularTradingStart(GetYYYYMMDD()) and
          GetTime() <= RegularTradingEnd(GetYYYYMMDD());
def PrevRTHbar1 = if RTHbar1 != RTHbar1[1]
                  then RTHbar1[1]
                  else PrevRTHbar1[1];
def indexBar = RTHbar1 - PrevRTHbar1;



plot prevVol = if IsNaN(c)
               then nan
               else GetValue(v, indexBar);
prevVol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
prevVol.SetDefaultColor(CreateColor(75, 75, 75));
prevVol.SetLineWeight(1);
plot Vol = v;
#Vol.SetPaintingStrategy(PaintingStrategy.SQUARED_HISTOGRAM);
Vol.AssignValueColor(if close > open then CreateColor(5, 250, 12) else CreateColor(250, 5, 25));
AssignPriceColor(if close > open then CreateColor(5, 250, 12) else CreateColor(250, 5, 25));
def avgPrev = Average(prevVol, avgLength);
def avgCurr = Average(Vol, avgLength);
def prevDailyVol = if RTH and !RTH[1]
                   then getValue(v, indexBar)
                   else if RTH
                        then compoundValue(1, prevDailyVol[1] + GetValue(v, indexBar), GetValue(v, indexBar))
                   else prevDailyVol[1];
AddLabel(1, "Prev D Vol = " + prevDailyVol +
          "  Prev Vol avg(" + avgLength + ") = " + Round(avgPrev, 0), prevVol.TakeValueColor());
AddLabel(1, "Current D Vol = " + vD +
          "  Curr Vol avg(" + avgLength + ") = " + Round(avgCurr, 0), if vD > prevDailyVol
                                                                      then color.green
                                                                      else color.red);
# End Code Volume Comparison
 
I've been testing the OP script and it seems to be off. To make sure I understand the script, I'm assuming the prev vol average is taking the average of the past two session min bar at the current bar position...ex if the current session is on the 30th min bar, it takes the 30th min bar from the past two days and averages it. If that is the case then it is off...or I don't understand the purpose of the script. Let me know please. Ex.

I have it set to 2 day averaging for easy observation.

tsla
39,940--- previous 2 day average on Oct 16 @ 13:09:59
53,193--- current on Oct 15 13:09:59
20,341--- current on Oct 14 13:09:59

The Calculation is off by 3,200. Previous 2 day average shows 39,940, but calculating the two days average bars actually comes up with 36,767.

tsla
51,108--- previous 2 day average on Oct 7 @ 13:09:59
65,631--- current on Oct 6 13:09:59
30,939--- current on Oct 5 13:09:59

The Calculation is off by 2,823. Previous 2 day average shows 51,108, but calculating the two days average bars actually comes up with 48,285.


That's a small amount, but once the volume is higher and more days added the variation would grow exponentially. Looking at the code I can't see why this would be. Am I missing something?


Also (tsla again) the previous day vol calculation is off. Oct 7, 13:20:00 shows the previous day volume is 20,960,474, and when I go to the previous day (Oct 6) and look at the current it shows 21,086,456. That's off by 125,982. Again, am I misunderstanding something?

Thanks.
 

Stoynks

New member
I want to compare the previous day's volume to the current. basically, I want a code like this

When the market opens in the morning, if the first bar (30-minute aggregation period) is greater than or equal to the previous day's whole volume then color code/highlight it.
 

XeoNoX

Well-known member
VIP
I want to compare the previous day's volume to the current. basically, I want a code like this

When the market opens in the morning, if the first bar (30-minute aggregation period) is greater than or equal to the previous day's whole volume then color code/highlight it.

here you go @Stoynks
This only works on 30 Minute Timeframe:
Code:
declare upper;

input pointInTime = 0929;

def isAppointedHour = if SecondsFromTime(pointInTime) > 0 then 1 else 0;

def volAtTime = if isAppointedHour == 1 and isAppointedHour[1] == 0 then volume
    else if isAppointedHour == 1 then volAtTime[1]
        else Double.NaN;
def vol30 = volAtTime;

def yvol =(volume(period = AggregationPeriod.DAY)[1]);

AddLabel (yes, "Yeterday Total Vol " +  (yvol) + "  Todays 30Min Vol " +  (vol30),(if vol30>yvol then color.green else color.red));
 

CharlieCOL

New member
VIP
Hi there, first post here.

i´m trying to write my own study, starting with first candle´s condition identifications, as follows (is not working btw).

any idea which one is my mistake?

Code:
# Identify if First Candle of Day is opening higher than previous close on day before

def Bar1 = GetDay() <> GetDay()[1];
def IsUp = close > open;
def IsDown = close < open;
def IsDoji = IsDoji();
def avgRange = 0.05 * Average(high - low, 20);
plot PatternPlot =
    IsDown[1] and
    IsUp[0] and
    close[1] > open[0];

PatternPlot.SetPaintingStrategy(PaintingStrategy.BOOLEAN_ARROW_UP);
PatternPlot.SetDefaultColor(CreateColor(255, 255, 255));
 

rad14733

Well-known member
VIP
@CharlieCOL Welcome to the usethinkscript forums... Have you tried using the search feature because if memory serves me correctly you're trying to reinvent the wheel... I'm fairly certain that Study already exists here in these forums...
 

Similar threads

Top