Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How do I get an absolute value in a pipeline?

I am trying to get an absolute value of the difference in 2 moving averages. You can kind of see what I am trying to do here below.

def make_pipeline():  
    mean_close_10 = SimpleMovingAverage(  
        inputs=[USEquityPricing.close],  
        window_length=10  
    )  
    mean_close_20 = SimpleMovingAverage(  
        inputs=[USEquityPricing.close],  
        window_length=20  
    )  
    latest_close = USEquityPricing.close.latest  
    vwap = VWAP(window_length=10)  
    mean_diff = abs(mean_close_20 - mean_close_10)  
    return Pipeline(  
        columns={  
            '10_day_mean_close': mean_close_10,  
            'VWAP 10': vwap,  
            '20_day_mean_close': mean_close_20,  
            'Mean Diff': mean_diff,  
            'latest_close_price': latest_close  
    }

)

This produces an error of:

<ipython-input-141-93b21dc74260> in make_pipeline()  
---> 15     mean_diff = abs(mean_close_20 - mean_close_10)

TypeError: bad operand type for abs(): 'NumExprFactor'  

I am guessing I am trying to get an absolute value for an array or something with a function meant for an individual number but that said I am not sure how I am supposed to get the result (the number displayed as an absolute value)

6 responses

Hi Joseph -

The simplest thing to do would be to take the absolute value of mean_diff of the output of the Pipeline (which is a Pandas series). The other approach would be to write a Pipeline Custom Factor that does the entire mean_diff computation within the factor. There, you could use numpy.absolute.

Thank you for the answer. Being a bit of a rookie with ipython I am getting hung up on the syntax.

I'm not sure where to place the line of code to calculate the absolute value from the pipeline or how it should be formed. I tried wrapping the column result in abs but got the same error.

Hi Joseph,

Your example was very close. I just changed it a bit to use the Factor.abs() function. See the attached notebook.

General Tip: If you're looking for code help in the forums, it's best to attach a notebook or backtest to your post. It makes it easier for someone to clone the code artifact.

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.

The approach that @Jamie M provided is elegant. There are a small number of built in methods which can be applied to factors. You got lucky with absolute value as one of them. Take a look at this post https://www.quantopian.com/posts/log-normal-return-built-in-factor . The notebook has a complete list of methods one can apply to factors. The 'log1p' method is one I use a lot for log returns. These aren't listed in the documentation btw.

Awesome guys, thank you for all the help and tips

Hi Jamie -

I did a search on the help page on "abs" and did not find the one you used.

I'd recommend putting a few of the more useful ones on the help page, if not all of them:

    Factor.abs,  
    Factor.arccos,  
    Factor.arccosh,  
    Factor.arcsin,  
    Factor.arcsinh,  
    Factor.arctan,  
    Factor.arctanh,  
    Factor.cos,  
    Factor.cosh,  
    Factor.exp,  
    Factor.expm1,  
    Factor.log,  
    Factor.log10,  
    Factor.log1p,  
    Factor.sin,  
    Factor.sinh,  
    Factor.sqrt,  
    Factor.tan,  
    Factor.tanh,