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

Manually remove identity consolidator #7968

Closed
4 tasks done
jhonabreul opened this issue Apr 22, 2024 · 0 comments · Fixed by #7995
Closed
4 tasks done

Manually remove identity consolidator #7968

jhonabreul opened this issue Apr 22, 2024 · 0 comments · Fixed by #7995
Assignees
Labels

Comments

@jhonabreul
Copy link
Collaborator

jhonabreul commented Apr 22, 2024

Expected Behavior

A manual call to SubscriptionManager.RemoveConsolidator(symbol, consolidator) removes registered consolidator when called from Python algorithm

Actual Behavior

When the consolidator is resolved to an IdentityDataConsolidator (might also be in other cases) SubscriptionManager.RemoveConsolidator doesn't remove the consolidator.

Potential Solution

The source of the bug might be because RegisterIndicator will try to convert the consolidator PyObject to an IDataConsolidator and fails, wrapping the object in a DataConsolidatorPythonWrapper. So after this, the actual stored consolidator referenced is wrapped and SubscriptionManager.RemoveConsolidator won't find it.

We might need to detect the wrapper and remove by its underlying python model.

Reproducing the Problem

Attached backtest: https://www.quantconnect.com/terminal/clone/-/ba9a391ee0a14a56cdc0729838675305/clone-of%3A-calculating-red-seahorse

class TestAlgorithm(QCAlgorithm):

    def initialize(self):
        self.set_start_date(2013, 10, 7)
        self.set_end_date(2013, 10, 11)

        self.spy = self.add_equity("SPY").symbol

        self.consolidator = self.resolve_consolidator(self.spy, Resolution.MINUTE)

        name = self.create_indicator_name(self.spy, "close", Resolution.MINUTE)
        identity = Identity(name)
        self.indicator = self.register_indicator(self.spy, identity, self.consolidator)

        self.schedule.on(self.date_rules.today, self.time_rules.before_market_close(self.spy), self.remove_consolidator)

    def remove_consolidator(self):
        self.subscription_manager.remove_consolidator(self.spy, self.consolidator)

        consolidator_count = sum(s.consolidators.count for s in self.subscription_manager.subscriptions)
        if consolidator_count > 0:
            raise Exception(f"The number of consolidator should be zero. Actual: {consolidator_count}")

NOTE: This fix is required in order to be able to use PearsonCorrelationPairsTradingAlphaModel in PearsonCorrelationPairsTradingAlphaModelFrameworkAlgorithm and any other algorithm. As of now, that algorithm is not importing the actual python model (with from Alphas.PearsonCorrelationPairsTradingAlphaModel import PearsonCorrelationPairsTradingAlphaModel) so it's using the C# one.
The Python PearsonCorrelationPairsTradingAlphaModel does a manual consolidator removal that has this bug and the algorithm final assertion for the consolidators count fails when using the python model.

System Information

Windows 11
QC Cloud

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants