Duplicate orders being sent? - page 2

 
DomGilberto:
I'm confused how you successfully use "OrderSelect()" before you can successfully use OrderTicket? I thought OrderTicket was an integral parameter to OrderSelect :s?
Use a loop, find the order you want then exit the loop, then you have the order you want selected.
 
ydrol:

The OrderTicket page is very clear, no ? Look at OrderSelect() use here Loops and Closing or Deleting Orders

No its not.... It's telling me I can use OrderSelect by assuming I know what the order ticket number already is, by the example it gives... I do not know what the order ticket is... therefore, if I cannot use "OrderTicket()" within OrderSelect because I am being told that I need to successfully select the order FIRST; yet I do not know how to select the relevant Order via OrderSelect without the order ticket number...

Sample:
  if(OrderSelect(12470, SELECT_BY_TICKET)==true)
    {
     Print("order #12470 open price is ", OrderOpenPrice());
     Print("order #12470 close price is ", OrderClosePrice());
    }
  else 

 
RaptorUK:
Use a loop, find the order you want then exit the loop, then you have the order you want selected.

Like this? (quickly written)
for(int c=OrdersTotal()-1; c>=0; c--)
  {  
    
  int ticketnumber = 0;
  if(OrderTicket() > 0)
     {
     ticketnumber = OrderTicket();
     }


    if(OrderSelect(ticketnumber,SELECT_BY_TICKET, MODE_TRADES))
      if(OrderMagicNumber()==MagicNumber)
         if(OrderSymbol() == Symbol())           
         {
            if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
               {
               CloseHalfOrder1(); 
               }
   
            if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
               {
               CloseHalfOrder(); 
               }   
         }
   }

}  
 
DomGilberto:
Like this? (quickly written)

Nope, you can't use OrderTicket() until you have selected your order . . .

  if(OrderTicket() > 0)
     {
     ticketnumber = OrderTicket();
     }

. . . if you don't know the ticket number you select by position NOT by ticket.

 
DomGilberto:

No its not.... It's telling me I can use OrderSelect by assuming I know what the order ticket number already is, by the example it gives... I do not know what the order ticket is... therefore, if I cannot use "OrderTicket()" within OrderSelect because I am being told that I need to successfully select the order FIRST; yet I do not know how to select the relevant Order via OrderSelect without the order ticket number...

Sample:


Look at the second link I posted.
 

Ah ok gotchya. I can see where I was going wrong by just comparing with my other for loops, and how I have correctly written them in the past... I'm hoping that fingers crossed that was the issue in relation to why I was getting duplicate orders.... Also, some of the pairs will not close the position in profit when it should do. Is this likely to do with the way I am trying to use OrderSelect within the Loop?

int PositionIndex; 

int TotalNumberOfOrders;

TotalNumberOfOrders  = OrdersTotal();

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
  {  
    if( OrderMagicNumber() == MagicNumber  // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUY           // <-- is the Order a Buy Order ? 
      ||   OrderType() == OP_SELL ) )      // <-- or is it a Sell Order ?          
         {
            if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_SELL)
               {
               CloseHalfOrder1(); 
               }
   
            if(OpenOrdersThisPair(Symbol())>0 && OrderSymbol()==Symbol() && OrderType()==OP_BUY)
               {
               CloseHalfOrder(); 
               }   
         }
   }

} 
 

You are still not selecting the order first

int PositionIndex; 

int TotalNumberOfOrders;

TotalNumberOfOrders  = OrdersTotal();

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --) 
  { 
    OrderSelect(PositionIndex,SELECT_BY_POS) //SELECT ORDER
    if( OrderMagicNumber() == MagicNumber  // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUY           // <-- is the Order a Buy Order ? 
      ||   OrderType() == OP_SELL ) )      // <-- or is it a Sell Order ?          
         {
            if(OpenOrdersThisPair(Symbol())>0 && OrderType()==OP_SELL) //No Need for && OrderSymbol()==Symbol() as already checked
               {
               CloseHalfOrder1(); 
               }
   
            if(OpenOrdersThisPair(Symbol())>0 && OrderType()==OP_BUY) //No Need for && OrderSymbol()==Symbol() as already checked
               {
               CloseHalfOrder(); 
               }   
         }
   }

} 
 

Ah, yea sorry, I forgot that part lol! I did mean to include that haha! Thanks :P

int PositionIndex; 

int TotalNumberOfOrders;

TotalNumberOfOrders  = OrdersTotal();

for(PositionIndex = TotalNumberOfOrders - 1; PositionIndex >= 0 ; PositionIndex --)
  {  
   if( ! OrderSelect(PositionIndex, SELECT_BY_POS, MODE_TRADES) ) continue;
    if( OrderMagicNumber() == MagicNumber  // <-- does the Order's Magic Number match our EA's magic number ? 
      && OrderSymbol() == Symbol()         // <-- does the Order's Symbol match the Symbol our EA is working on ? 
      && ( OrderType() == OP_BUY           // <-- is the Order a Buy Order ? 
      ||   OrderType() == OP_SELL ) )      // <-- or is it a Sell Order ?          
         {
            if(OpenOrdersThisPair(Symbol())>0 && OrderType()==OP_SELL)
               {
               CloseHalfOrder1(); 
               }
   
            if(OpenOrdersThisPair(Symbol())>0 && OrderType()==OP_BUY)
               {
               CloseHalfOrder(); 
               }   
         }
   }

}  
 
Thanks guys for you help :)
 
I'm actually going through my entire code now and wanted to double check all my loops. I assume this is ok?
int start()
 {
   if(IsNewCandle())
      {
      CheckForMaTrade();//signals, deletions and candle trails only need checked on a new candle.
      }

   if(OpenOrdersThisPair(Symbol())>0)    <<<<<<<<<<----- I assume this is logical and wouldn't cause any conflicts with other pairs using the same EA?
      {
      if(UseMoveToBreakEven)MoveToBreakEven();
      if(Use_MA_Trail)MA_Trail();
      }

 }


//+----------------------------------------------------------------------------------------------------------------------------------------+  
//| Check to see if any order open on this currency pair                                                                                   |
//+----------------------------------------------------------------------------------------------------------------------------------------+   

int OpenOrdersThisPair(string pair)
  {
   int total=0;
   for(int i=OrdersTotal()-1; i>=0; i--)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderSymbol()==pair) total++;
     }
   return(total);
  }
// If there is an order open, it will not place a new one^

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
Reason: