I got following code from the Quantopian Lecture about factor investing. Following is calculating momentum factor. Earlier in the lecture, instructor told that, we will sort the securities based on their factor score and go long on top quantile and short on bottom quantile. I think formula is wrong because if short-term return is greater than long-term return, we will get negative momentum factor score.
What I am missing here?
class MyFactor(CustomFactor):
""" Momentum factor """
inputs = [USEquityPricing.close,
Returns(window_length=126)]
window_length = 252
def compute(self, today, assets, out, prices, returns):
out[:] = ((prices[-21] - prices[-252])/prices[-252] -
(prices[-1] - prices[-21])/prices[-21]) / np.nanstd(returns, axis=0)
```