confusing trailing stop

 
Hi,

My trailing stop only works for sell order but not for buy order.

here's the code:

   for(int i=0; i<OrdersTotal(); i++)
     {
      OrderSelect(i,SELECT_BY_POS,MODE_TRADES);
      if(OrderMagicNumber()==magic || OrderSymbol()==Symbol())
        {
         if(OrderType()<=OP_SELL)
           {
            if(OrderType()==OP_BUY)
              {
               if(Bid-OrderOpenPrice()>Point*ts)
                 {
                  if(OrderStopLoss()<Bid-Point*ts)
                    {
                     while(Bid-OrderStopLoss()>Bid-Point*ts)
                       {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*ts,OrderTakeProfit(),0);
                        return(0);
                       }
                    }
                 }
              }
            else
              {
               if(OrderOpenPrice()-Ask>Point*ts)
                 {
                  if(OrderStopLoss()>Ask+Point*ts)
                    {
                     while(OrderStopLoss()+Ask>Ask+Point*ts)
                       {
                        OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*ts,OrderTakeProfit(),0);
                        return(0);
                       }
if I change while(Bid-OrderStopLoss()>Bid-Point*ts) to while Bid-OrderStopLoss()<Bid-Point*ts), it works both ways (for sell and buy orders), but I got huge loss as the result.

How can this be?

thanks.
note: I use "while" since without it the trailing stop will move up and down as the price moves and I usually end up with losses.
 

I can't follow your logic. Good luck.

 

   for(int i=0; i<OrdersTotal(); i++)
     {
      if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES) &&
         OrderMagicNumber()==magic && OrderSymbol()==Symbol())
        {
         if(OrderType()==OP_BUY)
           {
            if(Bid-Point*ts>OrderOpenPrice() &&
               Bid-Point*ts>OrderStopLoss())
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),Bid-Point*ts,OrderTakeProfit(),0);
              }
           }
         else if(OrderType()==OP_SELL)
           {
            if(Ask+Point*ts<OrderOpenPrice() &&
               Ask+Point*ts<OrderStopLoss())
              {
               OrderModify(OrderTicket(),OrderOpenPrice(),Ask+Point*ts,OrderTakeProfit(),0);
              }

  1. OrderSelect return value must be tested.
  2. Are you filtering for orders with certain magic number OR with chart symbol?
  3. Your trailing stop may and may not work because the return(0) will skip the rest of your orders.
  4. The condition test for the while loop is simply wrong.