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

markos

markos

Well-known member
VIP
Here is a treat for my Day Trading Friends: Volume comparison indicator for ThinkorSwim. :)



thinkScript Code

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

Shareable Link

https://tos.mx/bgqhlA
 
Last edited by a moderator:
S

skynetgen

Well-known member
Thank you!
For some reason was not working for me until i changed:
def RTHbar1 = #if GetTime() crosses above RegularTradingStart(GetYYYYMMDD())
if getday()<>getday()[1]
 
markos

markos

Well-known member
VIP
Thank you!
For some reason was not working for me until i changed:
@skynetgen So that we all learn, please post the line above and below (if possible) the part of the offending code. btw, using # to comment out is appreciated. Thanks!
Then, please repaste the whole code (within reason) along with the tos.mx share that has it corrected.

I don't want to look like a "dufus" if I ask Mobius or Paris in the thinkScript Lounge about the code. :oops::unsure:
 
markos

markos

Well-known member
VIP
This worked for me as advertised. I used 2 day average.
 
U

unoeit

New member
Hi All,
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:
Example 2
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?

Thank you.
 
Cheytac

Cheytac

New member
Hi Everyone,

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!
 
G

greges2109

New member
thanks for the script! this is really helpful, indeed
do you know how to input instead of a previous day volume at a bar, an average volume of previous 14days at a given bar and compare to the current one?
i'd be grateful for your help
 
markos

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!
 
G

greges2109

New member
@markos indeed, there is an input but it only averages the given number of bars. I did a careful testing on the script.
it would be great to have an input that would report on a number of days instead.
 
T

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.
 
G

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
 
T

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.
 
M

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;
 
markos

markos

Well-known member
VIP
You cannot use it on a daily agg. You may be better served using stock sizzle, which is adjustable.
 
M

magicfon

New member
You cannot use it on a daily agg. You may be better served using stock sizzle, which is adjustable.
Thanks for your reply.
Could you please give me more information?
what is stock sizzle ?
Thank you!
 
invsto

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
 

Similar threads

Top