18 using System.Collections.Generic;
42 private Func<DateTime, bool> _predicate;
44 private readonly
string _name;
58 _securities = securities;
64 if (_timeRule ==
null)
72 var compositeTimeRule = _timeRule as CompositeTimeRule;
73 if (compositeTimeRule !=
null)
75 var rules = compositeTimeRule.Rules;
76 _timeRule =
new CompositeTimeRule(rules.Concat(
new[] { rule }));
81 _timeRule =
new CompositeTimeRule(_timeRule, rule);
85 #region DateRules and TimeRules delegation
90 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.Every(params DayOfWeek[] days)
92 _dateRule = _schedule.DateRules.Every(days);
99 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.EveryDay()
101 _dateRule = _schedule.DateRules.EveryDay();
108 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.EveryDay(Symbol symbol)
110 _dateRule = _schedule.DateRules.EveryDay(symbol);
117 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.MonthStart()
119 _dateRule = _schedule.DateRules.MonthStart();
126 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.MonthStart(Symbol symbol)
128 _dateRule = _schedule.DateRules.MonthStart(symbol);
135 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.Where(Func<DateTime, bool> predicate)
137 _predicate = _predicate ==
null
139 : (time => _predicate(time) && predicate(time));
146 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.At(TimeSpan timeOfDay)
148 return SetTimeRule(_schedule.TimeRules.At(timeOfDay));
154 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.AfterMarketOpen(Symbol symbol,
double minutesAfterOpen,
bool extendedMarketOpen)
156 return SetTimeRule(_schedule.TimeRules.AfterMarketOpen(symbol, minutesAfterOpen, extendedMarketOpen));
162 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.BeforeMarketClose(Symbol symbol,
double minuteBeforeClose,
bool extendedMarketClose)
164 return SetTimeRule(_schedule.TimeRules.BeforeMarketClose(symbol, minuteBeforeClose, extendedMarketClose));
170 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.Every(TimeSpan interval)
172 return SetTimeRule(_schedule.TimeRules.Every(interval));
178 IFluentSchedulingTimeSpecifier IFluentSchedulingTimeSpecifier.Where(Func<DateTime, bool> predicate)
180 _predicate = _predicate ==
null
182 : (time => _predicate(time) && predicate(time));
189 ScheduledEvent IFluentSchedulingRunnable.Run(Action callback)
191 return ((IFluentSchedulingRunnable)
this).Run((name, time) => callback());
197 ScheduledEvent IFluentSchedulingRunnable.Run(Action<DateTime> callback)
199 return ((IFluentSchedulingRunnable)
this).Run((name, time) => callback(time));
205 ScheduledEvent IFluentSchedulingRunnable.Run(Action<string, DateTime> callback)
207 var name = _name ?? _dateRule.
Name +
": " + _timeRule.
Name;
209 var dates = ScheduleManager.GetDatesDeferred(_dateRule, _securities);
211 if (_predicate !=
null)
213 eventTimes = eventTimes.Where(_predicate);
215 var scheduledEvent =
new ScheduledEvent(name, eventTimes, callback);
216 _schedule.Add(scheduledEvent);
217 return scheduledEvent;
223 IFluentSchedulingRunnable IFluentSchedulingRunnable.Where(Func<DateTime, bool> predicate)
225 _predicate = _predicate ==
null
227 : (time => _predicate(time) && predicate(time));
234 IFluentSchedulingRunnable IFluentSchedulingRunnable.DuringMarketHours(Symbol symbol,
bool extendedMarket)
236 var security = GetSecurity(symbol);
237 Func<DateTime, bool> predicate = time =>
239 var localTime = time.ConvertFromUtc(security.Exchange.TimeZone);
240 return security.Exchange.IsOpenDuringBar(localTime, localTime, extendedMarket);
242 _predicate = _predicate ==
null
244 : (time => _predicate(time) && predicate(time));
248 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.On(
int year,
int month,
int day)
250 _dateRule = _schedule.DateRules.On(year, month, day);
254 IFluentSchedulingTimeSpecifier IFluentSchedulingDateSpecifier.On(params DateTime[] dates)
256 _dateRule = _schedule.DateRules.On(dates);
260 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.At(
int hour,
int minute,
int second)
262 return SetTimeRule(_schedule.TimeRules.At(hour, minute, second));
265 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.At(
int hour,
int minute, DateTimeZone timeZone)
267 return SetTimeRule(_schedule.TimeRules.At(hour, minute, 0, timeZone));
270 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.At(
int hour,
int minute,
int second, DateTimeZone timeZone)
272 return SetTimeRule(_schedule.TimeRules.At(hour, minute, second, timeZone));
275 IFluentSchedulingRunnable IFluentSchedulingTimeSpecifier.At(TimeSpan timeOfDay, DateTimeZone timeZone)
277 return SetTimeRule(_schedule.TimeRules.At(timeOfDay, timeZone));
280 private Security GetSecurity(Symbol symbol)
283 if (!_securities.TryGetValue(symbol, out security))
285 throw new KeyNotFoundException($
"{symbol} not found in portfolio. Request this data when initializing the algorithm.");