Possible loss of data due to type conversion (warning)

To add comments, please log in or register
clemmo
242
clemmo  

I've consulted the article on casting, https://www.mql5.com/en/docs/basis/types/casting and seen some other responses on this forum, but this particular instance seems different, possibly because it contains an array? The affected line is highlighted.  You can see that TradeList and ctTrade have both been initialized as integers.

      if(fifo==true)
        {
         int TradeList[][2];
         int ctTrade=0;
         for(int x=OrdersTotal()-1;x>=0;x--)
           {
            if(!OrderSelect(x,SELECT_BY_POS,MODE_TRADES))
               continue;
            if(OrderSymbol()==Symbol() && OrderMagicNumber()==mn && OrderType()==OP_BUY)
              {
               ctTrade++;
               ArrayResize(TradeList,ctTrade);
               TradeList[ctTrade-1][0]=OrderOpenTime();
               TradeList[ctTrade-1][1]=OrderTicket();
              }
           }
         ArraySort(TradeList,WHOLE_ARRAY,0,MODE_ASCEND);
         for(int i=0; i<ctTrade; i++)
           {
            if (OrderSelect(TradeList[i][1],SELECT_BY_TICKET))
            RefreshRates();
            if(!OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,0))
               Alert("OrderClose Error: ",GetLastError());
           }
        }
     }
Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Often a necessity occurs to convert one numeric type into another. Not all numeric types can be converted into another. Here is the scheme of allowed casting: Solid lines with arrows indicate changes that are performed almost without any loss of information. Instead of the char type, the bool type can be used (both take 1 byte of memory...
Icham Aidibe
11675
Icham Aidibe  
clemmo:

I've consulted the article on casting, https://www.mql5.com/en/docs/basis/types/casting and seen some other responses on this forum, but this particular instance seems different, possibly because it contains an array? The affected line is highlighted.  You can see that TradeList and ctTrade have both been initialized as integers.

OrderOpenTime

Returns open time of the currently selected order.

datetime  OrderOpenTime();

EDIT : "In a dog-eat-dog world, trade based on science not superstition" <---- great quote btw :)
Mehrdad Jeddi
29403
Mehrdad Jeddi  

So "OrderOpenTime()" function returns datetime type value,it should be converted type casting as Integer as below:

TradeList[ctTrade-1][0]=(int)OrderOpenTime();
lippmaje
1041
lippmaje  

Consider using a struct:

struct Trade
  {
   int        type;
   datetime   opentime;
   int        ticket;
  }

then

Trade tradelist[];
...
ArrayResize(tradelist,ctTrade);
tradelist[ctTrade-1].opentime=OrderOpenTime();
Icham Aidibe
11675
Icham Aidibe  
Mehrdad Jeddi:

So "OrderOpenTime()" function returns datetime type value,it should be converted type casting as Integer as below:

Curiously (int)datetimevar doesn't change the value of datetimevar like (int)doublevar would change the value of doublevar to an int. So it works but it shouldn't :p

  double a=1.5;
  datetime b=TimeGMT();
  printf("*** double var : %g -- datetime var : %g",(int)a,(int)b);
(EURUSD,H1) *** double var : 1 -- datetime var : 1.56725e+09
Mehrdad Jeddi
29403
Mehrdad Jeddi  
Icham Aidibe:

Curiously (int)datetimevar doesn't change the value of datetimevar like (int)doublevar would change the value of doublevar to an int. So it works but it shouldn't :p

So that is based on seconds from 1970 !

In this case it returns in seconds.
Icham Aidibe
11675
Icham Aidibe  
Mehrdad Jeddi:

So that is based on seconds from 1970 !

In this case it returns in seconds.

Yes you true, I just thought about. It is the number of seconds, so literally it is an integer but it's expressed as a datetime

clemmo
242
clemmo  
Thank you all. I don't really understand, but it does work now. :)
To add comments, please log in or register