How to make a loop
Forums › ProRealTime English forum › ProBuilder support › How to make a loop
- This topic has 10 replies, 3 voices, and was last updated 7 years ago by donaldthetrader.
-
-
10/25/2016 at 11:53 AM #15464
Hi All,
How do I loop the below sample code?
c1 = low < Low[1] and high > high[1]
c2 = low < Low[2] and high > high[2]
c3 = low < Low[3] and high > high[3]c4 = etc….
1234567i=1for i = 1 to 4 dolow < Low[i] and high > high[i]nextdoes not seem to loop it correctly.
Any thoughts?
10/25/2016 at 12:37 PM #15469>> For clarity of messages on ProRealCode’s forums, please use the “insert code PRT” button to separate the text of the code part! Thank you! <<
Your loop statement is ok, but you are not couting how much time your condition on line 5 is true. You should set it as a variable and count how much time this variable is returned true by storing this count in another variable.
10/25/2016 at 9:13 PM #15515Thank you Nicolas.
I am a novice at programming so you assistance is greatly appreciated.
How do I set line 5 as a variable to count how much time this variable is returned true and how do I sort this count in another variable?
10/26/2016 at 7:58 AM #15533You can do it this way:
12345678910111213count = 0for i = 1 to 4 dox = low < Low[i] and high > high[i]if x thencount = count +1endifnextreturn countx is your variable condition, if its true, then increment your new “count” variable. That’s all.
04/05/2017 at 12:06 PM #31104Good day all. I’m new to Prorealcode and want your help with what should be a “fairly” simple loop! this is the requirement:
for a stock, loop from the first bar (index 0) bar to the 5th bar (index 4) and determine if the stochastic at the previous bar (index n+1) is less than or equal to 50 (or 20) and the next bar (index n) is greater than or equal to 50 (or 20). Please see my code below. I’m not sure why its not working like it should.
12345678FOR Period = 0 TO 3 DOstochCon1 = (Stochastic[8,3](close)[Period+1]<=50)>0 AND (Stochastic[8,3](close)[Period]>=50)>0stochCon2 = (Stochastic[8,3](close)[Period+1]<=20)>0 AND (Stochastic[8,3](close)[Period]>=20)>0IF (stochCon1 OR stochCon2) THEN// Do stuffENDIFNEXT04/05/2017 at 12:29 PM #31106You could also code the loop like this: (always a better idea to declare your indicator outside of the loop). Since you want to test a crossover of the stochastic, we embed the indicator declaration into a boolean variable to test the cross.
12345678mystoch = Stochastic[8,3](close)stochCon = mystoch crosses over 50 or mystoch crosses over 20FOR i = 0 TO 3 DOIF stochCon[i] THEN// Do stuffENDIFNEXT04/05/2017 at 1:06 PM #31118@Nicolas, thanks for the quick reply! update the code and see the outcome…
04/05/2017 at 2:11 PM #31130@nicolas, i’ve tried the above code. I’m not looking for a %D and %K crossover i simply want to know if the %K has ever gone below 50 or 20 and now moving over 50 or 20 over “n” periods. In this case the last 5 bars.
04/05/2017 at 4:38 PM #31138and this is exactly what it does (example attached).
for 5 bars lookback instead of 3, the code need to be modified though:
1234567891011mystoch = Stochastic[8,3](close)stochCon = mystoch crosses over 50 or mystoch crosses over 20test=0FOR i = 0 TO 5 DOIF stochCon[i] THENtest=1ENDIFNEXTreturn test04/05/2017 at 6:31 PM #31152Nice!. Thanks @Nicolas
04/06/2017 at 12:40 PM #31248@ Nicolas, hope you are well..
Pls desperately need your input on this topic: https://www.prorealcode.com/topic/previous-years-highest-high-and-lowest-lows/
Thanks!
-
AuthorPosts