error code 4051 and invalid lot size during automization of EA

 
hi there, any one who really help me out regarding error code 4051 and invalid lot size during automization of my EA , as i am unable to publish it 
 
Syed Shanif Ahmed:
hi there, any one who really help me out regarding error code 4051 and invalid lot size during automization of my EA , as i am unable to publish it 

Check your lot sizes and make sure that whichever function parameter is invalid is valid.

 
Keith Watford:

Check your lot sizes and make sure that whichever function parameter is invalid is valid.

Hello Keith, thanks for showing interest , lotsize is multipe of lotsfactor which i already normalizedouble to 2 digits.
 
Syed Shanif Ahmed:
hi there, any one who really help me out regarding error code 4051 and invalid lot size during automization of my EA , as i am unable to publish it 
you can share your code to get proper solution
 
Syed Shanif Ahmed:
Hello Keith, thanks for showing interest , lotsize is multipe of lotsfactor which i already normalizedouble to 2 digits.

Yes, but do you check that it is a valid lot size?

 
Please edit your post and use the code button (Alt+S) to paste the code.
 
Keith Watford:
Please edit your post and use the code button (Alt+S) to paste the code.
//+---------------------------------------------------------------------+
//|                                                        GRID Trend EA|
//|                                                      March 14, 2020 |
//|                                                   Syed Shanif Ahmed |
//|     In no event will author be liable for any damages whatsoever.   |
//|                         Use at your own risk.                       |
//|                                                                     |
//+---------------------------------------------------------------------+
#property copyright "Copyright by Syed Shanif Ahmed"
#property version   "1.00"
#property script_show_inputs
#property strict
input string WARNING = "Using a robot carries risk, use on demo account FIRST";
enum TypeOfOrder {BUY=0,SELL=1}; 
enum SL {Enable=0,Disable=1};
extern int MagicNumber =1234;
input TypeOfOrder TradeType; //Type Of Order
extern double Take_Profit=0.01;
input SL Stoploss=1;
extern double Stop_Loss=0;
extern double Initial_Lot=0.01;
extern double Lots_Factor=2;
extern int Grid_Size=100;
extern int Max_Orders=10;
double price=0;
//+------------------------------------------------------------------+
//| MAIN FUNCTION START HERE                                         |
//+------------------------------------------------------------------+

void OnTick()
{
if(ProfitCheck()>=Take_Profit)
{CloseSellPositions();
ClosebuyPositions();
}
if(Stoploss==0 && ProfitCheck()<=-Stop_Loss)
{CloseSellPositions();
ClosebuyPositions();
}

if(TradeType==1)
if(CountOrder()==0)
//----EXECUTION OF SELL TYPE ORDER---------------------------------------------------------------------------------------------------
{OrderSend(Symbol(),OP_SELL,NormalizeDouble(Initial_Lot,2),Bid,3,0,0,NULL,MagicNumber,0,clrBlue);price=Bid;}
if(CountOrder()<Max_Orders && Bid >= price+Grid_sell()*Point)
{OrderSend(Symbol(),OP_SELL,NormalizeDouble(lot_size_total_sell()+lot_size_total_sell()*Lots_Factor,2),Bid,3,0,0,NULL,MagicNumber,0,clrBlue);price=Ask;}
//----EXECUTION OF BUY TYPE ORDER-----------------------------------------------------------------------------------------------------
if(TradeType==0)
if(CountOrder()==0)
{OrderSend(Symbol(),OP_BUY,NormalizeDouble(Initial_Lot,2),Ask,3,0,0,NULL,MagicNumber,0,clrBlue);price=Ask;}
if(CountOrder()<Max_Orders && Ask <= price-Grid_buy()*Point)
{OrderSend(Symbol(),OP_BUY,NormalizeDouble(lot_size_total_buy()+lot_size_total_buy()*Lots_Factor,2),Ask,3,0,0,NULL,MagicNumber,0,clrBlue);price=Ask;}
//----EXECUTION OF CHARTCOMMENT FUNCTION----------------------------------------------------------------------------------------------
ChartComment();
}
//+------------------------------------------------------------------+
//|MAIN FUNCTION ENDS HERE                                           |
//+------------------------------------------------------------------+

//---------------------------------------------------------------------------
// TO CLOSE ALL SELL POSITIONS FUNTIONS
//---------------------------------------------------------------------------

