Invalid Trade Parameters on BUY_STOP

 
I'm testing an EA and I occasionally get an error when opening a buy stop or sell stop.  I am setting the price at 30 pips above the ask, which should be  valid.  I have used MarketInfo(Symbol(),MODE_STOPLEVEL) to get the allowed stop value and still get this error on occasion.  Can anyone tell me why I might be getting this error?

Buy Trade params:
Symbol: EURUSDpro
Lot Size: 0.08
Ask: 1.26326
StopAsk: 1.26356
Slip: 3
Stoploss: 1.26226
TakeProfit: 1.27326

Thanks in advance.

 
Which error ?
 
This is the error in the tester log.
2016.01.13 12:56:35.597 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Failed to OpenSingleSell, error #3 - invalid trade parameters.
2016.01.13 12:56:35.597 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Sell Trade params: Symbol: EURUSDpro Lot Size: 0.16 Bid: 1.27871 StopBid: 1.27841 Slip: 3 Stoploss: 1.27971 TakeProfit: 1.26871

 
Here is an example from the log where you can see that it tries to place a sell stop, it fails, and immediately after is able to place a successful sell stop.

2016.01.13 12:56:35.769 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: modify #16 sell 0.16 EURUSDpro at 1.27837 sl: 1.27836 tp: 1.27803 ok
2016.01.13 12:56:35.769 2014.10.16 17:22  Tester: order #17, sell 0.08 EURUSDpro is opened at 1.27816
2016.01.13 12:56:35.754 2014.10.16 17:22  Tester: order #16, sell 0.16 EURUSDpro is opened at 1.27837
2016.01.13 12:56:35.746 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Successfully placed order with OpenSingleSell
2016.01.13 12:56:35.746 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: open #17 sell stop 0.08 EURUSDpro at 1.27816 sl: 1.27946 tp: 1.26846 ok
2016.01.13 12:56:35.650 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Successfully placed order with OpenSingleSell
2016.01.13 12:56:35.650 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: open #16 sell stop 0.16 EURUSDpro at 1.27837 sl: 1.27967 tp: 1.26867 ok
2016.01.13 12:56:35.642 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Sell Trade params: Symbol: EURUSDpro Lot Size: 0.16 Bid: 1.27865 StopBid: 1.27835 Slip: 3 Stoploss: 1.27965 TakeProfit: 1.26865
2016.01.13 12:56:35.642 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Failed to OpenSingleSell, error # invalid trade parameters.
2016.01.13 12:56:35.642 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: OrderSend error 3

 
marcboggs:
This is the error in the tester log.
2016.01.13 12:56:35.597 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Failed to OpenSingleSell, error #3 - invalid trade parameters.
2016.01.13 12:56:35.597 2014.10.16 17:22  MCB.ScalpWiz.9001 EURUSDpro,M1: Sell Trade params: Symbol: EURUSDpro Lot Size: 0.16 Bid: 1.27871 StopBid: 1.27841 Slip: 3 Stoploss: 1.27971 TakeProfit: 1.26871

Check your code. We can't help without seeing it.
 
I think some variable smaller than stoplevel.
 
Alain Verleyen:
Check your code. We can't help without seeing it.
In this case, the StopPriceBuffer = 30.  I've wondering if it has something to do with the spread and 
StopPriceBuffer falling between the value returned by MarketInfo(Symbol(),MODE_STOPLEVEL) and the Ask

