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….
i=1
for i = 1 to 4 do
low < Low[i] and high > high[i]
next
does not seem to loop it correctly.
Any thoughts?
>> 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.
Thank 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?
You can do it this way:
count = 0
for i = 1 to 4 do
x = low < Low[i] and high > high[i]
if x then
count = count +1
endif
next
return count
x is your variable condition, if its true, then increment your new “count” variable. That’s all.
Good 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.
FOR Period = 0 TO 3 DO
stochCon1 = (Stochastic[8,3](close)[Period+1]<=50)>0 AND (Stochastic[8,3](close)[Period]>=50)>0
stochCon2 = (Stochastic[8,3](close)[Period+1]<=20)>0 AND (Stochastic[8,3](close)[Period]>=20)>0
IF (stochCon1 OR stochCon2) THEN
// Do stuff
ENDIF
NEXT
@donaldthetrader
You 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.
mystoch = Stochastic[8,3](close)
stochCon = mystoch crosses over 50 or mystoch crosses over 20
FOR i = 0 TO 3 DO
IF stochCon[i] THEN
// Do stuff
ENDIF
NEXT
@Nicolas, thanks for the quick reply! update the code and see the outcome…
@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.
and this is exactly what it does (example attached).
for 5 bars lookback instead of 3, the code need to be modified though:
mystoch = Stochastic[8,3](close)
stochCon = mystoch crosses over 50 or mystoch crosses over 20
test=0
FOR i = 0 TO 5 DO
IF stochCon[i] THEN
test=1
ENDIF
NEXT
return test
@ 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!