OrderModify error 4051 & invalid stoploss for OrderModify function

 

Hi all I added a feature for auto lottery but after inserting it it gives me the problem of OrderModify error 4051 and invalid stoploss for OrderModify function


I will insert the code below, I can't figure out what the problem is



double lot;



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Expert::lotAdjust(string sym, double lots)
  {
   double value = 0;
   double lotStep = SymbolInfoDouble(sym, SYMBOL_VOLUME_STEP);
   double minLot  = SymbolInfoDouble(sym, SYMBOL_VOLUME_MIN);
   double maxLot  = SymbolInfoDouble(sym, SYMBOL_VOLUME_MAX);

   value = NormalizeDouble(lots / lotStep, 0) * lotStep;

   if(value < minLot)
      value = minLot;
   if(value > maxLot)
      value = maxLot;

   return(value);
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double Lot_Volume()
  {
   lot=(AccountBalance() / 1000) * 0.01;

   return(lot);
}



//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Expert::BuyOrder(string sym)
  {
   double bid = MarketInfo(sym,MODE_BID);
   double ask = MarketInfo(sym,MODE_ASK);
   double point=MarketInfo(sym,MODE_POINT);
   int digits=(int)MarketInfo(sym,MODE_DIGITS);

   if(digits == 3 || digits == 5)
      PipValue = 10;
   else
      PipValue = 1;

   double SL = ask - StopLoss*PipValue*point;
   if(StopLoss == 0)
      SL = 0;
   double TP = ask + TakeProfit*PipValue*point;
   if(TakeProfit == 0)
      TP = 0;
   int ticket = -1;
   if(true)
      ticket = OrderSend(sym, OP_BUY, lotAdjust(sym,Lot_Volume()), ask, Slippage, 0, 0, Commentary, Magic, 0, clrBlue);
   else
      ticket = OrderSend(sym, OP_BUY, lotAdjust(sym,Lot_Volume()), ask, Slippage, SL, TP, Commentary, Magic, 0, clrBlue);
   if(ticket > -1)
     {
      if(true)
        {
         bool sel = OrderSelect(ticket, SELECT_BY_TICKET);
         bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, clrBlue);
         if(ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }

     }
   else
     {
      Print("OrderSend() error - ", ErrorDescription(GetLastError()));
     }
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void Expert::SellOrder(string sym)
  {
   double bid = MarketInfo(sym,MODE_BID);
   double ask = MarketInfo(sym,MODE_ASK);
   double point=MarketInfo(sym,MODE_POINT);
   int digits=(int)MarketInfo(sym,MODE_DIGITS);

   if(digits == 3 || digits == 5)
      PipValue = 10;
   else
      PipValue = 1;

   double SL = bid + StopLoss*PipValue*point;
   if(StopLoss == 0)
      SL = 0;
   double TP = bid - TakeProfit*PipValue*point;
   if(TakeProfit == 0)
      TP = 0;
   int ticket = -1;
   if(true)
      ticket = OrderSend(sym, OP_SELL, lotAdjust(sym,Lot_Volume()), bid, Slippage, 0, 0, Commentary, Magic, 0, clrRed);
   else
      ticket = OrderSend(sym, OP_SELL, lotAdjust(sym,Lot_Volume()), bid, Slippage, SL, TP, Commentary, Magic, 0, clrRed);
   if(ticket > -1)
     {
      if(true)
        {
         bool sel = OrderSelect(ticket, SELECT_BY_TICKET);
         bool ret = OrderModify(OrderTicket(), OrderOpenPrice(), SL, TP, 0, clrRed);
         if(ret == false)
            Print("OrderModify() error - ", ErrorDescription(GetLastError()));
        }
     }
   else
     {
      Print("OrderSend() error - ", ErrorDescription(GetLastError()));
     }
  }
  
Files: