cant use var in sum() :(

tatl

Member
i need to count bars until a certain condition is met, and then addLabel a sum of the counted bars the next bar after.

there is no telling what the final count will be, it depends on the condition.

but i've learned that sum's length input only accepts constants:( i cant feed it a var based on the count.

is there anything to be done?
 
i need to count bars until a certain condition is met, and then addLabel a sum of the counted bars the next bar after.

there is no telling what the final count will be, it depends on the condition.

but i've learned that sum's length input only accepts constants:( i cant feed it a var based on the count.

is there anything to be done?

instead of fixating on a function, to fix a problem that isn't defined,

list out the conditions that the study should follow.
be as precise and short as possible.

what happens to Start counting bars?
What happens to stop counting bars?

maybe saving start and stop barnumbers, then subtracting them, could give the quantity of bars between start and stop?

partial code...
def startbn = if start then barnumber() else startbn[1];
def bars = if stop then barnumber() - startbn else bars[1];
 

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

instead of fixating on a function, to fix a problem that isn't defined,

list out the conditions that the study should follow.
be as precise and short as possible.

what happens to Start counting bars?
What happens to stop counting bars?

maybe saving start and stop barnumbers, then subtracting them, could give the quantity of bars between start and stop?

partial code...
def startbn = if start then barnumber() else startbn[1];
def bars = if stop then barnumber() - startbn else bars[1];
thx for reply.

so

it's a indicator for the 1-tick chart. on the 1-tick chart, bars are obviously ticks.

if you're trying to synchronize the 1-tick chart with a 311tick chart for example, you can make a recurrent sum (of say, volume) every 311 bars and print that as a bubble.

however, the last 311-tick-cycle of the current trading session session gets truncated at 4pm. it doesn't finish its full cycle. it needs to start back at 1.

so i need the counter to stop at 4pm, count the amount of bars btwn 4pm and the last 311sumprint, and then sum the volumes of each tick to make a truncated volume sum.

i got the counter to stop at 4pm, and find the difference btwn the 4pm barnumber() and the last printed barnumber() ... i just cant plug that truncation value into the sum() length argument since it requires a constant. and the truncation value will always be different depending on the chart etc.
 
thx for reply.

so

it's a indicator for the 1-tick chart. on the 1-tick chart, bars are obviously ticks.

if you're trying to synchronize the 1-tick chart with a 311tick chart for example, you can make a recurrent sum (of say, volume) every 311 bars and print that as a bubble.

however, the last 311-tick-cycle of the current trading session session gets truncated at 4pm. it doesn't finish its full cycle. it needs to start back at 1.

so i need the counter to stop at 4pm, count the amount of bars btwn 4pm and the last 311sumprint, and then sum the volumes of each tick to make a truncated volume sum.

i got the counter to stop at 4pm, and find the difference btwn the 4pm barnumber() and the last printed barnumber() ... i just cant plug that truncation value into the sum() length argument since it requires a constant. and the truncation value will always be different depending on the chart etc.


Have you tried "BarNumber() % 311 = 0" and SecondsFromTime(1700) >= 0 and SecondsToTime(1700) <= 0"?

Using that (remainder or modulo function --- the "%" sign) will tell you when you have exactly enough bars to be divided by 311 (or whatever number) and you can use seconds from / to time to reset the count. You could use that as the condition to add the bubble. (times are EST for the time functions FYI).
 
Have you tried "BarNumber() % 311 = 0" and SecondsFromTime(1700) >= 0 and SecondsToTime(1700) <= 0"?

Using that (remainder or modulo function --- the "%" sign) will tell you when you have exactly enough bars to be divided by 311 (or whatever number) and you can use seconds from / to time to reset the count. You could use that as the condition to add the bubble. (times are EST for the time functions FYI).
thx for the try. all the counting is taken care of, this part i've already got fine. the problem is that at 4pm, 8pm, 7am, and 9:30am, the counter needs to reset to 1 without completing it's cycle of 311. i have that part fine as well. the only issue is i then need to sum the volume for that truncated amount of ticks. say it's 114. i can't plug 114 into the sum(volume, bars) because "bars" expects a constant. but my "114" changes depending on where the cycle had to get cut off. so it has to be a variable. and sum() won't accept a variable as a "length" input.
 
thx for the try. all the counting is taken care of, this part i've already got fine. the problem is that at 4pm, 8pm, 7am, and 9:30am, the counter needs to reset to 1 without completing it's cycle of 311. i have that part fine as well. the only issue is i then need to sum the volume for that truncated amount of ticks. say it's 114. i can't plug 114 into the sum(volume, bars) because "bars" expects a constant. but my "114" changes depending on where the cycle had to get cut off. so it has to be a variable. and sum() won't accept a variable as a "length" input.
I have a solution for you, but I don't have time to code it up right now. hopefully saturday afternoon or sunday afternoon will be free for something like this.

-mashume
 
thx for reply.

so

it's a indicator for the 1-tick chart. on the 1-tick chart, bars are obviously ticks.

if you're trying to synchronize the 1-tick chart with a 311tick chart for example, you can make a recurrent sum (of say, volume) every 311 bars and print that as a bubble.

however, the last 311-tick-cycle of the current trading session session gets truncated at 4pm. it doesn't finish its full cycle. it needs to start back at 1.

so i need the counter to stop at 4pm, count the amount of bars btwn 4pm and the last 311sumprint, and then sum the volumes of each tick to make a truncated volume sum.

i got the counter to stop at 4pm, and find the difference btwn the 4pm barnumber() and the last printed barnumber() ... i just cant plug that truncation value into the sum() length argument since it requires a constant. and the truncation value will always be different depending on the chart etc.

sorry, i am not familiar with tick charts.
 
It could look something like this:
Code:
declare lower;

def bar = BarNumber();
def first_mark = if secondsFromTime(0930) >= 0 and secondsFromTime(0930)[1] < 0 then 1 else 0;
def second_mark = if secondsFromTime(0700) >= 0 and secondsFromTime(0700)[1] < 0 then 1 else 0;
def third_mark = if secondsFromTime(1600) >= 0 and secondsFromTime(1600)[1] < 0 then 1 else 0;
def fourth_mark = if secondsFromTime(2000) >= 0 and secondsFromTime(2000)[1] < 0 then 1 else 0;
def bar_mark = if bar % 311 == 0 then 1 else 0;

def reset = if first_mark == 1 OR second_mark == 1 or third_mark == 1 or fourth_mark == 1 or bar_mark == 1 then 1 else 0;

def counter = if reset == 1 then 1 else counter[1] + 1;

plot three_eleven_count = counter;

Note what we've done and why: exact times seldom fall on bar boundaries on tick charts. So instead of using an exact time (secondsFromTime(x) == 0) we use a pair of bars to decide whether the time fell between this bar and the last bar. When that condition is met, we make the series == 1 (true) and go on.

We set a "reset" variable to be True (1) whenever one of the _mark variables is True.

Then we reset the counter when the reset tells us to (when it is True)

-mashume
 
It could look something like this:
Code:
declare lower;

def bar = BarNumber();
def first_mark = if secondsFromTime(0930) >= 0 and secondsFromTime(0930)[1] < 0 then 1 else 0;
def second_mark = if secondsFromTime(0700) >= 0 and secondsFromTime(0700)[1] < 0 then 1 else 0;
def third_mark = if secondsFromTime(1600) >= 0 and secondsFromTime(1600)[1] < 0 then 1 else 0;
def fourth_mark = if secondsFromTime(2000) >= 0 and secondsFromTime(2000)[1] < 0 then 1 else 0;
def bar_mark = if bar % 311 == 0 then 1 else 0;

def reset = if first_mark == 1 OR second_mark == 1 or third_mark == 1 or fourth_mark == 1 or bar_mark == 1 then 1 else 0;

def counter = if reset == 1 then 1 else counter[1] + 1;

plot three_eleven_count = counter;

Note what we've done and why: exact times seldom fall on bar boundaries on tick charts. So instead of using an exact time (secondsFromTime(x) == 0) we use a pair of bars to decide whether the time fell between this bar and the last bar. When that condition is met, we make the series == 1 (true) and go on.

We set a "reset" variable to be True (1) whenever one of the _mark variables is True.

Then we reset the counter when the reset tells us to (when it is True)

-mashume
yes, indeed. thank you. this part i had though.

here is my problem, i'm not allowed to do this:

def volumeSum = sum(volume, counter);
AddChartBubble(reset, high, volumeSum, Color.YELLOW, no);

can't do this because sum() won't accept counter as a length argument.

i am limited to doing:

def volumeSum = sum(volume, 311);
addChartBubble(reset, high, volumeSum, color.YELLOW, no);

which means that the candles that Dont last 311 ticks, but are Cut Off by 9:30am, 4:am etc, and last for Fewer Ticks, i get the wrong volume sum for the candle.
 
yes, indeed. thank you. this part i had though.

here is my problem, i'm not allowed to do this:

def volumeSum = sum(volume, counter);
AddChartBubble(reset, high, volumeSum, Color.YELLOW, no);

can't do this because sum() won't accept counter as a length argument.

i am limited to doing:

def volumeSum = sum(volume, 311);
addChartBubble(reset, high, volumeSum, color.YELLOW, no);

which means that the candles that Dont last 311 ticks, but are Cut Off by 9:30am, 4:am etc, and last for Fewer Ticks, i get the wrong volume sum for the candle.
Don't use sum()

do your volume sum this way:
Code:
def volumeSum = if reset != 1 then volumeSum[1] + VOLUME else 0;

-mashume
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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