I'm wondering if anyone could help me with a CustomFactor that spits out the number of days since the 52 week high (and low)? The below factors identifies the 52 week high and low respectively, but I would also like to know how many days since a stock hit the high and low respectively. Any help or hint on how to do this would be massively appreciated!
The idea of course is to use it as some sort of momentum factor (or contrarian/reversal, but more likely momentum). Having the number of new highs (and lows) in a rolling 52 week period would be great as well, but I'd imagine that would be more difficult to code?
class High252(CustomFactor):
window_length = 252 # ~52 weeks
inputs = [USEquityPricing.close]
def compute(self, today, asset_ids, out, close_prices):
out[:] = np.max(close_prices, axis=0)
class Low252(CustomFactor):
window_length = 252 # ~52 weeks
inputs = [USEquityPricing.close]
def compute(self, today, asset_ids, out, close_prices):
out[:] = np.min(close_prices, axis=0)