Posible loss of data due to type conversion

 

Hello,

Anyone can help me on solving the the warning message of "Posible loss of data due to type conversion" on the lines highlighted below:


void updateVolume(){

   long OneDay=60*60*24;

   datetime StartOfDay=StringToTime((string)MyServerTime.year+"."+(string)MyServerTime.mon+"."+(string)MyServerTime.day+" 00:00:00");

   LastVolumeDateTime = TimeCurrent();

   datetime tempStartDateTime=0;

   datetime tempEndDateTime=0;

   MqlDateTime DayCheck;

   int tempVolume=0;

   long TotalVolume = 0;

   AvgVolume = 0;

   DayVolume=0;

   int tempVolumePeriod = Volume_Period;   

   

   for(int x=0;x<=tempVolumePeriod;x++){

      if(IsStopped()) 

         break; 

         

      tempStartDateTime = (long)StartOfDay - (x*OneDay);

      tempEndDateTime = (long)LastVolumeDateTime - (x*OneDay);

      TimeToStruct(tempEndDateTime,DayCheck);

      //check if it was a working day

      if(StringFind(TradingDays,(string)DayCheck.day_of_week,0) == -1){

         tempVolumePeriod++;

      } else {

         if(TickVolume)

            tempVolume= CopyTickVolume(Symbol(),PERIOD_M1,tempStartDateTime,tempEndDateTime,VolumeBuffer);

         else

            tempVolume= CopyRealVolume(Symbol(),PERIOD_M1,tempStartDateTime,tempEndDateTime,VolumeBuffer);

            

         TotalVolume = 0;

         int VolBufferSize = ArraySize(VolumeBuffer);

         for(int y=0; y<VolBufferSize;y++)

            TotalVolume+=VolumeBuffer[y];

            

         if(x==0)

            DayVolume=TotalVolume;

         else

            AvgVolume += TotalVolume;

         if(Debug)      

            Print((string)tempStartDateTime+" "+(string)tempEndDateTime+" "+(string)TotalVolume);

      }

   }

   AvgVolume = (long)(AvgVolume/Volume_Period);

   if(Debug)      

      Print("Average volume over last "+ (string)Volume_Period + " at "+(string)LastVolumeDateTime+" is "+(string)AvgVolume);


Thanks!

 
Erico Magistro:

Hello,

Anyone can help me on solving the the warning message of "Posible loss of data due to type conversion" on the lines highlighted below:

Thanks!

Cast to datetime instead of long, and check your brackets:

      tempStartDateTime = datetime(StartOfDay - (x*OneDay));

      tempEndDateTime = datetime(LastVolumeDateTime - (x*OneDay));
 

thanks a lot Seng Joo Thio! Really helped!

Maybe you can also assist another issue I found, which is the Inconsistency in opening positions when I compare back test and live trading.

I noticed that some trades were executed at back test but not live. I did some investigations and notice the following but not sure if there is any relation with the live trading differences. It seems related to the Trading time filters.

Examples:

A)      I run a backs test using H1  timeframe with date range (2019.01.29 – 2019.01.31) using Trading Open Hour = 10, Trading Open Minute =00 and there is no trade at 10:00 on 2019.01.29.

B)      If I run same backtest with date range starting one day before (2019.01.28 – 2019.01.31), there is a trade at 10:00 on 2019.01.29. Why? EA is needy of more data and when time is allowed it could not calculate fast enough to trade at that time?

C)      If I run same back test with date range (2019.01.29 – 2019.01.31) with Trading Open Hour = 9, then there is a trade at 10:00 on 2019.01.29.

 

Below the code that maybe is generating this issue:


input string         TradingDays       ="12345";// Trading Days. 0:Sun 1:Mon ...

input uint           TradingOpenHr     =10;

input uint           TradingOpenMin    =00;

input uint           TradingCloseHr    =16;

input uint           TradingCloseMin   =00;

input uint           PositionCloseHr   =17;

input uint           PositionCloseMin  =00;

input bool           CloseOrdersAtClose= true;



//| Expert tick function                                             |

//+------------------------------------------------------------------+

void OnTick()

{

//--- Open for trade?

   TimeToStruct(TimeCurrent(),MyServerTime);

   GetMyPositions = getMyPositions();

   if(GetMyPositions != 0){

      if(CloseOrdersAtClose){

         if(

            MyServerTime.hour > MyPositionCloseTime.hour

            || (MyServerTime.hour == MyPositionCloseTime.hour && MyServerTime.min > MyPositionCloseTime.min)

         )

         doCloseAll();

      }

   }

   

   

   if(

      StringFind(TradingDays,(string)MyServerTime.day_of_week,0) == -1

      || MyServerTime.hour < MyOpenTime.hour

      || (MyServerTime.hour == MyOpenTime.hour && MyServerTime.min < MyOpenTime.min)

      || MyServerTime.hour > MyCloseTime.hour

      || (MyServerTime.hour == MyCloseTime.hour && MyServerTime.min > MyCloseTime.min)

   ){

      Comment("==== We are closed "+ThisVersion+" ====\n Current Time: "+(string)TimeCurrent()

         +"\n Open:"+(string)MyOpenTime.hour+":"+(string)MyOpenTime.min

         +"\n Close:"+(string)MyCloseTime.hour+":"+(string)MyCloseTime.min

       );

       DoneForToday=false;

       DailyProfit = 0;

       DailyTradeAllowed = true;

      return;

   }



//------------------------------------- INITS

bool initTimeRelated(){

   //CheckTimes

   //open

   bool Init=true;

   if(TradingOpenHr>23){

      Print("Trading Open Hour out of range");

      Init=false;

   }

      

   if(TradingOpenMin>59){

      Print("Trading Open Minutes out of range");

      Init=false;

   }

   //close

   if(TradingCloseHr>23){

      Print("Trading Close Hour out of range");

      Init=false;

   }

   if(TradingCloseMin>59){

      Print("Trading Close Minutes out of range");

      Init=false;

   }

   if(TradingOpenHr > TradingCloseHr){

      Print("Trading Open Hour out of range");

      Init=false;

   }

      

   OpenTime=(string)TradingOpenHr+":"+(string)TradingOpenMin;

   CloseTime=(string)TradingCloseHr+":"+(string)TradingCloseMin;

   PositionTime=(string)PositionCloseHr+":"+(string)PositionCloseMin;

   

//--- Init the variables

   TimeToStruct(StringToTime(OpenTime),MyOpenTime);

   TimeToStruct(StringToTime(CloseTime),MyCloseTime);

   TimeToStruct(StringToTime(PositionTime),MyPositionCloseTime);

   return Init; 

}





bool isNewBar()

{

   static datetime last_time=0;

   datetime lastbar_time;

   if(!SeriesInfoInteger(Symbol(),Period(),SERIES_LASTBAR_DATE,lastbar_time)){

      Print("Error getting last bar time");

      return false;

   };

   

   //--- if it is the first call of the function

   if(last_time==0)

     {

      //--- set the time and exit

      last_time=lastbar_time;

      return(false);

     }

   

   //--- if the time differs

   if(last_time!=lastbar_time)

     {

      //--- memorize the time and return true

      last_time=lastbar_time;

      return(true);

     }

   //--- if we passed to this line, then the bar is not new; return false

   return(false);

}

 

 
Erico Magistro:

thanks a lot Seng Joo Thio! Really helped!

Maybe you can also assist another issue I found, which is the Inconsistency in opening positions when I compare back test and live trading.

I noticed that some trades were executed at back test but not live. I did some investigations and notice the following but not sure if there is any relation with the live trading differences. It seems related to the Trading time filters.

Examples:

A)      I run a backs test using H1  timeframe with date range (2019.01.29 – 2019.01.31) using Trading Open Hour = 10, Trading Open Minute =00 and there is no trade at 10:00 on 2019.01.29.

B)      If I run same backtest with date range starting one day before (2019.01.28 – 2019.01.31), there is a trade at 10:00 on 2019.01.29. Why? EA is needy of more data and when time is allowed it could not calculate fast enough to trade at that time?

C)      If I run same back test with date range (2019.01.29 – 2019.01.31) with Trading Open Hour = 9, then there is a trade at 10:00 on 2019.01.29.

🤔 Check through your "if" statements... Some "<" and ">" can be replaced with "<=" and ">=".