本文整理汇总了C#中IAlgorithm.OnEndOfDay方法的典型用法代码示例。如果您正苦于以下问题:C# IAlgorithm.OnEndOfDay方法的具体用法?C# IAlgorithm.OnEndOfDay怎么用?C# IAlgorithm.OnEndOfDay使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IAlgorithm
的用法示例。
在下文中一共展示了IAlgorithm.OnEndOfDay方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: EverySecurityEndOfDay
/// <summary>
/// Creates a new <see cref="ScheduledEvent"/> that will fire before market close by the specified time
/// </summary>
/// <param name="algorithm">The algorithm instance the event is fo</param>
/// <param name="resultHandler">The result handler, used to communicate run time errors</param>
/// <param name="security">The security used for defining tradeable dates</param>
/// <param name="start">The first date for the events</param>
/// <param name="end">The date to end the events</param>
/// <param name="endOfDayDelta">The time difference between the market close and the event, positive time will fire before market close</param>
/// <param name="currentUtcTime">Specfies the current time in UTC, before which, no events will be scheduled. Specify null to skip this filter.</param>
/// <returns>The new <see cref="ScheduledEvent"/> that will fire near market close each tradeable dat</returns>
public static ScheduledEvent EverySecurityEndOfDay(IAlgorithm algorithm, IResultHandler resultHandler, Security security, DateTime start, DateTime end, TimeSpan endOfDayDelta, DateTime? currentUtcTime = null)
{
if (endOfDayDelta >= Time.OneDay)
{
throw new ArgumentException("Delta must be less than a day", "endOfDayDelta");
}
// define all the times we want this event to be fired, every tradeable day for the securtiy
// at the delta time before market close expressed in UTC
var times =
// for every date the exchange is open for this security
from date in Time.EachTradeableDay(security, start, end)
// get the next market close for the specified date
let marketClose = security.Exchange.Hours.GetNextMarketClose(date, security.IsExtendedMarketHours)
// define the time of day we want the event to fire before marketclose
let eventTime = marketClose.Subtract(endOfDayDelta)
// convert the event time into UTC
let eventUtcTime = eventTime.ConvertToUtc(security.Exchange.TimeZone)
// perform filter to verify it's not before the current time
where !currentUtcTime.HasValue || eventUtcTime > currentUtcTime
select eventUtcTime;
return new ScheduledEvent(CreateEventName(security.Symbol, "EndOfDay"), times, (name, triggerTime) =>
{
try
{
algorithm.OnEndOfDay(security.Symbol);
}
catch (Exception err)
{
resultHandler.RuntimeError(string.Format("Runtime error in {0} event: {1}", name, err.Message), err.StackTrace);
Log.Error(string.Format("ScheduledEvent.{0}: {1}", name, err.Message));
}
});
}
示例2: EveryAlgorithmEndOfDay
/// <summary>
/// Creates a new <see cref="ScheduledEvent"/> that will fire before market close by the specified time
/// </summary>
/// <param name="algorithm">The algorithm instance the event is fo</param>
/// <param name="resultHandler">The result handler, used to communicate run time errors</param>
/// <param name="start">The date to start the events</param>
/// <param name="end">The date to end the events</param>
/// <param name="endOfDayDelta">The time difference between the market close and the event, positive time will fire before market close</param>
/// <param name="currentUtcTime">Specfies the current time in UTC, before which, no events will be scheduled. Specify null to skip this filter.</param>
/// <returns>The new <see cref="ScheduledEvent"/> that will fire near market close each tradeable dat</returns>
public static ScheduledEvent EveryAlgorithmEndOfDay(IAlgorithm algorithm, IResultHandler resultHandler, DateTime start, DateTime end, TimeSpan endOfDayDelta, DateTime? currentUtcTime = null)
{
if (endOfDayDelta >= Time.OneDay)
{
throw new ArgumentException("Delta must be less than a day", "endOfDayDelta");
}
// set up an event to fire every tradeable date for the algorithm as a whole
var eodEventTime = Time.OneDay.Subtract(endOfDayDelta);
// create enumerable of end of day in algorithm's time zone
var times =
// for every date any exchange is open in the algorithm
from date in Time.EachTradeableDay(algorithm.Securities.Values, start, end)
// define the time of day we want the event to fire, a little before midnight
let eventTime = date + eodEventTime
// convert the event time into UTC
let eventUtcTime = eventTime.ConvertToUtc(algorithm.TimeZone)
// perform filter to verify it's not before the current time
where !currentUtcTime.HasValue || eventUtcTime > currentUtcTime.Value
select eventUtcTime;
return new ScheduledEvent(CreateEventName("Algorithm", "EndOfDay"), times, (name, triggerTime) =>
{
Log.Debug(string.Format("ScheduledEvent.{0}: Firing at {1}", name, triggerTime));
try
{
algorithm.OnEndOfDay();
Log.Debug(string.Format("ScheduledEvent.{0}: Fired On End of Day Event() for Day({1})", name, triggerTime.ToShortDateString()));
}
catch (Exception err)
{
resultHandler.RuntimeError(string.Format("Runtime error in {0} event: {1}", name, err.Message), err.StackTrace);
Log.Error(string.Format("ScheduledEvent.{0}: {1}", name, err.Message));
}
});
}