how to close ?

 

if buy Profit + sell Profit>0;

I want to close the buy ticket and the sell ticket, the lots is difficult. but it is error. how to slove it ?

//close 
void zcloseall(){
   bool   result;
   double bprice;
   int    cmd,error;

int total = OrdersTotal();
   for (int i = total - 1; i >= 0; i --)
   {

      if(OrderSelect(i,SELECT_BY_POS))
     {
      cmd=OrderType();
      //---- if  order is buy 
      if(cmd==OP_BUY )
        {bprice=Bid;
         while(true)
           {
             Profit=OrderProfit();
             k=OrderTicket();
             // call sell ticket
             mcloseall();
            
             
           }
        }
     }
     
   else Print( "Error when order select ", GetLastError());
   }


}


void mcloseall(){
   bool   result;

    double sprice;
   int    cmd,error;

int total = OrdersTotal();
   for (int i = total - 1; i >= 0; i --)
   {

      if(OrderSelect(i,SELECT_BY_POS))
     {
      cmd=OrderType();
    //---- if  order is sell
      if(cmd==OP_SELL)
        {
         while(true)
           {sprice=Ask;
          
             if (Profit+OrderProfit()>0&&Profit+OrderProfit()<5)
               { result=OrderClose(OrderTicket(),OrderLots(),sprice,3,CLR_NONE) ;
                 OrderClose(k,OrderLots(),sprice,3,CLR_NONE) ;
               }
             
             
           }
        }
     }
     
   else Print( "Error when order select ", GetLastError());
   }
//----

}
 

To close orders use OrderClosePrice () function instead of Bid or Ask or any other price.

Example: "OrderClose ( OrderTicket (), OrderLots (), OrderClosePrice (), 0 ) ;"

 
leyou1233:

if buy Profit + sell Profit>0;

I want to close the buy ticket and the sell ticket, the lots is difficult. but it is error. how to slove it ?

First sum the profit, then close all
double total=0;
for(pos = OrdersTotal()-1; pos >= 0 ; pos--) if (
    OrderSelect(pos, SELECT_BY_POS)                 // Only my orders w/
&&  OrderMagicNumber()  == magic.number             // my magic number
&&  OrderType()         <= OP_SELL ){               // open orders only,
&&  OrderSymbol()       == Symbol()                 // and my pair.
    total += OrderProfit();
}
if (total > 0) 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.
    if (!OrderClose( OrderTicket(), OrderLots(), OrderClosePrice(),
                     Slippage.Pips*pips2points, CLR_NONE) ))    Alert(
        "OrderClose(ticket=", OrderTicket(), ", ...) failed: ", GetLastError());
}
Reason: