While Loop not running
Mostra commenti meno recenti
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 Commenti
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
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.
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.
Risposte (1)
Voss
il 30 Apr 2024
0 voti
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
Categorie
Scopri di più su Programming in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!