Back to Community
Custom Factor issue while computing US Equity Pricing weekly

Hi there! I'm having some hard time trying to filter weekly data with US Equity Pricing inside a Custom Factor. I would like to compute MACD for a weekly timeframe. Do you have any clues how to do it. I've researched a little bit between old posts I tried things like this one :
input = [USEquityPricing.close.downsample('week_start')] but that throw me some errors.

class GetMacDHistSlope(CustomFactor):  
    #inputs = [USEP.close]  
    inputs = [USEP.close.latest.downsample('week_start')]  
    window_length = 50  
    def compute(self, today, assets, out, close):  
        hists = []  
        for stock_close in close.T:  
            try:  
                #Compute only end of week equity prices!!  
                macd, signal, hist = talib.MACD(stock_close, fastperiod=12,  
                                                slowperiod=26, signalperiod=9)  
                if (hist[-3] > hist[-2]) and (hist[-2] > hist[-1]):  
                    trend = -1  
                elif (hist[-3] < hist[-2]) and (hist[-2] < hist[-1]):  
                    trend = 1  
                else:  
                    trend = 0  
                hists.append(trend)  
            except:  
                hists.append(np.nan)  
        out[:] = hists

NonWindowSafeInput: Can't compute windowed expression GetMacDHistSlope([DownsampledFactor(...)], 50) with windowed input DownsampledFactor([EquityPricing<US>.close], 1).  
Loading notebook preview...
7 responses

This is what I've tried:

...
class GetMacDHistSlope(CustomFactor):  
    #inputs = [USEP.close]  
    inputs = [USEP.close.latest.downsample('week_start')]  
    window_length = 50  
    def compute(self, today, assets, out, close):

...

Hi Augusto,

What are you trying to create, exactly?

Do you want the moving average of N days, computed once a week, or a moving average of weekly closes? We support the first, but not the second.

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

Hello Augusto,

Have you tried to resample daily data using numpy :

class myCustomFactor(CustomFactor):  
    inputs = [USEP.close]  
    window_length = 50  
    def compute(self, today, assets, out, close):  
        weekly_close = close[::5,:]  
        out[:] = weekly_close  

Thanks for your replies Cal and Mathieu, I supose I'll have to use the suggested workaround suggested by Mathieu... my idea was to use weekly closes which of course is not the same as slicing the array by 5.

I hardly appreciate your support guys.

A good solution would be to make downsampling possible with window_safe=True in the source code.
I honestly can't see why this isn't done because it seems to me there is no extra risk (the data is downsampled) and it would help a lot.

Generally, price and volume data is NOT 'window_safe'. The following factor will change depending upon splits and dividends and therefore cannot be used as an input to other factors.

USEquityPricing.close.latest.downsample('week_start')  

One can easily make a custom factor to set window_safe = True like this

class Make_Window_Safe(CustomFactor):  
    window_safe = True 

    def compute(self, today, assets, out, input_factor):  
        out[:] = input_factor

# A factor can be set to 'window_safe' and then used as an input to another factor like this  
my_window_safe_factor = Make_Window_Safe(inputs=[my_factor])  
sma_my_factor = SimpleMovingAverage(inputs=[my_window_safe_factor], window_length=20)

However, BE CAREFUL. As noted, most price and volume factors (unless they are a ratio) are NOT window_safe.

There is a similar response towards the end of this post https://www.quantopian.com/posts/feature-requests-what-changes-would-you-like-to-see

Disclaimer

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 Quantopian. In addition, the material offers no opinion with respect to the suitability of any security or specific investment. No information contained herein should be regarded as a suggestion to engage in or refrain from any investment-related course of action as none of Quantopian nor any of its affiliates is undertaking to provide investment advice, act as an adviser to any plan or entity subject to the Employee Retirement Income Security Act of 1974, as amended, individual retirement account or individual retirement annuity, or give advice in a fiduciary capacity with respect to the materials presented herein. If you are an individual retirement or other investor, contact your financial advisor or other fiduciary unrelated to Quantopian about whether any given investment idea, strategy, product or service described herein may be appropriate for your circumstances. All investments involve risk, including loss of principal. Quantopian 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.

This is indeed a solution. However I don't see a work around for:
close_weekly = USEquityPricing.close.latest.downsample('week_start')
I would like to process the weekly prices as window safe but your solution doesn't work for this case.

By the way: in my opinion the whole window safe design solution is a rather unfortunate.
In my opinion security data should always be based of the latest stock price and then compensated for splits, dividends, etc. This is true for closing prices in Quantopian but not for data which is derrived from it. Which in my eyes seems strange.
But I don't know what the background of the chosen design philosophy is.