Invalid price for OrderClose function

 

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?

 

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.

 

hi

i tested alpari four my EA but it did not work i think your broker is very important,some of broker does not let you to test EA

i suggest you fibo for test your EA

 
it only works if the code does not contain errors. If it does not work then it will give you error messages and by reading and understanding them you can find out what the error in your code is. This is pretty much how it works.
 

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??

Reason: