Market validation error

 

Hi, why in the following code cant I get it passed validation without having to have lotsize = 1.0

Any smaller lot size and i get error 131?   

//+------------------------------------------------------------------+
//| Expert initialization function                                   |
//+------------------------------------------------------------------+
int OnInit()
{
   //--- 
   double price=Ask+(1000*_Point);
   double SL=NormalizeDouble(price-(50*_Point),_Digits);
   double TP=NormalizeDouble(price+(50*_Point),_Digits);
   //--- perform a check
   PrintFormat("Buy at %.5f   SL=%.5f   TP=%.5f  Bid=%.5f",price,SL,TP,Bid);
   if(!CheckStopLoss_Takeprofit(OP_BUYSTOP,price,SL,TP))  // POINT 5    Returns true   
      Print("The StopLoss or TakeProfit level is incorrect!");
   //--- try to buy anyway, in order to see the execution result
   Buy(price,SL,TP);
   
   Print("  -----------------");

//---
   return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| Expert tick function                                             |
//+------------------------------------------------------------------+
void OnTick()
{
   //+------------------------------------------------------------------+
   // Test Buy Order
   //+------------------------------------------------------------------+
   double price=Ask+(1000*_Point);
   double SL=NormalizeDouble(price-(50*_Point),_Digits);
   double TP=NormalizeDouble(price+(50*_Point),_Digits);
   int hour=Hour();
   
   if(hour >= 0)      
   {
      static int count = 0;
      static datetime timeCur; 
      datetime timePre = timeCur; 
      timeCur=Time[0];    // To count seconds change Time[0] to Seconds();  
      bool isNewBar = timeCur != timePre;
      
      if(isNewBar) count++;

      if(count==2)
      {
        bool select = OrderSelect(gBuyTicket,SELECT_BY_TICKET);
         
        if(OrderType() == OP_BUYSTOP)
         {                 
            bool close = OrderDelete(gBuyTicket,clrGreen);
            gBuyTicket = 0;
            
            
         }
      }           
   }
}

//+------------------------------------------------------------------+
//| Perform a Buy               
//+------------------------------------------------------------------+
bool Buy(double price,double sl,double tp)
  {
   //--- buy    
   gBuyTicket=OrderSend(Symbol(),OP_BUYSTOP,LotSize,price,10,sl,tp);
   if(gBuyTicket<0)
      PrintFormat("OrderSend error %d",GetLastError());

//---
   return(false);
  }
 

You have to adjust your volume size based on the the Lot Step and check for both Maximum and Minimum amounts. Here is an example from another thread (pay special attention to beginning and end of the code below):

Forum on trading, automated trading systems and testing trading strategies

How to calculate lots using multiplier according to number of opened orders?

Fernando Carreiro, 2017.09.01 21:57

Don't use NormalizeDouble(). Here is some guidance (code is untested, just serves as example):

// Variables for Symbol Volume Conditions
double
   dblLotsMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
   dblLotsMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
   dblLotsStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );
   
// Variables for Geometric Progression
double
   dblGeoRatio = 2.8,
   dblGeoInit  = dblLotsMinimum;
   
// Calculate Next Geometric Element
double
   dblGeoNext  = dblGeoInit * pow( dblGeoRatio, intOrderCount + 1 );
   
// Adjust Volume for allowable conditions
double
   dblLotsNext = fmin( dblLotsMaximum,                                     // Prevent too greater volume
                   fmax( dblLotsMinimum,                                   // Prevent too smaller volume
                     round( dblGeoNext / dblLotsStep ) * dblLotsStep ) );  // Align to Step value

 
Fernando Carreiro:

You have to adjust your volume size based on the the Lot Step and check for both Maximum and Minimum amounts. Here is an example from another thread (pay special attention to beginning and end of the code below):


Usually i would have used the following code that is similar to the one you gave. 

But i still get the problem of no trades being placed?

//+------------------------------------------------------------------+
//| Check the correctness of the order volume                        |
//+------------------------------------------------------------------+
bool CheckVolumeValue(double volume)
  {
//--- minimal allowed volume for trade operations
   double min_volume=MarketInfo(Symbol(),MODE_MINLOT);
   if(volume<min_volume)
     {
      Print("Volume is less than the minimal allowed. Minimum allowed is ",min_volume);
      return(false); // If womeone types lower than minimum lotvolume it returns print
     }

//--- maximal allowed volume of trade operations
   double max_volume=MarketInfo(Symbol(),MODE_MAXLOT);
   if(volume>max_volume)
     {
      Print("Volume is greater than the maximal allowed. Maximum allowed is ",max_volume);
      return(false);
     }

//--- get minimal step of volume changing
   double volume_step=MarketInfo(Symbol(),MODE_LOTSTEP);

   int ratio=(int)MathRound(volume/volume_step);
   if(MathAbs(ratio*volume_step-volume)>0.0000001)
     {
      PrintFormat("Volume is not a multiple of the minimal step SYMBOL_VOLUME_STEP=%.2f, the closest correct volume is %.2f",
                               volume_step,ratio*volume_step);
      return(false);
     }
   Print("Correct volume value");
   return(true);
  }
Am i right in realising the validation test can only as with 1.0 lot size?  As ive tried both 0.1 and 10.0 lots and validation fails but its okay only with 1.0?