Close the position???

 

Why doesn't works? I need to close the position BUY when new bar is close below close earlier bar the same on SELL close then BAR close under the earlier close bar:

if(OrdersTotal()>0){

for(i=1; i<=OrdersTotal(); i++){ // Cycle searching in orders

if (OrderSelect(i-1,SELECT_BY_POS)==true){

if(iClose(NULL,PERIOD_M30,2)>iClose(NULL,PERIOD_M30,1)){

if(OrderMagicNumber()==MagicNumber1&&closebuy==1) { OrderClose(OrderTicket(),OrderLots(),Bid,Slippage,CLR_NONE); }

}

if(iClose(NULL,PERIOD_M30,2)<iClose(NULL,PERIOD_M30,1)){

if(OrderMagicNumber()==MagicNumber2&&closesell==1) { OrderClose(OrderTicket(),OrderLots(),Ask,Slippage,CLR_NONE); }

}

}

}

}

 
pietra997:

Why doesn't works? I need to close the position BUY when new bar is close below close earlier bar the same on SELL close then BAR close under the earlier close bar:

*Use Alert or Print or Comment to Debug your Error.

*Use GetLastError() when Debugging your codes.

*Count back-ward when Closing or Deleting Orders.

*Use the Search Box in the Upper-Right of the Site more Info.

 

Your for loop is wrong, the first order position is 0 not 1 . . and the last is OrdersTotal() - 1

Always count DOWN if you are deleting or closing orders.

You aren't checking the OrderType() . . . how do you know what is a buy and what is a sell ?

Check the return values from your OrderClose() and if it fails report the error to the log . . . . it will help you.

 
RaptorUK:

Your for loop is wrong, the first order position is 0 not 1 . . and the last is OrdersTotal() - 1

Always count DOWN if you are deleting or closing orders.

You aren't checking the OrderType() . . . how do you know what is a buy and what is a sell ?

Check the return values from your OrderClose() and if it fails report the error to the log . . . . it will help you.

>Your for loop is wrong, the first order position is 0 not 1 . . and the last is OrdersTotal() - 1

For some reason the OP is using i-1 within the OrderSelect so that is not an error.

>You aren't checking the OrderType() . . . how do you know what is a buy and what is a sell ?

Looks like the OP is using different magic numbers for BUY and SELL orders. Again not an error (but idiosyncratic!)

Otherwise, I agree completely!

 
dabbler:

Otherwise, I agree completely!

LOL . . . too busy with my own code tonight so not paying too much attention . . . thanks for spotting my mistakes :-)
 
RaptorUK:
LOL . . . too busy with my own code tonight so not paying too much attention . . . thanks for spotting my mistakes :-)

It not only happens to you It Happens me also ...... and I think no one can say it doesn't happen to me... but gladly someone else will spot our mistakes
 
Ok I have something like that, ther is no error but doesn't close position after new bar ;/ anybody know what is wrong?



if(Hour()>7 && 21>Hour()){


// Open buy direct
if(BarOneUp==1&&BarTwoUp==1&&BarThreeUp==1&&halt1!=1){
 ticket=OrderSend(Symbol(),OP_BUY,Lots,NormalizeDouble(Ask,Digits),1,slb,tpb,"Candle bug buy order",MagicNumber1,0,Blue);
 closebuy=1;
 }
// Open sell direct
if(BarOneDown==1&&BarTwoDown==1&&BarThreeDown==1&&halt2!=1){
  ticket1=OrderSend(Symbol(),OP_SELL,Lots,NormalizeDouble(Bid,Digits),1,sls,tps,"Candle bug sell order",MagicNumber2,0,Green);
closebuy=1;
 }
}
//-------------------------------------------------------------------------------------------------
// Closing criteria
//-----------------------------------------------------------------------------------------------
if(Volume[0]>1) return;
                                 if(iOpen(NULL,PERIOD_M30,1)>iClose(NULL,PERIOD_M30,1))int CloseBuy=1;
                                 if(iOpen(NULL,PERIOD_M30,1)<iClose(NULL,PERIOD_M30,1))int CloseSell=1;
                                 Comment(CloseBuy,CloseSell);
if(closesell==1||closebuy==1){// start

                  
                           if(OrdersTotal()>0){
                               
                             for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
  
           if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
        
          
          if(OrderMagicNumber()==MagicNumber1&&CloseBuy==1) { OrderClose(ticket,OrderLots(),NormalizeDouble(Bid,Digits),Slippage,CLR_NONE); }
          
          if(OrderMagicNumber()==MagicNumber2&&CloseSell==1) { OrderClose(ticket1,OrderLots(),NormalizeDouble(Ask,Digits),Slippage,CLR_NONE); }
}                                   
                                }
                           }
                           }

}// stop
 
pietra997:
Ok I have something like that, ther is no error but doesn't close position after new bar ;/ anybody know what is wrong?

     if(OrderMagicNumber()==MagicNumber1&&CloseBuy==1) { OrderClose(ticket,OrderLots(),NormalizeDouble(Bid,Digits),Slippage,CLR_NONE); }
          
          if(OrderMagicNumber()==MagicNumber2&&CloseSell==1) { OrderClose(ticket1,OrderLots(),NormalizeDouble(Ask,Digits),Slippage,CLR_NONE); }

Do you think that ticket or ticket1 is same as OrderTicket().....

Check it.....

 
pietra997:
Ok I have something like that, ther is no error but doesn't close position after new bar ;/ anybody know what is wrong?

As I already said . . . .

"Check the return values from your OrderClose() and if it fails report the error to the log . . . . it will help you."

. . . why post asking for help and then ignore the advice given to you ? ?

 
if(OrdersTotal()>0){
    for(i=1; i<=OrdersTotal(); i++){          // Cycle searching in orders
        if (OrderSelect(i-1,SELECT_BY_POS)==true){ // If the next is available
            if(OrderMagicNumber()==MagicNumber1&&CloseBuy==1) { 
                OrderClose(ticket, OrderLots(), NormalizeDouble(Bid, Digits), 
                            Slippage, CLR_NONE); }
            if(OrderMagicNumber()==MagicNumber2&&CloseSell==1) { 
                OrderClose(ticket1, OrderLots(), NormalizeDouble(Ask, Digits), 
                            Slippage, CLR_NONE); }
}                                   
  1. The first IF is unnecessary
  2. Always count down
  3. Not checking order type
  4. ticket, ticket1 is undefined
  5. Never use NormalizeDouble, ever. Always unnecessary or wrong
  6. Use can use OrderClosePrice() instead of bid/ask
  7. Always test return codes.
  8. Not adjusting Slippage for 4/5 digit brokers?
From my code
//+------------------------------------------------------------------+
//| Partial order close.                                             |
//+------------------------------------------------------------------+
bool    CloseOrder(int ticket=EMPTY, double size=INF){  // INF == entire.
    if      (ticket == EMPTY)   ticket = OrderTicket();
    else if (!OrderSelect(ticket, SELECT_BY_TICKET)){
        Alert("OrderSelect(",ticket,",ticket) failed: ", GetLastError());
                                                                return(false); }
    double  minLot      = MarketInfo(chart.symbol, MODE_MINLOT),
            lotStep     = MarketInfo(chart.symbol, MODE_LOTSTEP),
            sizeCurr    = OrderLots(),
            sizeClose   = MathRound(size/lotStep)*lotStep,
            sizeRem     = sizeCurr - sizeClose;

    if (sizeClose < minLot)                                     return(false);
    if (sizeRem   < minLot){    sizeClose = sizeCurr;   // Close all
        color   op.color    = IfI(Color.Buy, Color.Sell);   }
    else        op.color    = Aqua;

    if (GetTradeContext() < TC_LOCKED)                          return(false);
    if (OrderClose( ticket, sizeClose, OrderClosePrice(),
                    Slippage.Pips*pips2points, op.color )){
        RelTradeContext();                                      return(true);  }
    Alert("OrderClose(ticket=", ticket, ", ...) [1] failed: ", GetLastError());
    RelTradeContext();      // After GetLastError
                                                                return(false);
}
void CloseAllOrders(int op = -1){
    for(int iPos = OrdersTotal()-1; iPos >= 0 ; iPos--) if (
        OrderSelect(iPos, SELECT_BY_POS)                    // Only my orders w/
    &&  OrderMagicNumber() == Magic.Number                  // my magic number
    &&  OrderSymbol()      == chart.symbol                  // and my pair,
    &&  (op < 0 || op == OrderType())                       // and wanted type.
    ){  // Don't combine with &&'s Compiler bug.
        CloseOrder();
    }
Reason: