Hello, I recently started using Quantconnect but I have come to a severe stop since I'm not able to properly consolidate several symbols.
It gives me the following error when trying to backtest "During the algorithm initialization, the following exception has occurred: Trying to dynamically access a method that does not exist throws a TypeError exception. To prevent the exception, ensure each parameter type matches those required by the AddEquity method. Please checkout the API documentation. at Initialize in main.py:line 28 TypeError : No method matches given arguments for AddEquity"
Tomas Christensen
from Alphas.EmaCrossAlphaModel import EmaCrossAlphaModel from Portfolio.BlackLittermanOptimizationPortfolioConstructionModel import BlackLittermanOptimizationPortfolioConstructionModel from Execution.ImmediateExecutionModel import ImmediateExecutionModel from Risk.NullRiskManagementModel import NullRiskManagementModel from datetime import datetime, timedelta class BasicTemplateFrameworkAlgorithm(QCAlgorithmFramework): def Initialize(self): # Set requested data resolution self.UniverseSettings.Resolution = Resolution.Minute self.SetStartDate(2019, 1, 1) #Set Start Date self.SetEndDate(2019, 12, 12) #Set End Date self.SetCash(7750) #Set Strategy Cash self.UniverseSettings.Resolution = Resolution.Minute tickers = ["TRVN", "AAPL", "BA", "TTP"] symbols = [ Symbol.Create(x, SecurityType.Equity, Market.USA) for x in tickers ] self.SetUniverseSelection( ManualUniverseSelectionModel(symbols) ) self.AddEquity(tickers, Resolution.Minute) thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=15)) # attach our event handler. The event handler is a function that will # be called each time we produce a new consolidated piece of data. thirtyMinuteConsolidator.DataConsolidated += self.ThirtyMinuteBarHandler # this call adds our 30-minute consolidator to # the manager to receive updates from the engine self.SubscriptionManager.AddConsolidator(tickers, thirtyMinuteConsolidator) def ThirtyMinuteBarHandler(self, sender, bar): self.Debug(str(self.Time) + " " + str(bar)) def OnData(self, data): pass thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=15)) # attach our event handler. The event handler is a function that will # be called each time we produce a new consolidated piece of data. thirtyMinuteConsolidator.DataConsolidated += self.ThirtyMinuteBarHandler # this call adds our 30-minute consolidator to # the manager to receive updates from the engine self.SubscriptionManager.AddConsolidator("TRVN", thirtyMinuteConsolidator) def ThirtyMinuteBarHandler(self, sender, bar): self.Debug(str(self.Time) + " " + str(bar)) def OnData(self, data): pass thirtyMinuteConsolidator = TradeBarConsolidator(timedelta(minutes=15)) # attach our event handler. The event handler is a function that will # be called each time we produce a new consolidated piece of data. thirtyMinuteConsolidator.DataConsolidated += self.ThirtyMinuteBarHandler # this call adds our 30-minute consolidator to # the manager to receive updates from the engine self.SubscriptionManager.AddConsolidator(tickers, thirtyMinuteConsolidator) def ThirtyMinuteBarHandler(self, sender, bar): self.Debug(str(self.Time) + " " + str(bar)) def OnData(self, data): pass self.SetAlpha(EmaCrossAlphaModel(50, 200, Resolution.Minute)) self.SetPortfolioConstruction(BlackLittermanOptimizationPortfolioConstructionModel()) self.SetExecution(ImmediateExecutionModel()) self.SetRiskManagement(NullRiskManagementModel()) def OnOrderEvent(self, orderEvent): if orderEvent.Status == OrderStatus.Filled: # self.Debug("Purchased Stock: {0}".format(orderEvent.Symbol)) pass
Code is as follows
Jack Simonson
Hi Tomas,
I've looked at the code you posted an noticed a number of things that will help with the issue you're encountering. First, when you use Universe Selection the symbols are automatically subscribed to and the self.AddEquity() call in the Initialize() method is unnecessary. Additionally, creating consolidators for multiple symbols in a Framework Algorithm is best done in the Alpha Model. I've attached a backtest in which I copied-and-pasted the EMACrossAlphaModel and added a section that builds these consolidators. This is done in the SymbolData class, which creates the Indicators for each symbol in the Alpha Model. With the added lines, the consolidators will be created and the indicators will be attached to the consolidators. Have a look at the backtest and hopefully this will help you design the algorithm. Additionally, I would recommend reading this forum post to see a discussion on the same topic.
Vncne
Hi Jack Simonson ,
I used your code in order to try and consolidate minute resolution data into daily bars by changing
self.consolidator = TradeBarConsolidator(timedelta(minutes = 15))
into
self.consolidator = TradeBarConsolidator(timedelta(days = 1))
But the output or the consolidated bar is always generated at 9:31 everyday(1 minute after market open), is there a way for it to be generated before market open, or as soon as the market closes? This is so that the algorithm can send MarkerOnOpen orders before the market opens.
Derek Melchin
Hi Vncne,
If we consolidate to daily bars, the consolidated bar is created after the first data point of the next day is received. When using minute data, the first data point of the next day is received at 9:31AM. To receive the consolidated bar earlier in the day than 9:31AM, we should subscribe to extended market hours.
self.UniverseSettings.ExtendedMarketHours = True
This will produce the consolidated bars at 4:01AM. See the attached backtest for reference.
Best,
Derek Melchin
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.
Vncne
Hi @derek-melchin, I tried to forgo with using the original algorithm from this thread because it contains a lot of things I don't really need, so I started from scratch with a simpler algorithm and tried doing your suggestion on this newer one -
But it doesn't seem to work, though it worked with the algorithm you attached in your reply.
The data is still coming in at 9:31 and so is 1 day late from when I'll need it
Thanks 😊
Derek Melchin
Hi Vncne,
To have the consolidated bar passed at midnight, we can use
See the attached backtest for reference.
Best,
Derek Melchin
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.
Vncne
Hi Derek Melchin ,
What is happening exactly when I add the line of code that you suggested, and what causes it to allow the data to be passed at midnight?
Varad Kabade
Hi Vncne,
The Scan method checks whether the consolidated period has passed. If so, it emits a consolidated bar. The above code forces the consolidator to update the current working bar in the consolidator at self.Time.
Best,
Varad Kabade
Nottakumasato
The solution that @jack-simonson posted seems to work on cloud but not on local LEAN with custom datasets (reading from local csv files).
I tried the same code, just changed
with
and it doesn't work = getting empty backtest results logged but with one key log:
which seems to suggest that
Also tried:
but still doesn't work.
Any help would be appreciated Varad Kabade Derek Melchin
Varad Kabade
Hi Nottakumasato,
Please try the given algorithm inside the cloud. Would you mind attaching the code snippet/backtest highlighting the issue for a precise diagnosis?
Best,
Varad Kabade
Nottakumasato
Varad Kabade the issue was resolved in
The issue was the QCAlgorithmFramework not being able to handle custom local datasets
Tomas Christensen
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!