While Loop not running

The following is while loop code that I have written for a project. No errors are popping up when and everything with the code appears to be fine but it never actually completes running. I am very stuck and would greatly appreciate some help. Here is my code:
i=3;
while i <= length(Datamatrix)
if isTwoInsideDays == true
L_entry_price = prevInsideDayHigh(i) + 0.0010;
L_stop_loss = prevInsideDayLow(i) - 0.0010; % Stop 10 pips below
L_take_profit = L_entry_price + 2 * (L_stop_loss - L_entry_price);
i=i+1;
while Close>L_entry_price==false;
i=i+1;
end
Signal(i)=1;
i=i+1;
while (Close(i) > L_take_profit || Close(i) < L_stop_loss) == false
i=i+1;
end
Signal(i)=-1;
end
end

3 Comments

VBBV
VBBV on 30 Apr 2024
Edited: VBBV on 30 Apr 2024
Use an if-statement, in place of while loop, since inner while loops never finish, with the stopping criteria is not satisfied.
i=3;
while i <= length(Datamatrix)
if isTwoInsideDays == true
L_entry_price = prevInsideDayHigh(i) + 0.0010;
L_stop_loss = prevInsideDayLow(i) - 0.0010; % Stop 10 pips below
L_take_profit = L_entry_price + 2 * (L_stop_loss - L_entry_price);
i=i+1;
if Close(i)>L_entry_price; % use an if-statement (close(i))
i=i+1;
end
Signal(i)=1;
i=i+1;
if (Close(i) > L_take_profit | Close(i) < L_stop_loss) % use if-statement
i=i+1;
end
Signal(i)=-1;
end
end
Torsten
Torsten on 30 Apr 2024
Edited: Torsten on 30 Apr 2024
while Close(i)>L_entry_price==false;
i=i+1;
end
while (Close(i) > L_take_profit || Close(i) < L_stop_loss) == false
i=i+1;
end
versus
if Close(i)>L_entry_price; % use an if-statement (close(i))
i=i+1;
end
if (Close(i) > L_take_profit | Close(i) < L_stop_loss) % use if-statement
i=i+1;
end
Note that the "i" in the if-statement will be increased at most once while it will be increased as long as the condition becomes false in the while-loop .
Thus the two formulations are not equivalent.
VBBV
VBBV on 1 May 2024
Edited: VBBV on 1 May 2024
i=3;
if i <= length(Datamatrix)
  if isTwoInsideDays == true
              L_entry_price = prevInsideDayHigh(i) + 0.0010;
              L_stop_loss = prevInsideDayLow(i) - 0.0010; % Stop 10 pips below
              L_take_profit = L_entry_price + 2 * (L_stop_loss - L_entry_price);
              %i=i+1;      
              while Close(i)>L_entry_price; % use an if-statement (close(i))
                  i=i+1;
              end
              Signal(i)=1;
              i=i+1;
              while (Close(i) > L_take_profit | Close(i) < L_stop_loss) % use if-statement 
                  i=i+1;
              end              
Signal(i)=-1; 
  end
end

Yes, possibly correct from your previous comments , that the first if-statement is never true after entering first while loop. Since the code is not given complete , it's difficult to tell how other variables are interdependent in rest of the code (not shown). In this case the best guess is outer while loop may not be necessary.

Sign in to comment.

Answers (1)

Voss
Voss on 30 Apr 2024

0 votes

This loop will never terminate if entered
while Close>L_entry_price==false;
i=i+1;
end
because only i changes. Close and L_entry_price do not change with i, so the condition is always true if it's true initially.
Should it be like this?
while Close(i)>L_entry_price==false;
i=i+1;
end
or, perhaps more straight-forward:
while Close(i) <= L_entry_price
i=i+1;
end

3 Comments

I tried both of those ways and unfortunatly did not see a change. Any other suggestions you might have would be greatly appreciated!
Torsten
Torsten on 30 Apr 2024
Edited: Torsten on 30 Apr 2024
Maybe "isTwoInsideDays" is false and "length(Datamatrix)" is >= 3 ?
Then you will never escape the main outer while-loop.
If the problem remains, we need to be able to reproduce the error with complete executable code.
Voss
Voss on 30 Apr 2024
I would add a breakpoint before the outer while loop and step through the code as it runs to see what's going on.

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Products

Tags

Asked:

on 30 Apr 2024

Edited:

on 1 May 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!