Having Trouble undertanding Time in following context

 

Hello forum, I am having real trouble understanding and using time correctly in my code

I have read the documentation many times, but still not grasping concept properly

I am trying to create a pivot point indicator that will calculate values based only on the bars occurring within the range I have specified

(Between MarketOpenTime and MarketCloseTime where these two times are variables)

Raptor assisted me in an earlier post in defining this time period for the "current day only"

string StartTime = "00:50", EndTime = "07:30";
int StartBar, EndBar;
double Val;

StartBar = iBarShift(NULL, 0, StrToTime(StartTime) ); 
EndBar = iBarShift(NULL, 0, StrToTime(EndTime) );

Val = High[iHighest(NULL, 0, MODE_HIGH, StartBar - EndBar + 1, EndBar) ];

But I have got myself into trouble when trying to expand to allow for situations where my pivot values need to be based on the previous days data

The code (attached) I have put together compiles ok but is not giving me the correct answer (in fact i get a low) and I am pretty sure the error is to do with the way I am defining "time"

(Have only bothered to specify a single high line and not all pivot calculations, while I try to get the concept of time between two points right in my head).

Would someone have  look at this code and tell me if I am incorrectly converting my open and close times to date time, or point out what is wrong please?

 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
 //|                                         http://www.metaquotes.ru |
 //+------------------------------------------------------------------+

 //---- indicator settings
   #property  indicator_chart_window
   #property  indicator_buffers 0
 //----
  
 //---- indicator parameters
   
   extern bool LINE = true;               // set true to display MA's
   string      MarketOpenTime  = "00:50";
   string      MarketCloseTime = "07:30";
   int         StartBar, EndBar;
   double      HVal;
   double      StartTime;
   double      EndTime;
    
 //+------------------------------------------------------------------+
   //| Custom indicator initialization function                       |
 //+------------------------------------------------------------------+
   int init()
     {
 //---- DataWindow and indicator subwindow label
      IndicatorShortName("PivotHigh_Line");
 //---- initialization done
      return(0);
     }
 //+------------------------------------------------------------------+
 //---- Custom indicator deinitialization function                    |
 //+------------------------------------------------------------------+
   int deinit()
       {
        ObjectDelete("LINE");
        ObjectDelete("LINE_label");
        
   return(0);
   }
 //+------------------------------------------------------------------+
   int start()
     {
      int nLimit, i;
      int nCountedBars;
      int ShiftDif;
          nCountedBars=IndicatorCounted();
 //---- check for possible errors
      if(nCountedBars<0)
         return(-1);
 //---- last counted bar will be recounted
      if(nCountedBars>0)
         nCountedBars--;
      nLimit=Bars-nCountedBars;
      //---- main loop
      for(i=0; i<nLimit; i++)
      
 //---- 2 situations for calculating pivot points, so 2 DIFFERENT StartTimes and EndTimes
 //---- Pivot point always to be calulated on the previous completed day market session
 //---- Day market session open between MarketOpenTime and MarketCloseTime
 
 //---- Any time fromm "00:00" to MarketCloseTime: Calculate pivot high from "PREVIOUS DAY" market open to close interval
      {
      if         ( (Time[0]>=iTime(Symbol(),PERIOD_D1,0)) && (Time[0]<StrToTime(MarketCloseTime)) )
      StartTime =(iTime(Symbol(),PERIOD_D1,1)+StrToTime(MarketOpenTime));
      EndTime   =(iTime(Symbol(),PERIOD_D1,1)+StrToTime(MarketCloseTime));
      
      StartBar  = iBarShift(NULL, 0, StrToTime(StartTime) ); 
      EndBar    = iBarShift(NULL, 0, StrToTime(EndTime) );

      HVal      = High[iHighest(NULL, 0, MODE_HIGH, StartBar - EndBar + 1, EndBar) ];
      }
 //---- Any Time after MarketCloseTime until "23:59", Calculate pivot high from TODAY's market open to close interval
      {         
      if         ( (Time[0]>StrToTime(MarketCloseTime))&&(Time[0]<=(iTime(Symbol(),PERIOD_D1,0)+23*60*60+59*60)) )
      StartTime =(iTime(Symbol(),PERIOD_D1,0)+StrToTime(MarketOpenTime)) ;
      EndTime   =(iTime(Symbol(),PERIOD_D1,0)+StrToTime(MarketCloseTime));
      
      StartBar  = iBarShift(NULL, 0, StrToTime(StartTime)); 
      EndBar    = iBarShift(NULL, 0, StrToTime(EndTime));
      
      HVal      = High[iHighest(NULL, 0, MODE_HIGH, StartBar - EndBar + 1, EndBar) ];
      }
           
    if (LINE) // draw if true
    {
    // bool ObjectCreate(string name, int type, int window, datetime time1, double price1, datetime time2=0,
    //   double price2=0, datetime time3=0, double price3=0)
    
    ObjectCreate("LINE", OBJ_TREND, 0, (Time[0]+500),HVal, (Time[0]+1500),HVal);
    ObjectSet("LINE", OBJPROP_RAY, false);
    ObjectSet("LINE", OBJPROP_TIME1, (Time[0]+500));
    ObjectSet("LINE", OBJPROP_TIME2, (Time[0]+1500));
    ObjectSet("LINE", OBJPROP_COLOR, Lime);
    ObjectSet("LINE",OBJPROP_WIDTH,3);
    
    }
 //+------------------------------------------------------------------+
      return(0);
     }
 //+------------------------------------------------------------------+

Also and this is a basic one, I see time referenced as eg. "7:30" and "7.30" and not sure of the difference between using  ":" or  "."

Your help would be greatly appreciated

thanks as always

dave

 
pullend:

Hello forum, I am having real trouble understanding and using time correctly in my code

I have read the documentation many times, but still not grasping concept properly

I am trying to create a pivot point indicator that will calculate values based only on the bars occurring within the range I have specified

(Between MarketOpenTime and MarketCloseTime where these two times are variables

So you want the Highest and Lowest between 00:50 and 07:30,  so why are you trying to calculate this at 05:00 or 06:00 ?  you have already calculated it for the previous day so why do it for every single bar ? 
 
RaptorUK:
So you want the Highest and Lowest between 00:50 and 07:30,  so why are you trying to calculate this at 05:00 or 06:00 ?  you have already calculated it for the previous day so why do it for every single bar ? 


Yes, of course, I was thinking that I needed to look at the high of every bar between start and end and forgot that High(iHighest etc would take care of that.

When I remove the loop, I think the problem lies in subtracting ny time values,

I assume my HVal calculations for the different days :

HVal      = High[iHighest(NULL, 0, MODE_HIGH, StartBar - EndBar + 1, EndBar) ]; become

HVal      =High( iHighest (NULL, 0, MODE_HIGH, StrToTime(00:50)-StrToTime(7:30)+1,  StrToTime(7:30)  ) and 

the      StrToTime(00:50)-StrToTime(7:30)+1     does not look like it is calculating properly

Do I need to specify these times differently, I think I am getting a very large incorrect bar range, otherwise I can't explain why the indicator is plotting a value under the intended price action?

Still confused with specifying these time'

thanks dave

 
pullend:

Yes, of course, I was thinking that I needed to look at the high of every bar between start and end and forgot that High(iHighest etc would take care of that.

When I remove the loop, I think the problem lies in subtracting ny time values, 

 What is the purpose of the loop in an Indicator ?  if you don't know why did you remove it ?  if you do know . . . then please explain why you removed it ?   there are logical reasons to keep it and remove it bt they depend on the end result that you are looking for . . . 

 

pullend:

I assume my HVal calculations for the different days :

HVal      = High[iHighest(NULL, 0, MODE_HIGH, StartBar - EndBar + 1, EndBar) ]; become

HVal      =High( iHighest (NULL, 0, MODE_HIGH, StrToTime(00:50)-StrToTime(7:30)+1,  StrToTime(7:30)  ) and 

the      StrToTime(00:50)-StrToTime(7:30)+1     does not look like it is calculating properly

Do I need to specify these times differently, I think I am getting a very large incorrect bar range, otherwise I can't explain why the indicator is plotting a value under the intended price action?

Still confused with specifying these time'

Read up on datetimes . . .   "Its internal representation is a long integer number of 4 bytes. The value represents the amount of seconds elapse from 00:00 Jan 1, 1970." . . .  frm, so a more recent date has a higher number compared to an older date.  A datetime is not a bar number . . .  one is a number is seconds since 1970 the other is a bar number . . .

 

Look at the documentation for iHighest() it uses the following parameters . . .  int iHighest(string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

one string and the rest are ints . . .  why are you giving it datetimes ?

 

To get the correct values for StartBar and EndBar you need to find the bar number for your datetimes . . .   iBarShift()  is your best friend ;-) 

 
RaptorUK:

 What is the purpose of the loop in an Indicator ?  if you don't know why did you remove it ?  if you do know . . . then please explain why you removed it ?   there are logical reasons to keep it and remove it bt they depend on the end result that you are looking for . . . 

 

Read up on datetimes . . .   "Its internal representation is a long integer number of 4 bytes. The value represents the amount of seconds elapse from 00:00 Jan 1, 1970." . . .  frm, so a more recent date has a higher number compared to an older date.  A datetime is not a bar number . . .  one is a number is seconds since 1970 the other is a bar number . . .

 

Look at the documentation for iHighest() it uses the following parameters . . .  int iHighest(string symbol, int timeframe, int type, int count=WHOLE_ARRAY, int start=0)

one string and the rest are ints . . .  why are you giving it datetimes ?

 

To get the correct values for StartBar and EndBar you need to find the bar number for your datetimes . . .   iBarShift()  is your best friend ;-) 


HELP!

Have spent most of day (Aust) looking at time documentation and fiddling,

Have FINALLY my code calculating the right levels, that is looking at the right bar intervals

Took almost all the variables out to find errors, the code compiles and draws a level Line, BUT

Now something wrong with my loops which I can't find !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!     :-(

(You are right I need to do more work understanding these basics)

When I change my market open and close times (manually in lines 66 and 76 of the code, the object is not redrawing based on the other HVal value

It must be something obvious but I have looked at it for so long, can no longer see .....anything   :-)

if someone would point out my error, I may remain sane, maybe ?????????

thanks, dave

 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
 //|                                         http://www.metaquotes.ru |
 //+------------------------------------------------------------------+

 //---- indicator settings
   #property  indicator_chart_window
   #property  indicator_buffers 0
 //----
  
 //---- indicator parameters
   
   extern bool LINE = true;               // set true to display MA's
   string  MarketOpenTime  ="00:50";
   string  MarketCloseTime ="07:30";  
   int         StartBar1,StartBar2, EndBar1,EndBar2;
   double      HVal1,HVal2,HVal;
   
    
 //+------------------------------------------------------------------+
   //| Custom indicator initialization function                       |
 //+------------------------------------------------------------------+
   int init()
     {
 //---- DataWindow and indicator subwindow label
      IndicatorShortName("PivotHigh_Line");
 //---- initialization done
      return(0);
     }
 //+------------------------------------------------------------------+
 //---- Custom indicator deinitialization function                    |
 //+------------------------------------------------------------------+
   int deinit()
       {
        ObjectDelete("LINE");
        ObjectDelete("LINE_label");
        
   return(0);
   }
 //+------------------------------------------------------------------+
   int start()
     {
 
 //-----------------------------------------------------------------------+
 //---- Day market session open between MarketOpenTime and MarketCloseTime|
 //-----------------------------------------------------------------------+
     
 //---- 2 situations for calculating pivot points, so 2 DIFFERENT StartTimes and EndTimes
 //---- Pivot point always to be calulated on the previous completed day market session

 

 //-----------------------------------------------------------------------+
 //---- NOTES FROM FORUM POSTS FOR MY INFORMATION ONLY                    |
 //-----------------------------------------------------------------------+  
     
 //---- Note  StrToTime("23:00") will return today date at 23:00 which will always be in the future (or current H1 bar.)
 //---- However yesterday may not be a trading day (weekend, bank holidays)
 //---- iTime(Symbol(),PERIOD_D1,1) is the previous trading day.
 //---- datetime bod = iTime(Symbol(),PERIOD_D1,0); // Beginning of the day
 //---- bool ObjectCreate(string name, int type, int window, datetime time1, double price1, datetime time2=0,
 //----   double price2=0, datetime time3=0, double price3=0)
 //-----------------------------------------------------------------------------------------------------------------------
 //  Line 65-----------------------------------------------------------------------------------------------------------------------              
      if   (00:00<=Time[0]<=7:30) //-- Any time fromm "00:00" to MarketCloseTime:
                                   //-- Calculate pivot high from "PREVIOUS DAY" market open to market close interval.
      {                                 
      StartBar1  = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,1)+50*60)); 
      EndBar1    = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,1)+(7*60*60+30*60)));  
      HVal1      = High[iHighest(NULL, 0, MODE_HIGH, StartBar1 - EndBar1 + 1, EndBar1) ];
      HVal = HVal1;
      }
 //-----------------------------------------------------------------------------------------------------------------------      
 //--Line 75                            
      if   (7:30<Time[0]<=23:59)  //-- Any Time after MarketCloseTime until "23:59":
                                   //-- Calculate pivot high from TODAY's market open to market close interval.
       {                                 
      StartBar2  = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,0)+50*60)); 
      EndBar2    = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,0)+7*60*60+30*60));
      HVal2      = High[iHighest(NULL, 0, MODE_HIGH, StartBar2 - EndBar2 + 1, EndBar2) ]; 
      HVal = HVal2; 
      }
      
      if (LINE) // draw if true
      ObjectCreate("LINE", OBJ_TREND, 0, (Time[0]+500),HVal, (Time[0]+1500),HVal);
      ObjectSet("LINE", OBJPROP_RAY, false);
      ObjectSet("LINE", OBJPROP_TIME1, (Time[0]+500));
      ObjectSet("LINE", OBJPROP_TIME2, (Time[0]+1500));
      ObjectSet("LINE", OBJPROP_COLOR, Lime);
      ObjectSet("LINE",OBJPROP_WIDTH,3);
       
   
 //+------------------------------------------------------------------+
      return(0);
     }
 //+------------------------------------------------------------------+
 
pullend:


(You are right I need to do more work understanding these basics)

When I change my market open and close times (manually in lines 66 and 76 of the code, the object is not redrawing based on the other HVal value

You can't do this . . . .  well you can but it most probably isn't doing what you want it to do . . .

if   (00:00<=Time[0]<=7:30) 

 Read this:  https://www.mql5.com/en/forum/141790  and my post just above it.

 
RaptorUK:

You can't do this . . . .  well you can but it most probably isn't doing what you want it to do . . .

 Read this:  https://www.mql5.com/en/forum/141790  and my post just above it.

 


Solved thank you!!

Iin my attempt to dumb things down while I tried to grapple with the various foms of time, I had not realised I had made my if time condition the problem.

Working code is below, might be a little clumsy, but now can systematically add my other pivot line calculations and labels

Thanks again !! Raptor and WHRoeder, I will study those posts and links further

with interest, cheers Dave

 //+------------------------------------------------------------------+
 //|                                                                  |
 //|                 Copyright © 1999-2008, MetaQuotes Software Corp. |
 //|                                         http://www.metaquotes.ru |
 //+------------------------------------------------------------------+

 //---- indicator settings
   #property  indicator_chart_window
   #property  indicator_buffers 0
 //----
  
 //---- indicator parameters
   
   extern bool     LINE = true;               // set true to display MA's
   extern int      MarketOpenHour  =0;
   extern int      MarketOpenMin   =50;
   extern int      MarketCloseHour =7; 
   extern int      MarketCloseMin  =30; 
   int             StartBar1,StartBar2, EndBar1,EndBar2;
   double          HVal1,HVal2,HVal;
   
 //+-------------------------------------------------------------------------------------------------------+
   //| Custom indicator initialization function                                                            |
 //+-------------------------------------------------------------------------------------------------------+
   int init()
     {
 //---- DataWindow and indicator subwindow label
      IndicatorShortName("PivotHigh_Line");
 //---- initialization done
      return(0);
     }
 //+-------------------------------------------------------------------------------------------------------+
 //---- Custom indicator deinitialization function                                                         |
 //+-------------------------------------------------------------------------------------------------------+
   int deinit()
       {
        ObjectDelete("LINE");
        ObjectDelete("LINE_label");
        
   return(0);
   }
 //+-------------------------------------------------------------------------------------------------------+
   int start()
     {
 //--------------------------------------------------------------------------------------------------------+
 //----Calculate the pivot point levels based on the last "COMPLETED" Day Market Session, not entire 24Hrs | 
 //----Day market session open between "MarketOpenHour:MarketOpenMin" and "MarketCloseHour:MarketCloseMin" |
 //----                                                                                                    |
 //----2 Situations to be calculated for each 24 Hour Cycle as defined below:                              |
 //----                                                                                                    |
 //--------------------------------------------------------------------------------------------------------+
     
 //-- Situation1: Any time fromm Start of Day to MarketCloseTime:
 //-------------- Calculate pivot high from "PREVIOUS DAY" market open to market close interval.
 
      if(  (Time[0]>=(iTime(Symbol(),PERIOD_D1,0)))
        && (Time[0]<=(iTime(Symbol(),PERIOD_D1,0)+MarketCloseHour*60*60+MarketCloseMin*60))
        )
        
     {                                 
      StartBar1  = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,1)+MarketOpenHour*60*60+MarketOpenMin*60)); 
      EndBar1    = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,1)+(MarketCloseHour*60*60+MarketCloseMin*60)));  
      HVal1      = High[iHighest(NULL, 0, MODE_HIGH, StartBar1 - EndBar1 + 1, EndBar1) ];
      HVal = HVal1;
     }
     
 //-- Situation2: Any Time after MarketCloseTime until "23:59":
 //-------------- Calculate pivot high from TODAY's market open to market close interval. 
                           
      if(  (Time[0]>(iTime(Symbol(),PERIOD_D1,0)+MarketCloseHour*60*60+MarketCloseMin*60))  
        && (Time[0]<=(iTime(Symbol(),PERIOD_D1,0)+23*60*60+59*60))
        )
          
     {                                 
      StartBar2  = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,0)+MarketOpenHour*60*60+MarketOpenMin*60)); 
      EndBar2    = iBarShift(NULL, 0, (iTime(Symbol(),PERIOD_D1,0)+MarketCloseHour*60*60+MarketCloseMin*60));
      HVal2      = High[iHighest(NULL, 0, MODE_HIGH, StartBar2 - EndBar2 + 1, EndBar2) ]; 
      HVal = HVal2; 
     }
      
 //-- Draw Pivot High Line
      if (LINE) // draw if true
      ObjectCreate("LINE", OBJ_TREND, 0, (Time[0]+500),HVal, (Time[0]+1500),HVal);
      ObjectSet("LINE", OBJPROP_RAY, false);
      ObjectSet("LINE", OBJPROP_TIME1, (Time[0]+500));
      ObjectSet("LINE", OBJPROP_TIME2, (Time[0]+1500));
      ObjectSet("LINE", OBJPROP_COLOR, Lime);
      ObjectSet("LINE",OBJPROP_WIDTH,3);
     
 //+-------------------------------------------------------------------------------------------------------+
      return(0);
     }
 //+-------------------------------------------------------------------------------------------------------+
 
pullend:

Solved thank you!!

Iin my attempt to dumb things down while I tried to grapple with the various foms of time, I had not realised I had made my if time condition the problem.

Working code is below, might be a little clumsy, but now can systematically add my other pivot line calculations and labels

Thanks again !! Raptor and WHRoeder, I will study those posts and links further

with interest, cheers Dave

You are welcome,  thanks for sticking with it and learning along the way,  it is undoubtedly the best and most rewarding way.  :-) 
Reason: