Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How to build a customfactor to calculate new high after 30 days low?

Could not figure out how to calculate new high after 30 days low. Please help me to change the commented line.

from quantopian.pipeline import CustomFactor
from numpy import nanmin, nanmax, nanargmin, nanargmax
class MultipleOutputs(CustomFactor):
inputs = [USEquityPricing.close]
outputs = ['highs', 'highs_idx', 'lows','lows_idx','highsAfterL']
window_length = 30

def compute(self, today, assets, out, close):  
    highs = nanmax(close, axis=0)  
    highs_idx = nanargmax(close, axis=0)  
    lows = nanmin(close, axis=0)  
    lows_idx=nanargmin(close, axis=0)  

**#!!! Need help on calculate the high after the 30 days low

highsAfterL = nanmax(close[lows_idx:], axis=0)**

    print(type(highs_idx[0]))  
    out.highs[:] = highs  
    out.highs_idx[:] = highs_idx  
    out.lows[:] = lows  
    out.lows_idx[:] = lows_idx  
    out.highsAfterL[:] = highsAfterL  
1 response

Hello Alvin,
Have a look at an algorithm that uses a stop loss because they keep track of old highs and lows and then update a context.something accordingly. Maybe a similar idea could be used by keeping track of the days that pass by adding a +1 count somehwere. Then after 30 days is up new highs are added to a context.newhighvalue = max(context.thirtydaylow, context.newhighs). I don't know how to code that but I hope I've given you an idea in the logic on how to implement it.