-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathDateRules.cs
More file actions
731 lines (649 loc) · 41.7 KB
/
Copy pathDateRules.cs
File metadata and controls
731 lines (649 loc) · 41.7 KB
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
/*
* 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 NodaTime;
using System.Linq;
using System.Globalization;
using QuantConnect.Interfaces;
using QuantConnect.Securities;
using System.Collections.Generic;
namespace QuantConnect.Scheduling
{
/// <summary>
/// Helper class used to provide better syntax when defining date rules
/// </summary>
public class DateRules : BaseScheduleRules
{
/// <summary>
/// Initializes a new instance of the <see cref="DateRules"/> helper class
/// </summary>
/// <param name="algorithm">The algorithm instance</param>
/// <param name="securities">The security manager</param>
/// <param name="timeZone">The algorithm's default time zone</param>
/// <param name="marketHoursDatabase">The market hours database instance to use</param>
public DateRules(IAlgorithm algorithm, SecurityManager securities, DateTimeZone timeZone, MarketHoursDatabase marketHoursDatabase)
: base(algorithm, securities, timeZone, marketHoursDatabase)
{
}
/// <summary>
/// Sets the default time zone
/// </summary>
/// <param name="timeZone">The time zone to use for helper methods that can't resolve a time zone</param>
public void SetDefaultTimeZone(DateTimeZone timeZone)
{
TimeZone = timeZone;
}
/// <summary>
/// Specifies an event should fire only on the specified day
/// </summary>
/// <param name="year">The year</param>
/// <param name="month">The month</param>
/// <param name="day">The day</param>
/// <returns></returns>
public IDateRule On(int year, int month, int day)
{
// make sure they're date objects
var dates = new[] {new DateTime(year, month, day)};
return new FuncDateRule(string.Join(",", dates.Select(x => x.ToShortDateString())), (start, end) => dates.Where(x => x >= start && x <= end));
}
/// <summary>
/// Specifies an event should fire only on the specified days
/// </summary>
/// <param name="dates">The dates the event should fire</param>
public IDateRule On(params DateTime[] dates)
{
// make sure they're date objects
dates = dates.Select(x => x.Date).ToArray();
return new FuncDateRule(string.Join(",", dates.Select(x => x.ToShortDateString())), (start, end) => dates.Where(x => x >= start && x <= end));
}
/// <summary>
/// Specifies an event should only fire today in the algorithm's time zone
/// using _securities.UtcTime instead of 'start' since ScheduleManager backs it up a day
/// </summary>
public IDateRule Today => new FuncDateRule("TodayOnly",
(start, e) => {
return new[] { Securities.UtcTime.ConvertFromUtc(TimeZone).Date };
}
);
/// <summary>
/// Specifies an event should only fire tomorrow in the algorithm's time zone
/// using _securities.UtcTime instead of 'start' since ScheduleManager backs it up a day
/// </summary>
public IDateRule Tomorrow => new FuncDateRule("TomorrowOnly",
(start, e) => new[] {Securities.UtcTime.ConvertFromUtc(TimeZone).Date.AddDays(1)}
);
/// <summary>
/// Specifies an event should fire on each of the specified days of week
/// </summary>
/// <param name="day">The day the event should fire</param>
/// <returns>A date rule that fires on every specified day of week</returns>
public IDateRule Every(DayOfWeek day) => Every(new[] { day });
/// <summary>
/// Specifies an event should fire on each of the specified days of week
/// </summary>
/// <param name="days">The days the event should fire</param>
/// <returns>A date rule that fires on every specified day of week</returns>
public IDateRule Every(params DayOfWeek[] days)
{
var hash = days.ToHashSet();
return new FuncDateRule(string.Join(",", days), (start, end) => Time.EachDay(start, end).Where(date => hash.Contains(date.DayOfWeek)));
}
/// <summary>
/// Specifies an event should fire every day
/// </summary>
/// <returns>A date rule that fires every day</returns>
public IDateRule EveryDay()
{
return new FuncDateRule("EveryDay", Time.EachDay);
}
/// <summary>
/// Specifies an event should fire every day the symbol is trading
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine tradable dates</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires every day the specified symbol trades</returns>
public IDateRule EveryDay(string symbol, bool extendedMarketHours = false) => EveryDay(GetSymbol(symbol), extendedMarketHours);
/// <summary>
/// Specifies an event should fire every day the symbol is trading
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine tradable dates</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires every day the specified symbol trades</returns>
public IDateRule EveryDay(Symbol symbol, bool extendedMarketHours = false)
{
var securitySchedule = GetSecurityExchangeHours(symbol);
return new FuncDateRule($"{symbol.Value}: EveryDay", (start, end) => Time.EachTradeableDay(securitySchedule, start, end, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on the first of each year + offset
/// </summary>
/// <param name="daysOffset"> The amount of days to offset the schedule by; must be between 0 and 365.</param>
/// <returns>A date rule that fires on the first of each year + offset</returns>
public IDateRule YearStart(int daysOffset = 0)
{
return YearStart((Symbol)null, daysOffset, false);
}
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified symbol of each year
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first tradable date of the year</param>
/// <param name="daysOffset"> The amount of tradable days to offset the schedule by; must be between 0 and 365</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the first tradable date + offset for the
/// specified security each year</returns>
public IDateRule YearStart(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => YearStart(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified symbol of each year
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first tradable date of the year</param>
/// <param name="daysOffset"> The amount of tradable days to offset the schedule by; must be between 0 and 365</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the first tradable date + offset for the
/// specified security each year</returns>
public IDateRule YearStart(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
// Check that our offset is allowed
if (daysOffset < 0 || 365 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.YearStart() : Offset must be between 0 and 365");
}
SecurityExchangeHours securityExchangeHours = null;
if (symbol != null)
{
securityExchangeHours = GetSecurityExchangeHours(symbol);
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "YearStart", daysOffset), (start, end) => YearIterator(securityExchangeHours, start, end, daysOffset, true, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on the last of each year
/// </summary>
/// <param name="daysOffset"> The amount of days to offset the schedule by; must be between 0 and 365</param>
/// <returns>A date rule that fires on the last of each year - offset</returns>
public IDateRule YearEnd(int daysOffset = 0)
{
return YearEnd((Symbol)null, daysOffset, false);
}
/// <summary>
/// Specifies an event should fire on the last tradable date - offset for the specified symbol of each year
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last tradable date of the year</param>
/// <param name="daysOffset">The amount of tradable days to offset the schedule by; must be between 0 and 365.</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the last tradable date - offset for the specified security each year</returns>
public IDateRule YearEnd(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => YearEnd(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the last tradable date - offset for the specified symbol of each year
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last tradable date of the year</param>
/// <param name="daysOffset">The amount of tradable days to offset the schedule by; must be between 0 and 365.</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the last tradable date - offset for the specified security each year</returns>
public IDateRule YearEnd(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
// Check that our offset is allowed
if (daysOffset < 0 || 365 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.YearEnd() : Offset must be between 0 and 365");
}
SecurityExchangeHours securityExchangeHours = null;
if (symbol != null)
{
securityExchangeHours = GetSecurityExchangeHours(symbol);
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "YearEnd", -daysOffset), (start, end) => YearIterator(securityExchangeHours, start, end, daysOffset, false, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on the first of each quarter + offset
/// </summary>
/// <param name="daysOffset"> The amount of days to offset the schedule by; must be between 0 and 92.</param>
/// <returns>A date rule that fires on the first of each quarter + offset</returns>
public IDateRule QuarterStart(int daysOffset = 0)
{
return QuarterStart((Symbol)null, daysOffset, false);
}
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified symbol of each quarter
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first tradable date of the quarter</param>
/// <param name="daysOffset"> The amount of tradable days to offset the schedule by; must be between 0 and 92</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the first tradable date + offset for the
/// specified security each quarter</returns>
public IDateRule QuarterStart(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => QuarterStart(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified symbol of each quarter
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first tradable date of the quarter</param>
/// <param name="daysOffset"> The amount of tradable days to offset the schedule by; must be between 0 and 92</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the first tradable date + offset for the
/// specified security each quarter</returns>
public IDateRule QuarterStart(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
// Check that our offset is allowed
if (daysOffset < 0 || 92 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.QuarterStart() : Offset must be between 0 and 92");
}
SecurityExchangeHours securityExchangeHours = null;
if (symbol != null)
{
securityExchangeHours = GetSecurityExchangeHours(symbol);
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "QuarterStart", daysOffset), (start, end) => QuarterIterator(securityExchangeHours, start, end, daysOffset, true, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on the last of each quarter
/// </summary>
/// <param name="daysOffset"> The amount of days to offset the schedule by; must be between 0 and 92</param>
/// <returns>A date rule that fires on the last of each quarter - offset</returns>
public IDateRule QuarterEnd(int daysOffset = 0)
{
return QuarterEnd((Symbol)null, daysOffset, false);
}
/// <summary>
/// Specifies an event should fire on the last tradable date - offset for the specified symbol of each quarter
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last tradable date of the quarter</param>
/// <param name="daysOffset">The amount of tradable days to offset the schedule by; must be between 0 and 92.</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the last tradable date - offset for the specified security each quarter</returns>
public IDateRule QuarterEnd(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => QuarterEnd(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the last tradable date - offset for the specified symbol of each quarter
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last tradable date of the quarter</param>
/// <param name="daysOffset">The amount of tradable days to offset the schedule by; must be between 0 and 92.</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the last tradable date - offset for the specified security each quarter</returns>
public IDateRule QuarterEnd(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
// Check that our offset is allowed
if (daysOffset < 0 || 92 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.QuarterEnd() : Offset must be between 0 and 92");
}
SecurityExchangeHours securityExchangeHours = null;
if (symbol != null)
{
securityExchangeHours = GetSecurityExchangeHours(symbol);
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "QuarterEnd", -daysOffset), (start, end) => QuarterIterator(securityExchangeHours, start, end, daysOffset, false, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on the first of each month + offset
/// </summary>
/// <param name="daysOffset"> The amount of days to offset the schedule by; must be between 0 and 30.</param>
/// <returns>A date rule that fires on the first of each month + offset</returns>
public IDateRule MonthStart(int daysOffset = 0)
{
return new FuncDateRule(GetName(null, "MonthStart", daysOffset), (start, end) => MonthIterator(null, start, end, daysOffset, true, false));
}
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified symbol of each month
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first tradable date of the month</param>
/// <param name="daysOffset"> The amount of tradable days to offset the schedule by; must be between 0 and 30</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the first tradable date + offset for the
/// specified security each month</returns>
public IDateRule MonthStart(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => MonthStart(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified symbol of each month
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first tradable date of the month</param>
/// <param name="daysOffset"> The amount of tradable days to offset the schedule by; must be between 0 and 30</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the first tradable date + offset for the
/// specified security each month</returns>
public IDateRule MonthStart(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
// Check that our offset is allowed
if (daysOffset < 0 || 30 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.MonthStart() : Offset must be between 0 and 30");
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "MonthStart", daysOffset), (start, end) => MonthIterator(GetSecurityExchangeHours(symbol), start, end, daysOffset, true, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on the last of each month
/// </summary>
/// <param name="daysOffset"> The amount of days to offset the schedule by; must be between 0 and 30</param>
/// <returns>A date rule that fires on the last of each month - offset</returns>
public IDateRule MonthEnd(int daysOffset = 0)
{
return new FuncDateRule(GetName(null, "MonthEnd", -daysOffset), (start, end) => MonthIterator(null, start, end, daysOffset, false, false));
}
/// <summary>
/// Specifies an event should fire on the last tradable date - offset for the specified symbol of each month
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last tradable date of the month</param>
/// <param name="daysOffset">The amount of tradable days to offset the schedule by; must be between 0 and 30.</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the last tradable date - offset for the specified security each month</returns>
public IDateRule MonthEnd(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => MonthEnd(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the last tradable date - offset for the specified symbol of each month
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last tradable date of the month</param>
/// <param name="daysOffset">The amount of tradable days to offset the schedule by; must be between 0 and 30.</param>
/// <param name="extendedMarketHours">True to include days with extended market hours only, like sunday for futures</param>
/// <returns>A date rule that fires on the last tradable date - offset for the specified security each month</returns>
public IDateRule MonthEnd(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
// Check that our offset is allowed
if (daysOffset < 0 || 30 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.MonthEnd() : Offset must be between 0 and 30");
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "MonthEnd", -daysOffset), (start, end) => MonthIterator(GetSecurityExchangeHours(symbol), start, end, daysOffset, false, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on Monday + offset each week
/// </summary>
/// <param name="daysOffset">The amount of days to offset monday by; must be between 0 and 6</param>
/// <returns>A date rule that fires on Monday + offset each week</returns>
public IDateRule WeekStart(int daysOffset = 0)
{
// Check that our offset is allowed
if (daysOffset < 0 || 6 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.WeekStart() : Offset must be between 0 and 6");
}
return new FuncDateRule(GetName(null, "WeekStart", daysOffset), (start, end) => WeekIterator(null, start, end, daysOffset, true, false));
}
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified
/// symbol each week
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first
/// tradeable date of the week</param>
/// <param name="daysOffset">The amount of tradable days to offset the first tradable day by</param>
/// <param name="extendedMarketHours">True to include extended market hours, false otherwise</param>
/// <returns>A date rule that fires on the first + offset tradable date for the specified
/// security each week</returns>
public IDateRule WeekStart(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => WeekStart(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the first tradable date + offset for the specified
/// symbol each week
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the first
/// tradeable date of the week</param>
/// <param name="daysOffset">The amount of tradable days to offset the first tradable day by</param>
/// <param name="extendedMarketHours">True to include extended market hours, false otherwise</param>
/// <returns>A date rule that fires on the first + offset tradable date for the specified
/// security each week</returns>
public IDateRule WeekStart(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
var securitySchedule = GetSecurityExchangeHours(symbol);
var tradingDays = securitySchedule.MarketHours.Values
.Where(x => x.IsClosedAllDay == false).OrderBy(x => x.DayOfWeek).ToList();
// Limit offsets to securities weekly schedule
if (daysOffset > tradingDays.Count - 1)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset),
$"DateRules.WeekStart() : {tradingDays.First().DayOfWeek}+{daysOffset} is out of range for {symbol}'s schedule," +
$" please use an offset between 0 - {tradingDays.Count - 1}; Schedule : {string.Join(", ", tradingDays.Select(x => x.DayOfWeek))}");
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "WeekStart", daysOffset), (start, end) => WeekIterator(securitySchedule, start, end, daysOffset, true, extendedMarketHours));
}
/// <summary>
/// Specifies an event should fire on Friday - offset
/// </summary>
/// <param name="daysOffset"> The amount of days to offset Friday by; must be between 0 and 6 </param>
/// <returns>A date rule that fires on Friday each week</returns>
public IDateRule WeekEnd(int daysOffset = 0)
{
// Check that our offset is allowed
if (daysOffset < 0 || 6 < daysOffset)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset), "DateRules.WeekEnd() : Offset must be between 0 and 6");
}
return new FuncDateRule(GetName(null, "WeekEnd", -daysOffset), (start, end) => WeekIterator(null, start, end, daysOffset, false, false));
}
/// <summary>
/// Specifies an event should fire on the last - offset tradable date for the specified
/// symbol of each week
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last
/// tradable date of the week</param>
/// <param name="daysOffset"> The amount of tradable days to offset the last tradable day by each week</param>
/// <param name="extendedMarketHours">True to include extended market hours, false otherwise</param>
/// <returns>A date rule that fires on the last - offset tradable date for the specified security each week</returns>
public IDateRule WeekEnd(string symbol, int daysOffset = 0, bool extendedMarketHours = true) => WeekEnd(GetSymbol(symbol), daysOffset, extendedMarketHours);
/// <summary>
/// Specifies an event should fire on the last - offset tradable date for the specified
/// symbol of each week
/// </summary>
/// <param name="symbol">The symbol whose exchange is used to determine the last
/// tradable date of the week</param>
/// <param name="daysOffset"> The amount of tradable days to offset the last tradable day by each week</param>
/// <param name="extendedMarketHours">True to include extended market hours, false otherwise</param>
/// <returns>A date rule that fires on the last - offset tradable date for the specified security each week</returns>
public IDateRule WeekEnd(Symbol symbol, int daysOffset = 0, bool extendedMarketHours = true)
{
var securitySchedule = GetSecurityExchangeHours(symbol);
var tradingDays = securitySchedule.MarketHours.Values
.Where(x => x.IsClosedAllDay == false).OrderBy(x => x.DayOfWeek).ToList();
// Limit offsets to securities weekly schedule
if (daysOffset > tradingDays.Count - 1)
{
throw new ArgumentOutOfRangeException(nameof(daysOffset),
$"DateRules.WeekEnd() : {tradingDays.Last().DayOfWeek}-{daysOffset} is out of range for {symbol}'s schedule," +
$" please use an offset between 0 - {tradingDays.Count - 1}; Schedule : {string.Join(", ", tradingDays.Select(x => x.DayOfWeek))}");
}
// Create the new DateRule and return it
return new FuncDateRule(GetName(symbol, "WeekEnd", -daysOffset), (start, end) => WeekIterator(securitySchedule, start, end, daysOffset, false, extendedMarketHours));
}
/// <summary>
/// Determine the string representation for a given rule
/// </summary>
/// <param name="symbol">Symbol for the rule</param>
/// <param name="ruleType">Rule type in string form</param>
/// <param name="offset">The amount of offset on this rule</param>
/// <returns></returns>
private static string GetName(Symbol symbol, string ruleType, int offset)
{
// Convert our offset to +#, -#, or empty string if 0
var offsetString = offset.ToString("+#;-#;''", CultureInfo.InvariantCulture);
var name = symbol == null ? $"{ruleType}{offsetString}" : $"{symbol.Value}: {ruleType}{offsetString}";
return name;
}
/// <summary>
/// Get the closest trading day to a given DateTime for a given <see cref="SecurityExchangeHours"/>.
/// </summary>
/// <param name="securityExchangeHours"><see cref="SecurityExchangeHours"/> object with schedule for this Security</param>
/// <param name="baseDay">The day to base our search from</param>
/// <param name="offset">Amount to offset the schedule by tradable days</param>
/// <param name="searchForward">Search into the future for the closest day if true; into the past if false</param>
/// <param name="boundary">The boundary DateTime on the resulting day</param>
/// <param name="extendedMarketHours">True to include extended market hours, false otherwise</param>
private static DateTime GetScheduledDay(SecurityExchangeHours securityExchangeHours, DateTime baseDay, int offset, bool searchForward, bool extendedMarketHours, DateTime? boundary = null)
{
// By default the scheduled date is the given day
var scheduledDate = baseDay;
// If its not open on this day find the next trading day by searching in the given direction
if (!securityExchangeHours.IsDateOpen(scheduledDate, extendedMarketHours))
{
scheduledDate = searchForward
? securityExchangeHours.GetNextTradingDay(scheduledDate)
: securityExchangeHours.GetPreviousTradingDay(scheduledDate);
}
// Offset the scheduled day accordingly
for (var i = 0; i < offset; i++)
{
scheduledDate = searchForward
? securityExchangeHours.GetNextTradingDay(scheduledDate)
: securityExchangeHours.GetPreviousTradingDay(scheduledDate);
}
// If there is a boundary ensure we enforce it
if (boundary.HasValue)
{
// If we are searching forward and the resulting date is after this boundary we
// revert to the last tradable day equal to or less than boundary
if (searchForward && scheduledDate > boundary)
{
scheduledDate = GetScheduledDay(securityExchangeHours, (DateTime)boundary, 0, false, extendedMarketHours);
}
// If we are searching backward and the resulting date is after this boundary we
// revert to the last tradable day equal to or greater than boundary
if (!searchForward && scheduledDate < boundary)
{
scheduledDate = GetScheduledDay(securityExchangeHours, (DateTime)boundary, 0, true, extendedMarketHours);
}
}
return scheduledDate;
}
private static IEnumerable<DateTime> BaseIterator(
SecurityExchangeHours securitySchedule,
DateTime start,
DateTime end,
int offset,
bool searchForward,
DateTime periodBegin,
DateTime periodEnd,
Func<DateTime, DateTime> baseDateFunc,
Func<DateTime, DateTime> boundaryDateFunc,
bool extendedMarketHours)
{
// No schedule means no security, set to open everyday
if (securitySchedule == null)
{
securitySchedule = SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork);
}
foreach (var date in Time.EachDay(periodBegin, periodEnd))
{
var baseDate = baseDateFunc(date);
var boundaryDate = boundaryDateFunc(date);
// Determine the scheduled day for this period
if (date == baseDate)
{
var scheduledDay = GetScheduledDay(securitySchedule, baseDate, offset, searchForward, extendedMarketHours, boundaryDate);
// Ensure the date is within our schedules range
if (scheduledDay >= start && scheduledDay <= end)
{
yield return scheduledDay;
}
}
}
}
private static IEnumerable<DateTime> MonthIterator(SecurityExchangeHours securitySchedule, DateTime start, DateTime end, int offset, bool searchForward, bool extendedMarketHours)
{
// Iterate all days between the beginning of "start" month, through end of "end" month.
// Necessary to ensure we schedule events in the month we start and end.
var beginningOfStartMonth = new DateTime(start.Year, start.Month, 1);
var endOfEndMonth = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month));
// Searching forward the first of the month is baseDay, with boundary being the last
// Searching backward the last of the month is baseDay, with boundary being the first
Func<DateTime, DateTime> baseDateFunc = date => searchForward ? new DateTime(date.Year, date.Month, 1) : new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month));
Func<DateTime, DateTime> boundaryDateFunc = date => searchForward ? new DateTime(date.Year, date.Month, DateTime.DaysInMonth(date.Year, date.Month)) : new DateTime(date.Year, date.Month, 1);
return BaseIterator(securitySchedule, start, end, offset, searchForward, beginningOfStartMonth, endOfEndMonth, baseDateFunc, boundaryDateFunc, extendedMarketHours);
}
private static IEnumerable<DateTime> QuarterIterator(SecurityExchangeHours securitySchedule, DateTime start, DateTime end, int offset, bool searchForward, bool extendedMarketHours)
{
// Iterate all days between the beginning of "start" quarter, through end of "end" quarter.
// Necessary to ensure we schedule events in the quarter we start and end.
var startQuarterFirstMonth = ((start.Month - 1) / 3) * 3 + 1;
var beginningOfStartQuarter = new DateTime(start.Year, startQuarterFirstMonth, 1);
var endQuarterLastMonth = ((end.Month - 1) / 3) * 3 + 3;
var endOfEndQuarter = new DateTime(end.Year, endQuarterLastMonth, DateTime.DaysInMonth(end.Year, endQuarterLastMonth));
// Searching forward the first day of the quarter is baseDay, with boundary being the last day
// Searching backward the last day of the quarter is baseDay, with boundary being the first day
Func<DateTime, DateTime> baseDateFunc = date =>
{
var quarterFirstMonth = ((date.Month - 1) / 3) * 3 + 1;
if (searchForward)
{
return new DateTime(date.Year, quarterFirstMonth, 1);
}
var quarterLastMonth = quarterFirstMonth + 2;
return new DateTime(date.Year, quarterLastMonth, DateTime.DaysInMonth(date.Year, quarterLastMonth));
};
Func<DateTime, DateTime> boundaryDateFunc = date =>
{
var quarterFirstMonth = ((date.Month - 1) / 3) * 3 + 1;
if (searchForward)
{
var quarterLastMonth = quarterFirstMonth + 2;
return new DateTime(date.Year, quarterLastMonth, DateTime.DaysInMonth(date.Year, quarterLastMonth));
}
return new DateTime(date.Year, quarterFirstMonth, 1);
};
return BaseIterator(securitySchedule, start, end, offset, searchForward, beginningOfStartQuarter, endOfEndQuarter, baseDateFunc, boundaryDateFunc, extendedMarketHours);
}
private static IEnumerable<DateTime> YearIterator(SecurityExchangeHours securitySchedule, DateTime start, DateTime end, int offset, bool searchForward, bool extendedMarketHours)
{
// Iterate all days between the beginning of "start" year, through end of "end" year
// Necessary to ensure we schedule events in the year we start and end.
var beginningOfStartOfYear = new DateTime(start.Year, start.Month, 1);
var endOfEndYear = new DateTime(end.Year, end.Month, DateTime.DaysInMonth(end.Year, end.Month));
// Searching forward the first of the year is baseDay, with boundary being the last
// Searching backward the last of the year is baseDay, with boundary being the first
Func<DateTime, DateTime> baseDateFunc = date => searchForward ? new DateTime(date.Year, 1, 1) : new DateTime(date.Year, 12, 31);
Func<DateTime, DateTime> boundaryDateFunc = date => searchForward ? new DateTime(date.Year, 12, 31) : new DateTime(date.Year, 1, 1);
return BaseIterator(securitySchedule, start, end, offset, searchForward, beginningOfStartOfYear, endOfEndYear, baseDateFunc, boundaryDateFunc, extendedMarketHours);
}
private static IEnumerable<DateTime> WeekIterator(SecurityExchangeHours securitySchedule, DateTime start, DateTime end, int offset, bool searchForward, bool extendedMarketHours)
{
// Determine the weekly base day and boundary to schedule off of
DayOfWeek weeklyBaseDay;
DayOfWeek weeklyBoundaryDay;
if (securitySchedule == null)
{
// No schedule means no security, set to open everyday
securitySchedule = SecurityExchangeHours.AlwaysOpen(TimeZones.NewYork);
// Searching forward Monday is baseDay, with boundary being the following Sunday
// Searching backward Friday is baseDay, with boundary being the previous Saturday
weeklyBaseDay = searchForward ? DayOfWeek.Monday : DayOfWeek.Friday;
weeklyBoundaryDay = searchForward ? DayOfWeek.Saturday + 1 : DayOfWeek.Sunday - 1;
}
else
{
// Fetch the securities schedule
var weeklySchedule = securitySchedule.MarketHours.Values
.Where(x => x.IsClosedAllDay == false).OrderBy(x => x.DayOfWeek).ToList();
// Determine our weekly base day and boundary for this security
weeklyBaseDay = searchForward ? weeklySchedule.First().DayOfWeek : weeklySchedule.Last().DayOfWeek;
weeklyBoundaryDay = searchForward ? weeklySchedule.Last().DayOfWeek : weeklySchedule.First().DayOfWeek;
}
// Iterate all days between the beginning of "start" week, through end of "end" week.
// Necessary to ensure we schedule events in the week we start and end.
// Also if we have a sunday for start/end we need to adjust for it being the front of the week when we want it as the end of the week.
var startAdjustment = start.DayOfWeek == DayOfWeek.Sunday ? -7 : 0;
var beginningOfStartWeek = start.AddDays(-(int)start.DayOfWeek + 1 + startAdjustment); // Date - DayOfWeek + 1
var endAdjustment = end.DayOfWeek == DayOfWeek.Sunday ? -7 : 0;
var endOfEndWeek = end.AddDays(-(int)end.DayOfWeek + 7 + endAdjustment); // Date - DayOfWeek + 7
// Determine the schedule for each week in this range
foreach (var date in Time.EachDay(beginningOfStartWeek, endOfEndWeek).Where(x => x.DayOfWeek == weeklyBaseDay))
{
var boundary = date.AddDays(weeklyBoundaryDay - weeklyBaseDay);
var scheduledDay = GetScheduledDay(securitySchedule, date, offset, searchForward, extendedMarketHours, boundary);
// Ensure the date is within our schedules range
if (scheduledDay >= start && scheduledDay <= end)
{
yield return scheduledDay;
}
}
}
}
}