Hello,
I’m working on a code that should only buy when the condition occurs for the second or more times. Unfortunately, I’m facing an issue where the counter doesn’t seem to increase and remains at 1, causing the “IF counter>x” condition to always return the same result, regardless of x.
// Reset for each day
if Date[1] <> Date[0] then
counter = 0
endif
Divg = DivergenceMACD[12,26,9,20](close)
//counter
IF Divg>0 THEN
counter=counter+1
ENDIF
// Conditions to enter long positions
IF counter>3 and Divg>0 THEN
BUY 1 SHARES AT MARKET
ENDIF
et comme ceci
// Reset for each day
if Date[1] <> Date[0] then
counter = 0
endif
Divg=0
Divg = DivergenceMACD[12,26,9,20](close)
//counter
IF Divg>0 THEN
counter=counter+1
ENDIF
// Conditions to enter long positions
IF counter>3 and Divg>0 THEN
BUY 1 SHARES AT MARKET
ENDIF
Hi Andres,
Start out with this :
Once counter = 0 // counter must have an initial value.
if Date[1] <> Date[0] then
counter = 0
endif
If you don’t do this, counter will have an undefined value which probably is a huge negative number. So it will add up, but only when a new day has started, it will reset to 0 for the first time.
Maybe what fifi added helps too ( I don’t readily see why he does it) and possibly it is so that both amendments are required to get it going.
You can let your code end with
graph counter
graph Divg
Then you can see what values these variables receive.
Thank you very much fifi743 and PeterSt for your answers. Thanks PeterSt specially for the command “graph” I didn’t know it.
Actually the code was working I could see it with “graph counter”. The problem is that it jumps from 0 to 4, or to 0 to 20, etc. So I’m going to adjust it to start from 4, or something else.