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

Handle zero price and target quantity case - Collective2 #8525

Conversation

JosueNina
Copy link
Contributor

Description

Handled the case where price and target quantity are zero to prevent exceptions.

Related Issue

Closes #8523

Motivation and Context

Prevents the algorithm from throwing exceptions when the price and target quantity are zero, ensuring smoother execution.

Requires Documentation Change

N/A

How Has This Been Tested?

Tested with unit tests to ensure no exceptions are thrown and the result is zero when both price and target quantity are zero.

Types of changes

  • Bug fix (non-breaking change which fixes an issue)
  • Refactor (non-breaking change which improves implementation)
  • Performance (non-breaking change which improves performance. Please add associated performance test and results)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Non-functional change (xml comments/documentation/etc)

Checklist:

  • My code follows the code style of this project.
  • I have read the CONTRIBUTING document.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • My branch follows the naming convention bug-<issue#>-<description> or feature-<issue#>-<description>

@AlexCatarino
Copy link
Member

In the ConvertHoldingsToCollective2 method, we can add that if the security is not invested and the target quantity is zero, there is nothing to do:

protected bool ConvertHoldingsToCollective2(SignalExportTargetParameters parameters, out List<Collective2Position> positions)
{
    _algorithm = parameters.Algorithm;
    var targets = parameters.Targets;
    positions = new List<Collective2Position>();
    foreach (var target in targets)
    {
        if (target == null)
        {
            _algorithm.Error("One portfolio target was null");
            return false;
        }

        // Don't create a signal that doesn't represent a position
        if (target.Quantity == 0 && !_algorithm.Portfolio[target.Symbol].Invested)
        {
            continue;
        }

        if (!ConvertTypeOfSymbol(target.Symbol, out string typeOfSymbol))
        {
            return false;
        }

@Martin-Molinero Martin-Molinero merged commit d633fed into QuantConnect:master Jan 13, 2025
4 of 7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failure to Calculate Quantity Should not Throw Collective2 Signal
3 participants