Skip to content

Trade mapped contract for canonical Future order requests#9467

Open
AlexCatarino wants to merge 2 commits intoQuantConnect:masterfrom
AlexCatarino:bug-9463-trade-mapped-on-canonical-future
Open

Trade mapped contract for canonical Future order requests#9467
AlexCatarino wants to merge 2 commits intoQuantConnect:masterfrom
AlexCatarino:bug-9463-trade-mapped-on-canonical-future

Conversation

@AlexCatarino
Copy link
Copy Markdown
Member

Description

Routes order requests submitted with a canonical (continuous) Future Symbol to the currently mapped contract. The redirection happens in QCAlgorithm.GetSecurityForOrder: when the requested symbol is canonical and the security implements IContinuousSecurity with a non-null Mapped, the corresponding contract security in Securities is used instead of the canonical. If Mapped is unset (or the mapped contract isn't in Securities), behavior falls back to the previous path (the canonical security is returned, leading to a NonTradableSecurity rejection).

The Continuous Future and Future Rollover basic templates (C# and Python) were updated to demonstrate the simpler API by passing the canonical Symbol directly to the trading method.

Related Issue

Closes #9463

Motivation and Context

Today, calling set_holdings(future.Symbol, ...) (or any other trading method) with the canonical Future Symbol fails with The security with symbol '/GC' is marked as non-tradable. Users were forced to manually look up future.Mapped and pass that contract symbol. With this change, the canonical Symbol is automatically routed to the active contract, matching the behavior users intuitively expect.

Requires Documentation Change

No public API change; existing docs/examples remain valid. The basic Continuous Future and Future Rollover templates were updated to use the canonical Symbol so that the documentation generated from them reflects the simpler pattern.

How Has This Been Tested?

  • Added unit tests in Tests/Algorithm/AlgorithmTradingTests.cs:
    • OrdersOnCanonicalFutureSymbolAreRoutedToMappedContract — verifies MarketOrder(canonical.Symbol, 1) produces an order targeting the mapped contract.
    • OrdersOnCanonicalFutureSymbolAreRejectedWhenNoMappedContract — verifies fallback behavior still rejects with NonTradableSecurity when Mapped is null.
  • Built QuantConnect.Algorithm, QuantConnect.Algorithm.CSharp, and QuantConnect.Tests — no errors.

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>

Route order requests on a canonical (continuous) Future symbol to the
currently mapped contract via IContinuousSecurity.Mapped, falling back
to the canonical security when no contract is mapped.

Closes QuantConnect#9463
if (isCanonical && security is IContinuousSecurity { Mapped: not null } continuousSecurity &&
Securities.TryGetValue(continuousSecurity.Mapped, out var mappedSecurity))
{
return mappedSecurity;
Copy link
Copy Markdown
Member

@jaredbroad jaredbroad May 7, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️ Nice keeps it simple.

When the target symbol is a canonical (continuous) Future, base the
buying-power math and the resulting target on the currently mapped
contract. If no contract is mapped, surface a dedicated error message
instead of silently returning a zero-quantity result.

Adds a unit test asserting SetHoldings on a canonical Future submits an
order against the mapped contract sized off the mapped contract's price.
@AlexCatarino AlexCatarino requested a review from jaredbroad May 7, 2026 18:08
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.

Automatically trade the mapped asset

2 participants