///////////////////////////////////////////////////////////////////////
//
// Opens buy/buy stop and multiplies lot size based on signal strength
//
bool OpenSingleBuy(int strength,string cmt) 
{

    int ticket = -1;

    RefreshRates();
    double AskStopPrice = Ask + StopPriceBuffer * Point;
    double BuyTP        = Ask + TakeProfit      * Point;
    double BuySL        = Ask - OpenStopLoss    * Point;
    double dLots        = PercentPurchase(Ask,AccountBalance(),strength);
    
    if(UseStops)
      ticket = OrderSend( Symbol(), OP_BUYSTOP, dLots, AskStopPrice, Slippage, BuySL, BuyTP, cmt, MagicNumber, ExpDate, Lime);
    else
      ticket = OrderSend( Symbol(), OP_BUY, dLots, Ask, Slippage, BuySL, BuyTP, cmt, MagicNumber, ExpDate, Lime);
    
    if(ticket < 0) 
    {
      Print ("Failed to OpenSingleBuy, error # ", Error(GetLastError()));
      Print("Buy Trade params: Symbol: " + Symbol() +
            " Lot Size: "   + dLots +
            " Ask: "        + Ask +
            " Bid: "        + Bid +
            " StopAsk: "    + AskStopPrice +
            " Slip: "       + Slippage +
            " Stoploss: "   + BuySL + 
            " TakeProfit: " + BuyTP);
      return (false);
    } 
    else 
    {
      PlaySound("alert2.wav");
      Print ("Successfully placed order with OpenSingleBuy");
      return (true);
    }
   return (false);
}
 
bool OpenSingleBuy(int strength,string cmt) 
{

    int ticket = -1;

    RefreshRates();
    double Price = Ask;
    if(UseStops) Price  = Ask + MathMax(StopPriceBuffer * Point,MarketInfo(Symbol(),MODE_STOPLEVEL)* Point);
    double BuyTP        = Price + TakeProfit * Point;
    double BuySL        = Price - OpenStopLoss * Point;
    double dLots        = PercentPurchase(Ask,AccountBalance(),strength);
    
    if(UseStops)
      ticket = OrderSend( Symbol(), OP_BUYSTOP, dLots, Price, Slippage, BuySL, BuyTP, cmt, MagicNumber, ExpDate, Lime);
    else
      ticket = OrderSend( Symbol(), OP_BUY, dLots, Price, Slippage, BuySL, BuyTP, cmt, MagicNumber, ExpDate, Lime);
    
    if(ticket < 0) 
    {
      Print ("Failed to OpenSingleBuy, error # ", Error(GetLastError()));
      Print("Buy Trade params: Symbol: " + Symbol() +
            " Lot Size: "   + dLots +
            " Ask: "        + Ask +
            " Bid: "        + Bid +
            " StopAsk: "    + Price +
            " Slip: "       + Slippage +
            " Stoploss: "   + BuySL + 
            " TakeProfit: " + BuyTP);
      return (false);
    } 
    else 
    {
      PlaySound("alert2.wav");
      Print ("Successfully placed order with OpenSingleBuy");
      return (true);
    }
   return (false);
}
 
marcboggs:
if you use minimum stoplevel, you must attention this

example:

to set buyStopLoss, you must use Bid

http://my.jetscreenshot.com/20838/20160114-xkkr-17kb

double BuySL        = Bid - OpenStopLoss    * Point;
double BuyTP       = Ask + OpenStopLoss    * Point;
 
Siti Latifah:
if you use minimum stoplevel, you must attention this

example:

to set buyStopLoss, you must use Bid

http://my.jetscreenshot.com/20838/20160114-xkkr-17kb

I knew there was something I was missing.  I could have looked at some of my other EAs to see that, too.  

Unfortunately, the same error is being thrown when sending buy_stop/sell_stop order.  Again, it is not every time.  The good news:  The net profit is identical when running tests with same parameters.
 
Trinh Dat:
Hi Trinh,

I tried your suggestion and I am met with the same result:

MCB.ScalpWiz.9001 EURUSDpro,M1: Buy Trade params: Symbol: EURUSDpro Lot Size: 0.02 Ask: 1.18932 Bid: 1.18912 StopAsk: 1.19002 Slip: 3 Stoploss: 1.18002 TakeProfit: 1.20002
MCB.ScalpWiz.9001 EURUSDpro,M1: Failed to OpenSingleBuy, error # invalid trade parameters.