possible loss of data due to type conversion error

To add comments, please log in or register
Franzel Botha
902
Franzel Botha  

I am busy developing an EA.
EA works but I have 2 warnings in code when compiling.
does anyone have a solution towards removing the warnings?

1st one 

possible loss of data due to type conversion 1st Hedge EA overlay v7 timer.mq4 229 81

2nd one 

possible use of uninitialized variable 'ticket' 1st Hedge EA overlay v7 timer.mq4 234 11


{  
int ticket; 
if(type== OP_BUY){
RefreshRates();
ticket=OrderSend(symbol,OP_BUY,0.01,NormalizeDouble(MarketInfo(symbol,MODE_ASK),MarketInfo(symbol,MODE_DIGITS)),10,0,0,StringConcatenate(magic),magic,0,clrNONE); // 229 81 // 
}
if(type== OP_SELL){
ticket=OrderSend(symbol,OP_SELL,0.01,NormalizeDouble(MarketInfo(symbol,MODE_BID),MarketInfo(symbol,MODE_DIGITS)),10,0,0,StringConcatenate(magic),magic,0,clrNONE);
}
   return(ticket); // 243 11 //
}
Anthony Garot
3272
Anthony Garot  
1st one 


possible loss of data due to type conversion 1st Hedge EA overlay v7 timer.mq4 229 81

MarketInfo() returns a double.

NormalizeDouble is expecting an integer for the digits argument.

Anthony Garot
3272
Anthony Garot  

2nd one 

possible use of uninitialized variable 'ticket' 1st Hedge EA overlay v7 timer.mq4 234 11

So initialize it.

int ticket = -1;
Jack Thomas
1626
Jack Thomas  
Franzel Botha:

I am busy developing an EA.
EA works but I have 2 warnings in code when compiling.
does anyone have a solution towards removing the warnings?

1st one 

possible loss of data due to type conversion 1st Hedge EA overlay v7 timer.mq4 229 81

2nd one 

possible use of uninitialized variable 'ticket' 1st Hedge EA overlay v7 timer.mq4 234 11



The code you provided:

{  
int ticket; 
if(type== OP_BUY){
RefreshRates();
ticket=OrderSend(symbol,OP_BUY,0.01,NormalizeDouble(MarketInfo(symbol,MODE_ASK),MarketInfo(symbol,MODE_DIGITS)),10,0,0,StringConcatenate(magic),magic,0,clrNONE); // 229 81 // 
}
if(type== OP_SELL){
ticket=OrderSend(symbol,OP_SELL,0.01,NormalizeDouble(MarketInfo(symbol,MODE_BID),MarketInfo(symbol,MODE_DIGITS)),10,0,0,StringConcatenate(magic),magic,0,clrNONE);
}
   return(ticket); // 243 11 //


Use can use type casting to convert that from double to int.

For example:

int myDigits = (int)MarketInfo(Symbol(),MODE_DIGITS); //This gives you the digits as an Integer.
string mySymbol = Symbol(); //This gives you the current symbol of the chart
int ticket = -1; //initialize the ticket variable


Then if you want to send an order it would look something like as follows.

ticket=OrderSend(symbol,OP_BUY,0.01,NormalizeDouble(mySymbol,myDigits),10,0,0,StringConcatenate(magic),magic,0,clrNONE);
Franzel Botha
902
Franzel Botha  
Thnx for the replies.

It makes great sense now.
pradit thongget
39
pradit thongget  
void CheckLOT()
{
               OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES);
                  int Type=OrderType();
                  double Lot=OrderLots();
               if(OrdersTotal()>0)
                  { 
                     for(int i=0; i < OrdersTotal(); i++)
                        {
                     if(Type==OP_BUY) {Lot1 = Lot*i; }       
                     if(Type==OP_SELL){Lot2 = Lot*i; } 
                     }
                     _Lots = Lot1+Lot2;
                     
                  }
               if(OrdersTotal()==0)
                  {
                      Lot1 = 0;
                      Lot2 = 0;
                     _Lots = 0;
                  } 
}                   
return value of 'OrderSelect' should be checked

kypa
1079
kypa  

OrderSelect might not select an order and return false. Then all the things below will be done to the previously selected order or not done at all.

if(OrderSelect(OrdersTotal()-1,SELECT_BY_POS,MODE_TRADES))
You should probably check OrdersTotal before selecting an order.
pradit thongget
39
pradit thongget  

possible loss of data due to type conversion error


#property strict
#define __STRATEGY_MAGIC 1001000000
#define __SLEEP_AFTER_EXECUTION_FAIL 400

input double _TrailingStop    = 0;                 // TrailingStop
 

int init() {

   return(0);
}

