Hi
I’m trying to code UNTIL using the below example. I used IF, WHILE and BREAK but an infinite loop was detected and stopped the backtest. Help please to correct my code.
If my long trade closes early due to a stop loss, then I don’t want the system to enter into another long trade while x is still greater than y UNTIL y > x i.e. exit long position condition is met which should reset a back to zero.
I have tried to code a = 1 whilst x is still greater than y when my long trade closes early due to a stop loss.
x = AroonUp[10]
y = AroonDown[10]
a = 0
IF PositionPerf(1) < 0 AND TRADEPRICE(1) < TRADEPRICE(2) THEN
WHILE (x > y)
a = 1
IF (y > x) THEN
BREAK
ENDIF
WEND
ENDIF
// Conditions to enter long positions
//
IF a = 0 AND (x > y) AND (x > 70) THEN
BUY 1 PERPOINT AT MARKET
ENDIF
// Conditions to exit long positions
//
IF (y > x) THEN
SELL AT MARKET
ENDIF
Thanks
Sachin
It’s an infinite loop, in between WHILE ….WEND initial conditions never change.
Indeed there’s no need for an iteration, another IF…ENDIF will do.
Replace lines 7-12 with:
If x > y then
a = 1
ENDIF
Thanks Roberto
However, wouldn’t the suggested code change prevent the next long trade even after y becomes greater than x if there is no other trade executed after the stop loss exit trade?
You’ll have to assign a different value to a when it’s time to, not always.
Replace line 4 with:
ONCE a = 0
Then you’ll need to add:
a = 0
somewhere in the code to reenable trading when you want to, according to your conditions.