Error 131 (Invalid volumes in trade operations)

 

Hello everyone, someone have idea what is problem here? I am getting error 131 when I want EA to upload in the market, can someone help? can someone tell me what can I do?

here is code: 

extern int Candle_size = 150;
extern int MagicNumber = 999;
extern string TradeComment = "";
extern int StopLoss = 15;//Distance and StopLoss must be same
extern double MinLots = 0.01;
extern double MaxLots = 0.2;
extern double Risk = 5.0;
extern double FixedLots = 0.1;
extern bool Lot_Increment = FALSE;
extern double MaxSpreadPlusCommission = 15.0;
extern int Cut_Start_limit = 25;
extern int Distance = 0;//Distance and StopLoss must be same


double sprd[30];
int rnddigit = 0;
double point = 0.0;
int emvalue;
double minLot;
double maxLot;
double risk;
double maxSpreadPlusCommission;
double cutStartLimit;
double distance;
double normalizedPoint_0;
bool lotIncrement;
double normalizeDBL;
int intZero = 0;
double point_0 = 0.0;
bool boolTimeframe = TRUE;
double timeframePeriod = 240.0;
double point_0001;
int slippage = 3;

int init()
{


//---



   int timeframe_8;
   ArrayInitialize(sprd, 0);
   rnddigit = Digits;
   point = Point;
   Print("Digits: " + rnddigit + " Point: " + DoubleToStr(point, rnddigit));
   double lotstep_0 = MarketInfo(Symbol(), MODE_LOTSTEP);
   emvalue = MathLog(lotstep_0) / MathLog(0.1);
   minLot = MathMax(MinLots, MarketInfo(Symbol(), MODE_MINLOT));
   maxLot = MathMin(MaxLots, MarketInfo(Symbol(), MODE_MAXLOT));
   risk = Risk / 100.0;
   maxSpreadPlusCommission = NormalizeDouble(MaxSpreadPlusCommission * point, rnddigit + 1);
   cutStartLimit = NormalizeDouble(Cut_Start_limit * point, rnddigit);
   distance = NormalizeDouble(Distance * point, rnddigit);
   normalizedPoint_0 = NormalizeDouble(point * Candle_size, rnddigit);
   lotIncrement = FALSE;
   normalizeDBL = NormalizeDouble(point_0 * point, rnddigit + 1);
   if (!IsTesting()) {
      //f0_8();
      if (boolTimeframe) {
         timeframe_8 = Period();
         switch (timeframe_8) {
         case PERIOD_M1:
            timeframePeriod = 5;
            break;
         case PERIOD_M5:
            timeframePeriod = 15;
            break;
         case PERIOD_M15:
            timeframePeriod = 30;
            break;
         case PERIOD_M30:
            timeframePeriod = 60;
            break;
         case PERIOD_H1:
            timeframePeriod = 240;
            break;
         case PERIOD_H4:
            timeframePeriod = 1440;
            break;
         case PERIOD_D1:
            timeframePeriod = 10080;
            break;
         case PERIOD_W1:
            timeframePeriod = 43200;
            break;
         case PERIOD_MN1:
            timeframePeriod = 43200;
         }
      }
      point_0001 = 0.0001;
   }
   return (0);
}

int deinit()
{
   Comment("");
   return (0);
}

