Hi,
Trying to do some calculation with date and time. For example, how long ago a certain signal was created by an indicator. Or how long a certain S/R line has been active. After searching the forum and internet I came across the post of bowlesj3 http://www.multicharts.com/discussion/v ... 617#p50617.
He has made a function to calculate with date and time (many thanks), but you need to determine the date and time separately. Although there is a reserved word for getting the date and time together ("DateTime").
Is there a way to calculate how many minutes, seconds, hours, days has passed with DateTime?
And is there an easy way to only calculate the min, sec, hours etc. of the sessions that are on the chart. When calculating time, it will now also count time during weekends and before and after the trade sessions which are not on the chart.
Easy way of calculation with time [SOLVED]
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Easy way of calculation with time
Depending on your definition of "easy", working with times is not hard.Hi,
Trying to do some calculation with date and time. For example, how long ago a certain signal was created by an indicator. Or how long a certain S/R line has been active.
(...)
Is there a way to calculate how many minutes, seconds, hours, days has passed with DateTime?
Let's take it step by step:
1) There is a bar a while ago. Let's say this bar is 22 bars ago. Now get the DateTime value from this bar:
Code: Select all
ourBarTime = DateTime[22];
Code: Select all
currentBarTime = DateTime;
Code: Select all
currentBarTime = ComputerDateTime;
3) Now both times (current bar time and the time of the bar 22 bars ago) are in a format suitable for calculations (the DateTime format). So let's calculate the time interval between both:
Code: Select all
timeInterval = currentBarTime - ourBarTime;
Code: Select all
// Intermittent step, to make the operations clear
Print("ourBarTime: ", NumToStr(ourBarTime, 10));
Print("currentBarTime: ", NumToStr(currentBarTime, 10));
Print("timeInterval: ", NumToStr(timeInterval, 10));
Code: Select all
ourBarTime: 41883.0868055556
currentBarTime: 41883.5451388889
timeInterval: 0.4583333333
Code: Select all
oneMinuteDT = ELTimeToDateTime(1);
Code: Select all
minutesTimeInterval = timeInterval / oneMinuteDT;
Code: Select all
Print("ourBarTime: ",
FormatDate("d-M", ourBarTime),
FormatTime(" HH:mm:ss", ourBarTime));
Print("currentBarTime: ",
FormatDate("d-M", currentBarTime),
FormatTime(" HH:mm:ss", currentBarTime));
Print("Time difference (minutes): ",
NumToStr(minutesTimeInterval, 2));
Code: Select all
ourBarTime: 1-9 02:05:00
currentBarTime: 1-9 13:05:00
Time difference (minutes): 660.00
***************************
Full code:
Code: Select all
Variables:
ourBarTime(0),
currentBarTime(0),
timeInterval(0),
oneMinuteDT(0),
minutesTimeInterval(0);
once ClearDebug;
once (LastBarOnChart_s = true) begin
ourBarTime = DateTime[22];
currentBarTime = DateTime;
timeInterval = currentBarTime - ourBarTime;
// Intermittent step, to make the operations clear
Print("ourBarTime: ", NumToStr(ourBarTime, 10));
Print("currentBarTime: ", NumToStr(currentBarTime, 10));
Print("timeInterval: ", NumToStr(timeInterval, 10));
oneMinuteDT = ELTimeToDateTime(1);
minutesTimeInterval = timeInterval / oneMinuteDT;
// Checking
Print("ourBarTime: ",
FormatDate("d-M", ourBarTime),
FormatTime(" HH:mm:ss", ourBarTime));
Print("currentBarTime: ",
FormatDate("d-M", currentBarTime),
FormatTime(" HH:mm:ss", currentBarTime));
Print("Time difference (minutes): ",
NumToStr(minutesTimeInterval, 2));
end;
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Easy way of calculation with time
Evdl, see [Functions] Calculating time intervals easily for functions that do the process discussed above.
****************
What do you mean with a time that is not on the chart? A user-provided string with the time (like "13:33:48")?
****************
Oh nice ideas. Don't have time now, so will look at that later.Do you maybe also have a solution to exclude weekends and time that is not on the chart?
What do you mean with a time that is not on the chart? A user-provided string with the time (like "13:33:48")?
-
- Posts: 401
- Joined: 19 Jan 2011
- Location: Netherlands
- Has thanked: 85 times
- Been thanked: 125 times
Re: Easy way of calculation with time
No I mean, to only count minutes, hours of the sessions that are on the chart. As it is now with the time calculations it will take a begin time and end time. Tradinghours of the dax for example are from 8 - 22. The calculating will count 24 hours a day. I would like to only count the tradinghours. Weekend the same. If I have a start time on friday and an end time on monday. The calculation also include the time of the weekend. Weekends are not on the chart because exchanges are closed.
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Easy way of calculation with time
For only time-based charts or also for other charts (ticks, range, volume etc)?The calculating will count 24 hours a day. I would like to only count the tradinghours.
-
- Posts: 401
- Joined: 19 Jan 2011
- Location: Netherlands
- Has thanked: 85 times
- Been thanked: 125 times
Re: Easy way of calculation with time
Well, I only use tick and time charts. But I assume that time on other types of charts can be accessed in the same way.
What I am after is only count hours, minutes etc of the sessions on the chart and not weekends and out of trading hours. With the current calculation it will count everything between the start time and end time.
So the result of the calculation should be corrected with another calculation. That of all the time that is not in the sessions. For example an support and resistance line that starts today at 16.00 and ends tomorrow at 11.00. Will now give as result 16.00 - 24.00= today 8 hours and 0 - 11.00 tomorrow = 11 hours. In total 19 hours. But I would like to have the total time in the session of the DAX in this example. 16.00 - 22.00 and 8.00 - 11.00 is in total 9 hours.
What I am after is only count hours, minutes etc of the sessions on the chart and not weekends and out of trading hours. With the current calculation it will count everything between the start time and end time.
So the result of the calculation should be corrected with another calculation. That of all the time that is not in the sessions. For example an support and resistance line that starts today at 16.00 and ends tomorrow at 11.00. Will now give as result 16.00 - 24.00= today 8 hours and 0 - 11.00 tomorrow = 11 hours. In total 19 hours. But I would like to have the total time in the session of the DAX in this example. 16.00 - 22.00 and 8.00 - 11.00 is in total 9 hours.
- JoshM
- Posts: 2195
- Joined: 20 May 2011
- Location: The Netherlands
- Has thanked: 1544 times
- Been thanked: 1565 times
- Contact:
Re: Easy way of calculation with time [SOLVED]
One way to do that would be (as I understand it) the following:
1) Get the total DateTime difference between both bars.
2) Calculate the average DateTime difference for bars during the trading week.
3) Loop to all bars and use the value from (2) to determine that are not actual trading hours.
4) Then subtract the value from (3) from (1) to have the date time of the trading hours.
For the code example, the bar 55 bars ago has the time of 29-8 21:00. The current bar on which the script is evaluated is 2-9 20:30. The code says there are 1650 minutes 'market time', let's check:
29-8 21:00 till 22:00 --> 60 min
1-9 8:00 till 22:00 --> 840 min
2-9 8:00 till 20:30 --> 750 min
total amount of minutes: 1650
1) Get the total DateTime difference between both bars.
2) Calculate the average DateTime difference for bars during the trading week.
3) Loop to all bars and use the value from (2) to determine that are not actual trading hours.
4) Then subtract the value from (3) from (1) to have the date time of the trading hours.
Code: Select all
Variables:
ourBarTime(0),
currentBarTime(0),
timeInterval(0),
oneMinuteDT(ELTimeToDateTime(1)),
minutesTimeInterval(0),
avgDt(0),
totalDt(0),
aMonday(false),
dtDiff(0),
sessionStartTimeDT(0);
once ClearDebug;
once (LastBarOnChart_s = true) begin
value10 = 55; // Number of bars back
ourBarTime = DateTime[value10];
currentBarTime = DateTime;
timeInterval = currentBarTime - ourBarTime;
// Average DateTime difference per bar
avgDt = 0;
for value1 = value10 DownTo 0 begin
aMonday =
IFFLogic(DayOfWeek(JulianToDate(DateTime[value1])) = monday,
true, false);
dtDiff = DateTime[value1] - DateTime[value1 + 1];
// Calculate the average for all bars except the first bar on monday
if not(aMonday and (dtDiff > 1)) then
avgDt = avgDt + dtDiff;
end;
avgDt = avgDt / (value10 + 1);
Print(NumToStr(avgDt, 5),
Spaces(3),
"AvgDt: ", FormatTime("HH:mm:ss", avgDt));
// Loop through all bars; if the time difference
// is greater than the average, add up a sum (totalDt)
totalDt = 0;
for value1 = value10 DownTo 0 begin
dtDiff = DateTime[value1] - DateTime[value1 + 1];
if (dtDiff > (avgDt * 3)) then begin
sessionStartTimeDT =
ELTimeToDateTime(SessionStartTime(0, SessionCount(0)));
// Correct DateTime difference for bar closing time <> bar opening time
dtDiff = dtDiff - (FracPortion(DateTime[value1]) - sessionStartTimeDT);
totalDt = totalDt + dtDiff;
end;
end;
// Subtract the DateTime between closing and opening
// from the total amount of time
timeInterval = timeInterval - totalDt;
minutesTimeInterval = timeInterval / oneMinuteDT;
// Minutes without correction
value80 = (currentBarTime - ourBarTime) / oneMinuteDT;
// Checking
Print(NewLine, "ourBarTime: ",
FormatDate("d-M", ourBarTime),
FormatTime(" HH:mm:ss", ourBarTime));
Print("currentBarTime: ",
FormatDate("d-M", currentBarTime),
FormatTime(" HH:mm:ss", currentBarTime));
Print("Time difference (minutes): ",
NumToStr(minutesTimeInterval, 2));
Print("Time difference without correction: ",
NumToStr(value80, 2));
{
CORRECT - 2-9-14
Returns 1650 minutes for
ourBarTime: 29-8 21:00:00
currentBarTime: 2-9 20:30:00
(are 55 bars * 30 min)
}
end;
29-8 21:00 till 22:00 --> 60 min
1-9 8:00 till 22:00 --> 840 min
2-9 8:00 till 20:30 --> 750 min
total amount of minutes: 1650
-
- Posts: 401
- Joined: 19 Jan 2011
- Location: Netherlands
- Has thanked: 85 times
- Been thanked: 125 times
Re: Easy way of calculation with time
Thank you JoshM!
Nice work. With the code in front of me, it is still a puzzle to me. Have to check how you did this
In the meantime, I can incorporate this in my strategy. Perfect.
Nice work. With the code in front of me, it is still a puzzle to me. Have to check how you did this
In the meantime, I can incorporate this in my strategy. Perfect.