Why is this happening ??

 
This is my code for a trailing stop that should loop through all open orders, find the matching symbol and modify it.
It works fine but once the order is modified it continues to try and modify the same order over and over.
This is message in journal:

2005.07.05 07:00:00 '104539': modify order #1056277 sell 12.80 EURUSD at 1.1975 sl: 1.2191 tp: 0.0000 -> sl: 1.2191 tp: 0.0000

=================================================

for ( i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if( OrderSymbol()==Symbol() && OrderType()==OP_BUY)
{
if(OrderStopLoss()<llow- Pips*Point)
{
OrderModify(OrderTicket(),OrderOpenPrice(), llow- Pips*Point, OrderTakeProfit(),0,Green); }
return(0);
}
}

for ( i = 0; i < OrdersTotal(); i++) {
OrderSelect(i, SELECT_BY_POS, MODE_TRADES);

if( OrderSymbol()==Symbol()&& OrderType()==OP_SELL)
{
if((OrderStopLoss() >hhigh+ Pips*Point))
{
OrderModify(OrderTicket(),OrderOpenPrice(),hhigh+ Pips*Point,OrderTakeProfit(),0,Red ); }
return(0);
}
}
 
Well I have tried for a week solid to try and fix my problem so I ask for the forums help again..

This is a simple parabolic sar expert.

It has the same problem as above, it works fine but once the order is modified it continues to try and modify the same order over and over ............

These are the messages in my journal:

2005.07.12 15:22:38 '104539': modify order #1131284 buy 14.30 EURUSD at 1.1970 sl: 1.2088 tp: 0.0000 -> sl: 1.2088 tp: 0.0000
2005.07.12 15:22:38 '104539': modify order #1131284 buy 14.30 EURUSD at 1.1970 sl: 1.2088 tp: 0.0000 -> sl: 1.2088 tp: 0.0000
2005.07.12 15:22:36 '104539': modify order #1131284 buy 14.30 EURUSD at 1.1970 sl: 1.2088 tp: 0.0000 -> sl: 1.2088 tp: 0.0000
2005.07.12 15:22:36 '104539': modify order #1131284 buy 14.30 EURUSD at 1.1970 sl: 1.2088 tp: 0.0000 -> sl: 1.2088 tp: 0.0000

PLEASE Anyone what is wrong ?????????????

