invalid stops / avoitenko function question

 

Hello all.  I am trying to put together an EA that uses Limit Pending Orders.  This is the function used.  As one can see, it is the exact same as the function in the avoitenko EA, except ORDER_TYPE_BUY_STOP has been changed to ORDER_TYPE_BUY_LIMIT.  The function is only called when the current tick bid value is above Calc High - order step (by over 15 pips).  The buy limit order is set below the current price.  However, The EA fails to open any orders and thus does not execute any trades.  I see in the exerts log and the journal tab, error 10016, invalid stops, buy order at 1.27354, sl 1.27154, tp 1.27654.  That information.   Why is that invalid stops?  Why won't the function work after changing just the order type?  it works in the avoitenko EA  How can this be fixed?  I've looked around the forum and docs but can't seem to answer my own question.  If anyone could help, that would be greatly appreciated.

 

 void OpenOrderBuyStop(void)

//+------------------------------------------------------------------+

  {

   lot=Calculate_Lot(LOT,LOT_TYPE,ORDER_TYPE_BUY); //Do not specify ORDER_TYPE_BUY_STOP.


   request.price=NormalizeDouble(CalcHigh-order_step*_Point+spread,_Digits);


   //--- Adjust the price of orders

   if(tick.ask+stop_level*_Point>request.price) request.price=tick.ask+stop_level*_Point;


   //--- Calculation of stop loss

   if(stop_loss==0) request.sl=0;

   else request.sl=NormalizeDouble(request.price-MathMax(stop_loss,stop_level)*_Point-spread,_Digits);


   //--- Calculation of take profit

   if(take_profit==0) request.tp=0;

   else request.tp=NormalizeDouble(request.price+MathMax(take_profit,stop_level)*_Point-spread,_Digits);


   request.action       = TRADE_ACTION_PENDING;

   request.symbol       = _Symbol;

   request.volume       = lot;

   request.deviation    = slippage;

   request.type         = ORDER_TYPE_BUY_LIMIT;

   request.type_filling = ORDER_FILLING_AON;

   request.type_time    = ORDER_TIME_GTC;

   request.comment      = IntegerToString(MAGIC_NUMBER);

   request.magic        = MAGIC_NUMBER;


   OrderSend(request,result);

   if(result.retcode==10009 || result.retcode==10008)

     {

      buy_open=false;

      Print("Opened an order BuyStop #",result.order);

     }

   else

      printf("Error opened an order BuyStop. Error code: %d",result.retcode);


  }

Documentation on MQL5: Standard Constants, Enumerations and Structures / Trade Constants / Order Properties
  • www.mql5.com
Standard Constants, Enumerations and Structures / Trade Constants / Order Properties - Documentation on MQL5