QuantConnect / QuantConnect/Lean

Adds Algorithm Exception Handler

Open
#8,239 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature
Dominant language
C#
Stars
21.7k
Forks
5.3k
Avg merge
2d 22h
Merged PRs (30d)
34

Description

Expected Behavior

Be able to take action when a runtime exception is raised.

Actual Behavior

If a runtime error occurs, we cannot take actions such as close positions and open orders.

Potential Solution

Add IAlgorithm.OnError method.

Reproducing the Problem
from AlgorithmImports import *
class FormalFluorescentYellowPenguin(QCAlgorithm):
    def initialize(self):
        self.set_start_date(2024, 6, 30)
        self.add_crypto("BTCUSD")
    def on_data(self, data):
        x=1/0
    def on_end_of_algorithm(self):
        self.liquidate()

In this case, on_end_of_algorithm is not called, so we cannot close positions and open orders if we want to.

Checklist
  • I have completely filled out this template
  • I have confirmed that this issue exists on the current master branch
  • I have confirmed that this is not a duplicate issue by searching issues
  • I have provided detailed steps to reproduce the issue

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied Python reproduction, focusing on the on_data and on_end_of_algorithm entry points and how runtime exceptions affect the algorithm lifecycle. Trace the existing exception-handling path, then verify that an error callback can run the requested cleanup actions, including liquidating positions and closing open orders.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, python
Domain
api, backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.