Time comparison

 

Hello,

 I am trying for a couple of days already to find a way comparing a given time with the time of a specific bar.

The idea is that my indicator should only print an arrow if the time of that bar is within the range StartTime and EndTime (no matter the day).

In the end all arrows  disappear when I compile this... 

input string                  StartTime                     = "06:00";
input string                  EndTime                       = "10:00";
&& (Time[current+1] > StrToTime(StartTime) && Time[current+1] < StrToTime(EndTime))

 Does anybody have a clue how I could fix this ?

 I would be very happy if someone could give me a hint of what I am doing wrong.

 

Kind regards

Stefan 

 

why don't you use

sinput datetime
 
Marco vd Heijden:

why don't you use

Maybe I dont understand you properly but if I use datetime will the indicator be able to draw the arrows on the given time span (lets say on a period of 500.000 historical bars) for the

period "StartTime"/"EndTime" ?

 

Thank you 

 

if you include the day, not only the hour, because how is it suppose to know the day?

Datetime Type

The datetime type is intended for storing the date and time as the number of seconds elapsed since January 01, 1970. This type occupies 8 bytes of memory.

Constants of the date and time can be represented as a literal string, which consists of 6 parts showing the numerical value of the year, month, day (or day, month, year), hours, minutes and seconds. The constant is enclosed in single quotation marks and starts with the D character.

Values range from 1 January, 1970 to 31 December, 3000. Either date (year , month, day) or time (hours, minutes, seconds), or all together can be omitted.

With literal date specification, it is desirable that you specify year, month and day. Otherwise the compiler returns a warning about an incomplete entry.

Examples:

datetime NY=D'2015.01.01 00:00';     // Time of beginning of year 2015
datetime d1=D'1980.07.19 12:30:27';  // Year Month Day Hours Minutes Seconds
datetime d2=D'19.07.1980 12:30:27';  // Equal to D'1980.07.19 12:30:27';
datetime d3=D'19.07.1980 12';        // Equal to D'1980.07.19 12:00:00'
datetime d4=D'01.01.2004';           // Equal to D'01.01.2004 00:00:00'
datetime compilation_date=__DATE__;             // Compilation date
datetime compilation_date_time=__DATETIME__;    // Compilation date and time
datetime compilation_time=__DATETIME__-__DATE__;// Compilation time
//--- Examples of declarations after which compiler warnings will be returned
datetime warning1=D'12:30:27';       // Equal to D'[date of compilation] 12:30:27'
datetime warning2=D'';               // Equal to __DATETIME__

The string representation of datetime type depends on compilation mode:

  datetime date=D'2014.03.05 15:46:58';
  string str="mydate="+date;
//--- str="mydate=1394034418" - without #property strict
//--- str="mydate=2014.03.05 15:46:58" - with #property strict

See also

Structure of the Date Type, Date and Time, TimeToString, StringToTime

 
StrToTime("06:00") or StringToTime("06:00") would give the time for 6AM for the current day only. If you want to get its counterparts for the other days, you will need to deduct 86400 seconds (1 day) for each day from the current day.
 
Enrico Lambino:
StrToTime("06:00") or StringToTime("06:00") would give the time for 6AM for the current day only. If you want to get its counterparts for the other days, you will need to deduct 86400 seconds (1 day) for each day from the current day.
#property version   "1.00"
#property indicator_chart_window
#property indicator_buffers 2
#property strict


//--- input parameters
input string                  StartTime                     = "06:00";
input string                  EndTime                       = "10:00";
double                        Up[];
double                        Down[];

//+------------------------------------------------------------------+
//| Custom indicator initialization function                         |
//+------------------------------------------------------------------+
int OnInit()
{
SetIndexBuffer                         (0,Up);
SetIndexBuffer                         (1,Down);
SetIndexStyle                          (0,DRAW_ARROW,0,2,clrYellow);
SetIndexStyle                          (1,DRAW_ARROW,0,2,clrYellow);
SetIndexArrow                          (0,233);
SetIndexArrow                          (1,234);
return(0);
}

int Deinit()
{
return(0);
}

//+------------------------------------------------------------------+
//| Custom indicator iteration function                              |
//+------------------------------------------------------------------+
int start()
{
if(Bars<=3) return(0);
int ExtCountedBars=IndicatorCounted();
if (ExtCountedBars<0) return(-1);
int current=Bars-2;
if(ExtCountedBars>2) current=Bars-ExtCountedBars-1;
while(current>=0)
{
if
(
iClose(NULL, 0, current+2)-iLow(NULL, 0, current+2)>0
&& (Time[current+1] > StrToTime(StartTime) && Time[current+1] < StrToTime(EndTime))

)
{
Up[current+1]=Low[current+1]-10*Point;
}
else
{
Up[current+1]=EMPTY_VALUE;
}
if
(
iHigh(NULL, 0, current+2)-iClose(NULL, 0, current+2)>0
&& (Time[current+1] > StrToTime(StartTime) && Time[current+1] < StrToTime(EndTime))
)
{
Down[current+1]=High[current+1]+10*Point;
}
else
{
Down[current+1]=EMPTY_VALUE;
}
current--;
}
return(0);
}

I use this code to draw arrows on previous bar. The idea is that the arrow should just be placed if the broker time is inside range of given time. But no matter what I put in the code. It either writes the arrows on any time or writes no arrows at all. 

 
sjust2fast:

Hello,

 I am trying for a couple of days already to find a way comparing a given time with the time of a specific bar.

The idea is that my indicator should only print an arrow if the time of that bar is within the range StartTime and EndTime (no matter the day).

In the end all arrows  disappear when I compile this... 

 Does anybody have a clue how I could fix this ?

 I would be very happy if someone could give me a hint of what I am doing wrong.

 

Kind regards

Stefan 

sjust2fast:

I use this code to draw arrows on previous bar. The idea is that the arrow should just be placed if the broker time is inside range of given time. But no matter what I put in the code. It either writes the arrows on any time or writes no arrows at all. 

It may be better to use iBarShift

iBarShift - MQL4 Documentation
  • docs.mql4.com
iBarShift - MQL4 Documentation
Reason: