forked from QuantConnect/Lean
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTime.cs
266 lines (243 loc) · 9.68 KB
/
Time.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* QUANTCONNECT.COM - Democratizing Finance, Empowering Individuals.
* Lean Algorithmic Trading Engine v2.0. Copyright 2014 QuantConnect Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using QuantConnect.Logging;
using QuantConnect.Securities;
namespace QuantConnect
{
/// <summary>
/// Time helper class collection for working with trading dates
/// </summary>
public static class Time
{
/// <summary>
/// One Day TimeSpan Period Constant
/// </summary>
public static readonly TimeSpan OneDay = TimeSpan.FromDays(1);
/// <summary>
/// One Hour TimeSpan Period Constant
/// </summary>
public static readonly TimeSpan OneHour = TimeSpan.FromHours(1);
/// <summary>
/// One Minute TimeSpan Period Constant
/// </summary>
public static readonly TimeSpan OneMinute = TimeSpan.FromMinutes(1);
/// <summary>
/// One Second TimeSpan Period Constant
/// </summary>
public static readonly TimeSpan OneSecond = TimeSpan.FromSeconds(1);
/// <summary>
/// Live charting is sensitive to timezone so need to convert the local system time to a UTC and display in browser as UTC.
/// </summary>
public struct DateTimeWithZone
{
private readonly DateTime utcDateTime;
private readonly TimeZoneInfo timeZone;
/// <summary>
/// Initializes a new instance of the <see cref="QuantConnect.Time+DateTimeWithZone"/> struct.
/// </summary>
/// <param name="dateTime">Date time.</param>
/// <param name="timeZone">Time zone.</param>
public DateTimeWithZone(DateTime dateTime, TimeZoneInfo timeZone)
{
utcDateTime = TimeZoneInfo.ConvertTimeToUtc(dateTime, timeZone);
this.timeZone = timeZone;
}
/// <summary>
/// Gets the universal time.
/// </summary>
/// <value>The universal time.</value>
public DateTime UniversalTime { get { return utcDateTime; } }
/// <summary>
/// Gets the time zone.
/// </summary>
/// <value>The time zone.</value>
public TimeZoneInfo TimeZone { get { return timeZone; } }
/// <summary>
/// Gets the local time.
/// </summary>
/// <value>The local time.</value>
public DateTime LocalTime
{
get
{
return TimeZoneInfo.ConvertTime(utcDateTime, timeZone);
}
}
}
/// <summary>
/// Create a C# DateTime from a UnixTimestamp
/// </summary>
/// <param name="unixTimeStamp">Double unix timestamp (Time since Midnight Jan 1 1970)</param>
/// <returns>C# date timeobject</returns>
public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
{
var time = DateTime.Now;
try
{
// Unix timestamp is seconds past epoch
time = new DateTime(1970, 1, 1, 0, 0, 0, 0);
time = time.AddSeconds(unixTimeStamp);
}
catch (Exception err)
{
Log.Error("Time.UnixTimeStampToDateTime(): " + unixTimeStamp + err.Message);
}
return time;
}
/// <summary>
/// Convert a Datetime to Unix Timestamp
/// </summary>
/// <param name="time">C# datetime object</param>
/// <returns>Double unix timestamp</returns>
public static double DateTimeToUnixTimeStamp(DateTime time)
{
double timestamp = 0;
try
{
timestamp = (time - new DateTime(1970, 1, 1, 0, 0, 0, 0)).TotalSeconds;
}
catch (Exception err)
{
Log.Error("Time.DateTimeToUnixTimeStamp(): " + time.ToOADate() + err.Message);
}
return timestamp;
}
/// <summary>
/// Get the current time as a unix timestamp
/// </summary>
/// <returns>Double value of the unix as UTC timestamp</returns>
public static double TimeStamp()
{
return DateTimeToUnixTimeStamp(DateTime.UtcNow);
}
/// <summary>
/// Parse a standard YY MM DD date into a DateTime. Attempt common date formats
/// </summary>
/// <param name="dateToParse">String date time to parse</param>
/// <returns>Date time</returns>
public static DateTime ParseDate(string dateToParse)
{
try
{
//First try the exact options:
DateTime date;
if (DateTime.TryParseExact(dateToParse, DateFormat.SixCharacter, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
if (DateTime.TryParseExact(dateToParse, DateFormat.EightCharacter, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
if (DateTime.TryParseExact(dateToParse.Substring(0, 19), DateFormat.JsonFormat, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
if (DateTime.TryParseExact(dateToParse, DateFormat.US, CultureInfo.InvariantCulture, DateTimeStyles.None, out date))
{
return date;
}
if (DateTime.TryParse(dateToParse, out date))
{
return date;
}
}
catch (Exception err)
{
Log.Error("Time.ParseDate(): " + err.Message);
}
return DateTime.Now;
}
/// <summary>
/// Define an enumerable date range and return each date as a datetime object in the date range
/// </summary>
/// <param name="from">DateTime start date</param>
/// <param name="thru">DateTime end date</param>
/// <returns>Enumerable date range</returns>
public static IEnumerable<DateTime> EachDay(DateTime from, DateTime thru)
{
for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
yield return day;
}
/// <summary>
/// Define an enumerable date range of tradeable dates - skip the holidays and weekends when securities in this algorithm don't trade.
/// </summary>
/// <param name="securities">Securities we have in portfolio</param>
/// <param name="from">Start date</param>
/// <param name="thru">End date</param>
/// <returns>Enumerable date range</returns>
public static IEnumerable<DateTime> EachTradeableDay(SecurityManager securities, DateTime from, DateTime thru)
{
for (var day = from.Date; day.Date <= thru.Date; day = day.AddDays(1))
{
if (TradableDate(securities, day))
{
yield return day;
}
}
}
/// <summary>
/// Make sure this date is not a holiday, or weekend for the securities in this algorithm.
/// </summary>
/// <param name="securities">Security manager from the algorithm</param>
/// <param name="day">DateTime to check if trade-able.</param>
/// <returns>True if tradeable date</returns>
public static bool TradableDate(SecurityManager securities, DateTime day)
{
try
{
foreach (var security in securities.Values)
{
if (security.Exchange.IsOpenDuringBar(day.Date, day.Date.AddDays(1), security.IsExtendedMarketHours)) return true;
}
}
catch (Exception err)
{
Log.Error("Time.TradeableDate(): " + err.Message);
}
return false;
}
/// <summary>
/// Could of the number of tradeable dates within this period.
/// </summary>
/// <param name="securities">Securities we're trading</param>
/// <param name="start">Start of Date Loop</param>
/// <param name="finish">End of Date Loop</param>
/// <returns>Number of dates</returns>
public static int TradeableDates(SecurityManager securities, DateTime start, DateTime finish)
{
var count = 0;
Log.Trace("Time.TradeableDates(): Security Count: " + securities.Count);
try
{
foreach (var day in Time.EachDay(start, finish))
{
if (Time.TradableDate(securities, day))
{
count++;
}
}
}
catch (Exception err)
{
Log.Error("Time.TradeableDates(): " + err.Message);
}
return count;
}
}
}