try to use RefreshReates() before OrderClose
Thank you for your answers. I tried both, but it doesn't work.
I wrote a function and wanted to close ALL open orders or limits on my account:
int CloseAll() { int check = 1; int err; bool rueck; for(int i=0;i<OrdersTotal();i++) { if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break; if(OrderType()== (OP_BUY || OP_BUYLIMIT || OP_BUYSTOP)) { rueck = OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),Slippage,White); if(rueck == false) check = -1; } if(OrderType()== (OP_SELL || OP_SELLLIMIT || OP_SELLSTOP)) { rueck = OrderClose(OrderTicket(),OrderLots(), OrderClosePrice(),Slippage,White); if(rueck == false) check = -1; } } return (check); }
But it doesn't work! Sometimes it closed one orders, but the others are running.
Can anybody explain me why?? I don't understand it.
let it count backwards because after closing an order all following orders will have their relative position in the list reduced by 1. This results in jumping over every second order. Alternatively you could decrease the loop variable i by one after each successful close, but messing with a for-loop variable is an ugly hack, not always allowed or even possible and should generally be avoided.
Counting backwards will of course not work in one of these crippled FIFO accounts. There it needs to be a bit more sophisticated, for example putting all tickets into an array, sorting the array by open time and then closing them all by ticket, oldest first.
I tested this
for(int i=OrdersTotal();i>0;i--)
but it also doesn't work!
for (int i=OrdersTotal() - 1; i >=0; i--)
You also need to THINK about what you are doing, not only copy-paste some code snippets.
Thank you for your help! Another error was in this line:
if(OrderType()== (OP_BUY || OP_BUYLIMIT || OP_BUYSTOP))
I had to seperate it into
if(OrderType()==OP_BUY)... and if(OrderType()==OP_BUYLIMIT || OrderType()==OP_BUYSTOP)...
Now it works!
@7bit
I read your answer once again and realized, that I haven't realy understood.
Can you give me a few examples from FIFO accounts??
When I use Alpari, ActiveTrades etc. can I use the backwards-counting for-loop??
Or should I always try to avoid for-loops and and close the oldest first??
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I am using Alpari and when my EA wants to Close an open Order I get the message:
"invalid price 1.32819000 for OrderClose function"
my function call is:
OrderClose(OrderTicket(),OrderLots(),Bid,2,White);
What is going wrong?