Possible loss of data due to type conversion.

 

Hello,

Wondering if someone can assist with troubleshooting an EA that I am currently developing.

I am getting the following error: Possible loss of data due to type conversion.

At the following line:

               if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic_num)
                 {
                  size++;
                  ArrayResize(tradeList, size);
                  tradeList[size-1][0]=OrderOpenTime();
                  tradeList[size-1][1]=OrderTicket();
                 }
              }

I have managed to get "past" this issue by swapping OrderOpenTime(); to (int)OrderOpenTime(); but I am noticing during forward testing that after a few entries it stops entering further positions for no reason

Could this be part of the problem? If so, can someone help?

MQL5 forum
MQL5 forum
  • www.mql5.com
MQL5: Forum on automated trading systems and strategy testing
 
Topics concerning MT4 and MQL4 have their own section.
In future please post in the correct section.
I have moved your topic to the MQL4 and Metatrader 4 section.
 
dark000soul: I am getting the following error: Possible loss of data due to type conversion.D

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button).
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem

We can't possibly know what tradelist is.

Unless you need to sort it (ArraySort)(by time) why do you need to store anything but tickets?

 
William Roeder:

Do you really expect an answer? There are no mind readers here and our crystal balls are cracked. Always post all relevant code (using Code button).
     How To Ask Questions The Smart Way. 2004
          Be precise and informative about your problem

We can't possibly know what tradelist is.

Unless you need to sort it (ArraySort)(by time) why do you need to store anything but tickets?

Hello William,

Apologies for this, please find below the full code:

//+------------------------------------------------------------------+
//|                                                                  |
//|                                                                  |
//+------------------------------------------------------------------+

const string ver = "1.0.0";
input int magic_num=2021; //Magic Number
input string time_open_str="05:00"; //Trading Window Start Time (GMT)
input string time_close_str="04:59"; //Trading Window End Time (GMT)
input int max_trades=0; //Max Trades Per Trading Window (0 = unlimited)
input int max_cycles=1; //Max Cycles Per Trading Window (0 = unlimited)
input int qde=325; //Indicator Indicator Entry Value (325 = default)
input int qdc=325; //Indicator Indicator Exit Value (325 = default)
input int slip=10; //Order Slippage
input double lots1=0.01; //Lot Size (Trades 1-19)
input double lots2=0.02; //Lot Size (Trades 20-30)
input double lots3=0.03; //Lot Size (Trades 31-45)
input double lots4=0.05; //Lot Size (Trades 46-55)
input double lots5=0.08; //Lot Size (Trades 56-70)
input double lots6=0.13; //Lot Size (Trades 71-77)
input double lots7=0.21; //Lot Size (Trades 78-84)
input double lots8=0.34; //Lot Size (Trades 85-90)
input double lots9=0.55; //Lot Size (Trades 91-95)
input double lots10=0.89; //Lot Size (Trades 96 & >)
input double sl_pct=0; //% Equity SL For All Trades (positive number e.g 2.5)
input double tp_pct=0; //% Equity TP For All Trades (positive number e.g 2.5)
input double sl_dollar=0; //$ Amount SL For All Trades (positive number e.g 100.00)
input double tp_dollar=0; //$ Amount TP For All Trades (positive number e.g 100.00)
input int sl_points=0; //SL In Pips Per Trade (e.g 50 = 5 pips)
input int tp_points=0; //TP In Pips Per Trade (e.g 50 = 5 pips)
input int min_price_diff=0; //Pip Distance Between Trades (e.g 50 = 5 pips)

