RoundDown(HL2,0) as Loop Count Limit in Fold Not Working

Sesqui

New member
VIP
declare lower;
def Price = HL2;
def limit = RoundDown(Price, 0);
def line = fold count = 0 to limit with p = 0 do p+1;
plot myLine = line;

But myLine = N/A instead of a line representing cumulative values of p+1

However, the following do work:
def limit =RoundDown(10.2,0);
def line = fold count = 0 to limit with p = 0 do p+1;
plot myLine = line;

As well as :
def limit = RoundDown(10.0,0);
def line = fold count = 0 to limit with p = 0 do p+1;
plot myLine = line;

But drawing a blank as to why setting limit = RoundDown(Price,0) returns N/A when used in a fold.

Any insight into why greatly appreciated.

Sesqui
 
Solution
Thanks for such timely insight. Turns out for some reason the problem was caused by the value of limit returned by def limit = RoundDown(Price, 0); was occasionally NaN which disrupted the fold. By including a check for IsNan, the fold worked as desired, as follows:

def limit = if isNaN(RoundDown(Price)) then limit[1] else RoundDown(Price);
The price could be too high, like with certain indices or futures priced in thousands of points, which exceeds the limit on fold iterations. You may also need to prevent it from evaluating the right side expansion area.
 
Last edited:
Thanks for such timely insight. Turns out for some reason the problem was caused by the value of limit returned by def limit = RoundDown(Price, 0); was occasionally NaN which disrupted the fold. By including a check for IsNan, the fold worked as desired, as follows:

def limit = if isNaN(RoundDown(Price)) then limit[1] else RoundDown(Price);
 
Solution
Thanks for such timely insight. Turns out for some reason the problem was caused by the value of limit returned by def limit = RoundDown(Price, 0); was occasionally NaN which disrupted the fold. By including a check for IsNan, the fold worked as desired, as follows:

def limit = if isNaN(RoundDown(Price)) then limit[1] else RoundDown(Price);

it is trying to read a price after the last bar, in the expansion area like joshua said. there is no price there so it comes back NAN.

i was doing some experimenting and came to the same conclusion.
when i restrict limit to be a value from only valid candles, like you did, it works.

Code:
#rounddown_loop_0

declare lower;

def na = double.nan;
def bn = barnumber();
def Price = HL2;

def n = 500;

#def limit = RoundDown(Price, 0);
def limit = floor(Price);
#def limit = Price;
#def limit = floor(close);

#def limit;
# if close == floor(close) then {
# limit = floor(close);
#} else {
# limit = floor(close);
#}

#def limit = if close == floor(close) then close else floor(close);
#def limit = power(floor(close),1);
#def limit = ceil(close);

# this works
#def limit = if isnan(price) then limit[1] else floor(price);


#--------------------------
#def line = fold count = 0 to limit with p = 0 do p + 1;
#def line = fold count = 0 to limit
#  with p = 0
#  do p + 1;


def line;
if isnan(close) then {
#if bn > 195 then {
 line = na;
} else {
 line = fold count = 0 to limit
  with p = 0
  do p + 1;
}

plot myLine = line;

addchartbubble(0, 0,
bn + "\n" +
limit + "\n" +
line
, color.yellow, no);
#
 

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
326 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