Hi everyone,
I am stuck at:
“Runtime Error: Please register to receive data for symbol ' W5373A5QVCRP' using the AddSecurity() function. in Indicators.cs:line 2213”,
when I try to backtest. Should I add a class for SymbolData, or change how I update the moving average current values?
Here is my python file below:
#region imports
from AlgorithmImports import *
from QuantConnect.Data.UniverseSelection import *
#endregion
class ShortAlgorithm(QCAlgorithm):
def __init__(self):
self.reb = 1
self.num_fine = 10
self.symbols = None
self.averages = {}
def Initialize(self):
self.SetCash(100000)
self.SetStartDate(2022, 1, 1)
self.AddIndex("VIX", Resolution.Daily)
self.spy = self.AddEquity("SPY", Resolution.Daily).Symbol
self.s200 = self.SMA("SPY", 200, Resolution.Daily)
self.SetBenchmark("SPY")
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction,self.FineSelectionFunction)
self.Schedule.On(self.DateRules.EveryDay(self.spy),
self.TimeRules.AfterMarketOpen(self.spy, 0), Action(self.rebalance))
def OnData(self, data):
if not data["SPY"]:
return
if not self.s200.IsReady:
return
def CoarseSelectionFunction(self, coarse):
if self.reb != 1:
return self.symbols
for cf in coarse:
if cf.Symbol not in self.averages:
self.averages[cf.Symbol] = self.SMA(cf.Symbol, 200)
self.averages[cf.Symbol].Update(cf.EndTime, cf.AdjustedPrice)
filtered_coarse = [x.Symbol for x in coarse if (x.HasFundamentalData)
and x.Price > 5
and x.Price < self.averages[x.Symbol].Current.Value
and x.Volume > 1e5
]
return filtered_coarse
def FineSelectionFunction(self, fine):
if self.reb != 1:
return self.symbols
self.reb = 0
filtered_fine = [x for x in fine if
x.MarketCap > 2e9
]
top = sorted(filtered_fine, key = lambda x: x.MarketCap, reverse=True)[:self.num_fine]
self.symbols = [x.Symbol for x in top]
return self.symbols
def OnData(self, data):
pass
def rebalance(self):
long_short_list = self.symbols
for i in self.Portfolio.Values:
if (i.Invested) and (i not in long_short_list):
self.Liquidate(i.Symbol)
if self.Securities["SPY"].Price < self.s200.Current.Value:
if self.Securities["VIX"].Price > 20:
for i in self.symbols:
self.SetHoldings(i, -1/self.num_fine)
else:
self.SetHoldings("SPY", 1)
else:
self.SetHoldings("SPY", 1)
self.reb = 1
Any feedback is much appreciated!
Mia Alissi
Hi David,
The error message you're seeing is typically due to trying to use a symbol that hasn't been added to your algorithm using the
AddSecurity()
orAddEquity()
function. In your case, it seems like you're trying to use a symbol 'W5373A5QVCRP' that hasn't been added.In your
CoarseSelectionFunction
, you're creating a Simple Moving Average (SMA) indicator for each coarse object. However, you're not adding these securities to your algorithm. You should add these securities using theAddEquity()
function before creating the SMA indicator.Here's how you can modify your
CoarseSelectionFunction
:This will ensure that each symbol you're trying to create an SMA for is added to your algorithm.
Also, please note that you have two
OnData
methods in your code. Python doesn't support method overloading like C#, so the secondOnData
method will overwrite the first one. You should remove or rename one of them.I hope this helps! Let me know if you have any other questions.
Sources:
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.
David L
Hi Mia,
Thanks for the quick response! In that case since I am trying to find the SMAs for only the equities being coarse/fine selected is there a method in which I could add all available equities with data, such as self.AddEquity([], Resolution.Daily)? Or is this not possible as it would be too much data to backtest on the cloud?
Mia Alissi
Hi David,
Adding all available equities is not feasible due to the sheer volume of data. It would be computationally expensive and could exceed the memory limit, causing your algorithm to fail.
Instead, you can use the Universe Selection feature, which is designed to handle large amounts of data. With Universe Selection, you can filter down the universe of all available equities to a manageable size based on your specific criteria.
In your case, you're already using the
CoarseSelectionFunction
to filter down the universe. You can continue to use this approach to select the equities you're interested in and calculate the SMA for those equities.Remember, you can add the equities to your algorithm in the
CoarseSelectionFunction
as I suggested in the previous response. This way, you're only adding the equities that pass your coarse selection criteria, which should be a manageable number.Here's the modified
CoarseSelectionFunction
again for reference:I hope this helps! Let me know if you have any other questions.
Sources:
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.
David L
Thank you again, that works out. The quantconnect indicator universes documentation really helped too.
Mia Alissi
I'm glad to hear that you found the solution helpful and the QuantConnect Indicator Universes documentation useful. If you have any more questions or run into any other issues, feel free to ask. Happy coding!
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.
David L
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!