Up and Down Volume Accumulator for ThinkorSwim

korygill

korygill

Active member
VIP
This study shows the up and down volume based on a bar's open versus closing price. It resets the counter based on a specified time of day. One could modify this code to reset the counter on a given weekday, once a month, etc. Adding an option for this as input may be a future enhancement, or perhaps someone else on this forum wants to add that.
  • If close is greater than open, add volume to upVolume.
  • If close is less than open, add volume to dnVolume.
  • If close is equal to open, add 1/2 volume to upVolume and dnVolume.
You can set a "mode" to display upVolume and dnVolume as "lines", or as a difference as a histogram.



Here is the code and a link to use in ThinkOrSwim that you can copy/paste into Setup | Open Shared Item.

CODE

Code:
#
# UpDownVolume
#
# Study to indicate volume on up and down bars.
#
# Author: Kory Gill, @korygill
#
# VERSION HISTORY (sortable date and time (your local time is fine), and your initials
# 20200216-1500-KG    - created
# ...
# ...
#

declare lower;
declare once_per_bar;

input startTime = 1015;
input timeOffsetFromEST = -300; #hint timeOffsetFromEST: 24-hour time offset from EST (-300 for PST)
input mode = {default line, histogram};

def nan = double.NaN;
def bn = BarNumber();
def vOpen = open;
def vClose = close;
def vVolume = volume;

def upVolume;
def dnVolume;
def sft = SecondsFromTime(startTime - timeOffsetFromEST);

def resetCounter = if (sft == 0
                       or
                       (sft[1] < 0 and sft > 0)
                      )
                      then 1
                      else 0;

if (bn == 1 or resetCounter)
{
    upVolume = 0;
    dnVolume = 0;
}
else if (vClose > vOpen)
{
    upVolume = upVolume[1] + volume;
    dnVolume = dnVolume[1];
}
else if (vClose < vOpen)
{
    upVolume = upVolume[1];
    dnVolume = dnVolume[1] + volume;
}
else
{
    upVolume = upVolume[1] + volume / 2;
    dnVolume = dnVolume[1] + volume / 2;
}

plot pUpVolume = if (mode == mode.line) then upVolume else nan;
plot pDnVolume = if (mode == mode.line) then dnVolume else nan;;
pUpVolume.SetDefaultColor(Color.Green);
pDnVolume.SetDefaultColor(Color.Red);

def diffVolume = upVolume - dnVolume;
plot pHistogram = if (mode == mode.histogram) then diffVolume else nan;
#pHistogram.SetChartType(ChartType.BAR);
pHistogram.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
phistogram.AssignValueColor(if diffVolume < 0 then Color.Red else Color.Green);

# end of UpDownVolume
Link to use in ThinkOrSwim that you can copy/paste into Setup | Open Shared Item
UpDownVolume_v202002161500 @ http://tos.mx/kPD4coK

Enjoy,
Kory Gill, @korygill
 
Last edited by a moderator:
W

Whiteke12

New member
You wouldn't happen to have this for a tos scan to look at difference over x amount? Thanks in advance.
 
J

john3

Active member
2019 Donor
Warehouse
@korigill Would you please add an option to display it as Up/Down ratio? Thank you.
 

Top