# Premarket Relative Volume Percentage Watchlist for ThinkorSwim

#### CashPrince

##### New member
VIP
You guys rock! Do you happen to have a study that calculates the premarket relative volume percentage that is color-coded in a watchlist for ThinkorSwim? It would also be nice to have it in a study with labels.

Gracias,
Prince

Code:
``````# Cumulative Overnight Volume
# UpTheCreek
# V01.2016

declare lower;
declare hide_on_daily;
input StartTime = 1700;
input EndTime = 0930;

def OverNightActive =  if (SecondsFromTime(StartTime) >= 0 and
SecondsTillTime(0000) <= 0)
or
(SecondsFromTime(0000) >= 0 and
SecondsFromTime(EndTime) < 0)
then 1
else Double.NaN;

def CumVol1 = if !IsNaN(OverNightActive) and IsNaN(OverNightActive[1])
then volume
else if !IsNaN(OverNightActive)
then if !IsNaN(volume)
then CumVol1[1] + volume
else CumVol1[1]
else 0;

plot cumvolP1=cumvol1;

AddLabel(yes, "Cumulative Overnight Volume = " + CumVol1);``````

#### Eagle X

##### New member
I would like to see something like this as well. A current day premarket relative volume compared to the average premarket volume from the last 20 days. I'm thinking it could even be set up to capture a certain timeframe. In this case, the premarket hours of 4:00 A.M. to 9:30 A.M.

#### zeek

##### Active member
2019 Donor
I second this request, would be very useful for my strategy. I hope someone here can code something like this

#### OldBallz

##### New member
2019 Donor
@zeek did you ever find a solution for this?

#### zeek

