Volume script - adjust existing script

om4

New member
I have the below script
https://usethinkscript.com/threads/time-based-volume-indicator-for-thinkorswim.124/
that looks at average volume over a period of 100 days compared to current bar. i want the volume it gets to only display on the prior bar. can someone adjust the script to only display for previous bar?

# source: Time Based Volume
# original author: Robert Payne

declare lower;

input LookBack = 20;
def nMinutes = GetAggregationPeriod() / 60000;
def nBars = RoundUp(390 / nMinutes, 0);

def pvSum = fold idx = 1 to LookBack + 1 with a=0 do a + GetValue(volume, idx * nBars, LookBack * nBars);

def pvAvg = pvSum / LookBack;
def VolPct = (volume / pvAvg) * 100;

AddLabel(
yes,"Volume Buzz: " +
VolPct,
Color.green
);

plot avgLine = 100;
avgLine.SetDefaultColor(Color.GRAY);
avgLine.SetStyle(Curve.LONG_DASH);

def lastUp = if IsNaN(lastUp[1]) then 0 else if (close > open) then VolPct else lastUp[1];
def lastDn = if IsNaN(lastDn[1]) then 0 else if (close < open) then VolPct else lastDn[1];

plot Vol = VolPct;
Vol.SetPaintingStrategy(PaintingStrategy.HISTOGRAM);
Vol.SetLineWeight(3);
Vol.DefineColor("Bullish", Color.GREEN);
Vol.DefineColor("Bullish Smaller", Color.DARK_GREEN);
Vol.DefineColor("Bearish", Color.RED);
Vol.DefineColor("Bearish Smaller", Color.DARK_RED);
Vol.AssignValueColor(if (close > open) and (VolPct > lastUp[1]) then Vol.Color("bullish" ) else if close > open then Vol.Color("bullish smaller" ) else if close < open and VolPct > lastDn[1] then Vol.Color("bearish" ) else Vol.Color("bearish smaller" ));
AssignPriceColor(if (close > open) and (VolPct > lastUp[1]) then Vol.Color("bullish" ) else if close > open then Vol.Color("bullish smaller" ) else if close < open and VolPct > lastDn[1] then Vol.Color("bearish" ) else Vol.Color("bearish smaller" ));
 
Last edited by a moderator:
Solution
i think you are mistaken about what that study does.
it does not look back at 100 days of volume.

it looks back at 20 days,
( input LookBack = 20; )
reads the volume from ONE candle per day,
( GetValue(volume, idx * nBars )
the same time candle as the current candle.
it adds up those 20 volumes
( do a + getvalue(... )
and calculates an average.
( def pvAvg = pvSum / LookBack; )
You can, perhaps use something like:
Code:
def penultimate_bar = if barNumber() == highestAll(BarNumber() - 1) then 1 else 0;

but I would need to spend more time with your code to know where to put it in

happy trading,
mashume

EDIT
of course, you might also have it this way:
Code:
def penultimate_bar = if isNaN(CLOSE) then 1 else 0;
 
i think you are mistaken about what that study does.
it does not look back at 100 days of volume.

it looks back at 20 days,
( input LookBack = 20; )
reads the volume from ONE candle per day,
( GetValue(volume, idx * nBars )
the same time candle as the current candle.
it adds up those 20 volumes
( do a + getvalue(... )
and calculates an average.
( def pvAvg = pvSum / LookBack; )
 
Solution

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

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
533 Online
Create Post

Similar threads

Similar threads

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