Error code 1 but not all the time ?

 
TRailing stop code ;

for(cnt=0;cnt<total;cnt++)
{
OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);

if(OrderType()<=OP_SELL && OrderSymbol()==Symbol()) //&& OrderMagicNumber()==MagicNumber
{

if(OrderType()==OP_BUY) //<-- Long position is opened
{
if(Bid-OrderOpenPrice()>TrailingStop*Point && OrderStopLoss()<Bid-TrailingStop*Point )
{

OrderModify(OrderTicket(), OrderOpenPrice(), Bid-Point*TrailingStop, OrderTakeProfit(), 0,Green);
return(0);
}
}
if(OrderType()==OP_SELL) //<-- Go to short position
{
if(OrderOpenPrice()-Ask>TrailingStop*Point && OrderStopLoss()>Ask+TrailingStop*Point)
{

OrderModify(OrderTicket(), OrderOpenPrice(), Ask+Point*TrailingStop, OrderTakeProfit(), 0,Red);//OrderOpenPrice() OrderTakeProfit()
return(0);
}
}
}
}

example log, order modify correctly, afer error afer correct ????

2007.08.28 23:24:03 2007.08.23 12:14 SurfConquest GBPUSD, H1: modify #32 buy 1. 00 GBPUSD at 1.9875 sl: 1.9923 tp: 1.9995 ok
2007.08.28 23:24:03 2007.08.23 12:14 SurfConquest GBPUSD, H1: OrderModify error 1
2007.08.28 23:24:03 2007.08.23 12:13 SurfConquest GBPUSD, H1: modify #32 buy 1. 00 GBPUSD at 1.9875 sl: 1.9922 tp: 1.9995 ok
2007.08.28 23:24:03 2007.08.23 12:13 SurfConquest GBPUSD, H1: modify #32 buy 1. 00 GBPUSD at 1.9875 sl: 1.9921 tp: 1.9995 ok
2007.08.28 23:24:03 2007.08.23 12:12 SurfConquest GBPUSD, H1: OrderModify error 1
2007.08.28 23:24:03 2007.08.23 12:12 SurfConquest GBPUSD, H1: OrderModify error 1
2007.08.28 23:24:03 2007.08.23 12:11 SurfConquest GBPUSD, H1: OrderModify error 1
2007.08.28 23:24:03 2007.08.23 12:11 SurfConquest GBPUSD, H1: modify #32 buy 1. 00 GBPUSD at 1.9875 sl: 1.9920 tp: 1.9995 ok

thank you for your assistance !
 
maje:

2007.08.28 23:24:03 2007.08.23 12:12 SurfConquest GBPUSD, H1: OrderModify error 1


Error code 1 means that there is no change, i.e. OrderModify() arguments are the same that corresponding parameters of the modified order.
This might be due to double comparison issue. Search highlighted keywords here for the answers.

As one of the workarounds try this

if (Bid - OrderOpenPrice() > (TrailingStop + 0.5) * Point && OrderStopLoss() < Bid - (TrailingStop + 0.5) * Point)
 

Some Brokers require a minimum difference from market price for TakeProfit or StopLoss Modify calls. Find same symbol existing open order in a demo or live account. In the Terminal window, Right-click on order and then click "Modify or Delete Order". At bottom of this window you will see minimum spread needed to avoid error 1. You will need to use Irtron's suggestion to hardcode a minimum modify difference.

 
wackena:

Some Brokers require a minimum difference from market price for TakeProfit or StopLoss Modify calls. Find same symbol existing open order in a demo or live account. In the Terminal window, Right-click on order and then click "Modify or Delete Order". At bottom of this window you will see minimum spread needed to avoid error 1. You will need to use Ortron's suggestion to hardcode a minimum modify difference.

Thank you with all, the problem is regulated by adding = 0.5.

Goodllock for conquest!
 
Okay, guys, I have just the same problem. I've been tweaking the code for quite a while now and before I go angry, I want to kindly ask you to look at the below code yourselves with your unbiased view and tell me, how do you think the Error 1 can be avoided. Just like in maje's case, it only occurs a couple of times.

What I want to achieve: go through all the open trades, if a trade is 200 points in profit, lock in 90 pips of profit by moving the stop loss. Do this every four hours.

The code:

void MoveStop()
{
  double open;
  int type;
  
  for(int count=OrdersTotal()-1; count>=0; count--)
  {
    if(OrderSelect(count,SELECT_BY_POS))
    {
      open = OrderOpenPrice();
      type = OrderType();
      
      if ( (type == OP_SELL) && (((open - Bid)/Point) > 200) )
      {
        if (OrderStopLoss() != (OrderOpenPrice() - 90*Point))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open - 90*Point),OrderTakeProfit(),0,Red);
        }
      }
        
      if ( (type == OP_BUY) &&  (((Bid - open)/Point) > 200) )
      {
        if (OrderStopLoss() != (OrderOpenPrice() + 90*Point))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open + 90*Point) ,OrderTakeProfit(),0,Blue);
        }
      }
    }
  }
}
 
janklimo:
Okay, guys, I have just the same problem. I've been tweaking the code for quite a while now and before I go angry, I want to kindly ask you to look at the below code yourselves with your unbiased view and tell me, how do you think the Error 1 can be avoided. Just like in maje's case, it only occurs a couple of times.

What I want to achieve: go through all the open trades, if a trade is 200 points in profit, lock in 90 pips of profit by moving the stop loss. Do this every four hours.

The code:

void MoveStop()
{
  double open;
  int type;
  
  for(int count=OrdersTotal()-1; count>=0; count--)
  {
    if(OrderSelect(count,SELECT_BY_POS))
    {
      open = OrderOpenPrice();
      type = OrderType();
      
      if ( (type == OP_SELL) && (((open - Bid)/Point) > 200) )
      {
        if (OrderStopLoss() != (OrderOpenPrice() - 90*Point))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open - 90*Point),OrderTakeProfit(),0,Red);
        }
      }
        
      if ( (type == OP_BUY) &&  (((Bid - open)/Point) > 200) )
      {
        if (OrderStopLoss() != (OrderOpenPrice() + 90*Point))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open + 90*Point) ,OrderTakeProfit(),0,Blue);
        }
      }
    }
  }
}

Dammit, note to myself: Use NormalizeDouble when doing this. Solved (for good I hope).

void MoveStop()
{
  double open;
  int type;
  
  for(int count=OrdersTotal()-1; count>=0; count--)
  {
    if(OrderSelect(count,SELECT_BY_POS))
    {
      open = OrderOpenPrice();
      type = OrderType();
      
      if ( (type == OP_SELL) && (((open - Bid)/Point) > 200) )
      {
        if (NormalizeDouble(OrderStopLoss(),2) != NormalizeDouble((OrderOpenPrice() - 90*Point),2))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open - 90*Point),OrderTakeProfit(),0,Red);
        }
      }
        
      if ( (type == OP_BUY) &&  (((Bid - open)/Point) > 200) )
      {
        if (NormalizeDouble(OrderStopLoss(),2) != NormalizeDouble((OrderOpenPrice() + 90*Point),2))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open + 90*Point) ,OrderTakeProfit(),0,Blue);
} } } } }
 

You code sets for 0.00 precision. For both 0.00 and 0.0000 precision pairs, You should use the Digits precision for the NormalizeDouble() function.

void MoveStop()
{
  double open;
  int type;
  
  for(int count=OrdersTotal()-1; count>=0; count--)
  {
    if(OrderSelect(count,SELECT_BY_POS))
    {
      open = OrderOpenPrice();
      type = OrderType();
      
      if ( (type == OP_SELL) && (((open - Bid)/Point) > 200) )
      {
        if (NormalizeDouble(OrderStopLoss(),Digits) != NormalizeDouble((OrderOpenPrice() - 90*Point),Digits))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open - 90*Point),OrderTakeProfit(),0,Red);
        }
      }
        
      if ( (type == OP_BUY) &&  (((Bid - open)/Point) > 200) )
      {
        if (NormalizeDouble(OrderStopLoss(),Digits) != NormalizeDouble((OrderOpenPrice() + 90*Point),Digits))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open + 90*Point) ,OrderTakeProfit(),0,Blue);        }
      }
    }
  }
}
 
wackena:

You code sets for 0.00 precision. For both 0.00 and 0.0000 precision pairs, You should use the Digits precision for the NormalizeDouble() function.

void MoveStop()
{
  double open;
  int type;
  
  for(int count=OrdersTotal()-1; count>=0; count--)
  {
    if(OrderSelect(count,SELECT_BY_POS))
    {
      open = OrderOpenPrice();
      type = OrderType();
      
      if ( (type == OP_SELL) && (((open - Bid)/Point) > 200) )
      {
        if (NormalizeDouble(OrderStopLoss(),Digits) != NormalizeDouble((OrderOpenPrice() - 90*Point),Digits))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open - 90*Point),OrderTakeProfit(),0,Red);
        }
      }
        
      if ( (type == OP_BUY) &&  (((Bid - open)/Point) > 200) )
      {
        if (NormalizeDouble(OrderStopLoss(),Digits) != NormalizeDouble((OrderOpenPrice() + 90*Point),Digits))
        {
          OrderModify(OrderTicket(),OrderOpenPrice(), (open + 90*Point) ,OrderTakeProfit(),0,Blue);        }
      }
    }
  }
}
Thanks, man, that's a good point. This one is written specifically for GBP/JPY though.
Reason: