Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Pipeline custom factor not working correctly

Hello,

I have a problem with applying custom factor which should select stocks with minimum 5% of mean intraday price range over some period. Below part of code:

class IntraDayMeanDifference(CustomFactor):  
    inputs = [USEquityPricing.high, USEquityPricing.low, USEquityPricing.close]  
    window_length = 20  
    def compute(self, today, asset_ids, out, high, low, close):  
        diff = (high - low)/close*100  
        diff = np.sort(diff) #sort results  
        diff = diff[4:-2] #filter some border values  
        out[:] = np.mean(diff, axis=0)  

The rest please find in attached backtest.
The problem is that based on my custom factor, pipeline returns stocks like Google or Paypal, which we know that are not so volatile.

2016-07-05  PRINT       dollar_volume   high_low_diff  latest_close  
Equity(45971 [AAL])    3.535842e+08       5.198106       29.33  
Equity(45993 [HLT])    2.024517e+08       5.217377         22.88  
Equity(46631 [GOOG])   8.382134e+08       5.683298        699.28  
Equity(46979 [JD])     2.828034e+08       6.038528         21.30  
Equity(47415 [SYF])    2.765042e+08       6.650303         25.33  
Equity(47430 [MBLY])   2.106279e+08       6.687589         47.12  
Equity(47740 [BABA])   6.974509e+08       7.033304         79.67  
Equity(49073 [LABU])   1.510457e+08      11.622539         30.88  
Equity(49229 [KHC])    2.018926e+08      15.681655         88.22  
Equity(49242 [PYPL])   3.059107e+08      16.513557         36.35  

On the other hand, research platform gives rational results when calculating this intraday price range.
Any help/explanation will be appreciated, thanks.

1 response

Hi Andrzej,

According to the documentation here, np.sort automatically sorts along the last axis; in this case this causes all the highest price range values to be sorted to the rightmost columns, which have the highest associated SIDs. So that's why all the high SIDs, including GOOG and PYPL, are showing such high values.

To sort along the correct axis, specify np.sort(diff, axis=0). This will sort them over time as desired, keeping all the data associated with the correct assets.

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.