Loops, While

Vision

Member
2019 Donor
Does thinkscript allow you to create a loop which is always testing for conditions without using a set number of iterations as required by fold? Could you use multiple switch statements with only one operation in each?
 
Solution
Thanks.

input lookback = 40;
# if while is used, fold loops as long as while statement is true
# when while becomes false, the loop stops, even if fold hasn't reached the highest count value
def bigbarcnt = fold k = 1 to lookback
with p
while (high > getvalue(high, k) and low < getvalue(low, k))
do p + 1;


What happens when lookback reaches 41? Does the while process stop? Does it keep going but start over? To keep this code operating like a traditional while statement and to be safe would a very large lookback number be used so the condition will be found before the lookback is reached?

i updated the link in my post #2.
in that code i ran a loop to 40, to look for a condition, that i thought should happen within 20...
Thanks.

input lookback = 40;
# if while is used, fold loops as long as while statement is true
# when while becomes false, the loop stops, even if fold hasn't reached the highest count value
def bigbarcnt = fold k = 1 to lookback
with p
while (high > getvalue(high, k) and low < getvalue(low, k))
do p + 1;


What happens when lookback reaches 41? Does the while process stop? Does it keep going but start over? To keep this code operating like a traditional while statement and to be safe would a very large lookback number be used so the condition will be found before the lookback is reached?
 
ThinkScript itself is already iterated on a bar by bar basis; all variables, functions, etc., are essentially arrays. The fold terminates at to, the fold can be interrupted prematurely by while. The fold itself executes every bar, so for example, in this particular case, on bar 60 it will fold back to bar 20 unless interrupted. On bar 61 it will fold back to bar 21, and so on.

In the case of a continuous non-terminating loop, you simply rely on the primary iteration itself, combined with logical statements. A variable can always be assigned its prior value, ie X = X[1], to maintain its persistence throughout the primary iteration. This type of operation will naturally begin at the beginning of the chart, and continue as new data arrives indefinitely.

May I ask; what is it exactly that you are trying to do?
 
I'm trying to create: if condition1 and condition2 happened, while condition2 is happening do an action.
In another language it would be easy, use an if then with a while statement. Or maybe use a flag.
 
Thanks.

input lookback = 40;
# if while is used, fold loops as long as while statement is true
# when while becomes false, the loop stops, even if fold hasn't reached the highest count value
def bigbarcnt = fold k = 1 to lookback
with p
while (high > getvalue(high, k) and low < getvalue(low, k))
do p + 1;


What happens when lookback reaches 41? Does the while process stop? Does it keep going but start over? To keep this code operating like a traditional while statement and to be safe would a very large lookback number be used so the condition will be found before the lookback is reached?

i updated the link in my post #2.
in that code i ran a loop to 40, to look for a condition, that i thought should happen within 20 bars.

if you are unsure when a condition would become true to trigger the while statement, yes, you can loop to a big number. i would keep it < 500. experiment to find out what is realistic.
--------------------

lookback is a constant, it won't change. it will be 40 until the user types in a different number.

regarding this, and assuming there is no while statement,

input lookback = 40;
fold k = 1 to lookback...

with a to value of 40,
on each bar, k will count up to 39.
when k=39 , it will process the formulas within the loop, then stop. k won't reach 40. it won't process k being equal to 40.

if a while statement is used in a fold, and the while formula becomes false, the loop will stop and the program will procede to the next code line.
 
Solution

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