Possible Loss Of Data Due To Type Conversion Warning

 

Ive come across a warning when writing an expert advisor that I don't understand. I have some ideas on why I'm getting it but was hoping someone could clarify. I'm getting the warning possible loss of data due to type conversion warning when compiling the code attached. I know this error is normally caused when trying to use the wrong data type with the normaliseDouble function but im passing a double type variable into it in onTick. I'm also not sure if the warning is something to do with the pass by reference element and possibly my improper use of it? 

//Declared in Global Scope  

 double   lot=0.127364;                     // Lot Size


  void lotSizeChecks(double &lotSize)
  {
  double volumeStep;                                           // Holds Volume Step as retrieved from broker
  double roundingDigits;                                       // No of normailseDouble rounding digits
   if (lot<SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN))        
      {
        lotSize=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MIN);   // Change lot value to minimum if below broker minimum 
      }
   if(lot>SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX))               
     {
        lotSize=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_MAX);   // Change lot value to maximum if above broker max
     }
   volumeStep=SymbolInfoDouble(_Symbol,SYMBOL_VOLUME_STEP);    // Holds the mininimum volume increment.
   
   if(volumeStep==0.01)
     {
      roundingDigits=2;
     }
   if(volumeStep==0.001)
     {
      roundingDigits=3;                   // Determine what number of digits to round off to for correct increment
     }
   if(volumeStep==0.1)
     {
      roundingDigits=1;
     }
   if(volumeStep==1)
     {
      roundingDigits=0;
     }
   lotSize=NormalizeDouble(lotSize,roundingDigits);                         // Round lot to lot minimum increment value 
}

// In OnTick

  lotSizeChecks(lot);
 
toma4186:

Ive come across a warning when writing an expert advisor that I don't understand. I have some ideas on why I'm getting it but was hoping someone could clarify. I'm getting the warning possible loss of data due to type conversion warning when compiling the code attached. I know this error is normally caused when trying to use the wrong data type with the normaliseDouble function but im passing a double type variable into it in onTick. I'm also not sure if the warning is something to do with the pass by reference element and possibly my improper use of it? 

This is the proper way to adjust the Volume (lots) for an Order (don't use NormalizeDouble) :

void lotSizeChecks( double &lotSize )
{
   double
      volumeMinimum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MIN  ),
      volumeMaximum = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_MAX  ),
      volumeStep    = SymbolInfoDouble( _Symbol, SYMBOL_VOLUME_STEP );

   lotSize =
      fmin( volumeMaximum,                                  // Prevent too greater volume
         fmax( volumeMinimum,                               // Prevent too smaller volume
            round( lotSize / volumeStep ) * volumeStep ) ); // Align to Step value
}
 

Also, the reason you were getting a "possible loss of data", was because of the "roundingDigits" argument which should have been of the "int" type and not a "double" (for NormalizeDouble).

For warnings and errors, you should always look at the line number, and column position, to identify the problem.

 
Thanks very much for taking the time to help me. I didn't think of checking the roundingDigits argument for the data type at all. I couldn't understand at all where it was coming from. I'm not the best with coding presently as I'm sure you can tell. I don't understand the code you've given at first glance but I will work on it and incorporate it into my ea. Thanks again for your help.
 
toma4186: Thanks very much for taking the time to help me. I didn't think of checking the roundingDigits argument for the data type at all. I couldn't understand at all where it was coming from. I'm not the best with coding presently as I'm sure you can tell. I don't understand the code you've given at first glance but I will work on it and incorporate it into my ea. Thanks again for your help.
You are welcome!
 
I encountered a disclaimer while writing an expert advisor that I don't understand. I have some ideas on why I am getting it, but I was hoping someone could clarify. I am getting the possible data loss warning due to a type conversion warning while compiling the attached code.
Files:
ST10.mq5  14 kb
 
I haven't looked at your code, but in short:

If you get this warning, then you are assigning a variable type with more data to a variable type with less data.

For example, if you assign a signed into to an unsigned int, you will "loose" the signed information.

If you assign a long to an int, you will loose 4 bytes of data storage.

This is where the warning is coming from.

Same goes for other data types. Look here:

 
Th trick is that for instance SymbolInfoInteger is a long instead of an integer as you might be unaware of, so the quick fix would be to precede this kind of  function with the explicit converter (int) when appropriate. That would supress a significant source ot those warnings.
Reason: