Problem with OrderClose()

 

Hi,

I'm currently doing an EA which use Bollinger Bands, it sends Buy/Sell Order when I want but I also wanted it to close all Buy/Sell Position when it opened a new Sell/Buy Position, and it doesn't work like it should.. Sometimes, my function sends buy order like it has to, while there is previous sell order, and it doesn't close it, so that I have buy and sell orders at the same time..

I've attached a screen with the backtest:

Here is my close function, it closes orders sometimes, but not always, like if it couldn't close several orders at the same time:

void closeBollinger(){
    int i = 0;
    string bollinger = checkForBollinger();
    if(bollinger == "UPPER_BREAK" || bollinger == "LOWER_HIT"){
        for(i = 0; i < OrdersTotal(); i++){
            OrderSelect(i, SELECT_BY_POS);
            if(OrderType() == OP_SELL && OrderMagicNumber() == MAGICBOL)      OrderClose(OrderTicket(), OrderLots(), Ask, 3);
        }
    }else if(bollinger == "LOWER_BREAK" || bollinger == "UPPER_HIT"){
        for(i = 0; i < OrdersTotal(); i++){
            OrderSelect(i, SELECT_BY_POS);
            if(OrderType() == OP_BUY && OrderMagicNumber() == MAGICBOL)   OrderClose(OrderTicket(), OrderLots(), Bid, 3);
        }
    }
}

And the orderSend function:

void orderBollinger(int stopLossMargin = 30){
    string bollinger = checkForBollinger();
    int ticket;
    double curUpperBand = iBands(NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_UPPER, 0);
    double curLowerBand = iBands(NULL, 0, 20, 2, 0, PRICE_CLOSE, MODE_LOWER, 0);

    //Buy Orders
    if(bollinger == "UPPER_BREAK") ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, curUpperBand - stopLossMargin * Point, 0, "", MAGICBOL);
    else if(bollinger == "LOWER_HIT") ticket = OrderSend(Symbol(), OP_BUY, Lots, Ask, 3, Low[1] - 400 * Point, 0, "", MAGICBOL);

    //Sell Orders    
    else if(bollinger == "LOWER_BREAK") ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, curLowerBand + stopLossMargin * Point, 0, "", MAGICBOL);
    else if(bollinger == "UPPER_HIT") ticket = OrderSend(Symbol(), OP_SELL, Lots, Bid, 3, High[1] + 400 * Point, 0, "", MAGICBOL);

}
 

When working through orders always count down not up . . .

for(i = 0; i < OrdersTotal(); i++){  // <--- No

for(i = OrdersTotal()-1; i >= 0; i--){   // <--- Yes

. . . this may be part of your problem . . even if it's not it's still good practice.

Does your OrderSelect work ? always check . . .

OrderSelect(i, SELECT_BY_POS);    // <--- No
            if(OrderType() == OP_SELL && OrderMagicNumber() == MAGICBOL)      OrderClose(OrderTicket(), OrderLots(), Ask, 3);


if(  OrderSelect(i, SELECT_BY_POS)  && OrderType() == OP_SELL && OrderMagicNumber() == MAGICBOL)        // <--- Yes
   OrderClose(OrderTicket(), OrderLots(), Ask, 3);
 
Are you checking if your OrderSend worked and reporting any errors ?
 
RaptorUK:

When working through orders always count down not up . . .

. . . this may be part of your problem . . even if it's not it's still good practice.


I don't know why, but it works with:

for(i = OrdersTotal()-1; i >= 0; i--)

Thanks :)

 
Arkenis:


I don't know why, but it works with:

Thanks :)

If you count up and then Close an order you will skip one . . .
 
RaptorUK:
If you count up and then Close an order you will skip one . . .

Exact ! Thanks