Skip to content

Commit

Permalink
End execution of Lean if an error in a scheduled event is thrown
Browse files Browse the repository at this point in the history
  • Loading branch information
quietjoy committed Sep 25, 2017
1 parent eabd369 commit be986a5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Common/Scheduling/ScheduledEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ protected void OnEventFired(DateTime triggerTime)
}
catch (Exception ex)
{
// This scheduled event failed, so move on to the next one
// This scheduled event failed, so don't repeat the same event
_needsMoveNext = true;
throw new ScheduledEventException(ex.ToString());
}
Expand Down
41 changes: 22 additions & 19 deletions Engine/RealTime/LiveTradingRealTimeHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,30 +108,33 @@ public void Run()
// continue thread until cancellation is requested
while (!_cancellationTokenSource.IsCancellationRequested)
{
try
{
var time = DateTime.UtcNow;

// pause until the next second
var nextSecond = time.RoundUp(TimeSpan.FromSeconds(1));
var delay = Convert.ToInt32((nextSecond - time).TotalMilliseconds);
Thread.Sleep(delay < 0 ? 1 : delay);
var time = DateTime.UtcNow;

// pause until the next second
var nextSecond = time.RoundUp(TimeSpan.FromSeconds(1));
var delay = Convert.ToInt32((nextSecond - time).TotalMilliseconds);
Thread.Sleep(delay < 0 ? 1 : delay);

// poke each event to see if it should fire
foreach (var scheduledEvent in _scheduledEvents)
// poke each event to see if it should fire
foreach (var scheduledEvent in _scheduledEvents)
{
try
{
scheduledEvent.Value.Scan(time);
}
}
catch (ScheduledEventException scheduledEventException)
{
var errorMessage = $"LiveTradingRealTimeHandler: There was an error thrown in the scheduled event: {scheduledEventException.ScheduledEventExceptionMessage}";
Log.Error(errorMessage);
_resultHandler.ErrorMessage(errorMessage);
}
catch (Exception err)
{
Log.Error(err);
catch (ScheduledEventException scheduledEventException)
{
var errorMessage = $"LiveTradingRealTimeHandler.Run(): There was an error in a scheduled event {scheduledEvent.Key}. The error was {scheduledEventException}";

Log.Error(errorMessage);

_resultHandler.RuntimeError(errorMessage);

// Errors in scheduled event should be treated as runtime error
// Runtime errors should end Lean execution
_algorithm.RunTimeError = new Exception(errorMessage);
}
}
}

Expand Down

0 comments on commit be986a5

Please sign in to comment.