possible loss of data due to type conversion

 
I have written an indicator that copies trend and horizontal lines from one chart to another. It works ok. But, when I compile the code I get 8 warnings. I can't figure out how to eliminate the warnings. Any suggestions would be appreciated.  Regards  ----Tom
//+------------------------------------------------------------------+
//|                                                  Line Copier.mq4 |
//|                                     Copyright 2021, Tom Parimore |
//|                                              tparimore@yahoo.com |
//+------------------------------------------------------------------+
#property copyright "Tom Parimore"
#property link      "tparimore@yahoo.com"
#property version   "1.00"
#property strict
#property indicator_chart_window





input bool OmitFirstChart = true;

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
  {

   //--- set the flag of receiving chart object creation events
   ChartSetInteger(ChartID(),CHART_EVENT_OBJECT_CREATE,true);
   
   return(INIT_SUCCEEDED);
  }
//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int OnCalculate(const int rates_total,
                const int prev_calculated,
                const datetime &time[],
                const double &open[],
                const double &high[],
                const double &low[],
                const double &close[],
                const long &tick_volume[],
                const long &volume[],
                const int &spread[])
  {

   return(rates_total);
  }
//+------------------------------------------------------------------+

void OnChartEvent(const int id,
                   const long &lparam,
                   const double &dparam,
                   const string &sparam)
  {

     long currChart = ChartFirst(), CopierChart = ChartID() ;
     datetime TL_OpenTime,TL_CloseTime;
     double TL_OpenPrice,TL_ClosePrice, HL_Price;
     color TL_Color, HL_Color;
     int TL_Width, TL_Style, HL_Width, HL_Style;
     
     // Chart Event
     if(    id==CHARTEVENT_OBJECT_CREATE
         || id==CHARTEVENT_OBJECT_DRAG
         || id==CHARTEVENT_OBJECT_CHANGE
     
       ){             
      
         // Trend Line
         if(ObjectType(sparam) == OBJ_TREND)
          {
            TL_OpenTime   = ObjectGet(sparam,OBJPROP_TIME1);
            TL_CloseTime  = ObjectGet(sparam,OBJPROP_TIME2);
            TL_OpenPrice  = ObjectGet(sparam,OBJPROP_PRICE1);
            TL_ClosePrice = ObjectGet(sparam,OBJPROP_PRICE2);      
            TL_Color      = ObjectGet(sparam,OBJPROP_COLOR);
            TL_Width      = ObjectGet(sparam,OBJPROP_WIDTH);        
            TL_Style      = ObjectGet(sparam,OBJPROP_STYLE);      
                    
            while(currChart >= 0)
             {
               if(     ChartSymbol(currChart) == ChartSymbol(CopierChart)
                   &&  currChart != CopierChart
                   && (currChart != ChartFirst()  || !OmitFirstChart)
                 ){
                    ObjectDelete(currChart,sparam + "1");
                    
                    ObjectCreate(currChart,sparam + "1",OBJ_TREND,0,TL_OpenTime,TL_OpenPrice,TL_CloseTime,TL_ClosePrice);
                    ObjectSetInteger(currChart,sparam + "1",OBJPROP_COLOR,TL_Color);
                    ObjectSetInteger(currChart,sparam + "1",OBJPROP_STYLE,TL_Style);
                  }
               
               currChart = ChartNext(currChart);
             }
          }
          
          
        // Horizontal Line
         if(ObjectType(sparam) == OBJ_HLINE)
          {
            HL_Price   = ObjectGet(sparam,OBJPROP_PRICE1);      
            HL_Color   = ObjectGet(sparam,OBJPROP_COLOR);
            HL_Width   = ObjectGet(sparam,OBJPROP_WIDTH);        
            HL_Style   = ObjectGet(sparam,OBJPROP_STYLE);      
                  
            while(currChart >= 0)
             {
               if(     ChartSymbol(currChart) == ChartSymbol(CopierChart)
                   &&  currChart != CopierChart
                   && (currChart != ChartFirst()  || !OmitFirstChart)
                 ){
                    ObjectDelete(currChart,sparam + "1");
                 
                    ObjectCreate(currChart,sparam + "1",OBJ_HLINE,0,0,HL_Price);
                    ObjectSetInteger(currChart,sparam + "1",OBJPROP_COLOR,HL_Color);
                    ObjectSetInteger(currChart,sparam + "1",OBJPROP_STYLE,HL_Style);
                  }
               
               currChart = ChartNext(currChart);
             }
          } 
                  
       }
              
        
    }    
 
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2.             TL_OpenTime   = ObjectGet(sparam,OBJPROP_TIME1);
    You have aDatetime = aDouble Perhaps you should cast to the correct data type.
 
William Roeder #:
  1. Why did you post your MT4 question in the MT5 Indicators section instead of the MQL4 section, (bottom of the Root page)?
              General rules and best pratices of the Forum. - General - MQL5 programming forum? (2017)
    Next time, post in the correct place. The moderators will likely move this thread there soon.


  2. You have aDatetime = aDouble Perhaps you should cast to the correct data type.

First Let me apologize for posting in the wrong place. By all means place it in the correct form. 

Now I realize that there is a data type mismatch. But, I can't seem to work it out. For instance: In the link you posted, there is an ObjectGet  example for getting color. I used this very example and I get a warning.

// LINKED EXAMPLE
color oldColor=ObjectGet("hline12", OBJPROP_COLOR);


// MY CODE
color TL_Color, HL_Color;

TL_Color = ObjectGet(sparam,OBJPROP_COLOR);   // Yields possible loss warning???


  

 
TL_Color = ObjectGet(sparam,OBJPROP_COLOR);   // Yields possible loss warning???
What part of “Perhaps you should cast to the correct data type” was unclear?
 
William Roeder #:
What part of “Perhaps you should cast to the correct data type” was unclear?

What is a cast? How do I cast color, datetime... I am declaring the variables. Is that a cast?  Sorry for the ignorance.  ---Tom

 
tparimore #: What is a cast?

Read the following documentation on Typecasting.

ObjectGet returns a "double" data-type. You will need to typecast it into a "datetime" or a "color", or whatever other data-type is expected to be returned depending on your needs.

TL_OpenTime = (datetime) ObjectGet(sparam,OBJPROP_TIME1);
TL_Color    = (color)    ObjectGet(sparam,OBJPROP_COLOR);
Documentation on MQL5: Language Basics / Data Types / Typecasting
Documentation on MQL5: Language Basics / Data Types / Typecasting
  • www.mql5.com
Typecasting - Data Types - Language Basics - MQL5 Reference - Reference on algorithmic/automated trading language for MetaTrader 5
 
Fernando Carreiro #:

Read the following documentation on Typecasting.

ObjectGet returns a "double" data-type. You will need to typecast it into a "datetime" or a "color", or whatever other data-type is expected to be returned depending on your needs.

Thank you soooooo much!  I hated the warnings when compiling. When William told me I needed to cast the data, I found Typecasting. However, I couldn't figure out what to do.  I have made the changes and it now works great with no warnings.  Thanks again. ---Tom

 
Fernando Carreiro #:

Read the following documentation on Typecasting.

ObjectGet returns a "double" data-type. You will need to typecast it into a "datetime" or a "color", or whatever other data-type is expected to be returned depending on your needs.

I reread the Typecasting (key being "Type"). I get it now. Thanks for your guidance.  Kind Regards  ----Tom

 
tparimore #: I reread the Typecasting (key being "Type"). I get it now. Thanks for your guidance.  Kind Regards  ----Tom
You are welcome!