Infinite Loop

Human

Member
I am calculating some values that continue into the future. I don't have any code that ends the loop. It seems to work without any problems. I think I have read in the past that this is not a good practice for some reason. I searched for posts about this but have not found anything addressing this situation. I am sure I can figure out how to stop plotting/calculating infinitely but I want to leave it as it is and not add needless lines of code. Any advice?
 
I am calculating some values that continue into the future. I don't have any code that ends the loop. It seems to work without any problems. I think I have read in the past that this is not a good practice for some reason. I searched for posts about this but have not found anything addressing this situation. I am sure I can figure out how to stop plotting/calculating infinitely but I want to leave it as it is and not add needless lines of code. Any advice?
post the code

use 'while' , in fold loops. remember to use getvalue() as needed.
while something is true, keep looping. ( use a huge number as the to #)


example that uses while
https://usethinkscript.com/threads/...quantities-of-smaller-bars.15055/#post-123021
 

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

Thanks for the quick answer. I will study the code. I am currently using an if then else statement. Should I have a concern about plotting into the future?
oh, you said loop, so that implies using fold. which is why i said what i said.

if all you have is an if then, then we will have to see it to get a better idea of what you have.

issue with future plots? what does that mean, after the last bar? that is doable.
there won't be any candles or price data, so you have to configure a variable to hold some price values for things to plot. again, need something more specific before guessing any more.
 
oh, you said loop, so that implies using fold. which is why i said what i said.

if all you have is an if then, then we will have to see it to get a better idea of what you have.

issue with future plots? what does that mean, after the last bar? that is doable.
there won't be any candles or price data, so you have to configure a variable to hold some price values for things to plot. again, need something more specific before guessing any more.
Actually, my questions relate to what I learned from you here:

https://usethinkscript.com/threads/expansion-area-ma-lines-bubble.11201/post-97853

I used the method in a few different ways. The code below is just a focused example and the plot is to show what it is doing. It is for diagnostic purposes only. Here is the little example:

Code:
# get price from last bar, and keep
def lastclose = if barnumber() == 1 then 0 else if lastbar then close else lastclose[1];
plot LastClosePlot = LastClose;

My question is about the plot going into the future. It does not stop. I understand that the fold function should not keep looping, but does this self-referencing code cause problems in some way?
 
Actually, my questions relate to what I learned from you here:

https://usethinkscript.com/threads/expansion-area-ma-lines-bubble.11201/post-97853

I used the method in a few different ways. The code below is just a focused example and the plot is to show what it is doing. It is for diagnostic purposes only. Here is the little example:

Code:
# get price from last bar, and keep
def lastclose = if barnumber() == 1 then 0 else if lastbar then close else lastclose[1];
plot LastClosePlot = LastClose;

My question is about the plot going into the future. It does not stop. I understand that the fold function should not keep looping, but does this self-referencing code cause problems in some way?

fold has no bearing on this and just adds confusion. there is no fold code example, so to say 'it should stop looping' makes no sense and can't be answered.
a basic fold loop iterates through a set number of loops.
a fold loop can use 'while' to stop the looping before it reaches the 'to' number.

problems? no. it is just another way to draw a line.
reading from a previous bar is common.
plotting after the last candle is fine.
after the last candle, there are placeholders (what i call them) for future bars, that have valid bar numbers, all the way to the right edge of the chart.

here is a test code to show the bar numbers. (untested , TOS is down)
( fixed addchart spelling)

Code:
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
def lastclose = if barnumber() == 1 then close else if lastbar then close else lastclose[1];

addchartbubble(1, lastclose,
bn + "\n" +
lastclose
, color.yellow, no);
 
Last edited:
fold has no bearing on this and just adds confusion. there is no fold code example, so to say 'it should stop looping' makes no sense and can't be answered.
a basic fold loop iterates through a set number of loops.
a fold loop can use 'while' to stop the looping before it reaches the 'to' number.

problems? no. it is just another way to draw a line.
reading from a previous bar is common.
plotting after the last candle is fine.
after the last candle, there are placeholders (what i call them) for future bars, that have valid bar numbers, all the way to the right edge of the chart.

here is a test code to show the bar numbers. (untested , TOS is down)

Code:
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
def lastclose = if barnumber() == 1 then close else if lastbar then close else lastclose[1];

addcahrtbubble(1, lastclose,
bn + "\n" +
lastclose
, color.yellow, no);
TOS is running now. The code illustrates your point. I changed one small thing to make it work. Here is the corrected code.

Code:
def bn = barnumber();
def lastbn = HighestAll(If(IsNaN(close), 0, bn));
def lastbar = if (bn == lastbn) then 1 else 0;
def lastclose = if barnumber() == 1 then close else if lastbar then close else lastclose[1];

addchartbubble(1, lastclose,
bn + "\n" +
lastclose
, color.yellow, no);
 
Last edited:

Similar threads

Not the exact question you're looking for?

Start a new thread and receive assistance from our community.

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