Up and Down Volume Accumulator for ThinkorSwim

korygill

Well-known 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:

Join useThinkScript to post your question to a community of 21,000+ developers and traders.

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
295 Online
Create Post

The Market Trading Game Changer

Join 2,500+ subscribers inside the useThinkScript VIP Membership Club
  • Exclusive indicators
  • Proven strategies & setups
  • Private Discord community
  • ‘Buy The Dip’ signal alerts
  • Exclusive members-only content
  • Add-ons and resources
  • 1 full year of unlimited support

Frequently Asked Questions

What is useThinkScript?

useThinkScript is the #1 community of stock market investors using indicators and other tools to power their trading strategies. Traders of all skill levels use our forums to learn about scripting and indicators, help each other, and discover new ways to gain an edge in the markets.

How do I get started?

We get it. Our forum can be intimidating, if not overwhelming. With thousands of topics, tens of thousands of posts, our community has created an incredibly deep knowledge base for stock traders. No one can ever exhaust every resource provided on our site.

If you are new, or just looking for guidance, here are some helpful links to get you started.

What are the benefits of VIP Membership?
VIP members get exclusive access to these proven and tested premium indicators: Buy the Dip, Advanced Market Moves 2.0, Take Profit, and Volatility Trading Range. In addition, VIP members get access to over 50 VIP-only custom indicators, add-ons, and strategies, private VIP-only forums, private Discord channel to discuss trades and strategies in real-time, customer support, trade alerts, and much more. Learn all about VIP membership here.
How can I access the premium indicators?
To access the premium indicators, which are plug and play ready, sign up for VIP membership here.
Back
Top