Folding: integer 'to' is expected: NaN

muscleman

New member
I am writing an indicator that will mark the last swing low within 100 bars and swing high after it. Right now I have the following code. It kinda outputs what I wanted so far, but the warning on the chart says "Folding: integer 'to' is expected: NaN", which makes me wonder if there is anything wrong I did. Can anyone please help?

Code:
def condition;
def swingLowOffset;
def swingHighOffset;
def swingHigh;
def currentLow; # lowest in the last 3 bars
def currentHigh; # highest in the last 3 bars

if (IsNaN(close[-1] and !IsNaN(close[0]))) {
    # Only run this in the last bar.
    # Find the lowest bar in the last 100 bars excluding the current 3 bars
    swingLowOffset = GetMinValueOffset(low[3], 100) + 3;
    currentLow = Lowest(low, 3);
    currentHigh = Highest(high, 3);
    swingHigh = fold i = 0 to swingLowOffset with tempHigh = high[0] do Max(tempHigh, GetValue(high, i));
    swingHighOffset = 0;
    if (swingHighOffset == swingLowOffset or swingHighOffset <= 2) {
        condition = no;
    }
    else {

        condition = yes;
    }
}
else {
    condition = no;
    swingLowOffset = 0;
    swingHighOffset = 0;
    swingHigh = 0;
    currentLow = 0;
    currentHigh = 0;
}
AddChartBubble(1, low, BarNumber(), Color.WHITE, no);
AddChartBubble(swingLowOffset != 0, high, swingLowOffset + ":" + swingHighOffset + ":" + swingHigh, Color.WHITE, no);
plot data = condition;
 
Last edited:
I can not reproduce the error. My original suspicion was that you may be running the script on a chart that doesn't have 100+ bars of price history, but in that case, GetMinValueOffset() already throws the assertion before Fold does. Can you provide more context, such the exact symbol, aggregation, and chart interval - or whether its for a scan, chart, or column?

Thinkscript is quite fluid in terms of resolving expressions in place of variables, assuming a constant is not expected.

fold i = 0 to (if isNAN(swingLowOffset) then 0 else swingLowOffset) with tempHigh = high do Max(tempHigh, GetValue(high, i))

Although, this may just end pushing the error to another line down the road, its just something to keep in mind.
 

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

I am writing an indicator that will mark the last swing low within 100 bars and swing high after it. Right now I have the following code. It kinda outputs what I wanted so far, but the warning on the chart says "Folding: integer 'to' is expected: NaN", which makes me wonder if there is anything wrong I did. Can anyone please help?

Code:
def condition;
def swingLowOffset;
def swingHighOffset;
def swingHigh;
def currentLow; # lowest in the last 3 bars
def currentHigh; # highest in the last 3 bars

if (IsNaN(close[-1] and !IsNaN(close[0]))) {
    # Only run this in the last bar.
    # Find the lowest bar in the last 100 bars excluding the current 3 bars
    swingLowOffset = GetMinValueOffset(low[3], 100) + 3;
    currentLow = Lowest(low, 3);
    currentHigh = Highest(high, 3);
    swingHigh = fold i = 0 to swingLowOffset with tempHigh = high[0] do Max(tempHigh, GetValue(high, i));
    swingHighOffset = 0;
    if (swingHighOffset == swingLowOffset or swingHighOffset <= 2) {
        condition = no;
    }
    else {

        condition = yes;
    }
}
else {
    condition = no;
    swingLowOffset = 0;
    swingHighOffset = 0;
    swingHigh = 0;
    currentLow = 0;
    currentHigh = 0;
}
AddChartBubble(1, low, BarNumber(), Color.WHITE, no);
AddChartBubble(swingLowOffset != 0, high, swingLowOffset + ":" + swingHighOffset + ":" + swingHigh, Color.WHITE, no);
plot data = condition;


...last swing low within 100 bars and swing high after it.

what happens if there isn't a swing high after the last swing low?

i would look for the last pattern, swing high.
save the barnumber of it.

then look for a swing low, that has the highest barnumber AND is less than the barnumber of the swing high.
 
The warning I see is an exclamation mark on the upper left of the chart. It is not an error. Only a warning. I think what it wants to warn is that fold's "to" needs to be an integer but I passed in a variable so it is not sure if it is an integer, But since I get this variable value from GetMinValueOffset call, I know it is good. But thinkscript doesn't have the notion of an integer as Double is the customer defined variable type.
Anyway.. thank you for responding to my question. It is a warning so it doesn't really impact the functionality of the script.
 
@muscleman
It's is your expansion area causing the issue.
Change your definition of 'swingLowOffset' to:
Ruby:
swingLowOffset = if !isnan(close) then GetMinValueOffset(low[3], 100) + 3 else 0;
or you could just change your expansion area to zero.
 

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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