Hi
I am new to Quantconnect community and I am thrilled with the potential of this platform as a learning tool.
As I understood from previous descussions we don't have option to analyse volume on pre market during backtesting?
I want to know if it's possible to check most traded stocks based on volume up to 10:00 of the same day. I am reusing code from this thread but I am getting an errors for every stock in the list:
"The security does not have an accurate price as it has not yet received a bar of data.."
Can someone kindly point me to what I am doing wrong here.
My code:
class ParticleTransdimensionalAutosequencers(QCAlgorithm):
def Initialize(self):
self.SetStartDate(2020, 11, 26)
self.SetEndDate(2020, 11, 27)
self.SetCash(100000)
self.AddEquity("SPY", Resolution.Minute)
self.AddUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction))
self.UniverseSettings.Resolution = Resolution.Minute
self.Schedule.On(
self.DateRules.EveryDay("SPY"),
self.TimeRules.At(10, 0),
self.SelectUniverse
)
self.universe = []
self.volume_by_symbol = {}
self.logged = False
def OnData(self, data):
if len(self.volume_by_symbol) == 0:
if not self.logged:
self.logged = True
self.Log(f"Universe size after volume filter: {len(self.universe)}")
return
for symbol in self.volume_by_symbol.keys():
if data.ContainsKey(symbol) and data[symbol] is not None:
self.volume_by_symbol[symbol] += data[symbol].Volume
def CoarseSelectionFunction(self, coarse):
self.volume_by_symbol = {c.Symbol: 0 for c in coarse if c.Price > 10}
self.Log(f"Universe size before volume filter: {len(self.volume_by_symbol)}")
return list(self.volume_by_symbol.keys())
def SelectUniverse(self):
self.universe = []
for symbol, volume in self.volume_by_symbol.items():
if volume > 50000:
self.universe.append(symbol)
self.volume_by_symbol.clear()
self.logged = False
def OnSecuritiesChanged(self, changes):
self.changes = changes
self.Log(f"OnSecuritiesChanged({self.Time}):: {changes}")
for security in self.changes.RemovedSecurities:
if security.Invested:
self.Liquidate(security.Symbol)
for security in self.changes.AddedSecurities:
self.SetHoldings(security.Symbol, 0.18)
self.Debug("BUY")
self.Debug(security.Symbol)
self.Debug(security.Price)
self.Debug(self.Time)
Derek Melchin
Hi Yuliia,
Welcome to QC! This error message is thrown because the algorithm is trading in OnSecuritiesChanged. When we try to place orders for securities that has just been added to the universe, they are "cold", meaning that the data feed didn't update their price it. So the price is zero and that's deemed as invalid by the algorithm order processing logic. To resolve this, we can move the order logic to the SelectUniverse method. See the attached backtest for reference. Note that the universe resolution has been increased to the minute level to allow trading at 10AM.
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.
Yuliia Zubets
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!