Hello, I am new to QuantConnect and am running into an error that I can't figure out:
"Runtime Error: ArgumentException : Please subscribe to this symbol before adding a consolidator for it. Symbol: GC27V21
at QuantConnect.Data.SubscriptionManager.AddConsolidator(Symbol symbol, IDataConsolidator consolidator) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Data/SubscriptionManager.cs:line 156
at QuantConnect.Data.SubscriptionManager.AddConsolidator(Symbol symbol, PyObject pyConsolidator) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Data/SubscriptionManager.cs:line 184
at ConsolidateFuture
self.SubscriptionManager.AddConsolidator(contract.Symbol in main.py: line 165 (Open Stacktrace)"
As you can see I am adding a list of futures in the Initialize method and filtering them by front month. Currently I just have one future being used in the list for testing.
class FiveOneFractalBreakoutCopy(QCAlgorithm):
consolidator_by_symbol = {}
@property
def tickers(self):
metals = [
Futures.Metals.Gold,
#Futures.Metals.Silver,
#Futures.Metals.Platinum,
#Futures.Metals.Palladium,
#Futures.Metals.Copper,
]
tickers = metals
return tickers
def Initialize(self):
self.SetStartDate(2021, 9, 15) # Set Start Date
self.SetCash(100000) # Set Strategy Cash
from future_set_up import FutureSetUp
newFutureSetUp = FutureSetUp(self)
self.SetSecurityInitializer(newFutureSetUp.OpenInterestSecurityInitializer)
universe: typing.Dict[str, Future] = {s: self.AddFuture(s, Resolution.Minute) for s in self.tickers}
[future.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen()) for future in universe.values()]
#gc = self.AddFuture(Futures.Metals.Gold, Resolution.Minute)
#gc.SetFilter(lambda x: x.FrontMonth().OnlyApplyFilterAtMarketOpen())
#self.Consolidate(gc.Symbol, timedelta(minutes=5), self.OnDataConsolidated)
In the ConsolidateFuture() method I am trying to consolidate the front month contract. When debugging the first front month contract that runs it works perfectly, but once there is a new front month contract later in time it gives me that error.
def OnDataConsolidated(self, bar):
#5M DATA SETUP
self.liquidContract = self.liquidContractFive
if self.liquidContract is None:
return
self.bar = bar
self.longTradeBarWindow.Add(self.bar)
if not self.longTradeBarWindow.IsReady:
return
from consolidated_fractals import ConsolidatedFractals
newConsolidatedFractals = ConsolidatedFractals(self)
#5M UP-FRACTAL FORMULA
self.FiveUp = newConsolidatedFractals.UpFractalFormula()
#5M DN-FRACTAL FORMULA
self.FiveDown = newConsolidatedFractals.DownFractalFormula()
#SET SUPPORT, RESET NON-DIRECTIONAL STATUS
self.support = newConsolidatedFractals.SetSupport(self.FiveUp)
#SET RESISTANCE, RESET NON-DIRECTIONAL STATUS
self.resistance = newConsolidatedFractals.SetResistance(self.FiveDown)
if self.support is None:
return
if self.resistance is None:
return
if self.partial_exit_fill_safety_long is True and self.Securities[self.liquidContract].Low <= self. partial_fill_liq_price:
self.Liquidate(self.liquidContract)
self.partial_exit_fill_safety_long = False
self.partial_fill_liq_price = None
if self.partial_exit_fill_safety_short is True and self.Securities[self.liquidContract].Low >= self.partial_fill_liq_price:
self.Liquidate(self.liquidContract)
self.partial_exit_fill_safety_short = False
self.partial_fill_liq_price = None
#SET BULLISH DIRECTIONAL STATUS
if self.Securities[self.liquidContract].Close > self.resistance:
self.breakout = True
#SET BEARISH DIRECTIONAL STATUS
if self.Securities[self.liquidContract].Close < self.support:
self.breakdown = True
def FiveMinuteHandler(self, sender, bar):
# Bar period is now 5 min from the consolidator above.
self.Debug(str(bar.EndTime - bar.Time) + " " + bar.ToString())
pass
def ConsolidateFuture(self, contract):
contractSymbol = contract.Symbol
contractValue = contract.Symbol.Value
dailyConsolidator = TradeBarConsolidator(timedelta(minutes=5))
dailyConsolidator.DataConsolidated += self.FiveMinuteHandler
self.SubscriptionManager.AddConsolidator(contract.Symbol, dailyConsolidator)
self.consolidator_by_symbol[contract.Symbol] = dailyConsolidator
def OnData(self, slice):
from future_set_up import FutureSetUp
from quick_window import QuickWindow
from fractals import Fractals
#Set Contract
newFutureSetUp = FutureSetUp(self)
liquidContract = newFutureSetUp.SetContract(slice)
if(liquidContract == None):
return
#Make sure Contract is consolidated
if liquidContract.Symbol not in self.consolidator_by_symbol:
self.ConsolidateFuture(liquidContract)
If anyone can help me find the solution to this problem that would be awesome!
Thanks
Fred Painchaud
Hi Isaac,
Analyzing your algo in depth would take some time but here is some basic help:
if liquidContract.Symbol not in self.consolidator_by_symbol:
self.ConsolidateFuture(liquidContract)
gets executed for “liquidContrat”s on which you are not subscribed (AddFuture has not been called).
😊
Cheers,
Fred
Isaac Rochester
Sorry for the late response…
I've been updating a lot of code. Ultimately I did take your advice by making sure AddFuture() was done first. I did this by doing all my consolidation in the OnSecuritiesChanged() event method. That fixed the error.
However, I'm running into a new error (only when running live) regarding symbol consolidation…
Runtime Error: InvalidOperationException : Consolidators can only be used with a single symbol. The previous consolidated SecurityIdentifier (CL JL) is not the same as in the current data (CL XVC8K2PX0IIP).
at QuantConnect.Data.Consolidators.PeriodCountConsolidatorBase`2.Update(T data) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Data/Consolidators/PeriodCountConsolidatorBase.cs:line 212 Stack Trace: InvalidOperationException : Consolidators can only be used with a single symbol. The previous consolidated SecurityIdentifier (CL JL) is not the same as in the current data (CL XVC8K2PX0IIP).
at QuantConnect.Data.Consolidators.PeriodCountConsolidatorBase`2.Update(T data) in /LeanCloud/CI.Builder/bin/Debug/src/QuantConnect/Lean/Common/Data/Consolidators/PeriodCountConsolidatorBase.cs:line 212
This is how I'm doing my consolidation in OnSecuritiesChanged():
As you can probably read in my comments, I'm only consolidating the contracts and not the futures in OnSecurititesChanged(). Also, I'm making checks to ensure that only one contract per future is being consolidated. This all works perfectly in backtesting, but as soon as it runs live it doesn't work and throws that error.
This is the whole main.py:
This error makes me curious if it's a QC data problem when running live? I don't see anyone else online having this problem. Again, this code all works when running back tests.
Thanks!
Fred Painchaud
Hi Isaac,
Unfortunately for you, I believe you have exacerbated two things:
1- The LEAN code on the Live trading side is different (at least slightly) than the code on the backtesting side.
2- There's a bug in the live trading side which is not present in the backtesting side.
From your error log, there's an exception raised in the LEAN code, in file PeriodCountConsolidatorBase.cs, line 212. That line must be code that raises that exception, and from that code, you can normally understand what happened (sometimes, it takes time 😊).
But here, line 212 is a “{”, which makes no sense that line is reported as the source of the exception (https://github.com/QuantConnect/Lean/blob/bfa58b46928da416321e610800204edb40ef6ecf/Common/Data/Consolidators/PeriodCountConsolidatorBase.cs#L212). It looks like the code on GH is not necessarily 100% identical to the code running on live trading nodes.
From git, I can see that code was done in 2016 and it is specifically handling quote data (well, again, that codes does not seem to be the exact one running live but the live one should not be THAT different).
You should report that to support@quantconnect.com. They will open a ticket on GH and fix it.
Cheers,
Fred
Martin Molinero
Hey Isaac!
I've just tested using that last ‘main.py’ implementation you shared, simplifying it a bit because it didn't compile, and was not able to reproduce the live issue using QC as a data source, is QC what you are using?. Could you please provide the complete algorithm reproducing the issue (the simpler the algorithm the better) or if you can share the issue with support would be great, so if there is any bug in lean with fix it ASAP.
Thanks!
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Fred Painchaud
Hi Martin,
Please note the error is supposed to only happen when live trading. Isaac wrote it works fine while backtesting.
Or is that what you meant by “live issue”? 😊
Cheers,
Fred
Isaac Rochester
I have included a functioning version of the algorithm that should allow you to reproduce the live trading error. To clarify, we are using QC's data for live trading when getting this runtime error. Also, we aren't always getting this error immediately upon deployment – it typically takes an hour or two.
Thank you Martin and Fred for your help!
Fred Painchaud
No problem Isaac. An hour or 2 at minute/5m res on quote bars, yes, it looks similar to a different issue that was fixed 9 months ago in the same code. Anyway, you're in good hands with Martin. Thanks for reporting.
Fred
Isaac Rochester
I just reviewed the code that was included in the algorithm I sent over and noticed that it seems to be missing a lot. It's most definitely not “functioning.” We have onData, onDataConsolidated, and “futureExecution" in a library. Apologies!
Our code in onDataConsolidated:
The code we're using to execute trades:
Fred Painchaud
😊
hehehehehe
I hope Martin gets your runtime error while live trading this… AND is able to pinpoint some bug relatively quick. As this is not very small bug reproducing code 😊. Fingers crossed.
Cheers,
Fred
P.S. Coffee is free for devs in your shop right? 😊 Just kidding…
Isaac Rochester
I hope so as well – I know it's a lot of code to look at. I've been impressed with everyone at QC so I have no doubt I'm in good hands with this. You guys are running a tight ship.
No free coffee unfortunately. But if you're ever in the Sioux City/Omaha area, you can have a cup on me!
Jared Broad
“I've been impressed with everyone at QC so I have no doubt I'm in good hands with this. You guys are running a tight ship.”
❤️ Thanks Isaac!
We're pushing the fix now. Just a note we haven't officially announced the QC futures live feed yet it's in "stealth" beta =D. Reports like this are important and appreciated to make sure it's ready for prime time.
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
Isaac Rochester
The material on this website is provided for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation or endorsement for any security or strategy, nor does it constitute an offer to provide investment advisory services by QuantConnect. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. QuantConnect makes no guarantees as to the accuracy or completeness of the views expressed in the website. The views are subject to change, and may have become unreliable for various reasons, including changes in market conditions or economic circumstances. All investments involve risk, including loss of principal. You should consult with an investment professional before making any investment decisions.
To unlock posting to the community forums please complete at least 30% of Boot Camp.
You can continue your Boot Camp training progress from the terminal. We hope to see you in the community soon!