Refresh indicator with with the start of new day?

 

Using this thread as a guide (https://www.mql5.com/en/forum/121681) I put together the below indicator as a way of indicating the FTSE Open and Close with some vertical lines on the chart window. I was hoping that as the server time passed through 00:00, the indicator would draw in more lines for the new day (and that this would repeat with each new day), however, this does not appear to be the case.

Can anyone suggest a way in which the code can be improved so that I do not have to add the indicator to the chart window with each new day in order for the lines to be drawn in?

Thanks

#property indicator_chart_window

extern string LineNameSOD = "FTSE_SOD";
extern color LineColorSOD = CornflowerBlue; 
extern int LineStyleSOD = STYLE_SOLID;
extern int HoursSOD = 8;

extern string LineNameEOD = "FTSE_EOD";
extern color LineColorEOD = CornflowerBlue; 
extern int LineStyleEOD = STYLE_DASHDOT;
extern double HoursEOD = 16.5;

extern bool SetAsBackground = True;
int i = 0;

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

int init()
  {
   return(0);
  }


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

int start()
  {

int Unused = 0;
datetime LineTime;


for ( i=0; i<=1; i++ )
  {
   LineTime = iTime( NULL, PERIOD_D1, 0 ) + HoursSOD * 3600;     // *3600 because datetime variables are in seconds
   LineNameSOD = "FTSE_SOD";   
   ObjectCreate( LineNameSOD, OBJ_VLINE, 0, LineTime, Unused );
   ObjectSet( LineNameSOD, OBJPROP_STYLE, LineStyleSOD );
   ObjectSet( LineNameSOD, OBJPROP_COLOR, LineColorSOD );
   ObjectSet( LineNameSOD, OBJPROP_BACK, SetAsBackground );
  }   

for ( i=0; i<=1; i++ )
  {
   LineTime = iTime( NULL, PERIOD_D1, 0 ) + HoursEOD * 3600;     // *3600 because datetime variables are in seconds
   LineNameEOD = "FTSE_EOD";   
   ObjectCreate( LineNameEOD, OBJ_VLINE, 0, LineTime, Unused );
   ObjectSet( LineNameEOD, OBJPROP_STYLE, LineStyleEOD );
   ObjectSet( LineNameEOD, OBJPROP_COLOR, LineColorEOD );
   ObjectSet( LineNameEOD, OBJPROP_BACK, SetAsBackground );
  } 


   return(0);
  }


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

int deinit()
  {
  
int objTotal = ObjectsTotal();

int i = 0;

while(i < objTotal)
   {
   LineNameSOD = ObjectName(i);

    if( StringSubstr( LineNameSOD, 0, 8 ) == "FTSE_SOD" )
       ObjectDelete( LineNameSOD );
   
   LineNameEOD = ObjectName(i);
    
    if( StringSubstr( LineNameEOD, 0, 8 ) == "FTSE_EOD" )
       ObjectDelete( LineNameEOD ); 
    
    else i++;
   }         

   return(0);
  }
 
marktheplant:

Using this thread as a guide (https://www.mql5.com/en/forum/121681) I put together the below indicator as a way of indicating the FTSE Open and Close with some vertical lines on the chart window. I was hoping that as the server time passed through 00:00, the indicator would draw in more lines for the new day (and that this would repeat with each new day), however, this does not appear to be the case.

Can anyone suggest a way in which the code can be improved so that I do not have to add the indicator to the chart window with each new day in order for the lines to be drawn in?

Thanks

Take a look at this thread: https://www.mql5.com/en/forum/146426
 
marktheplant: I was hoping that as the server time passed through 00:00, the indicator would draw in more lines for the new day
   LineTime = iTime( NULL, PERIOD_D1, 0 ) + HoursSOD * 3600;     // *3600 because datetime variables are in seconds
   LineNameSOD = "FTSE_SOD";   
   ObjectCreate( LineNameSOD, OBJ_VLINE, 0, LineTime, Unused );
  1. Hoping? Code it properly. Think about it. RTFM. Do, or do not, there is no hope.
  2. An object as a name, so once you create 'FTSE_SOD' the next time ObjectCreate fails and the line does not move. This you would have learned had you checked: What are Function return values ? How do I use them ? - MQL4 forum
  3. There are 2 choices. If you want the line to move, do it like my Tline() and Hline() functions
  4. If you want multiple lines, use a unique name for them.
       LineNameSOD = "FTSE_SOD" + LineTime;  

 

Hi RaptorUK and WHRoeder - thanks for your help. I think I have got there now and have a better understanding too. I believe that the below code will draw the lines just once a day and keep them on the chart in the format I want until I choose to delete them. Thanks

#property indicator_chart_window

extern string  LineNameSOD = "FTSE_SOD";
extern color   LineColorSOD = CornflowerBlue; 
extern int     LineStyleSOD = STYLE_SOLID;
extern int     HoursSOD = 8;

extern string  LineNameEOD = "FTSE_EOD";
extern color   LineColorEOD = CornflowerBlue; 
extern int     LineStyleEOD = STYLE_DASHDOT;
extern double  HoursEOD = 16.5;

extern bool    SetAsBackground = True;

datetime       LineTime;      
datetime       CandleTime;        //Can use to check if current tick is for a new candle  

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

int init()
  {
   return(0);
  }

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

int start()
  {
   if( iTime(NULL, PERIOD_D1, 1) == CandleTime )     // Only allow update of indicator once a day
     {
      return(0);
     }
   else   
     { 
      int     Day_ = Day();
      int     Month_ = Month();
      int     Year_ = Year();
    
      LineTime = iTime( NULL, PERIOD_D1, 0 ) + HoursSOD * 3600;     // *3600 because datetime variables are in seconds
      LineNameSOD = "FTSE_SOD" + " " + Year_ + " " + Month_ + " " + Day_;   
      ObjectCreate ( LineNameSOD, OBJ_VLINE, 0, LineTime, 0 );
      ObjectSet    ( LineNameSOD, OBJPROP_STYLE, LineStyleSOD );
      ObjectSet    ( LineNameSOD, OBJPROP_COLOR, LineColorSOD );
      ObjectSet    ( LineNameSOD, OBJPROP_BACK, SetAsBackground );

      LineTime = iTime( NULL, PERIOD_D1, 0 ) + HoursEOD * 3600;     // *3600 because datetime variables are in seconds
      LineNameEOD = "FTSE_EOD"  + " " + Year_ + " " + Month_ + " " + Day_;   
      ObjectCreate ( LineNameEOD, OBJ_VLINE, 0, LineTime, 0 );
      ObjectSet    ( LineNameEOD, OBJPROP_STYLE, LineStyleEOD );
      ObjectSet    ( LineNameEOD, OBJPROP_COLOR, LineColorEOD );
      ObjectSet    ( LineNameEOD, OBJPROP_BACK, SetAsBackground );

      CandleTime = iTime(NULL, PERIOD_D1, 1);
     } 
   return(0);
  }

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

int deinit()
  {
  
   int objTotal = ObjectsTotal();
   int i = 0;

   while(i < objTotal)
      {
       LineNameSOD = ObjectName(i);
       if( StringSubstr( LineNameSOD, 0, 8 ) == "FTSE_SOD" )
       ObjectDelete( LineNameSOD );
   
       LineNameEOD = ObjectName(i);
       if( StringSubstr( LineNameEOD, 0, 8 ) == "FTSE_EOD" )
       ObjectDelete( LineNameEOD ); 
    
       else i++;
      }         
   return(0);
  }