Error 130

Hi
just wondering if anyone can help me with this. I just keep getting error 130 when i run this. I looked on the mql4 website and says the code is a due to invalid stoploss value. I looked through the code many times but see no error in the logic. Also, the function eventually manages to produce a valid ordermodify but only after 20-40 error 130s. if anyone could tell me what i did wrong, I really appreciate it.
Thanks

BreakEvenAt = 25;
BreakEvenProfit = 5;

void BreakEvenFunction()
{
Spread = Ask - Bid;
for(int i=0; i<OrdersTotal();i++)
{
if(OrderSelect(i,SELECT_BY_POS,MODE_TRADES)==false) break;
if(OrderType()==OP_BUY)
{
//if Profit > 25pips
if((Bid-OrderOpenPrice()) > (BreakEvenAt*Point))
{
//if StopLoss is below Open Price
if(OrderStopLoss() < OrderOpenPrice())
{
OrderModify(OrderTicket(),OrderOpenPrice(), OrderOpenPrice() + (BreakEvenProfit+1)*Point + Spread, OrderTakeProfit(),0,GreenYellow);
return(0);
}
}

  }
  if(OrderType()==OP_SELL)
  {
     //if profit is &gt; 25Pips
     if((OrderOpenPrice()-Ask) &gt; (BreakEvenAt*Point))
     {
        //if StopLoss is above Open Price
        if(OrderStopLoss() &gt; OrderOpenPrice())
        {  
           OrderModify(OrderTicket(),OrderOpenPrice(),OrderOpenPrice() - (BreakEvenProfit+1)*Point - Spread, OrderTakeProfit(),0,GreenYellow);
           return(0);           
        }
     }        
  }

}
}

Hi,
Does the error only happen on sell orders, or both?

First of all, I have seen MT4 build 220 giving Error 130 on sell orders without any obvious reason at least during backtests.

the value of the stop level you are setting may not be within legal stop levels for the currency pair.
For example, for your buy orders you have:

OrderOpenPrice() + (BreakEvenProfit+1)*Point + Spread

this will try to set a stop level at Price + (5+1)pips + 2pips, i.e. 8 pips away from the order open price, while many currency pairs have wider stop level requirements. Also, if there is enough time elapsed between when you opened the order and are trying to set the stop levels, the current Bid/Ask could be significantly different from your OrderOpenPrice().

you can check this, for example,

if (OrderType == OP_SELL) {
if ( MarketInfo(Symbol(), MODE_STOPLEVEL) >
MathAbs( Bid - Your_new_SL_price) )
{
stop loss is too close; widen the gap;
}
}

similarly, OP_BUY will be

if (OrderType == OP_BUY) {
if ( MarketInfo(Symbol(), MODE_STOPLEVEL) >
MathAbs( Ask - Your_new_SL_price) )
{
stop loss is too close; widen the gap;
}
}

or something similar…

Minor [B]correction[/B]:

if (OrderType == OP_SELL) {
     if ( MarketInfo(Symbol(), MODE_STOPLEVEL) [B]* Point[/B] > 
              MathAbs( Bid - Your_new_SL_price) )    
     {
            stop loss is too close; widen the gap;
     }
}

similarly, OP_BUY will be

if (OrderType == OP_BUY) {
     if ( MarketInfo(Symbol(), MODE_STOPLEVEL) [B]* Point[/B]> 
             MathAbs( Ask - Your_new_SL_price) )    
     {
            stop loss is too close; widen the gap;
     }
}