Break Even Function not working as expected

 

Hi All,

I've been going round in circles all day trying to figure this one out, so it is time to consult the blessed Forum masters.

I have a break even function that is called when a position is open, but it's not functioning correctly.

Here is the function code, all variables used are globals (bad practice I know, apologies for the insult to the forum).

StopBuffer is set by: ( StopBuffer = 400 * Point; ) in the Init funtion.

Any advice will be more than welcome!

void TrailTrade()
{
   for (int i = OrdersTotal() - 1; i >= 0; i--)
   {
      if (OrderSelect(i, SELECT_BY_POS, MODE_TRADES) == True)
      {
         if (OrderType() == OP_BUY)
         {
            if (BuyTrailed == False)
            {
               if (Bid > OrderOpenPrice() + StopBuffer)
               {
                  bool a = OrderModify(OrderTicket(), OrderOpenPrice(),  Bid - StopBuffer, OrderTakeProfit(), 0, clrGreen);
                  
                  BuyTrailed = True;               
               }
            }   
         }
         
         if (OrderType() == OP_SELL)
         {
            if (SellTrailed == False)
            {
               if (Ask < OrderOpenPrice() - StopBuffer)
               {
                  bool b = OrderModify(OrderTicket(), OrderOpenPrice(), Ask + StopBuffer, OrderTakeProfit(), 0, clrRed);
                  
                  SellTrailed = True;               
               }
            }   
         }      
      }
   }
}
 
To clarify on the current behavior of the algorithm: The break even is set as soon as the trade turns profitable, so once it crosses the spread.  I need it to wait 40 pips!  Hope you can help!