Back to Community
Creating a Custom Factor with multiple inputs

Hi everyone,

I'm currently looking into a relationship between short-term momentum and high turnover. I have developed my momentum custom factor and attempting to develop my turnover factor as a screen but, I'm currently having issues with forming a proper output.

Here is what i got:

from quantopian.pipeline import Pipeline, CustomFactor  
from import Fundamentals  
from import USEquityPricing  
from quantopian.pipeline.factors import Returns  
from quantopian.research import run_pipeline  
from quantopian.pipeline.filters import QTradableStocksUS  
import numpy as np

class ShortMom(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 21 # to examine short-term monthy mom in relation to turnvoer  
    def compute(self,today,assets,out,close):  
        out[:] = (close[self.window_length-1] - close[0])/close[0]  
mtm_factor = ShortMom()

class TO(CustomFactor):  
    inputs = [USEquityPricing.volume, Fundamentals.shares_outstanding]  
    window_length = 21 # monthly turnover is the one we need  
    def compute(self,today,assets_ids, out, volume, shares):  
        out[:] = (volume / shares)  
TO_High = TO()  
TO_High_Filter = (TO_High.percentile_between(75,100, mask=(TO_High > 0)))  

I know I have done something wrong with attempting to calculate the turnover but do not know how to fix it. If anyone can please help, it will be greatly appreciated!

1 response

The issue is with the 'TO' custom factor compute function. The following code

        out[:] = (close[self.window_length-1] - close[0])/close[0] 

creates an array with 21 rows (ie the window_length). The function output expects a singe row. I believe the equation you probably want is

        # Sum the months volume then divide by current  outstanding shares (ie [-1])  
        out[:] = (np.nansum(volume, axis=0) / shares[-1])  

Remember the compute function inputs are numpy 2D arrays. The rows are days (the number of rows will be the window_length), and the columns are the assets. However the output is a single 1D array. One needs to ensure the logic reduces the input arrays to this single 1D array.

See the attached notebook.

Loading notebook preview...

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.