From b6be95f8e59b103af60defe65fd652f7364353a8 Mon Sep 17 00:00:00 2001 From: Keshav Dalmia Date: Tue, 20 Aug 2024 16:19:00 +0000 Subject: [PATCH 1/2] added the exception handler --- .../QuantConnect.Algorithm.Python.csproj | 1 + Algorithm/QCAlgorithm.cs | 9 ++++++++ .../Python/Wrappers/AlgorithmPythonWrapper.cs | 22 +++++++++++++++++++ Common/Interfaces/IAlgorithm.cs | 7 ++++++ 4 files changed, 39 insertions(+) diff --git a/Algorithm.Python/QuantConnect.Algorithm.Python.csproj b/Algorithm.Python/QuantConnect.Algorithm.Python.csproj index 735f2871e9cf..0d69a05f997b 100644 --- a/Algorithm.Python/QuantConnect.Algorithm.Python.csproj +++ b/Algorithm.Python/QuantConnect.Algorithm.Python.csproj @@ -137,6 +137,7 @@ + diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index 7c965df2bdc5..edd151a50c6d 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -3367,5 +3367,14 @@ public void SetStatisticsService(IStatisticsService statisticsService) _statisticsService = statisticsService; } } + + public void OnError(Exception exception) + { + // Log the exception + DebugMessages.Enqueue(exception.ToString()); + + // Perform necessary actions such as closing positions and opening orders + Liquidate(); + } } } diff --git a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs index 68681ac269f7..c4eb7b521fec 100644 --- a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs +++ b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs @@ -65,6 +65,7 @@ public class AlgorithmPythonWrapper : BasePythonWrapper, IAlgorithm private dynamic _onAssignmentOrderEvent; private dynamic _onSecuritiesChanged; private dynamic _onFrameworkSecuritiesChanged; + private dynamic _onError; /// /// True if the underlying python algorithm implements "OnEndOfDay" @@ -120,6 +121,7 @@ public AlgorithmPythonWrapper(string moduleName) // determines whether OnData method was defined or inherits from QCAlgorithm // If it is not, OnData from the base class will not be called _onData = _algorithm.GetPythonMethod("OnData"); + _onError = _algorithm.GetPythonMethod("OnError"); _onMarginCall = _algorithm.GetPythonMethod("OnMarginCall"); @@ -1242,5 +1244,25 @@ public void SetTags(HashSet tags) { _baseAlgorithm.SetTags(tags); } + + /// + /// Handles exceptions thrown during the execution of the algorithm. + /// + /// The exception that was thrown + public void OnError(Exception exception) + { + if (_onError != null) + { + using (Py.GIL()) + { + _onError(exception); + } + } + else + { + // Default error handling if no custom OnError method is defined + Console.WriteLine($"Unhandled exception: {exception}"); + } + } } } diff --git a/Common/Interfaces/IAlgorithm.cs b/Common/Interfaces/IAlgorithm.cs index b1d37fe867ca..d115cc2014d0 100644 --- a/Common/Interfaces/IAlgorithm.cs +++ b/Common/Interfaces/IAlgorithm.cs @@ -919,5 +919,12 @@ Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool fillForw /// /// The tags void SetTags(HashSet tags); + + /// + /// Method to handle runtime errors. + /// + /// The exception that occurred. + void OnError(Exception exception); + } } From f5342064bf87a6771761f7e78a9ee4035e771afe Mon Sep 17 00:00:00 2001 From: Keshav Dalmia Date: Tue, 20 Aug 2024 16:46:12 +0000 Subject: [PATCH 2/2] added comments --- Algorithm.Python/QuantConnect.Algorithm.Python.csproj | 1 - Algorithm/QCAlgorithm.cs | 7 ++++--- AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs | 6 +++--- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/Algorithm.Python/QuantConnect.Algorithm.Python.csproj b/Algorithm.Python/QuantConnect.Algorithm.Python.csproj index 0d69a05f997b..735f2871e9cf 100644 --- a/Algorithm.Python/QuantConnect.Algorithm.Python.csproj +++ b/Algorithm.Python/QuantConnect.Algorithm.Python.csproj @@ -137,7 +137,6 @@ - diff --git a/Algorithm/QCAlgorithm.cs b/Algorithm/QCAlgorithm.cs index edd151a50c6d..a4cc9d795fcf 100644 --- a/Algorithm/QCAlgorithm.cs +++ b/Algorithm/QCAlgorithm.cs @@ -3368,12 +3368,13 @@ public void SetStatisticsService(IStatisticsService statisticsService) } } + /// + /// Handles the error event in the algorithm + /// + /// The statistics service instance public void OnError(Exception exception) { - // Log the exception DebugMessages.Enqueue(exception.ToString()); - - // Perform necessary actions such as closing positions and opening orders Liquidate(); } } diff --git a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs index c4eb7b521fec..343824f95188 100644 --- a/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs +++ b/AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs @@ -1246,9 +1246,9 @@ public void SetTags(HashSet tags) } /// - /// Handles exceptions thrown during the execution of the algorithm. - /// - /// The exception that was thrown + /// Handles exceptions thrown during the execution of the algorithm. + /// + /// The exception that was thrown public void OnError(Exception exception) { if (_onError != null)