Limiting one entry per pair - page 3

 

I have developed the following codes and it is working fine. The code is developed to limit one order in a pair if the order is opened manually. 


void OnTick()

  {

//---


      for(int cnt  = OrdersTotal()-1; cnt >=0 ; cnt--)

         {

            bool result=false;

            if(OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES))

              {

                  RefreshRates();

                  string symbol = OrderSymbol();

                  int ticket = OrderTicket();

                  Print(OrdersTotal());

                  

                  if(OrdersTotal()>2)

                    {

                     for(int i=OrdersTotal()-2;i>=0;i--)

                       {

                         if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES)) 

                           {

                             if(OrderSymbol() == symbol && ticket !=OrderTicket() )

                               {

                                Print("Symbol: "+symbol+"; OrderSymbol: "+OrderSymbol());

                                 if(OrderType()== OP_BUY)

                                    result=OrderClose(OrderTicket(),OrderLots( ) ,MarketInfo(OrderSymbol( ),MODE_BID) ,50, CLR_NONE);

                                 if(OrderType()== OP_SELL)

                                    result=OrderClose(OrderTicket(),OrderLots( ) ,MarketInfo(OrderSymbol( ),MODE_ASK) ,50,CLR_NONE);

                                 ////Print("Symbol: "+symbol+"; OrderSymbol: "+OrderSymbol());

                                 break;

                               }

                             else continue;

                           }

                       }

                    }

                  int Err=GetLastError();

                  if(Err!= 0)

                  Print ("Error Closing Trade " , Err);

               }

         }

  }

//+------------------------------------------------------------------+

Reason: