QuantConnect / QuantConnect/Lean

on_assignment_order_event() called before the shares are actually on the portfolio.

Open
#9,321 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Expected Behavior

Assigned shares should be in portoflio when this method is called to correctly handle assignment (cannot liquidate open another strategy, etc), current behavior this would make Example #1 and #2 in the docs behave incorrectly: Examples: Options Models - Assignment

Actual Behavior

Shares are not in your portfolio before when on_assignment_order_event() is called.

Potential Solution

The only workaround right now to handle the a assigned position is to use a on_securities_changes, below is an example:

def on_order_event(self, order_event: OrderEvent) -> None:
    """Handle assignment by liquidating delivered shares."""
    if (order_event.symbol.security_type == SecurityType.EQUITY and
        order_event.status == OrderStatus.FILLED):
        self.liquidate(self._spy, tag="Assignment liquidation")
Reproducing the Problem

Here is a backtest replicating it: Test on_assignment_order_event()
It gets called before the shares are actually on the portfolio, printing the message:
2024-09-03 10:00:00 Quit(): Invested on assignment: False

Here is a backtest checking on_order_event to see the order the events arrive there:
Test on_order_event() order of event arrival on_assignment_order_event() does get called before on_order_event()

System Information

Cloud master branch.

Checklist
  • [ X ] I have completely filled out this template
  • [ X ] I have confirmed that this issue exists on the current master branch
  • [ X ] 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 assignment and order-event callback flow exercised by the linked backtests, then trace when assigned shares are added to the portfolio. Compare the callback order shown in the reproductions with the expected portfolio state. Done means on_assignment_order_event() observes the assigned shares before assignment handling runs, with the linked backtests or a regression test confirming the ordering.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.