extern double TakeProfit = 0;
extern double Lots = 1;
extern double TrailingStop = 0;
extern double Stoploss = 0;
extern double Perc =10;
extern double step = 0.02;
extern double max = 0.2;

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
  double Levv,  equity, Lotss,trd,sar ;
  int cnt=0, total ;
         
  sar= iSAR(NULL, 0, step,max,0);
         
 //======================================================================
           
  if(Bars<2)
  {
    Print("bars less than 100");
    return(0); }
  //======================================================================   
  total=OrdersTotal();
   trd =0 ;
   
   for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()) trd++;
}
  
 //======================================================================
  {
    if(AccountFreeMargin()<(1*Lots))
    {
      Print("We have no money");
      return(0);}
      
   equity =AccountEquity();
   
  Lotss = MathAbs((equity*Perc/100/100)/10);
   
  Levv=NormalizeDouble(Lotss,1);
          
 //======================================================================
  
    // (BUY)
    
    if (trd != 1 && Bid > sar )
    {
      OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,0,0,0,Blue);
      
      return(0);
    }
    
    
    // (SELL)
    if ( trd != 1 && Ask < sar ) 
    {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,3, 0,0,0,0,0,Red);
     
      
      return(0);
    } 
   }
          
 //======================================================================
                
 //======================================================================      
      
  //CLOSE
  
   for ( int i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_BUY)   
           {
            
            if( Ask < sar )
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
                 return(0); 
                }
                }
                }
               
  for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_SELL)   
           {
            
            if(Bid > sar )
                {
                 OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
                 return(0); 
                }
                }
                }
 //================================================================================
               
               
              for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if( OrderSymbol()==Symbol())   
           
        {
         if(OrderType()==OP_BUY&& OrderSymbol()==Symbol())   
           {            
             if(TrailingStop>0)  
              
                 {
                  if(OrderStopLoss()!=sar)
                    {
          OrderModify(OrderTicket(),OrderOpenPrice(),sar, OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              } 
            
            else
               if(OrderType()==OP_SELL && OrderSymbol()==Symbol())
   {
    if(TrailingStop>0)  
              
                 {
                  if(OrderStopLoss()!=sar)
                    {
          OrderModify(OrderTicket(),OrderOpenPrice(),sar, OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }}}}}}
                         

 
sorry. there is our bug. fixed build coming soon.
 
mpfx,

One thing I noticed is I don't think you can use this:

if(OrderType()==OP_BUY...

You have to use this:

if(OrderType()==0...

Here's the list of OrderTypes:
OP_BUY = 0
OP_SELL = 1
OP_BUYLIMIT = 2
OP_SELLLIMIT = 3
OP_BUYSTOP = 4
OP_SELLSTOP = 5

The other thing I noticed is this:

The value for sar might be 1.20882348 which isn't equal to 1.2088, so that's why it keeps trying to modify it. Try modifying the value for sar by doing something like this:

sar=MathRound(sar/Point)*Point;
 
Thanks Slawa, look forward to next build......

======================================================

Thanks Vooch, I did not know about the order types list great bit of info ta.

I will wait till next build and add the mathround code, again thanks :)
 
download updated build 176 from our site and try your expert again
 
I downloaded from your site, installed and now the platform freezes when I try to close down the charts that automatically loaded when mt is first installed....

The same problem with the trailing stop is there also..

The strange thing is that I have 4 experts working with trailing stops that is coded the same way yet 2 of them work fine and 2 have the problem above

this is the code, I have tried every combination possible with the same result...........

 for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
             if( OrderSymbol()==Symbol() && OrderType()==OP_BUY)  
              {                 
                if(OrderStopLoss()!=llow- Pips*Point)
                    {
            OrderModify(OrderTicket(),OrderOpenPrice(),llow- Pips*Point,OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }

     for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
    if( OrderSymbol()==Symbol()&& OrderType()==OP_SELL)  
              {                 
               
                  if((OrderStopLoss() !=hhigh+ Pips*Point))
                    {
       OrderModify(OrderTicket(),OrderOpenPrice(),hhigh+ Pips*Point,OrderTakeProfit(),0,Red);
                     return(0);
                    }
                 }
              }
 
 
sorry. wrong version was uploaded. download please again
 
no worries...

I downloaded, freezing fixed,

trailing stop still the same.........
 
This SAR code seems to work

I added vooch's code..

Yet the code in 3 posts above still has same problem yet works fine on 2 other experts..

extern double TakeProfit = 0;
extern double Lots = 1;
extern double TrailingStop = 0;
extern double Stoploss = 0;
extern double Perc =10;
extern double step = 0.02;
extern double max = 0.2;

//+------------------------------------------------------------------+
//| expert start function |
//+------------------------------------------------------------------+

int start()
{
  double Levv,  equity, Lotss, trd,sar,sar2 ;
  int cnt=0, total ;
         
  sar= iSAR(NULL, 0, step,max,0);
  sar2 =MathRound(sar/Point)*Point;
      
 //======================================================================
           
  if(Bars<2)
  {
    Print("bars less than 100");
    return(0); }
  //======================================================================   
  total=OrdersTotal();
   trd =0 ;
   
   for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
if (OrderSymbol()==Symbol()) trd++;
}
  
 //======================================================================
  {
    if(AccountFreeMargin()<(1*Lots))
    {
      Print("We have no money");
      return(0);}
      
   equity =AccountEquity();
   
  Lotss = MathAbs((equity*Perc/100/100)/10);
   
  Levv=NormalizeDouble(Lotss,1);
          
 //======================================================================
  
    // (BUY)
    
    if (trd != 1 && Bid > sar2 )
    {
      OrderSend(Symbol(),OP_BUY,Lots,Ask,3,0,0,0,0,0,Blue);
      
      return(0);
    }
    
    
    // (SELL)
    if ( trd != 1 && Ask < sar2 ) 
    {
      OrderSend(Symbol(),OP_SELL,Lots,Bid,3, 0,0,0,0,0,Red);
     
      
      return(0);
    } 
   }
          
 //======================================================================     
      
  //CLOSE
  
   for ( int i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_BUY)   
           {
            
            if( Ask < sar2 )
                {
                 OrderClose(OrderTicket(),OrderLots(),Bid,3,Violet); 
                 return(0); 
                }
                }
                }
               
  for ( i = 0; i < OrdersTotal(); i++) {
      OrderSelect(i, SELECT_BY_POS, MODE_TRADES);
      
      if(OrderType()<=OP_SELL && 
         OrderSymbol()==Symbol())   
        {
         if(OrderType()==OP_SELL)   
           {
            
            if(Bid > sar2 )
                {
                 OrderClose(OrderTicket(),OrderLots(),Ask,3,Violet); 
                 return(0); 
                }
                }
                }
 //================================================================================
   // Modify            
               
              for(cnt=0;cnt<total;cnt++)
     {
      OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
      if(OrderType()<=OP_SELL &&    
         OrderSymbol()==Symbol())  
        {
         if(OrderType()==OP_BUY)   
           {            
                   if(TrailingStop>0)  
              
                 {
                  if(OrderStopLoss()<sar2)
                    {
          OrderModify(OrderTicket(),OrderOpenPrice(),sar2, OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }         
else
   {
    if(TrailingStop>0)  
              
                 {
                  if(OrderStopLoss()<sar2)
                    {
          OrderModify(OrderTicket(),OrderOpenPrice(),sar2, OrderTakeProfit(),0,Green);
                     return(0);
                    }
                 }
              }
              }}           
               }}}   

 
before modify check old stoploss value. sar value may not changed from tick to tick but you modify orders in any case
Reason: