I'm trying to impliment this part of a Quantopian algorithm:
returns_overall = Returns(window_length=136)
returns_recent = Returns(window_length=10)
momentum = returns_overall - returns_recent
And then sort my universe selction by momentum in fine selection.
Is there a simple way to do this in QuantConnect?
I found a momentum indicator that works, but I can't figure out how to subtract a mom(126) from a mom(10) and then sort based on that number...
averages = dict()
history = algorithm.History(symbols, 200, Resolution.Daily).close.unstack(0)
for symbol in symbols:
# Remove NaN: symbol does not have 200 daily data points
df = history[symbol].dropna()
if df.empty:
continue
mom = Momentum(136)
for time, close in df.iteritems():
mom.Update(time, close)
# Adds Momentum to dict only if it is ready
if mom.IsReady:
averages[symbol] = mom
# Update with current data
for symbol, mom in averages.items():
c = self.coarse.pop(symbol, None)
mom.Update(c.EndTime, c.AdjustedPrice)
sortedbyMomentum = sorted(averages.items(), key=lambda x: x[1], reverse=True)
Anyone have an idea? Thanks!
Rahul Chowdhury
Hey Nathan,
In our universe selection, we can make a history call for our symbols to create our momentum indicators. For the sake of example, we choose the top 1000 liquid stocks, but there are over 8000 stocks. We then store our momentum indicators in SymbolData objects. This way, instead of making a new history call each iteration for every symbol, we can simply update our previously calculated momentum indicators with new coarse data. Once we have our indicators for our symbols, we sort by the value of the difference of Momentum(10) - Momentum(136), and return the top 200 trending symbols.
Â
Anthony FJ Garner
Very helpful.Â
But course/fine selection is called periodically? How often is it called? Won't history be called each time?Â
Anthony FJ Garner
It seems to me that something like this ought also be incorporated in the alpha models found here.
The result of not calling history (somewhere) is that if the course/fine selection changes fairly regularly an awful lost of signals are going to be missed. Because without this each symbol which gets sent through to the next stage has to wait for its indicator to become ready.
it does not make sense not to standardise this in some way.
Jared Broad
 - Coarse-Fine is called daily at roughly 7am,
 - It's true if your indicators are not up to date they'll not generate signals, we have an automatic warm-up system but its a breaking change so it has not been enabled by default yet.Â
self.EnableAutomaticIndicatorWarmUp = TrueÂ
** Also we have not implemented this for all indicators yet.Â
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.
Nathan
Thank you Rahul, that is very helpful. Still trying to figure out how to make things work here after having learned a bit of Quantopian. Â Appreciate your help!
Nathan
Also, Rahul,
I needed to use the momentum filture in the Fine selection, is it alright to change AdjustedPrice to just Price since the Fine selector doesn't have access to AdjustedPrice (or at least I got an error saying that...)?
Â
# Now, we update the dictionary with the latest data for x in fine: symbol = x.Symbol if symbol in self.symboldict: self.symboldict[symbol].Update(x.EndTime, x.Price)
Â
Jack Simonson
Hi Nathan,
The Price property in Fine selection is the raw price, not the adjusted price. However, the raw price is good to use in Fine as this can also provide an accurate view fo the movement of a stock's price. If you want the adjusted price, you could make a History call and get the adjusted close price from the previous day if you want to use that instead.Â
Daniel Rosenwald
Hey Rahul,
Your example was really helpful for doing this indicator-style universe selection. I'm wondering how you would use it within the Algorithm Framework model? For example, how would you encapsulate that code into its own UniverseSelectionModel, and where would symboldict live? And I'm curious why you choose not to remove the dictionary items in symboldict for symbols that are no longer in the universe. Is that taking up a lot of extra RAM? Or just a little bit because the indicators don't actually store the whole history they're passed? Still wrapping my head around this stuff and it helps to learn from more experienced coders / traders.Â
Thanks,
Daniel
Derek Melchin
Hi Daniel,
To relocate the code into a universe selection model, we adjust the AddUniverse call to read
self.AddUniverseSelection(DeltaMomentumUniverseSelectionModel())
We then create the DeltaMomentumUniverseSelectionModel class, which is a subclass of FundamentalUniverseSelectionModel, and define its SelectCoarse method. See the attached backtest for reference.
`symboldict` grows without bounds in this example to ensure we only need to make 1 history call for each security that ever makes it into the `topDollarVolume` list. This practice doesn't consume much space, but we could run into issues if the length of `topDollarVolume` is increased significantly.
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.
Daniel Rosenwald
Hi Derek,
I appreciate your response! This all makes sense. From what you wrote, after the initial history call and warm up of the indicators inside SymbolData, it sounds like they're supposed to automatically update from subscribed data. Is that correct? If so, would all indicators automatically update in the same way if created in a universe selection model like this? And what if they're updated with different resolution data than the universe selection subscribes their symbols to? Would they automatically consolidate, or would you need to set those consoidators up?
Thanks,
Daniel
Derek Melchin
Hi Daniel,
The indicator are updated with each call to SelectCoarse. The logic below that's inside SelectCoarse ensures each symbol that makes it into the `symboldict` is updated each day.
for x in coarse: symbol = x.Symbol if symbol in self.symboldict: self.symboldict[symbol].Update(x.EndTime, x.AdjustedPrice)
> And what if they're updated with different resolution data than the universe selection subscribes their symbols to? Would they automatically consolidate, or would you need to set those consoidators up?
Consolidators only work for securities that the algorithm has an active subscription for. This contrasts the logic above as the code above ensures all of the securities in `symboldict` are updated each day. If we wanted to update the indicators with a lower resolution, we would need to manually create our own consolidator. If we wanted to update the indicators with a higher resolution, we would need to increase the universe resolution and setup a consolidator in the SymbolData class. Refer to the docs here for an example.
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.
Nathan
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!