##### Active member
2019 Donor
Unfortunately no @OldBallz Here`s hoping someone smart can figure it out and help code this for us.

#### alphasandstorm

##### New member
I found a script that can quantify the pre-market volume on TOS but this is only for one day.
There's not an average of premarket volume over the past n days.
"GetYYYYMMDD()" corresponds to the day whose trading session contains the current bar.
Does anyone know what "GetYYYYMMDD()[1]" corresponds to? Is it the next day?
If someone knows how to use GetYYYYMMDD() to get the nth previous day you I could likely use that to figure out how to get the average of the premarket volume over n days in another script.
Code:
``````# PreMarketForecast Volume
# TOS
def LabelTime = if SecondsFromTime(0800) > 0 and
SecondsTillTime(1000) >= 0
then 1
else 0;
def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def vol = if isRollover and beforeStart then volume else if beforeStart then vol[1] + volume else Double.NaN;
def PreMarketVol = if IsNaN(vol) then PreMarketVol[1] else vol;
AddLabel(LabelTime, "PreMarket Volume = " + PreMarketVol, Color.WHITE);``````
I would like to use this to create an RVOL script.
Does anyone know the best approach for using this data to create an RVOL script to find stocks in play before the market opens?

Thanks
Jackson

Last edited:

#### SirMc

##### New member
I'm looking for the same thing.

#### alphasandstorm

##### New member
@SirMc Have you explored the existing RVOL scripts on this site? What are your thoughts about them?

#### SirMc

##### New member
@SirMc Have you explored the existing RVOL scripts on this site? What are your thoughts about them?
Yes I have but they don't provide the solution I'm looking for which is to find the average volume for a specific time frame. For instance the relative volume of the first 5 minutes for the last N number of days vs today's first 5 minutes.

#### alphasandstorm

##### New member
I have made progress towards a solution but it is not complete / issues remain.

The following will plot the pre-market volume on a histogram below the chart and compare it to an average of the pre-market volume over the past 10 days:
Code:
``````declare lower;

def isRollover = GetYYYYMMDD() != GetYYYYMMDD()[1];
def beforeStart = GetTime() < RegularTradingStart(GetYYYYMMDD());
def vol = if isRollover and beforeStart then volume else if beforeStart then vol[1] + volume else Double.NaN;
def pmVol = if IsNaN(vol) then pmVol[1] else vol;

def volLast10DayAvg = (pmVol[1] + pmVol[2] + pmVol[3] + pmVol[4] + pmVol[5] + pmVol[6] + pmVol[7] + pmVol[8] + pmVol[9] + pmVol[10]) / 10;
plot avg = volLast10DayAvg;

plot pmVOLPlot = pmVol;
pmVOLPlot.setDefaultColor(color.red);
pmVOLPlot.setPaintingStrategy(paintingStrategy.HISTOGRAM);
AddLabel(1, "RVOL= " + pmVOL/avg, Color.YELLOW);
plot ratio = pmVol/avg;
ratio.setDefaultColor(color.GREEN);``````

The issues:
Now while this does allow you to quantify the pre-market volume and compare it to the pre-market volume of the past 10 days, it stops summing volume once the market opens and just holds the sum of the pre-market volume data over the course of the trading day.
I'm exploring whether / how to include the regular session volume.
I imagine this negatively affects the 10DMA.

I've included a screenshot of the indicator on AAPL on the same day an SMB trader on youtube mentioned the RVOL at market open for AAPL was ~5.5 on April 17, 2020.
The closest I've been able to get to that value with this script is ~4.5 on a 30min timeframe.
Further, when switching time frames the ratio of premarket volume to the average volume changes from a value that would get you interested on a 30min timeframe - to one that seems like the volume isn't as wild as the data shows.
Being able to have a constant or nearly constant value while switching time frames would be ideal.

I've been learning thinkscript, and in order to create a scan for this you would write a line to return a boolean (true or false) value as the result. So something like:
Code:
``plot result = ratio > 5;``
What I would love for this script to do is:
Calculate the sum of the current pre-market bar volume and compare it to the average sum of pre-market volume at the same relative time bar of the current day for the past x days.

For example, the current bar time is 6:15am EST.
Compare the sum of the pre-market volume at that time to the 6:15am EST candle over the past x days, and return the relative volume of the current bar to the x day average.

Any help is greatly appreciated.
@BenTen @horserider

Last edited:

#### alphasandstorm

##### New member
To comment to the potential of this type of indicator, check out how well it caught the bottom on CCL on April 2, 2020.

RVOL read as ~7, and combined with the exhaustion candle before the close the previous day, and some great news about the Saudis proclaiming a massive stake that am - the pre-market RVOL would've alerted you / added to your conviction for a great setup before the market opened.

#### vikram

##### New member
VIP
@alphasandstorm Any idea how this code can be modified to use for compare relative volume opening hour (9.30-10.30 EDT) for 10 days ?

#### BenTen

Staff
VIP
Someone on Github recently shared this indicator called After Hours Relative Price Movement.

Tracks the price difference from afterhour volume relative to yesterday's close. This was created to visually confirm if certain stock prices were primarily driven from afterhour movements over longer periods of time.

### thinkScript Code

Code:
``````# AfterHoursRelativePriceMovement tracks the price difference from afterhour volume relative to yesterday's close

# This was created so I could eyeball if certain stock prices were primarily driven from afterhour movements over longer periods of time
# Can be overlaid with AddMarketHoursUTC to visually see correlation from international market open/closes

# Inputting Market Hours is required due to thinkscript's date function limitations
input marketOpen = 0930;
input marketClose = 1600;

# AFAIK there is no better way to do this due to SecondsTillTime rolling over after midnight, lack of real date math, and the mish-mash of UTC/EST time handling from thinkscript
def isClosedMorning = if SecondsTillTime(marketOpen) > 0 then yes else no;
def isClosedNight = if SecondsTillTime(marketClose) < 0 then yes else no;

def change = if !isClosedMorning AND !isClosedNight then 0 else change[1] + (hl2 - hl2[1]);

plot yesterdayPrice = 0;

plot priceDifference = if change != 0 then change else Double.NaN;
priceDifference.AssignValueColor(if priceDifference > 0 then Color.UPTICK else if priceDifference < 0 then Color.DOWNTICK else Color.GRAY);

# This is to visually encapsulate a week because weekend price movements can be funky
def isStartOfWeek = if GetDayOfWeek(GetYYYYMMDD()) == 1 then yes else no;
def isEndOfWeek = if GetDayOfWeek(GetYYYYMMDD()) == 5 then yes else no;

AddVerticalLine(isStartOfWeek AND SecondsTillTime(marketOpen) == 0, "Monday Open", Color.GREEN);
AddVerticalLine(isEndOfWeek AND SecondsTillTime(marketClose) == 0, "Friday Close", Color.RED);``````

Credit:

#### Vimal Mittal

##### Member
@alphasandstorm I found this script for premarket scan if someone can transform in TOS ?

Code:
``````//@version=4
study("PMAI", overlay = false)

tickerid = syminfo.tickerid
//bgcolor(color=#FFFFFF, transp=70)

yesterday_close = security(tickerid, "D",close[1])
today_open = security(tickerid, "D", open)
today_close = security(tickerid, "D", close)

full = ((today_close-yesterday_close)/yesterday_close)*100
prem = ((today_open-yesterday_close)/yesterday_close)*100

plot(intrad, style=5, title="Intra-Day Return", linewidth=1, color=#FC6404, transp=0)//red
plot(prem, style=5, title="Pre-Market Return", linewidth=1, color=#228CBB, transp=60)//blue
plot(full, style=2, title="Total Return", linewidth=2, color=#000000) //black``````

How can I use this premarket script in my watchlist column? That would be lot easier to find stocks in my watchlist where relative volume is up . Can you write for watchlist?

#### vikram

##### New member
VIP
May I know if it's possible to get Relative Pre market buy volume in the watch list ?

#### Robby Luca

##### New member
Hello,

I am trying to get the Relative Volume during afterhours/premarket but I have been unsuccessful.
What I am looking for exactly is a script that will give me the current day volume (including premarket) divided by the Daily Average Volume of last 60 days.

My initial script:

Code:
``````input length = 60;
def avgvol = average(volume, length)[1];
plot rv = round(volume/avgvol,2);``````

The issue I encounter is that if I put Day Timeframe then during the extended hours, it does not calculate the current volume (during premarket).
Then if I put 1m Timeframe then it will be the average volume of last 60 minutes.

So I attempted using aggregation:

Code:
``````input length = 60;
def agg = aggregationPeriod.DAY;
def avgvol = average(volume(period = agg), length)[1];
plot rv = round(volume(period = agg)/avgvol,2);``````

No success. Any suggestions? Thank you!

#### RickAns

##### Active member
VIP
Robby, the poster Sonny has a few threads dealing with Relative Volume and premarket happenings. The code you posted is very similar to some in his watch list thread https://usethinkscript.com/threads/color-coded-watchlist-columns-for-thinkorswim.2977/ . If you have not seen it then it might interest you.

Similar bit:

Code:
``````RV1
#Relative Volume
def x = Average(volume, 60)[1];
def v = volume;
plot r = Round((v/x),1);
r.assignValueColor(if r >= 20 then color.CYAN else if r>=5 then createcolor(255,153,153) else createcolor(0,215,0));``````

#### Robby Luca

##### New member
That's the exact one I posted and used but like I mentioned does not work in premarket. Will check out the thread.

#### malone1020

##### Member
VIP
I have ended up with three different Relative Volume Watchlist codes and am now confused how these differ. Anyone willing to help me decipher these three? Is it just as simple as 1 = 60 days benchmark, 2 = 50 days and 3 = 10 days?

Thank you!

1. -------------------------------
Code:
``````## OneNote Archive Name: Relative Volume to past days Average Relative Volume _v02_Mobius
## Suggested Tos Name using JQ naming convention: RelVolumeVsAvgRelVolume_v02_Mobius
## OneNote Section: Volume
## Archive Date: 02.10.2019
## Provenance: Posted by Mobius himself in the Lounge on 02.05.2019

# Relative Volume to past days Average Relative Volume
# Mobius
# Chat Room Request 01.23.2017

input length = 60;

def v = volume(period = AggregationPeriod.Day);
def active = if secondsFromTime(0930) > 0 and
secondsTillTime(1600) >= 0
then 1
else 0;
/ GetAggregationPeriod());
def newDay = GetDay() != GetDay()[1];
def DayVol = if newDay
then v
else DayVol[1] + v;
def avgVolume = (fold day = 0 to length
with s
do s + GetValue(DayVol, day * Bars, (length - 1) * Bars)) / length;

plot R = DayVol / avgVolume * 100;
R.SetPaintingStrategy(PaintingStrategy.Histogram);
R.AssignValueColor(if R > 100
then color.green
else color.red);
# End Code``````

2. ------------------------------------------

Code:
``````# rvol watchlist column
# 10-31-2019

def volumeAvgPeriodDays = 50;
def volumeAvg = (fold index=1 to volumeAvgPeriodDays+1 with acc=1 do acc+volume(period="DAY")[index]) / volumeAvgPeriodDays;

def volumeToday = volume(period="DAY");

plot a = Round((volumeToday / volumeAvg), 2);``````

3. ---------------------------------------------------------------------------------

Code:
``````input STime = 0930 ; #hint STime: Start of normal trading hours
input ETime = 1600 ; #hint ETime: End of normal trading hours
input thold = 1.5 ; #hint told: Threshold for color break
def bpd = 26 ; # Bars per day during active trading hours
def daycnt = 10; # Number of days

def IsActive = if secondsTillTime(ETime) > 0 and
secondsFromTime(STime) >= 0
then 1
else 0;

# This is the cumulative volume for the current day between STime and ETime
def cumVolume = if IsActive and !IsActive[1]
then volume[1]
else if IsActive then cumVolume[1] + volume[1]
else cumVolume[1];

# This is the average cumulative volume over the same time period from the previous 10 days
def cumVolumePrev = if IsActive and !IsActive[1]
then (volume[(bpd*1)+1] + volume[(bpd*2)+1] + volume[(bpd*3)+1] + volume[(bpd*4)+1] + volume[(bpd*5)+1] + volume[(bpd*6)+1] + volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1] + volume[(bpd*10)+1] ) / daycnt
else if IsActive then cumVolumePrev[1] + (volume[(bpd*1)+1] + volume[(bpd*2)+1] + volume[(bpd*3)+1] + volume[(bpd*4)+1] + volume[(bpd*5)+1] + volume[(bpd*6)+1] + volume[(bpd*7)+1] + volume[(bpd*8)+1] + volume[(bpd*9)+1] + volume[(bpd*10)+1] ) / daycnt
else cumVolumePrev[1];

plot RelVol = cumVolume / cumVolumePrev ;

RelVol.assignValueColor(if RelVol > thold then CreateColor(0,128,0) else CreateColor(128,0,0) );``````

#### Think_Tank

##### New member
Happy day All!
I'm looking for a code for pre market relative volume scanner that averages the past "n" days, any help would be greatly appreciated!

Cheers!