int cycles = 0;
int trades = 0;
int trade_side = -1;
datetime day_curr;
datetime day_prev;
int bars = Bars;
double lastTradePrice = 0;

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int init()
  {
   return 0;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int deinit()
  {
   return 0;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
int start()
  {
   datetime time_curr = TimeGMT();
   datetime time_open = StrToTime(time_open_str);
   datetime time_close = StrToTime(time_close_str);
   MqlDateTime dtct, dto, dtc;
   TimeToStruct(time_curr, dtct);
   TimeToStruct(time_open, dto);
   TimeToStruct(time_close, dtc);
   dto.year = dtc.year = dtct.year;
   dto.mon = dtc.mon = dtct.mon;
   dto.day = dtc.day = dtct.day;
   time_open = StructToTime(dto);
   time_close = StructToTime(dtc);

   if(time_open > time_close)
     {
      //assume next day
      time_close += 86400;
     }

   bool barNext = false;

   day_curr=iTime(Symbol(),PERIOD_D1,0);

   if(day_curr > day_prev)
     {
      cycles=0;
      day_prev=iTime(Symbol(),PERIOD_D1,0);
     }

   checkCloseTrades();

   if(Bars > bars)
     {
      bars = Bars;
      barNext = true;
     }

   if((cycles < max_cycles || max_cycles == 0) && (trades < max_trades || max_trades == 0) && time_curr >= time_open && time_curr < time_close && barNext)
     {
      int nextTicket = -1;
      bool trade = false;
      double price = 0;
      double tp = 0, sl = 0;
      //buy
      if(iCustom(Symbol(),0,"Indicator",qde,0,1) > 0 && (trade_side == -1 || trade_side == OP_BUY))
        {
         trade_side = OP_BUY;
         trade = true;
         price = Ask;
         if(tp_points)
            tp = price + (tp_points*Point);
         if(sl_points)
            sl = price - (sl_points*Point);
        }
      else
         //sell
         if(iCustom(Symbol(),0,"Indicator",qde,1,1) > 0 && (trade_side == -1 || trade_side == OP_SELL))
           {
            trade_side = OP_SELL;
            trade = true;
            price = Bid;
            if(tp_points)
               tp = price - (tp_points*Point);
            if(sl_points)
               sl = price + (sl_points*Point);
           }
      //trade
      if(trade)
        {
         //check minimum dist
         if(min_price_diff != 0)
           {
            double diff = MathAbs(price - lastTradePrice);
            if(diff < (min_price_diff*Point))
              {
               Print("Minimum Price Diff Not Met: ",diff);
               return 0;
              }
           }

         nextTicket = OrderSend(Symbol(), trade_side, getLots(), price, slip, sl, tp, "Indicator", magic_num, 0, 0);

         if(nextTicket <= -1)
           {
            Print("OrderSend Error: ",GetLastError());
           }
         else
           {
            lastTradePrice = price;
            trades++;
           }
        }
     }

   return 0;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
double getLots()
  {
   double retlots = 0.01;

   if(trades>=0 && trades<19)
      retlots=lots1;
   if(trades>=19 && trades<30)
      retlots=lots2;
   if(trades>=30 && trades<45)
      retlots=lots3;
   if(trades>=45 && trades<55)
      retlots=lots4;
   if(trades>=55 && trades<70)
      retlots=lots5;
   if(trades>=70 && trades<77)
      retlots=lots6;
   if(trades>=77 && trades<84)
      retlots=lots7;
   if(trades>=84 && trades<90)
      retlots=lots8;
   if(trades>=90 && trades<95)
      retlots=lots9;
   if(trades>=95)
      retlots=lots10;

   return retlots;
  }

//+------------------------------------------------------------------+
//|                                                                  |
//+------------------------------------------------------------------+
void checkCloseTrades()
  {
   bool close = false;

   if(trades > 0)
     {
      if((sl_pct > 0 && AccountEquity() <= (AccountBalance() - (AccountBalance() * (sl_pct/100)))) ||
         (sl_dollar > 0 && AccountEquity() <= (AccountBalance() - sl_dollar)))
        {
         close = true;
         Print("Stoploss Triggered");
        }
      else
         if((tp_pct > 0 && AccountEquity() >= (AccountBalance() + (AccountBalance() * (tp_pct/100)))) ||
            (tp_dollar > 0 && AccountEquity() >= (AccountBalance() + tp_dollar)))
           {
            close = true;
            Print("Take Profit Triggered");
           }
         else
           {
            close = (iCustom(Symbol(),0,"Indicator",qdc,1,1) > 0 && trade_side == OP_BUY) || (iCustom(Symbol(),0,"Indicator",qdc,0,1) > 0 && trade_side == OP_SELL);
           }

      if(close)
        {
         while(trades > 0)
           {
            int tradeList[][2];
            int size = 0;

            for(int h = OrdersTotal()-1; h >= 0; h--)
              {
               if(!OrderSelect(h,SELECT_BY_POS,MODE_TRADES))
                  continue;
               if(OrderSymbol() == Symbol() && OrderMagicNumber() == magic_num)
                 {
                  size++;
                  ArrayResize(tradeList, size);
                  tradeList[size-1][0]=(int)OrderOpenTime();
                  tradeList[size-1][1]=OrderTicket();
                 }
              }

            if(size > 0)
              {
               ArraySort(tradeList);

               for(int i=0; i < size; i++)
                 {
                  if(!OrderSelect(tradeList[i][1],SELECT_BY_TICKET))
                     continue;
                  if(OrderClose(OrderTicket(),OrderLots(),OrderClosePrice(),0,0))
                    {
                     trades--;
                    }
                  else
                    {
                     Print("OrderClose Error: ",GetLastError());
                    }
                 }
              }
            else
              {
               trades = 0;
              }
           }
         cycles++;
         trade_side = -1;
         lastTradePrice = 0;
        }
     }
  }
//+------------------------------------------------------------------+

My main problem is there it's supposed to enter a position after every signal, which it does but after a certain period (no consitent) it stops and doesn't enter again, sometimes 10 trades sometimes 20 it depends

I am trying to understand why it doesn't enter anymore, and I know I had an alert about "Possible loss of data due to type conversion" with the following line:

tradeList[size-1][0]=OrderOpenTime();

I resolved this by changing it to the following:

tradeList[size-1][0]=(int)OrderOpenTime();

But I am still having the same problem where the positions stop stacking after a variable X amount of entries

Thank you!

 
           int tradeList[][2];
                  tradeList[size-1][0]=(int)OrderOpenTime();
                  tradeList[size-1][1]=OrderTicket();
OrderOpenTime does not fit into an int. You resolved nothing.
 
William Roeder:
OrderOpenTime does not fit into an int. You resolved nothing.
The problem is if I remove the (int) I will get the error of Possible data loss right. I am not sure where I am going wrong and why after a few orders go through it suddenly stops adding more orders even though it should add more, do you think you could shed some light?
 
dark000soul: The problem is if I remove the (int) I will get the error of Possible data loss right.
That compounds the problem. You can not store a datetime into an int. Use a larger data type and cast it to that.
 

... why not define tradeList as of type datetime instead of int?!

you're passing datetime instances anyway ...


...

// define array as of datatype datetime
datetime tradeList[][2];
 
...

// pass time object instead of int
tradeList[size-1][0]=OrderOpenTime();
 
ranxero: ... why not define tradeList as of type datetime instead of int?!

That will work. But because he is also storing a ticket number in tradeList[][1] means he will need to cast the ticket in and out.

 
William Roeder:

That will work. But because he is also storing a ticket number in tradeList[][1] means he will need to cast the ticket in and out.

Hello William,

It sounds like you know what you're doing, do you think you could fix this for me?

Let me know if you wish to charge for this service

-dark000soul

 
I have already stated what to do; you haven't done it. What part of "Use a larger data type and cast it to that" was unclear?
int tradeList[][2];
⋮
tradeList[size-1][0]=(int)OrderOpenTime();
Reason: