Hi QCers,
I'm on the brink of giving up trying to implement a coarse selection to my algorithm. No matter what variation of any algorithm published online I always get an empty list of symbols.
I think I have stripped the code down to the minimal example that should work, yet nothing gets printed out in my terminal.
Here is the code I'm running:
from AlgorithmImports import *
class CoarseFundamentalAlgorithm(QCAlgorithm):
def Initialize(self):
self.UniverseSettings.Resolution = Resolution.Daily
self.AddUniverse(self.CoarseSelectionFunction)
def CoarseSelectionFunction(self, coarse: List[CoarseFundamental]) -> List[Symbol]:
sorted_by_dollar_volume = sorted(coarse, key=lambda x: x.DollarVolume, reverse=True)[:20]
for cf in sorted_by_dollar_volume:
self.Debug(f"{cf.EndTime} :: {cf.Symbol} : {cf.AdjustedPrice} :: {cf.DollarVolume}")
return [ x.Symbol for x in sortedByDollarVolume]
def OnData(self, data):
pass
I am testing this live on a paper IB account using LEAN locally. Is there anything that I need to subscribe to for the coarse data? I am very confused.
Would appreciate any tips on what the heck is wrong!
Thanks!
Mia Alissi
Hi Stefan,
From the code you've shared, it seems like there's a small typo in your
CoarseSelectionFunction
. You've sorted the coarse data and stored it in the variablesorted_by_dollar_volume
, but when you're returning the symbols, you're referencingsortedByDollarVolume
which is not defined. Python is case-sensitive, so these are considered two different variables.Here's the corrected code:
This should return the top 20 symbols sorted by dollar volume. If you're still not seeing any output, it might be worth checking if you're receiving any data in the
coarse
object. You can do this by adding a debug statement at the start of yourCoarseSelectionFunction
like so:This will print out the number of
CoarseFundamental
objects you're receiving each time the function is called. If this number is consistently zero, it might indicate an issue with your data subscription.I hope this helps! Let me know if you have any other questions.
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.
Stefan Petkov
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!