Help with trailing stop !!

 

Hi all,

I have a problem with my EA, i created a trailing stop function, it works but in the tester is says "Order modify error 1" - i suppose this is for modifying the already modified order, but i need help for solving the problem - How can i make my function modify only once? Thanks in advance

     void trailingstop ()
    {
    bool modify = false;
  if(UseTrailingStop)
  {
    for(int i =0;i<OrdersTotal();i++)
    {
    if(!OrderSelect(i,SELECT_BY_POS))
    continue;
    if(  OrderMagicNumber()==magic &&  OrderType()==OP_BUY && Ask-OrderOpenPrice()>trailingcriteria*point && Ask>OrderOpenPrice())
    {
  modify =  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()+trailingstopsize*point,OrderTakeProfit(),0,clrAliceBlue);
  
    }
  else if ( OrderMagicNumber()==magic && OrderType()==OP_SELL &&  OrderOpenPrice()-Bid>trailingcriteria*point && Bid<OrderOpenPrice()) 
  {
    modify =  OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice()-trailingstopsize*point,OrderTakeProfit(),0,clrAliceBlue);  
   
  }
  }
  //------------
  if(modify)
  {
  Print("TRAILING STOP initialized!!!");
  }
  }
  //--------------
   return ;
    } 

 

 

Use bid when calculating TS for a buy, ask for a sell.

Before modifying the stop, check that the new stop will be higher than the existing stop (for a buy)

 
Thanks
Reason: