uint last=OrdersHistoryTotal(); ⋮ if(last>0) ⋮ for(uint i=last; i>=0; i--)
- No need for the if as the for loop will do nothing when the count is zero.
- If there are n items in history their positions are [0 … n-1]. Your first OrderSelect always fails.
- An unsigned int can never be less than zero. Your test is always true by definition. Either use a signed int,
or compare count, not index:
for(uint i=last; i>0;){ --i; // Count in the for, index inside the braces.
- Do not assume history has only closed orders.
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum
William Roeder:
- No need for the if as the for loop will do nothing when the count is zero.
- If there are n items in history their positions are [0 … n-1]. Your first OrderSelect always fails.
- An unsigned int can never be less than zero. Your test is always true by definition. Either use a signed int, or
compare count, not index:
- Do not assume history has only closed orders.
Do not assume history is ordered by date, it's not.
Could EA Really Live By Order_History Alone? (ubzen) - MQL4 programming forum
Hi William, thank you for your answers :)
1.- Sorry my ignorance, but why the the for loop does nothing when "last" is zero?
2.- Oh I guess in that case the for loop should start from last-1, right?
3. I do not understant the difference between
for(uint i=last; i>0;){ --i; // Count in the for, index inside the braces.
and
for(uint i=last; i>=0; i--)
(besides the highlighted parts). I guess that the difference is that in the second one i starts from "last" and at the beginning of the loop it goes -1?
Is that equivalent to this?
for(uint i=last-1; i>0; i--)
4. How can I check if the order is closed or not? How can I be sure that the last order retrieved is the last order closed/deleted/modified at that given time?
Thank you William!

You are missing trading opportunities:
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
Registration
Log in
You agree to website policy and terms of use
If you do not have an account, please register
Hello guys!
I have an issue regarding an EA I recently coded. I detected that sometimes it freezes the mt4 platform and I have to restart it or wait until it responses.
I manage to identify that the function that was causing the issue is this one:
The idea is to do something only if the last result is positive or negative and do not count the last result twice (thats why totalOrders). How can I manage this in a better way? And why does this causes an infinite loop and how do I avoid it/fix it?
I am assuming two things here. The first one is that this is the function that causes the problem (mainly because I commented the line in charge of executing the function and the problem dissapeard). The second one is that this gets into an infinite loop (why?!?).
The problem only occurs when I change an input in the EA while attached to a chart.
Any help?
Thank you in advance!!
:D