Several ways to fix
- NEVER assume what order is selected when using OrderType() etc. ALWAYS do an OrderSelect() first
- Change CountOrders to 'remember' any valid order as it scans, and then select it before returning.
- Change bool CountOrders to either int ChecktOrdersAndSelectReturnValueByPositionOrMinus1() or int CheckForAnyRelevantOrderAndReturnItsOrderNumberOrMinus1()
if return is -1 then no open/pending orders, otherwise you can select an order
NB do not complain if my functions names are too long. I just like self-documenting code ;-)
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if ( OrderSelect(pos, SELECT_BY_POS) // Only my orders w/ && OrderMagicNumber() == magic.number // my magic number && OrderSymbol() == Symbol() ){ // and my pair. return(true); } return(false);
OK .. So what if I assign a variable at the start that references an order count function. Later on when I use that variable is the function run again or does is the value of the variable stored when assigned.
Example:
int CountOrders() { int cnt = 0; int OrderCount = 0; for (cnt=OrdersTotal()-1;cnt>=0;cnt--) { if (!OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES)) continue; if (OrderSymbol() == Symbol() && OrderMagicNumber() == magic) { OrderCount++; } } return(OrderCount); } int start() { int TotalOrders = CountOrders(); if (TotalOrders == 1) { // When I use the variable TotalOrders is the function run here or is it run when the variable is assigned? } }
Regards,
JonDgls
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hello members,
Your help would be greatly appreciated. I am using the OrderSelect() function to cycle through orders for various reasons, however, when I do, the EA begins to affect trades on other currencies and magic numbers.
I believe this is because when the OrderSelect() function exits, it sets on the zero order which can be any symbol / magic number. Here is an example of what I am doing:
I know what is happening, the last order selected could be anything (any Magic / Symbol) and when I later use OrderTicket() it is using the ticket of the last order.
What I need some advice on is how to avoid this from happening. The above is just an example and I am not worried about fixing that particular code. I just need to know the basics on avoiding this issue. Here is what I had in mind but I would appreciate some feedback on whether this will work or not:
Thanks in advance.
Regards,
JonDgls