Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
daily returns

Hi I have the following code:

def make_pipeline():
#Step1. get all NYSE stocks
exchange = morningstar.share_class_reference.exchange_id.latest
nyse_filter = exchange.eq('NYS')
#Step 2. Top 100 traded NYSE stocks by dollar volume
dollar_volume = AverageDollarVolume(window_length=30)
high_dollar_volume2 = dollar_volume.top(100)
#Step 3. Create screen so that we can only have those top 100
top_100 = high_dollar_volume2
#step 4. Compute the returns over this period
returns = RateOfChangePercentage(inputs=[USEquityPricing.close], window_length=1, mask=high_dollar_volume2)

return Pipeline(columns={  
        'Stock': high_dollar_volume2,  
        'Daily Returns':returns},screen=top_100)

start = '2016-12-01'
end = '2016-12-31'
result_mean_reversion = run_pipeline(make_pipeline(), start, end)

once executed my daily returns column is showing 0 for all dates. I would just like the daily rate of change something like this:
returns = prices / prices.shift(-1) -1 however with the mask implemented. So why is my daily returns column showing 0 when window length is 1 and why if i was to use my own forumla such as prices/ prices.shift(-1) -1 would there by anyway to implement a mask to this? thank you

1 response

I'm not sure I understood the question exactly but here's my stab at an answer...

I assume that 'RateOfChangePercentage' is a custom factor and you want it to return the 1 day percent change. The calculation should be returns = prices / prices.shift(-1) -1. The input data 'prices' is a numpy array which doesn't have a method for 'shift' (pandas dataframes do but not numpy arrays). One way to 'shift' is simply reference the position you want. In this case the second from the last (ie prices[-2])This can be done like this


class RateOfChangePercentage(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 2

    def compute(self, today, assets, out, prices):  
        out[:] = (prices[-1] / prices[-2]) -1

. However, if that's all you want the custom factor to do, there is the built in factor called 'DailyReturns' (see https://www.quantopian.com/help#built-in-factors) which does this already.

    returns_built_in = DailyReturns(inputs=[USEquityPricing.close], window_length=2, mask=high_dollar_volume2)

I believe the reason you are getting 0 for all dates is that the window length is set to 1. You really want a window length of 2 since the calculation is based upon yesterday AND the day before yesterday data (2 days). Since you didn't show the code for the custom factor 'RateOfChangePercentage' there may be some other problem but I believe that's the issue.

A couple of other things, don't use the morningstar dataset. Use the new Fundamentals dataset instead. See https://www.quantopian.com/posts/faster-fundamental-data . Also, in the future, please attach a notebook or algorithm rather than simply pasting code. It makes it much easier to help and you will get more responses.

See attached notebook for how this works.

Good luck.