What Is The Proper Use of barNumber()?

joseraul

New member
Hi!

I'm trying to build a vwap that can ignore the first bars "under demand".
This is what I have so far:

Code:
input ignoreBars = 5;

def sinceBar = barNumber() - ignoreBars;
def priceVolume = close * volume;
def cumulativePriceVolume = sum(priceVolume, sinceBar);
def cumulativeVolume = sum(volume, sinceBar);
def vwapValue = cumulativePriceVolume / cumulativeVolume;

plot vwapPlot = vwapValue;

Apparently, "sum" doesn't like the dynamic "sinceBar" and it fails with "Only constants expected here"

Does anyone have an idea of how to fix this? Or maybe a better approach?
Thanks in advance!

PS: I'm pretty new on Thinkscript world, so I don't know if what I'm asking is very obvious :)
 

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

Hi, I’m not a coder so sorry if I’m asking a dumb question. I am trying to get the number of bars for today only to pop up on a label but my script currently has “barnumber()” and if I have a 3 day 15 min time frame it will all the bars in the last 3 days. Is there any way to show just today’s barnumbers w/0 premarket? Need this number for a formula. Thanks for any help.
 
Hi, I’m not a coder so sorry if I’m asking a dumb question. I am trying to get the number of bars for today only to pop up on a label but my script currently has “barnumber()” and if I have a 3 day 15 min time frame it will all the bars in the last 3 days. Is there any way to show just today’s barnumbers w/0 premarket? Need this number for a formula. Thanks for any help.

this will show 2 labels. one of them will have the quantity of bars in a normal trading day.

Code:
def bn = barnumber();
def price = close;
input chart_stat_labels = yes;

# 390min in a day
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
AddLabel(chart_stat_labels, "chartmin " + chartmin, Color.MAGENTA);

def daybarqty = roundup(390 / chartmin, 0);
AddLabel(chart_stat_labels, "bars per day " + daybarqty, Color.MAGENTA);
 
  • Like
Reactions: Nes
this will show 2 labels. one of them will have the quantity of bars in a normal trading day.

Code:
def bn = barnumber();
def price = close;
input chart_stat_labels = yes;

# 390min in a day
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
AddLabel(chart_stat_labels, "chartmin " + chartmin, Color.MAGENTA);

def daybarqty = roundup(390 / chartmin, 0);
AddLabel(chart_stat_labels, "bars per day " + daybarqty, Color.MAGENTA);

Thank you! It worked! Is there any way to get the barnumber live as the trading day progresses? For example, instead of showing the set number of bars in a day for a specific time frame it will show ( on a 5 min ) bars for day = 1 from 6:30 to 6:35, then from 6:35 to 6:40 it would show bars for day = 2, then from 6:40 to 6:45 it would show bars for day = 3 etc...
 
Last edited:
Thank you! It worked! Is there any way to get the barnumber live as the trading day progresses? For example, instead of showing the set number of bars in a day for a specific time frame it will show ( on a 5 min ) bars for day = 1 from 6:30 to 6:35, then from 6:35 to 6:40 it would show bars for day = 2, then from 6:40 to 6:45 it would show bars for day = 3 etc...
Sorry I didnt explain as well in the first msg.
 
Thank you! It worked! Is there any way to get the barnumber live as the trading day progresses? For example, instead of showing the set number of bars in a day for a specific time frame it will show ( on a 5 min ) bars for day = 1 from 6:30 to 6:35, then from 6:35 to 6:40 it would show bars for day = 2, then from 6:40 to 6:45 it would show bars for day = 3 etc...

this will,
..calculate the quantity of bars/day, for any time less than DAY.
..show a label , with the current bar# of the day
..show bubbles under the candles, with current bar# ( can turn off with an input variable)

Ruby:
# chart_time_stats2

def bn = barnumber();
def istoday = if GetLastDay() == GetDay() then 1 else 0;
def price = close;
input chart_stat_labels = yes;

# 390min in a day
def chartagg = GetAggregationPeriod();
def chartmin = (chartagg / 1000) / 60;
AddLabel(chart_stat_labels, "chartmin " + chartmin, Color.MAGENTA);

def minz;
if chartmin == 60 then {
 minz = 420;
} else if chartmin == 120 then {
 minz = 480;
} else if chartmin == 240 then {
 minz = 480;
} else {
minz = 390;
}

def daybarqty = roundup(minz / chartmin, 0);
AddLabel(chart_stat_labels, "bars per day " + daybarqty, Color.MAGENTA);

input start = 0930;
def mins = (secondsfromTime(start)/60);

def current_bar = ((mins/minz) * daybarqty) + 1;

addlabel(1, "bar # " + current_bar + " / " + daybarqty, color.cyan);

input test1_barcnt = yes;
addchartbubble(test1_barcnt, low,
 current_bar  + "\n" +
 daybarqty
, color.cyan, no);

input test2 = no;
addchartbubble(test2, low,
 mins + "\n" +
# (mins/390)  + "\n" +
 (mins/minz)  + "\n" +
 current_bar  + "\n" +
 daybarqty
, color.cyan, no);
#
 
  • Like
Reactions: Nes

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

87k+ Posts
196 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