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

Up and Down Volume Accumulator for ThinkorSwim

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.

xTtCu2C.png


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 @ https://tos.mx/kPD4coK

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

john3

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

Code:
#
# Copyright 2014 Scott J. Johnson (https://scottjjohnson.com)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS-IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

#
# UpDownVolumeRatio
#
# Ratio of the volume on up days versus down days. Higher numbers indicate
# more accumulation by large investors which is a positive.  Low numbers
# indicate distribution.
#
input Period = 50;
input MinimumUDVolumeForBuy = 1.00;

def Up = If(close > close[1], volume, 0);
def Down = If(close <= close[1], volume, 0);
def UpAvgVol = MovingAverage(AverageType.SIMPLE, Up, Period);
def DownAvgVol = MovingAverage(AverageType.SIMPLE, Down, Period);

plot UpDownVol = Round(UpAvgVol / DownAvgVol, 2);

UpDownVol.AssignValueColor(Color.CYAN);
UpDownVol.SetLineWeight(3);

# this line represents the minimum U/D Volume for a buy
plot MinUDVolLine = MinimumUDVolumeForBuy;
MinUDVolLine.AssignValueColor(Color.WHITE);
MinUDVolLine.SetLineWeight(2);
MinUDVolLine.HideBubble();
 

Similar threads

Top