Invalid volume

 
I get this error: 

2019.06.28 04:54:04 failed instant sell 0.41 XAUUSD at 1423.09 [Invalid volume]

However, I do run checks to look at size and lotstep before sending order. 

Can anyone see what I do wrong? 

void executeSell()
{
   // check if enough free margin
   double margin;
   if(OrderCalcMargin(ORDER_TYPE_SELL,_Symbol,Firstlot_adjusted,BidPrice,margin)&& margin > AccountInfoDouble(ACCOUNT_MARGIN_FREE)) {
      Print("Not enough margin to open sell position");
      return;
   }

   // making sure not get invalid volume error
   double minlot  = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);
   double maxlot  = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);
   double lotstep = SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);
    
   Firstlot_adjusted = MathMax(Firstlot_adjusted,minlot);
   Firstlot_adjusted = MathMin(Firstlot_adjusted,maxlot);
   Firstlot_adjusted = MathFloor(Firstlot_adjusted/lotstep)*lotstep;
   Firstlot_adjusted = MathMin(Firstlot_adjusted,SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_LIMIT));


   Trade.Sell(Firstlot_adjusted,_Symbol,BidPrice,0,0,,TradeComment);
}
 
Adjust to lot step, then min and max.
 
William Roeder #:
Adjust to lot step, then min and max.

Thank you for your response. I tried what you suggest but I get the same error. Any other ideas? 

I also tried the code as shown below (without using the intermediary variables), but I get the same error.. Are there any other checks in regaredds to volume limitations that I am missing?

void executeSell()
{
   // check if enough free margin
   double margin;
   if(OrderCalcMargin(ORDER_TYPE_SELL,inpSymbol,Firstlot_adjusted,BidPrice,margin)&& margin > AccountInfoDouble(ACCOUNT_MARGIN_FREE)) {
      Print("Not enough margin to open sell position");
      return;
   }

   // making sure not get invalid volume error
  Firstlot_adjusted = (int)(Firstlot_adjusted/SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_STEP))* SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_STEP);
  Firstlot_adjusted = MathMin(Firstlot_adjusted,SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_MAX));
  Firstlot_adjusted = MathMax(Firstlot_adjusted,SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_MIN));
  Firstlot_adjusted = MathMin(Firstlot_adjusted,SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_LIMIT));


   Trade.Sell(Firstlot_adjusted,inpSymbol,BidPrice,0,0,TradeComment);
}
 
altmql #:

Thank you for your response. I tried what you suggest but I get the same error. Any other ideas? 

I also tried the code as shown below (without using the intermediary variables), but I get the same error.. Are there any other checks in regaredds to volume limitations that I am missing?

I removed the check for SYMBOL_VOLUME_LIMIT and now I get no error messages. Thought I would share in case someone else has the same issue.. 

Reason: