How To Create A Loop Until Prior Bars Meet Conditions

joker12

New member
Guys, any idea how loop through last 3 candles ?..I did this what is working:
BUt at the end- if not 3 conditions are fullfiled I would like go throu code again but with open[2], close[2] etc..is here in thinkscript something like for loop?...
Ruby:
def opicka= open[1];
def zachodik= close[1];
#calculate 50% of candle
def midik= (opicka+zachodik)/2;
#measure range of yesterday
def rangik=(zachodik-opicka)/opicka*100;
def ran= if rangik<8 then 0 else 10;
#compare last price with midcandle
def c= ask;
def porovnavacka= (ask/midik-1)*100;
#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
def condition3 = ran == 10;
def fin= if condition3 then 8 else 0;
def tot = condition1+condition2+fin;
#plot z =tot;
 
Last edited by a moderator:
Solution
Guys, any idea how loop through last 3 candles ?..I did this what is working:
BUt at the end- if not 3 conditions are fullfiled I would like go throu code again but with open[2], close[2] etc..is here in thinkscript something like for loop?...
Ruby:
def opicka= open[1];
def zachodik= close[1];
#calculate 50% of candle
def midik= (opicka+zachodik)/2;
#measure range of yesterday
def rangik=(zachodik-opicka)/opicka*100;
def ran= if rangik<8 then 0 else 10;
#compare last price with midcandle
def c= ask;
def porovnavacka= (ask/midik-1)*100;
#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
def condition3 = ran == 10;
def fin= if condition3 then 8 else 0;
def tot =...
i think i understand what you are trying to do, check previous data until tot = 0 ?
but there are some problems,
i think you will have to change conditions 1 and 2.

putting this in a chart study, this line causes an error.
def c= ask;

i changed these 2 lines , using close instead.
def c = close;
def porovnavacka= ( c /midik-1)*100;

these 2 conditions will never both be false. so tot will never be = 0
#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
 
i think i understand what you are trying to do, check previous data until tot = 0 ?
but there are some problems,
i think you will have to change conditions 1 and 2.

putting this in a chart study, this line causes an error.
def c= ask;

i changed these 2 lines , using close instead.
def c = close;
def porovnavacka= ( c /midik-1)*100;

these 2 conditions will never both be false. so tot will never be = 0
#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
Thank you...I do it for watchlist not for chart....
I did it finally during night-without loop..I hope so..will test it today..Basically I just wanted find wider candle from previous 5 candles:
(then in next step I just check if its wide enough)
Ruby:
#find max value from last 5 values
def max=if(rangik1>rangik2) then rangik1 else rangik2;
def max2=if(max>rangik3) then max else rangik3;
def max3=if(max2>rangik4) then max2 else rangik4;
def max4=if(max3>rangik5) then max3 else rangik5;
 
Last edited by a moderator:
Thank you...I do it for watchlist not for chart....
I did it finally during night-without loop..I hope so..will test it today..Basically I just wanted find wider candle from previous 5 candles:
(then in next step I just check if its wide enough)
Ruby:
#find max value from last 5 values
def max=if(rangik1>rangik2) then rangik1 else rangik2;
def max2=if(max>rangik3) then max else rangik3;
def max3=if(max2>rangik4) then max2 else rangik4;
def max4=if(max3>rangik5) then max3 else rangik5;

maybe this will help when you want to try using a fold loop.

here is a study that looks at x previous bars and finds how many consecutive bars are smaller than the current bar.
the Conditions are after the while parameter so when it becomes false, it stops.
https://usethinkscript.com/threads/...st-65-most-recent-days.7483/page-2#post-72474

engulfing bar, loop
it finds previous consecutive smaller bars
while (high > getvalue(high, k) and low < getvalue(low, k))

you could change the > < , so that line looks for bigger bars.
find previous larger bars
while (high < getvalue(high, k) and low > getvalue(low, k))
 
wouldnt you know if there is some function to stop loop at certain time?lets say I runn it 2 hours after open , or 3, or 4 but still I want to stop it at open...never coded anything with fime function so far...

it is be possible, to process a formula, only during a specific time period, or ignore it.

an example outline, not actual code

x = true if , current bar is between time1 and time2
if x then
...do desired formula
else
...do something else ( or nothing)
 
Thank you...I do it for watchlist not for chart....
I did it finally during night-without loop..I hope so..will test it today..Basically I just wanted find wider candle from previous 5 candles:
(then in next step I just check if its wide enough)
Ruby:
#find max value from last 5 values
def max=if(rangik1>rangik2) then rangik1 else rangik2;
def max2=if(max>rangik3) then max else rangik3;
def max3=if(max2>rangik4) then max2 else rangik4;
def max4=if(max3>rangik5) then max3 else rangik5;

# these 4 lines