int start()
{




   int error;
   string errorDescription;
   int orderTicket;
   double orderPrice;
   bool orderModify;
   double orderOpenPrice;
   double orderStopLoss;
   double stopLoss;
   double orderProfitLoss;
   int mprice;
   int orderType;
   double lotSize;
   double dbint;

   double barHighPrice = iHigh(NULL, 0, 0);
   double barLowPrice = iLow(NULL, 0, 0);
   double barsHighestPrice = High[iHighest(Symbol(), 0, MODE_HIGH, 10, 1)];
   double barsLowestPrice= Low [iLowest(Symbol(), 0, MODE_LOW, 10, 1)];


   if (!lotIncrement)
   {
      for (int ordpost = OrdersHistoryTotal() - 1; ordpost >= 0; ordpost--)
      {
         if (OrderSelect(ordpost, SELECT_BY_POS, MODE_HISTORY))
         {
            if (OrderProfit() != 0.0) {
               if (OrderClosePrice() != OrderOpenPrice())
               {
                  if (OrderSymbol() == Symbol())
                  {
                     lotIncrement = TRUE;
                     orderProfitLoss = MathAbs(OrderProfit() / (OrderClosePrice() - OrderOpenPrice()));
                     normalizeDBL = (-OrderCommission()) / orderProfitLoss;
                     break;
                  }
               }
            }
         }
      }
   }

   double spreadd = Ask - Bid;
   ArrayCopy(sprd, sprd, 0, 1, 29);
   sprd[29] = spreadd;
   if (intZero < 30) intZero++;
   double r1peixe = 0;
   ordpost = 29;

   for (int count = 0; count < intZero; count++)
   {
      r1peixe += sprd[ordpost];
      ordpost--;
   }

   double rprice = r1peixe / intZero;
   double mnormal = NormalizeDouble(rprice + normalizeDBL, rnddigit + 1);
   double brprice = barHighPrice - barLowPrice;

   if (brprice > normalizedPoint_0)
   {
      if (Bid < barsLowestPrice) mprice = -1;
      else
         if (Bid > barsHighestPrice) mprice = 1;
   }

   int intcount = 0;
   for (ordpost = 0; ordpost < OrdersTotal(); ordpost++) {
      if (OrderSelect(ordpost, SELECT_BY_POS, MODE_TRADES)) {
         if (OrderMagicNumber() == MagicNumber) {
            orderType = OrderType();
            if (orderType == OP_BUYLIMIT || orderType == OP_SELLLIMIT) continue;
            if (OrderSymbol() == Symbol()) {
               intcount++;
               switch (orderType) {
               case OP_BUY:
                  if (Distance < 0) break;
                  orderStopLoss = NormalizeDouble(OrderStopLoss(), rnddigit);
                  stopLoss = NormalizeDouble(Bid - distance, rnddigit);
                  if (!((orderStopLoss == 0.0 || stopLoss > orderStopLoss))) break;
                  orderModify = OrderModify(OrderTicket(), OrderOpenPrice(), stopLoss, OrderTakeProfit(), 0, Lime);
                  if (!(!orderModify)) break;
                  error = GetLastError();
                  errorDescription = ErrorDescription(error);
                  Print("BUY Modify Error Code: " + error + " Message: " + errorDescription + " OP: " + DoubleToStr(orderPrice, rnddigit) + " SL: " + DoubleToStr(stopLoss, rnddigit) +
                     " Bid: " + DoubleToStr(Bid, rnddigit) + " Ask: " + DoubleToStr(Ask, rnddigit));
                  break;
               case OP_SELL:
                  if (Distance < 0) break;
                  orderStopLoss = NormalizeDouble(OrderStopLoss(), rnddigit);
                  stopLoss = NormalizeDouble(Ask + distance, rnddigit);
                  if (!((orderStopLoss == 0.0 || stopLoss < orderStopLoss))) break;
                  orderModify = OrderModify(OrderTicket(), OrderOpenPrice(), stopLoss, OrderTakeProfit(), 0, Orange);
                  if (!(!orderModify)) break;
                  error = GetLastError();
                  errorDescription = ErrorDescription(error);
                  Print("SELL Modify Error Code: " + error + " Message: " + errorDescription + " OP: " + DoubleToStr(orderPrice, rnddigit) + " SL: " + DoubleToStr(stopLoss, rnddigit) +
                     " Bid: " + DoubleToStr(Bid, rnddigit) + " Ask: " + DoubleToStr(Ask, rnddigit));
                  break;
               case OP_BUYSTOP:
                  orderOpenPrice = NormalizeDouble(OrderOpenPrice(), rnddigit);
                  orderPrice = NormalizeDouble(Ask + cutStartLimit, rnddigit);
                  if (!((orderPrice < orderOpenPrice))) break;
                  stopLoss = NormalizeDouble(orderPrice - StopLoss * Point, rnddigit);
                  orderModify = OrderModify(OrderTicket(), orderPrice, stopLoss, OrderTakeProfit(), 0, Lime);
                  if (!(!orderModify)) break;
                  error = GetLastError();
                  errorDescription = ErrorDescription(error);
                  Print("BUYSTOP Modify Error Code: " + error + " Message: " + errorDescription + " OP: " + DoubleToStr(orderPrice, rnddigit) + " SL: " + DoubleToStr(stopLoss, rnddigit) +
                     " Bid: " + DoubleToStr(Bid, rnddigit) + " Ask: " + DoubleToStr(Ask, rnddigit));
                  break;
               case OP_SELLSTOP:
                  orderOpenPrice = NormalizeDouble(OrderOpenPrice(), rnddigit);
                  orderPrice = NormalizeDouble(Bid - cutStartLimit, rnddigit);
                  if (!((orderPrice > orderOpenPrice))) break;
                  stopLoss = NormalizeDouble(orderPrice + StopLoss * Point, rnddigit);
                  orderModify = OrderModify(OrderTicket(), orderPrice, stopLoss, OrderTakeProfit(), 0, Orange);
                  if (!(!orderModify)) break;
                  error = GetLastError();
                  errorDescription = ErrorDescription(error);
                  Print("SELLSTOP Modify Error Code: " + error + " Message: " + errorDescription + " OP: " + DoubleToStr(orderPrice, rnddigit) + " SL: " + DoubleToStr(stopLoss, rnddigit) +
                     " Bid: " + DoubleToStr(Bid, rnddigit) + " Ask: " + DoubleToStr(Ask, rnddigit));
               }
            }
         }
      }
   }

   if (intcount == 0 && mprice != 0 && mnormal <= maxSpreadPlusCommission)
   {
      lotSize = AccountBalance() * AccountLeverage() * risk;
      if (!Lot_Increment) lotSize = FixedLots;
      dbint = NormalizeDouble(lotSize / MarketInfo(Symbol(), MODE_LOTSIZE), emvalue);
      dbint = MathMax(minLot, dbint);
      dbint = MathMin(maxLot, dbint);
      if (mprice < 0) {
         orderPrice = NormalizeDouble(Ask + cutStartLimit, rnddigit);
         stopLoss = NormalizeDouble(orderPrice - StopLoss * Point, rnddigit);
         orderTicket = OrderSend(Symbol(), OP_BUYSTOP, dbint, orderPrice, slippage, stopLoss, 0, TradeComment, MagicNumber, 0, Lime);
         if (orderTicket <= 0) {
            error = GetLastError();
            errorDescription = ErrorDescription(error);
            Print("BUYSTOP Send Error Code: " + error + " Message: " + errorDescription + " LT: " + DoubleToStr(dbint, emvalue) + " OP: " + DoubleToStr(orderPrice, rnddigit) + " SL: " +
               DoubleToStr(stopLoss, rnddigit) + " Bid: " + DoubleToStr(Bid, rnddigit) + " Ask: " + DoubleToStr(Ask, rnddigit));
         }
      } else {
         orderPrice = NormalizeDouble(Bid - cutStartLimit, rnddigit);
         stopLoss = NormalizeDouble(orderPrice + StopLoss * Point, rnddigit);
         orderTicket = OrderSend(Symbol(), OP_SELLSTOP, dbint, orderPrice, slippage, stopLoss, 0, TradeComment, MagicNumber, 0, Orange);
         if (orderTicket <= 0) {
            error = GetLastError();
            errorDescription = ErrorDescription(error);
            Print("BUYSELL Send Error Code: " + error + " Message: " + errorDescription + " LT: " + DoubleToStr(dbint, emvalue) + " OP: " + DoubleToStr(orderPrice, rnddigit) + " SL: " +
               DoubleToStr(stopLoss, rnddigit) + " Bid: " + DoubleToStr(Bid, rnddigit) + " Ask: " + DoubleToStr(Ask, rnddigit));
         }
      }
   }

   string note = "AvgSpread:" + DoubleToStr(rprice, rnddigit) + "  Commission rate:" + DoubleToStr(normalizeDBL, rnddigit + 1) + "  Real avg. spread:" + DoubleToStr(mnormal, rnddigit + 1);
   if (mnormal > maxSpreadPlusCommission)
   {
      note = note 
         + "\n" 
      + "The EA can not run with this spread ( " + DoubleToStr(mnormal, rnddigit + 1) + " > " + DoubleToStr(maxSpreadPlusCommission, rnddigit + 1) + " )";
   }
       
   return (0);
}