Skip to content

Commit

Permalink
Minor improvement for live trading shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Molinero committed Sep 19, 2024
1 parent f2f1d06 commit 7c42ea7
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions Engine/AlgorithmManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public class AlgorithmManager
private IAlgorithm _algorithm;
private readonly object _lock;
private readonly bool _liveMode;
private bool _cancelRequested;
private CancellationTokenSource _cancellationTokenSource;

/// <summary>
Expand Down Expand Up @@ -612,13 +613,20 @@ public void SetStatus(AlgorithmStatus state)
_algorithm.SetStatus(state);
}

if (state == AlgorithmStatus.Deleted)
if (!_cancellationTokenSource.IsCancellationRequested && !_cancelRequested)
{
if (!_cancellationTokenSource.IsCancellationRequested)
if (state == AlgorithmStatus.Deleted)
{
// if the algorithm was deleted or stopped, let's give the algorithm a few seconds to shutdown and cancel it out
_cancelRequested = true;
// if the algorithm was deleted, let's give the algorithm a few seconds to shutdown and cancel it out
_cancellationTokenSource.CancelAfter(TimeSpan.FromSeconds(5));
}
else if (state == AlgorithmStatus.Stopped)
{
_cancelRequested = true;
// if the algorithm was stopped, let's give the algorithm a few seconds to shutdown and cancel it out
_cancellationTokenSource.CancelAfter(TimeSpan.FromMinutes(1));
}
}
}
}
Expand Down

0 comments on commit 7c42ea7

Please sign in to comment.