#find max value from last 5 values
#def max=if(rangik1>rangik2) then rangik1 else rangik2;
#def max2=if(max>rangik3) then max else rangik3;
#def max3=if(max2>rangik4) then max2 else rangik4;
#def max4=if(max3>rangik5) then max3 else rangik5;


# can be reduced to this

def max = max(rangik1 , rangik2);
def max2 = max(max , rangik3);
def max3 = max(max2 , rangik4);
def max4 = max(max3 , rangik5);
 
Guys, any idea how loop through last 3 candles ?..I did this what is working:
BUt at the end- if not 3 conditions are fullfiled I would like go throu code again but with open[2], close[2] etc..is here in thinkscript something like for loop?...
Ruby:
def opicka= open[1];
def zachodik= close[1];
#calculate 50% of candle
def midik= (opicka+zachodik)/2;
#measure range of yesterday
def rangik=(zachodik-opicka)/opicka*100;
def ran= if rangik<8 then 0 else 10;
#compare last price with midcandle
def c= ask;
def porovnavacka= (ask/midik-1)*100;
#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
def condition3 = ran == 10;
def fin= if condition3 then 8 else 0;
def tot = condition1+condition2+fin;
#plot z =tot;

REF post #1

i must be missing something. i tried to put your code in a chart and add a loop at the end, but it causes errors on c = ask.

i added this in, which used to work in another study, but not now , this causes an error too ?
def bid1 = close(priceType = "Bid");
def ask1 = close(priceType = "Ask");

maybe tos changed how to read bid and ask in a chart ?

i added a fold loop after your code, to look at previous bars, to find when tot had a value greater than totminvalue.
i changed c =ask , to c = close


# copied form my engulfing code, posted on here somewhere
input lookback = 100;

def totminvalue = 5;
# i have no idea what this should be. just a guess

# 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 barcnt = fold k = 1 to (lookback + 1)
with p
# this looks for smaller bars before the current bar
# while (high > getvalue(high, k) and low < getvalue(low, k))
while ( getvalue(tot, k) < totminvalue )
do p + 1;

# barcnt will = the offset # , pointing back to the bar with the desired value of tot
def prev_tot = if barcnt < lookback then getvalue( tot, barcnt ) else 0;

def bn = barnumber();
# display some variables for debugging
addchartbubble(1, low, bn + "\n" + tot + "\n" + barcnt + "\n" + prev_tot, color.yellow, no);


================================================

if i replace def c = ask;
with this def c = close;
it doesn't have errors

it looks like tot has values of 1 or 2


here is my modified code, using close, instead of ask

Ruby:
def bn = BarNumber();
# lines_bidask_00
def bid1 = close(priceType = "Bid");
def ask1 = close(priceType = "Ask");
def Mark1 = close(priceType = "Mark");
def Last1 = close(priceType = "Last");


def opicka = open[1];
def zachodik = close[1];
#calculate 50% of candle
def midik = (opicka + zachodik) / 2;
#measure range of yesterday
def rangik = (zachodik - opicka) / opicka * 100;
def ran = if rangik < 8 then 0 else 10;
#compare last price with midcandle

#def c = ask;
#def porovnavacka = (ask / midik - 1) * 100;
#def c = ask1;
def c = close;
def porovnavacka = ( c / midik - 1) * 100;

#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
def condition3 = ran == 10;
def fin = if condition3 then 8 else 0;
def tot = condition1 + condition2 + fin;
#plot z = tot;

# this shows errors for tot on all bars
#addchartbubble(1, low, tot , color.yellow, no);

# this has good values
#addchartbubble(1, low, "O " + opicka + "\n" + "c " + zachodik  + "\n" + "m " +  midik, color.yellow, no);

# cond1 and 2 have n/a errors
#addchartbubble(1, low, "1 " + condition1 + "\n" + "2 " + condition2 + "\n" + "3 " + condition3 , color.yellow, no);

#addchartbubble(1, low, "c " + c + "\n" + "p " + porovnavacka , color.yellow, no);



# ------------------------------------

# copied form my engulfing code, posted on here somewhere
input lookback = 100;

def totminvalue = 5;
# i have no idea what this should be. just a guess

# 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 barcnt = fold k = 1 to ( lookback + 1)
with p
  #  this looks for smaller bars before the current bar
  #  while (high > getvalue(high, k) and low < getvalue(low, k))

# this compares prev values of tot to the constant, totminvalue.
# it keeps looping as long as tot is smaller.
# if tot is bigger, the loop stops , and p value is transferred to barcnt, 
while ( getvalue(tot, k) < totminvalue )
do p + 1;

# barcnt will = the offset # , pointing back to the bar with the desired value of tot
def prev_tot = if barcnt < lookback then getvalue( tot, barcnt ) else 0;

