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

VBBV
VBBV 2024년 4월 30일
편집: VBBV 2024년 4월 30일
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 2024년 4월 30일
편집: Torsten 2024년 4월 30일
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 2024년 5월 1일
편집: VBBV 2024년 5월 1일
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
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

댓글 수: 3

Marshall
Marshall 2024년 4월 30일
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 2024년 4월 30일
편집: Torsten 2024년 4월 30일
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 2024년 4월 30일
I would add a breakpoint before the outer while loop and step through the code as it runs to see what's going on.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

태그

질문:

2024년 4월 30일

편집:

2024년 5월 1일

Community Treasure Hunt

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

Start Hunting!

Translated by