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
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.
답변 (1개)
Voss
2024년 4월 30일
0 개 추천
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
카테고리
도움말 센터 및 File Exchange에서 Programming에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!