EA not closing trades as instructed

 

I have the following code that executes at certain times when i want to exit all open long positions (and another one for short positions)

void CloseLongs()
{
for(int pos = OrdersTotal() - 1; pos >= 0; pos--) if (
OrderSelect(pos, SELECT_BY_POS)            
&&  OrderMagicNumber() == magic ) 
   {
   if      (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
   }   
}

I expect this to cycle through open orders and for all those matching the EA's magic number, to close them. The problem is, it only exits one of my open positions at a time, the next time it's called it closes the next one, and so on. I even modified it to the following thinking it may have something to do with FIFO rules:

void CloseLongs()
{
for(int pos = 0 ; pos < OrdersTotal(); pos++) if (
OrderSelect(pos, SELECT_BY_POS)            
&&  OrderMagicNumber() == magic ) 
   {
   if      (OrderType()==OP_BUY) OrderClose(OrderTicket(),OrderLots(),Bid,3,Green);
   }   
}

Again, no dice. This is happening on Alpari UK on demo. The issue didn't happen with CMS-FX on demo account. Alpari is being such a pain that I would just ditch them, except that some signal services use it for broadcast. Does anyone have any idea why this is happening, is there something blatantly wrong in my code?

Thanks,

 

a) I personally would not write OrderSelect() and OrderMagicNumber() inside the same if statement.

b)you allow only a slippage of 3 which is 0.3 pips on alpari uk (also slippage needs to be 5 digits modificated)

c) RefreshRates() before OrderClose()

d) You should log and check for possible errors during OrderClose()

 

Good point, the slippage should be 30. And I can try your suggestion of storing OrderSelect() and OrderMagicNumber() values as int variables and then call the variables in OrderClose().

RefreshRates() is already called right before this code executes with no lengthy calculations in between, but I could do some error checking as you suggested.

 

But the OrderClose() itself takes some time, if the price changes in the meantime you still try to close the order on the same bid price as before.

Additionally i normally use OrderClosePrice() instead of bid/ask

 
Thanks for the tip, I'll use OrderClosePrice() instead
 

I thought OrderClosePrice returned closing price for a past (history order). Didnt know it worked on open orders and returned Ask/bid. OrderClosePrice

Can someone else confirm this as I don't have time currently for a test ? :)

 

I can confirm. Using OrderClosePrice() in combination with the correct slippage and doing a RefreshRates() on the line just prior to OrderClose() fixed the concurrent exit problem. Now i'm off to fix the next issue.

I'm getting an error that the maximum number of open orders has been met after 100 open orders. Apparently, Alpari UK only allows 100 concurrent orders, and this simply won't work with my experiment. It looks like this is a broker limitation and there is just no way around it. Soo... good-bye Alpari, I guess it's time to look for alternatives.

 
void ClosePositions(string sy="", int op=-1, int mn=-1) {
  int i, k=OrdersTotal();

  if (sy=="0") sy=Symbol();
  for (i=k-1; i>=0; i--) {
    if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) {
      if ((OrderSymbol()==sy || sy=="") && (op<0 || OrderType()==op)) {
        if (OrderType()==OP_BUY || OrderType()==OP_SELL) {
          if (mn<0 || OrderMagicNumber()==mn) ClosePosBySelect();
        }
      }
    }
  }
}

input vars: 

symbol = sy
   cmd = op
 magic = mn
 

 
  1.     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. 
           ...
        }
    
    You only want to close those orders on the current chart, not all charts the EA is attached to.
  2. zzuegg 2011.05.17 19:30 a) I personally would not write OrderSelect() and OrderMagicNumber() inside the same if statement.
    Personal choice. I'm selecting my orders so one statement for/if/openbrace to do that seems clearer. Multiple statements just make the indenting too large
        for(pos = OrdersTotal()-1; pos >= 0 ; pos--){
            if (OrderSelect(pos, SELECT_BY_POS)){
                if (OrderMagicNumber()  == magic.number     // my magic number
                &&  OrderSymbol()       == Symbol() ){      // and my pair.
                    ...
                }
            }
         }

  3. b)you allow only a slippage of 3 which is 0.3 pips on alpari uk (also slippage needs to be 5 digits modificated)
    You must adjust TP, SL, and SLIPPAGE for 5 digit brokers
    //++++ These are adjusted for 5 digit brokers.
    int     pips2points;    // slippage  3 pips    3=points    30=points
    double  pips2dbl;       // Stoploss 15 pips    0.0015      0.00150
    int     Digits.pips;    // DoubleToStr(dbl/pips2dbl, Digits.pips)
    int     init(){
        if (Digits == 5 || Digits == 3){    // Adjust for five (5) digit brokers.
                    pips2dbl    = Point*10; pips2points = 10;   Digits.pips = 1;
        } else {    pips2dbl    = Point;    pips2points =  1;   Digits.pips = 0; }
        // OrderSend(... Slippage.Pips * pips2points, Bid - StopLossPips * pips2dbl
    

  4. OrderClosePrice
    Can someone else confirm this as I don't have time currently for a test ? :)
    Yes it works just fine.
  5. Apparently, Alpari UK only allows 100 concurrent orders
    IBFX allows 1000
 

"You only want to close those orders on the current chart, not all charts the EA is attached to."

Yeah, normally I just assign different magic numbers to those EAs, but what you're suggesting adds an additional layer of robustness.

--------------------

" IBFX allows 1000"

I think I'm going with FXOpen ECN -- unlimited open orders (Or so they claim).

-------------------

By the way, how do you place different quotes on you message? I couldn't figure it out.

 
gatornuke:

"You only want to close those orders on the current chart, not all charts the EA is attached to."

Yeah, normally I just assign different magic numbers to those EAs, but what you're suggesting adds an additional layer of robustness.

--------------------

By the way, how do you place different quotes on you message? I couldn't figure it out.

  1. Too easy to attach the EA to another chart and forget about changing the MN. I opt for one MN per EA and let the EA handle the chart.
  2. Select the original text/copy/paste/select text/change from normal text to
    quotation (control-3)