#def bn = barnumber();
# display some variables for debugging
addchartbubble(1, low, bn + "\n" + tot + "\n" + barcnt + "\n" + prev_tot, color.yellow, no);
 
Last edited:
Solution
REF post #1

i must be missing something. i tried to put your code in a chart and add a loop at the end, but it causes errors on c = ask.

i added this in, which used to work in another study, but not now , this causes an error too ?
def bid1 = close(priceType = "Bid");
def ask1 = close(priceType = "Ask");

maybe tos changed how to read bid and ask in a chart ?

i added a fold loop after your code, to look at previous bars, to find when tot had a value greater than totminvalue.
i changed c =ask , to c = close


# copied form my engulfing code, posted on here somewhere
input lookback = 100;

def totminvalue = 5;
# i have no idea what this should be. just a guess

# 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 barcnt = fold k = 1 to (lookback + 1)
with p
# this looks for smaller bars before the current bar
# while (high > getvalue(high, k) and low < getvalue(low, k))
while ( getvalue(tot, k) < totminvalue )
do p + 1;

# barcnt will = the offset # , pointing back to the bar with the desired value of tot
def prev_tot = if barcnt < lookback then getvalue( tot, barcnt ) else 0;

def bn = barnumber();
# display some variables for debugging
addchartbubble(1, low, bn + "\n" + tot + "\n" + barcnt + "\n" + prev_tot, color.yellow, no);


================================================

if i replace def c = ask;
with this def c = close;
it doesn't have errors

it looks like tot has values of 1 or 2


here is my modified code, using close, instead of ask

Ruby:
def bn = BarNumber();
# lines_bidask_00
def bid1 = close(priceType = "Bid");
def ask1 = close(priceType = "Ask");
def Mark1 = close(priceType = "Mark");
def Last1 = close(priceType = "Last");


def opicka = open[1];
def zachodik = close[1];
#calculate 50% of candle
def midik = (opicka + zachodik) / 2;
#measure range of yesterday
def rangik = (zachodik - opicka) / opicka * 100;
def ran = if rangik < 8 then 0 else 10;
#compare last price with midcandle

#def c = ask;
#def porovnavacka = (ask / midik - 1) * 100;
#def c = ask1;
def c = close;
def porovnavacka = ( c / midik - 1) * 100;

#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
def condition3 = ran == 10;
def fin = if condition3 then 8 else 0;
def tot = condition1 + condition2 + fin;
#plot z = tot;

# this shows errors for tot on all bars
#addchartbubble(1, low, tot , color.yellow, no);

# this has good values
#addchartbubble(1, low, "O " + opicka + "\n" + "c " + zachodik  + "\n" + "m " +  midik, color.yellow, no);

# cond1 and 2 have n/a errors
#addchartbubble(1, low, "1 " + condition1 + "\n" + "2 " + condition2 + "\n" + "3 " + condition3 , color.yellow, no);

#addchartbubble(1, low, "c " + c + "\n" + "p " + porovnavacka , color.yellow, no);



# ------------------------------------

# copied form my engulfing code, posted on here somewhere
input lookback = 100;

def totminvalue = 5;
# i have no idea what this should be. just a guess

# 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 barcnt = fold k = 1 to ( lookback + 1)
with p
  #  this looks for smaller bars before the current bar
  #  while (high > getvalue(high, k) and low < getvalue(low, k))

# this compares prev values of tot to the constant, totminvalue.
# it keeps looping as long as tot is smaller.
# if tot is bigger, the loop stops , and p value is transferred to barcnt,
while ( getvalue(tot, k) < totminvalue )
do p + 1;

# barcnt will = the offset # , pointing back to the bar with the desired value of tot
def prev_tot = if barcnt < lookback then getvalue( tot, barcnt ) else 0;

#def bn = barnumber();
# display some variables for debugging
addchartbubble(1, low, bn + "\n" + tot + "\n" + barcnt + "\n" + prev_tot, color.yellow, no);
thank you.no idea about this error..in any case good for learning fold function..what is p there?did get it as I dont see any definition...
 
REF post #1

i must be missing something. i tried to put your code in a chart and add a loop at the end, but it causes errors on c = ask.

i added this in, which used to work in another study, but not now , this causes an error too ?
def bid1 = close(priceType = "Bid");
def ask1 = close(priceType = "Ask");

maybe tos changed how to read bid and ask in a chart ?

i added a fold loop after your code, to look at previous bars, to find when tot had a value greater than totminvalue.
i changed c =ask , to c = close


# copied form my engulfing code, posted on here somewhere
input lookback = 100;

def totminvalue = 5;
# i have no idea what this should be. just a guess

# 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 barcnt = fold k = 1 to (lookback + 1)
with p
# this looks for smaller bars before the current bar
# while (high > getvalue(high, k) and low < getvalue(low, k))
while ( getvalue(tot, k) < totminvalue )
do p + 1;

# barcnt will = the offset # , pointing back to the bar with the desired value of tot
def prev_tot = if barcnt < lookback then getvalue( tot, barcnt ) else 0;

def bn = barnumber();
# display some variables for debugging
addchartbubble(1, low, bn + "\n" + tot + "\n" + barcnt + "\n" + prev_tot, color.yellow, no);


================================================

if i replace def c = ask;
with this def c = close;
it doesn't have errors

it looks like tot has values of 1 or 2


here is my modified code, using close, instead of ask

Ruby:
def bn = BarNumber();
# lines_bidask_00
def bid1 = close(priceType = "Bid");
def ask1 = close(priceType = "Ask");
def Mark1 = close(priceType = "Mark");
def Last1 = close(priceType = "Last");


def opicka = open[1];
def zachodik = close[1];
#calculate 50% of candle
def midik = (opicka + zachodik) / 2;
#measure range of yesterday
def rangik = (zachodik - opicka) / opicka * 100;
def ran = if rangik < 8 then 0 else 10;
#compare last price with midcandle

#def c = ask;
#def porovnavacka = (ask / midik - 1) * 100;
#def c = ask1;
def c = close;
def porovnavacka = ( c / midik - 1) * 100;

#3 conditions..best result is 3 false
def condition1 = porovnavacka > 0;
def condition2 = porovnavacka < 3;
def condition3 = ran == 10;
def fin = if condition3 then 8 else 0;
def tot = condition1 + condition2 + fin;
#plot z = tot;

# this shows errors for tot on all bars
#addchartbubble(1, low, tot , color.yellow, no);

# this has good values
#addchartbubble(1, low, "O " + opicka + "\n" + "c " + zachodik  + "\n" + "m " +  midik, color.yellow, no);

# cond1 and 2 have n/a errors
#addchartbubble(1, low, "1 " + condition1 + "\n" + "2 " + condition2 + "\n" + "3 " + condition3 , color.yellow, no);

#addchartbubble(1, low, "c " + c + "\n" + "p " + porovnavacka , color.yellow, no);



# ------------------------------------

# copied form my engulfing code, posted on here somewhere
input lookback = 100;

def totminvalue = 5;
# i have no idea what this should be. just a guess

# 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 barcnt = fold k = 1 to ( lookback + 1)
with p
  #  this looks for smaller bars before the current bar
  #  while (high > getvalue(high, k) and low < getvalue(low, k))

# this compares prev values of tot to the constant, totminvalue.
# it keeps looping as long as tot is smaller.
# if tot is bigger, the loop stops , and p value is transferred to barcnt,
while ( getvalue(tot, k) < totminvalue )
do p + 1;

# barcnt will = the offset # , pointing back to the bar with the desired value of tot
def prev_tot = if barcnt < lookback then getvalue( tot, barcnt ) else 0;

#def bn = barnumber();
# display some variables for debugging
addchartbubble(1, low, bn + "\n" + tot + "\n" + barcnt + "\n" + prev_tot, color.yellow, no);
trying apply this fold function in my another script..could you pls advice if this should do what I am looking for? : look for 40 previous candles and if they have same high (with some space define in if) as current high , raise value of x by one..:
"
def x=0;
def sracka= fold k =2 to 40 do if(h/high[k]>0.998 && h/high[k]<1.0025) then x+1 else double.nan;
"
its not throwing me any errors but a lot of loadings..so hope its just because of weekend
 
thank you.no idea about this error..in any case good for learning fold function..what is p there?did get it as I dont see any definition...
p is a temporary variable, to hold the values from each time the do code line is evaluated.
when the fold is done, the value in p , is passed on to t
can define a starting value for p , by setting it equal to something. p = 1. just p is defaulted to be = 0.

def t = fold i = 1 to 10
with p = 1
while ( if stuff is true keep doing loop) (optional)
do ( do stuff )

some other info on fold
https://usethinkscript.com/threads/...onfirming-a-thinkscript-line.6662/#post-64430
 
trying apply this fold function in my another script..could you pls advice if this should do what I am looking for? : look for 40 previous candles and if they have same high (with some space define in if) as current high , raise value of x by one..:
"
def x=0;
def sracka= fold k =2 to 40 do if(h/high[k]>0.998 && h/high[k]<1.0025) then x+1 else double.nan;
"
its not throwing me any errors but a lot of loadings..so hope its just because of weekend

this should look at 38 previous bars and compare the past high's to h. if a comparison is true, then increment a counter

Code:
def sracka= fold k =2 to 40
with x
do x + ( if  ((h /  getvalue(high, k) > 0.998 ) and  (h / getvalue(high, k) <1.0025) ) then  1 else 0 );

input test1 = yes;
addchartbubble(test1, low, sracka , color.yellow, no);

double check the ( ) placement.... i think i have it correct.
 

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