Invalid volume

 

I get this error: failed instant buy 0.05 AUDCAD at 0.97083, close #2 sell 0.05 AUDCAD 0.97620111 [Invalid volume] I do not understand if the invalid volume error is related to opening the position or with closing the position. Before opening positions, I do the following checks to avoid using an invalid volume:

            // checking that lot size is not too large or too small for broker symbol

            if(lot>SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_MAX))
               lot=SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_MAX);
            if(lot<SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_MIN))
               lot=SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_MIN);

            double lotstep = SymbolInfoDouble(inpSymbol,SYMBOL_VOLUME_STEP);
            lot= MathFloor(lot/lotstep)*lotstep;

            lot = NormalizeDouble(lot,2);
Is this not sufficient to check for invalid volume? If the error is related to closing of the position, how is the EA allowed to open a 0.5 lot position but not close a 0.5 lot position and how can I check for that?

 
I believe the NormalizeDouble line is redundant. And is the source of issue probably.
 
altmql:

I get this error: failed instant buy 0.05 AUDCAD at 0.97083, close #2 sell 0.05 AUDCAD 0.97620111 [Invalid volume] I do not understand if the invalid volume error is related to opening the position or with closing the position. Before opening positions, I do the following checks to avoid using an invalid volume:

Is this not sufficient to check for invalid volume? If the error is related to closing of the position, how is the EA allowed to open a 0.5 lot position but not close a 0.5 lot position and how can I check for that?

I suggest you print the values to expert journal, to check what you are actually passing in.

Also, I would first check for lot step, then for max and min values boundaries.


 

I see the code for ronding the postion size is Ok. Check the code for closing the position. I think the fault is there.

 
Dominik Egert #:
I suggest you print the values to expert journal, to check what you are actually passing in.

Also, I would first check for lot step, then for max and min values boundaries.


I suggest you print the values to expert journal, to check what you are actually passing in. > the error is received i the automatic validation process on mql5, so I cant read print statements related to the error.

Also, I would first check for lot step, then for max and min values boundaries. > tried that, still get the same error
 
amrali #:

I see the code for ronding the postion size is Ok. Check the code for closing the position. I think the fault is there.

I do check if there is enough margin to open positions: 

  // check if enough free margin
            double margin;
            double bid = SymbolInfoDouble(inpSymbol,SYMBOL_BID);
            if(OrderCalcMargin(ORDER_TYPE_SELL,inpSymbol,lot,bid,margin)&& margin > AccountInfoDouble(ACCOUNT_MARGIN_FREE)) {
               Print("Not enough margin to open buy position");
               return;
            }

I am not aware of any checks that needs to run before being able to close a position. Could you please clarify? 

 
altmql #:

I do check if there is enough margin to open positions: 

I am not aware of any checks that needs to run before being able to close a position. Could you please clarify? 

Post the code you use to close your position, please.

 
amrali #:

Post the code you use to close your position, please.

      if((direction>0) && (ask >= tp)) {   
         for(int i=ArraySize(arr)-1; i>=0; i--) {
            ulong posTicket = arr[i];
            if(PositionSelectByTicket(posTicket)) {
               if(trade.PositionClose(posTicket)) {
               }
            }
         }
      }
 

Use this extra code for debugging to print to the log

if((direction>0) && (ask >= tp)) {   
   for(int i=ArraySize(arr)-1; i>=0; i--) {
      ulong posTicket = arr[i];
      if(PositionSelectByTicket(posTicket)) {
         trade.SetTypeFillingBySymbol( PositionGetString(POSITION_SYMBOL) );
         if(trade.PositionClose(posTicket)) {
         }
         else {
            Print("---===Request===---");
            string desc="Action: "+EnumToString(trade.RequestAction())+"\n";
            desc += "Symbol: " + trade.RequestSymbol() + "\n";
            desc += "Magic Number: " + (string)trade.RequestMagic() + "\n";
            desc += "Order ticket: " + (string)trade.RequestOrder() + "\n";
            desc += "Order type: " + EnumToString(trade.RequestType()) + "\n";
            desc += "Order filling: " + EnumToString(trade.RequestTypeFilling()) + "\n";
            desc += "Order time type: " + EnumToString(trade.RequestTypeTime()) + "\n";
            desc += "Order expiration: " + TimeToString(trade.RequestExpiration()) + "\n";
            desc += "Price: " + (string)trade.RequestPrice() + "\n";
            desc += "Deviation points: " + (string)trade.RequestDeviation() + "\n";
            desc += "Stop Loss: " + (string)trade.RequestSL() + "\n";
            desc += "Take Profit: " + (string)trade.RequestTP() + "\n";
            desc += "Stop Limit: " + (string)trade.RequestStopLimit() + "\n";
            desc += "Volume: " + (string)trade.RequestVolume() + "\n";
            desc += "Comment: " + trade.RequestComment() + "\n";
            desc += "Position: " + (string)trade.RequestPosition() + "\n";
            desc += "Position by: " + (string)trade.RequestPositionBy();
            Print(desc);
            
            Print("---===Result===---");
            desc="Retcode: "+(string)trade.ResultRetcode()+"\n";
            desc += "Retcode Desc: " + trade.ResultRetcodeDescription() + "\n";
            desc += "Order ticket: " + (string)trade.ResultOrder() + "\n";
            desc += "Deal ticket: " + (string)trade.ResultDeal() + "\n";
            desc += "Volume: " + (string)trade.ResultVolume() + "\n";
            desc += "Price: " + (string)trade.ResultPrice() + "\n";
            desc += "Bid: " + (string)trade.ResultBid() + "\n";
            desc += "Ask: " + (string)trade.ResultAsk() + "\n";            
            desc += "Comment: " + trade.ResultComment() + "\n";
            desc += "Retcode external: " + (string)trade.ResultRetcodeExternal() + "\n";
            Print(desc);
         }
      }
   }
}