Help with closing orders from my expert

 

Hi all, i want my expert to close sell order for a pair when it opens buy order for that pair and vice versa. I wrote the following code:

if (B1>10000&&Time0!=Time[0]){
      
      OrderSend(Symbol(),OP_SELL,Lots,Bid,SlipPage,StopLoss,TakeProfit,"Abdu EA",MagicNumberSell,0,Red);
      Time0=Time[0];
             
      if(OrderSelect(MagicNumberBuy, SELECT_BY_POS,MODE_TRADES)==true)  
      OrderClose(OrderTicket(),Lots,Ask,SlipPageClose,Blue); 
      PlaySound("Alert.wav");
      GetLastError( );
      if(AccountFreeMarginCheck(Symbol(),OP_SELL,Lots)<=0 || GetLastError()==134) return;
                            }
                       

However, the expert opens the sell order, but does not close the buy order that it had opened previously with the MagicNumberBuy.

I don't know how to let the expert know the ticket for the order that has to be closed, so i used the Magic Number.

I need some help, waiting for you reply sir, and thanks.

 
for (int i = OrdersTotal() - 1; i >= 0; i--)
   {
   if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)==true)
      {
      if (OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumberBuy)// or add && OrderType() == OP_BUY
         {
         OrderClose(OrderTicket(),Lots,Ask,SlipPageClose,Blue);
         }
      }
   }
 
qjol:


Thanks for your reply i placed the code instead of the two lines

and it didnt still work

 if(OrderSelect(MagicNumberBuy, SELECT_BY_POS,MODE_TRADES)==true)  
      OrderClose(OrderTicket(),Lots,Ask,SlipPageClose,Blue); 
 
OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),SlipPageClose,Blue);

& if it's doesn't go

put some Print(GetLastError()) or Alert(GetLastError()) to find out y

 
qjol:

& if it's doesn't go

put some Print(GetLastError()) or Alert(GetLastError()) to find out y


The error returned as follows:

2010.12.30 22:40:44 Abdu_First Of The Trend_EA EURUSD,H1: 3

 

u sure error 3

try

OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),SlipPageClose,Blue);
int err = GetLastError();
Print ("Last Error is: ", err);
 

I did it and returned with

2010.12.31 05:42:40 Abdu_First Of The Trend_EA EURUSD,H1: Last Error is: 3

 

you cant use magicnumber with OrderSelect.


 if(OrderSelect(MagicNumberBuy, SELECT_BY_POS,MODE_TRADES)==true)  
      OrderClose(OrderTicket(),Lots,Ask,SlipPageClose,Blue); 

loop through all orders and:

if(OrderMagicNumber() == MagicNumberBuy){
  //Do your stuff here
}


this is only an example, and should be a guidiance.

 
enotrek:

you cant use magicnumber with OrderSelect.


loop through all orders and:


this is only an example, and should be a guidiance.


Thanks for your help, but how could i loop through all the orders?
 
maybe this can help you

int cnt;
         
         for(cnt=0;cnt<OrdersTotal();cnt++)
               {
                  if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))
                     {
                        if(OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
                           {
                              if(OrderType()==OP_BUY)   // long position is opened
                                 {
                                    // should it be closed?
                                    
                                 }   
                              if(OrderType()==OP_SELL) // go to short position
                                 {
                                    // should it be closed?
                                              
                                 }
                           }
                     }
               }
 
Arwee:
maybe this can help you


for closing orders u should loop down

for(cnt = OrdersTotal() - 1; cnt >= 0; cnt--)
Reason: