Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added exception handler function #8266

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Algorithm/QCAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3367,5 +3367,15 @@ public void SetStatisticsService(IStatisticsService statisticsService)
_statisticsService = statisticsService;
}
}

/// <summary>
/// Handles the error event in the algorithm
/// </summary>
/// <param name="exception">The statistics service instance</param>
public void OnError(Exception exception)
{
DebugMessages.Enqueue(exception.ToString());
Liquidate();
}
}
}
22 changes: 22 additions & 0 deletions AlgorithmFactory/Python/Wrappers/AlgorithmPythonWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ public class AlgorithmPythonWrapper : BasePythonWrapper<IAlgorithm>, IAlgorithm
private dynamic _onAssignmentOrderEvent;
private dynamic _onSecuritiesChanged;
private dynamic _onFrameworkSecuritiesChanged;
private dynamic _onError;

/// <summary>
/// True if the underlying python algorithm implements "OnEndOfDay"
Expand Down Expand Up @@ -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");

Expand Down Expand Up @@ -1242,5 +1244,25 @@ public void SetTags(HashSet<string> tags)
{
_baseAlgorithm.SetTags(tags);
}

/// <summary>
/// Handles exceptions thrown during the execution of the algorithm.
/// </summary>
/// <param name="exception">The exception that was thrown</param>
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}");
}
}
}
}
7 changes: 7 additions & 0 deletions Common/Interfaces/IAlgorithm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -919,5 +919,12 @@ Security AddSecurity(Symbol symbol, Resolution? resolution = null, bool fillForw
/// </summary>
/// <param name="tags">The tags</param>
void SetTags(HashSet<string> tags);

/// <summary>
/// Method to handle runtime errors.
/// </summary>
/// <param name="exception">The exception that occurred.</param>
void OnError(Exception exception);

}
}