Back

Universe selection sorted by returns?

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!

Update Backtest







0

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.


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.

 

0

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.


Very helpful. 

But course/fine selection is called periodically? How often is it called? Won't history be called each time? 

0

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.

0

 - 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. 

0

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.


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!

0

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)

 

0

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. 

0

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.


Update Backtest





0

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.


Loading...

This discussion is closed