Hi,
I want to know if it's possible to check for example at 10:00 the most traded stocks based on Volume of stocks traded. For what I understand this code is not doing that, because it checks for "yesterday" volume, not the volume up to 10:00 of the same day:
def MyCoarseFilterFunction(self, coarse):
sortedByVolume = sorted(coarse, key=lambda x: x.Volume, reverse=True)
filtered = [ x.Symbol for x in sortedByVolume if x.Price > 10 and x.Volume > 50000 ]
return filtered[:100]
What I want is to check for universe of stocks that have more than 50.000 volume traded (not dollars, number of stocks), at 10:00.
I can acomplish this only by setting up "MyCoarseFilterFunction" inside a scheluded function?
Appreciate someone can point me in the right direction.
Derek Melchin
Hi Cristian,
It is not possible to subscribe to security datafeeds through cumulative intraday volume filtering inside a universe selection method. However, we can create a workaround to determine which of the securities in our universe pass the intraday volume requirements by add a universe and a scheduled event to our algorithm.
# Initialize self.AddUniverseSelection(CoarseFundamentalUniverseSelectionModel(self.CoarseSelectionFunction)) self.Schedule.On( self.DateRules.EveryDay("SPY"), self.TimeRules.At(10, 0), self.SelectUniverse ) self.universe = [] self.volume_by_symbol = {}
In the CoarseSelectionFunction, we filter the stocks by the price threshold.
def CoarseSelectionFunction(self, coarse): self.volume_by_symbol = {c.Symbol: 0 for c in coarse if c.Price > 400} return list(self.volume_by_symbol.keys())
We then track the cumulative volume for all the symbols in the universe from the open until 10AM.
def OnData(self, data): if len(self.volume_by_symbol) == 0: # universe is ready. Access with self.universe return for symbol in self.volume_by_symbol.keys(): if data.ContainsKey(symbol): self.volume_by_symbol[symbol] += data[symbol].Volume
Lastly, we define the SelectUniverse method to filter by the intraday volume seen thus far.
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()
See the attached backtest for reference. Also, consider reviewing our documentation on scheduled events.
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.
Derek Melchin
Hi Cristian,
In regards to the first question, the only limit imposed on the number of securities an algorithm's universe can have is from the amount of available RAM. If needed, the backtesting node on an account can be upgraded to increase the RAM. See the specs of the available nodes in our documentation.
To resolve the error the backtest throws when setting `c.Price > 300`, we need to add
and data[symbol] is not None
to the condition on line 32. See the attached backtest for reference.
For the second post above, setting the argument passed to the universe method to 3000 returns a larger universe. We just need to ensure we build the project after making changes.
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.
Brian tong
Many thanks for the code illustration Derek. That is very helpful.
I tried to clone the strategy and change only the date range, from the current 2020-1-21 to 2020-1-24 to 2022-12-5 to 2022-12-8. I hit a NonType error. Any idea why does that happen?
Derek Melchin
Hi Brian,
To fix the issue, we just need to replace
with
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.
Cristian correa
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!