void CloseSellPositions()
{
for(int b=OrdersTotal()-1; b >=0;b--)
{
OrderSelect(b,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_SELL && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{OrderClose(OrderTicket(),OrderLots(),Ask,3,NULL);}
}
}
//---------------------------------------------------------------------------
// CHART COMMENT FUNTION
//---------------------------------------------------------------------------

void ChartComment()
  {
   Comment("\n              GRID Trend EA                    ",
           "\nContact at +923152099871 / syedshanifahmed@gmail.com ",
           "\n___________________________________________ ",
           "\nCurrency Pair       = ", Symbol(),
           "\nMagic Number        = ", MagicNumber,
           "\nAccount info        = ", AccountInfoString(ACCOUNT_SERVER),
           "\nServer Time         = ", TimeToStr(TimeCurrent(), TIME_SECONDS),
           "\nAccount Balance     = ", MathRound(AccountBalance()),
           "\nAccount Equuty      = ", MathRound(AccountEquity()),
           "\nAccount Profit/Loss = ",(AccountEquity() - AccountBalance ()),
           "\nTotal Margin Used   = ", AccountInfoDouble(ACCOUNT_MARGIN),
           "\nFree Margin         = ", AccountInfoDouble(ACCOUNT_MARGIN_FREE),
           "\nOrders Total        = ", OrdersTotal(),
           "\n____________________________________________ ");
  }
//---------------------------------------------------------------------------
// PROFIT CHECK FUNTION
//---------------------------------------------------------------------------
  double ProfitCheck()
{
   double profit=0;
   int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==MagicNumber)
         {profit+=OrderProfit()+OrderCommission()+OrderSwap();}
      }
   return(profit);        
}

//---------------------------------------------------------------------------
// FUNCTION TO COUNT TOTAL NUMBER OF ORDERS BY EA
//---------------------------------------------------------------------------
int CountOrder()
{
   int orders=0;
   int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==MagicNumber)
         {orders++;}
      }
   return(orders);        
}
//---------------------------------------------------------------------------
// FUNTION TO CALCULATE TOTAL LOT SIZE OF SELL TYPE ORDER BY EA
//---------------------------------------------------------------------------
double lot_size_total_sell()
{
   double lotsizetotal=0;
   int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==MagicNumber)
         if(OrderType()==OP_SELL)
         {lotsizetotal+=Initial_Lot;}
      }
   return(lotsizetotal);        
}
//---------------------------------------------------------------------------
// FUNTION TO CALCULATE GRID SIZE FOR SELL TYPE ORDERS 
//---------------------------------------------------------------------------
double Grid_sell()
{
      int gridsize=0;
      int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==MagicNumber)
         if(OrderType()==OP_SELL)
         {gridsize=Grid_Size;}
      }
      return(gridsize);        
}

//---------------------------------------------------------------------------
// FUNTION TO CLOSE ALL BUY POSTIONS BY EA
//---------------------------------------------------------------------------
void ClosebuyPositions()
{
for(int b=OrdersTotal()-1; b >=0;b--)
{
OrderSelect(b,SELECT_BY_POS,MODE_TRADES);
if(OrderType()==OP_BUY && OrderSymbol()==Symbol() && OrderMagicNumber()==MagicNumber)
{OrderClose(OrderTicket(),OrderLots(),Bid,3,NULL);}
}
}

//---------------------------------------------------------------------------
// FUNTION TO CALCULATE TOTAL LOT SIZE OF BUY TYPE ORDER BY EA
//---------------------------------------------------------------------------
double lot_size_total_buy()
{
   double lotsizetotal=0;
   int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==MagicNumber)
         if(OrderType()==OP_BUY)
         {lotsizetotal+=Initial_Lot;}
      }
   return(lotsizetotal);        
}
//---------------------------------------------------------------------------
// FUNTION TO CALCULATE GRID SIZE FOR BUY TYPE ORDERS 
//---------------------------------------------------------------------------

double Grid_buy()
{
      int gridsize=0;
      int total  = OrdersTotal();
      for (int cnt = total-1 ; cnt >=0 ; cnt--)
      {
         OrderSelect(cnt, SELECT_BY_POS, MODE_TRADES);
         if(OrderSymbol()==Symbol())
         if(OrderMagicNumber()==MagicNumber)
         if(OrderType()==OP_BUY)
         {gridsize=Grid_Size;}
      }
      return(gridsize);        
}
 
extern double Initial_Lot=0.01;

Where do you check that this or any other lot size used by the EA is a valid lot size?

 
Keith Watford:

Where do you check that this or any other lot size used by the EA is a valid lot size?

no , not any function in this EA set to check valid lot size , actually i am new at coding , and not pretty in it .