How do I code a for loop?

Bassindora

Member
VIP
Can someone direct me to example code of a for loop?

I would like to look at the value of a variable that holds a value that I previously assigned for x (input from the user) number of bars.

I thought it would look something like this:

for loopvar = 0 to x
if signal[x] = 1 then do something
else do something else
increment loopvar

I have seen while and fold statements and am having difficulty deciphering them for my use.

Any help is appreciated.
 
Solution
Can someone direct me to example code of a for loop?

I would like to look at the value of a variable that holds a value that I previously assigned for x (input from the user) number of bars.

I thought it would look something like this:

for loopvar = 0 to x
if signal[x] = 1 then do something
else do something else
increment loopvar

I have seen while and fold statements and am having difficulty deciphering them for my use.

Any help is appreciated.


fold can be a confusing topic to learn. it took me quite awhile , then one day it clicked. i just kept reading and trying dozens of fold examples and adding a bubble to display some values.

this helped a lot...
Can someone direct me to example code of a for loop?

I would like to look at the value of a variable that holds a value that I previously assigned for x (input from the user) number of bars.

I thought it would look something like this:

for loopvar = 0 to x
if signal[x] = 1 then do something
else do something else
increment loopvar

I have seen while and fold statements and am having difficulty deciphering them for my use.

Any help is appreciated.


fold can be a confusing topic to learn. it took me quite awhile , then one day it clicked. i just kept reading and trying dozens of fold examples and adding a bubble to display some values.

this helped a lot.
https://jshingler.github.io/TOS-and-Thinkscript-Snippet-Collection/TOS & Thinkscript Collection.html#the-fold-function-explained

https://usethinkscript.com/threads/fold-function-learning.16047/


---------------

i don't understand your description.
you crammed everything into 1 sentence. spread it out. write several short statements/rules.
don't use variable names, keep it general.
when you are able to explain what you want, it will help you understand how to do it.

first thing is, you need to think about what you want to see on the chart.
..do you want to draw something on a bar in the past (before the last bar) , that has a certain value?
..or just read old values, find the bar with desired value, and do something on current bar?
this will help to set up the loop, to look at old bars or future bars.

what if there are multiple bars with the desired signal? just act on the first one found? do something on each bar found? add up some numbers?

do you want to run the loop on every bar? or just on the last bar?


-----------------

my guess,

# on every bar,
# read from some past bars,
# look for a desired value in signal,
# add up the quantity of bars that had a desired signal

# user input. how many bars back from current bar. (including current bar)



Code:
# fold_help_3

#https://usethinkscript.com/threads/how-do-i-code-a-for-loop.22279/
#How do I code a for loop?
#Bassindora  3/28


#Can someone direct me to example code of a for loop?

#I would like to look at the value of a variable that holds a value that I previously assigned for x (input from the user) number of bars.

#I thought it would look something like this:

#for loopvar = 0 to x
#if signal[x] = 1 then do something
#else do something else
#increment loopvar

#I have seen while and fold statements and am having difficulty deciphering them for my use.



#   my guess,
# on every bar,
#  read from some past bars,
#   look for a desired value in signal,
#    add up the quantity of bars that had a desired signal


# how many bars back to read data
input bars_back = 5;
# current bar , and past 4 bars


# define signal variable ,  0 or 1
# generate random numbers for this test
def signal = round((random() * 1) - 0, 0);

# display the values below each bar
addchartbubble(1, low*0.998,
signal
, color.cyan, 0);


def desired_value = 1;


# count the past bars with the desired value

def cnt = fold a = 0 to bars_back
#  create the loop. pick a letter to use , a
# start with offset 0, so it looks at the current bar
# when the loop counter 'a' reaches the end number, bars_back, it stops. it does not process the loop with a value of bars_back for a.
 with b
#  pick a letter to use as a temporary holder of data for the loop
 while !isnan(getvalue(close, a))
#  check if trying to read data from non existant bars. (if on the 5th bar, and try to read data from 10 bars back)
#  if  not an error , then keep looping
 do b + (if getvalue(signal, a) == desired_value then 1 else 0);
#  read a value from a past bar, and compare it to a value. if equal then increase the value of b
#  when the loop is all done , cnt will equal the value of b, the quantity of desired values


# how many of the 4 past bars , and current bar, had a value of 1
addchartbubble(1, high*1.001,
cnt
, color.yellow, 1);

#

in the image, it is looking at 5 bars, the past 4 past bars and the current bar.
the blue bubbles are the signal
the yellow bubbles are the counts
.
 

Attachments

  • img1.png
    img1.png
    68.8 KB · Views: 11
Last edited:
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
1242 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