int start() {

   bool _Trailing_SELL = false;  
   bool _TrailingSELL2 = false;
   bool _TrailingSELL3 = false;

   _Trailing_SELL       = Simple_Trailing_Stop(13, 0, _TrailingStop, 10);   
   _TrailingSELL2       = Simple_Trailing_Stop(15, 0, _TrailingStop, 10);
   _TrailingSELL3       = Simple_Trailing_Stop(17, 0, _TrailingStop, 10);

   if( __isOpenedPosition(2, Symbol()) && !__isExist( 13, Symbol() ) ) {
      _SellStop1 = _Number_Input_3 >= MarketInfo( Symbol() ,MODE_MINLOT);
      if( _SellStop1 == true ) _SellStop1 = OrderSend( Symbol(), 5, _Number_Input_3, NormalizeDouble( (_Sell_2 -(5)), (int)MarketInfo( Symbol(), MODE_DIGITS ) ), 0, __stopLossValue( Symbol(), 5, (_Sell_2 - (5)), _Number_Input_5 ), __takeProfitValue( Symbol(), 5, (_Sell_2 - (5)), _Number_Input_4 ), "PD" + "(" + WindowExpertName() + ") " + "SELL STOP1", __STRATEGY_MAGIC + 13, D'1970.01.01 00:00:00' ) >= 0;
      if( _SellStop1 == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); 
         }
         
         
   if( __isOpenedPosition(2, Symbol()) && !__isExist( 15, Symbol() ) ) {
      _SellStop2 = _Number_Input_3 >= MarketInfo( Symbol() ,MODE_MINLOT);
      if( _SellStop2 == true ) _SellStop2 = OrderSend( Symbol(), 5, _Number_Input_3, NormalizeDouble( (_Sell_2 -(10)), (int)MarketInfo( Symbol(), MODE_DIGITS ) ), 0, __stopLossValue( Symbol(), 5, (_Sell_2 -(10)), _Number_Input_5 ), __takeProfitValue( Symbol(), 5, (_Sell_2 - (10)), _Number_Input_4 ), "PD" + "(" + WindowExpertName() + ") " + "SELL STOP2", __STRATEGY_MAGIC + 15, D'1970.01.01 00:00:00' ) >= 0;
      if( _SellStop2 == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); 
         }
         
        
   if( __isOpenedPosition(2, Symbol()) && !__isExist( 17, Symbol() ) ) {
      _SellStop3 = _Number_Input_3 >= MarketInfo( Symbol() ,MODE_MINLOT);
      if( _SellStop3 == true ) _SellStop3 = OrderSend( Symbol(), 5, _Number_Input_3, NormalizeDouble( (_Sell_2 -(15)), (int)MarketInfo( Symbol(), MODE_DIGITS ) ), 0, __stopLossValue( Symbol(), 5, (_Sell_2 -(15)), _Number_Input_5 ), __takeProfitValue( Symbol(), 5, (_Sell_2 - (15)), _Number_Input_4 ), "PD" + "(" + WindowExpertName() + ") " + "SELL STOP3", __STRATEGY_MAGIC + 17, D'1970.01.01 00:00:00' ) >= 0;
      if( _SellStop3 == false ) Sleep(__SLEEP_AFTER_EXECUTION_FAIL); 
         }
 }

bool Simple_Trailing_Stop(int MagicIndex, int WaitForProfit, int TrailingStopPoints, int MinAdjustmentPoints)
{   
   double pnlPoints=0;   
   double price, sl, tp;
   double point = MarketInfo(Symbol(),MODE_POINT);
   int stopLevel = int(MarketInfo(Symbol(),MODE_STOPLEVEL) + MarketInfo(Symbol(),MODE_SPREAD));  
   int cmd; 
      
   bool result = true;   
   double newSl;

   int total = OrdersTotal();
      for(int i=total-1;i>=0;i--){
      if (!OrderSelect(i, SELECT_BY_POS)) continue;
      if(OrderMagicNumber() != __STRATEGY_MAGIC + MagicIndex || OrderSymbol() != Symbol()) continue;
      
      cmd = OrderType();      
      sl = NormalizeDouble(OrderStopLoss(),Digits);
      tp = OrderTakeProfit();
       
      if (OrderType() == OP_BUY)
      {
         price = MarketInfo(Symbol(),MODE_BID);
         newSl = NormalizeDouble(price - TrailingStopPoints * point, Digits);    
         if(((tp - price)/point) < stopLevel && tp != 0) continue;         
         if(((price - newSl)/point) < stopLevel)continue; 
         if(WaitForProfit == 0)
         {        
            pnlPoints = (price - OrderOpenPrice())/point; 
            if (pnlPoints < TrailingStopPoints ) continue;  
         }        
         if (sl + MinAdjustmentPoints*point>= newSl) continue;       
         
         if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSl, tp, 0))
         {
            printf("Error: Failed to modify trade. Ticket #%i, error code: %i", OrderTicket(), GetLastError());
            result = false;
            Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
         }
      }  
      else if (OrderType() == OP_SELL)
      {
         price = MarketInfo(Symbol(),MODE_ASK);
         newSl = NormalizeDouble(price+ TrailingStopPoints * point, Digits);
         if(((price - tp)/point) < stopLevel) continue;
         if(((newSl - price)/point) < stopLevel) continue;
         if(WaitForProfit == 0)
         {              
            pnlPoints = (OrderOpenPrice() - price)/point;
            if (pnlPoints < TrailingStopPoints) continue; 
         }         
         if (sl - MinAdjustmentPoints*point <= newSl && sl != 0) continue;
         
         if(!OrderModify(OrderTicket(), OrderOpenPrice(), newSl, tp, 0))
         {
            printf("Error: Failed to modify trade. Ticket #%i, error code: %i", OrderTicket(), GetLastError());
            result = false;
            Sleep(__SLEEP_AFTER_EXECUTION_FAIL);
         }         
       }      
   }   
   return(result);
}
William Roeder
20214
William Roeder  
pradit thongget: possible loss of data due to type conversion error
input double _TrailingStop    = 0;                 // TrailingStop
int start() {

   bool _Trailing_SELL = Simple_Trailing_Stop(13, 0, _TrailingStop, 10);
   bool _TrailingSELL2 = Simple_Trailing_Stop(15, 0, _TrailingStop, 10);
   bool _TrailingSELL3 = Simple_Trailing_Stop(17, 0, _TrailingStop, 10);
 }

bool Simple_Trailing_Stop(int MagicIndex, int WaitForProfit, int TrailingStopPoints,
To add comments, please log in or register