Back to Community
looking for help with Pipeline computation

Hi Quantopians,
how can I compute a filter in Pipeline that would return only stocks which where X days goingup in a row.
I realize that I can loop through the last days to check if close price is higher than the open price but I was wondering if there is a way to create this filter in the Make Pipeline function

Thanks for the input.

10 responses

does nobody know this?

Yes, you can do this with Pipeline.

Typically, the more work your bring to a question in the community, the more likely you are to get a response. If you look at the last few days, the questions with a lot of code and a question about a problem get a good answer, but questions that are more general (and less prep work) get fewer replies. In short: people like to help people who help themselves.

Have you gone through the pipeline tutorial? I think what you want to do is create a custom factor that reports back the number of positive days in a row.

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.

Hi Dan
yes I went through the tutorial.
I created two Custom Factors one should return True when the close 5 days ago is higher than the close yesterday
And another Custom Factor which should return True if yesterdays Close was higher than yesterdays open.

class downWeek(CustomFactor):
inputs = [USEquityPricing.close]
window_length = 10
def compute(self, today, assets, out, close):
out[:] = close[1] < close[5]

class yesterdayStrong(CustomFactor):
inputs = [USEquityPricing.close, USEquityPricing.open]
window_length = 10
def compute(self, today, assets, out, close, open):
out[:] = close[1] > open[1]

When creating the Pipeline I created a Filter like:
filter_down = downOnWeek == 1.0
filter_yesterdayUp = yesterdayUp == 1.0

However I am not getting any result back.
Any Idea?

For boolean comparisons (like <, >, and ==), it's best to use a filter instead of a factor. Attached is a notebook with a Pipeline that does what I think you want. I also added in a universe screen using the QTradableStocksUs.

Loading notebook preview...
Notebook previews are currently unavailable.
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.

Hi Jamie,
please have a look on my Notebook.
For some reason I dont get any results.
I like to filter for last close greater than EMA50, AverageVolume of the last 13 periods greater than 750k
And the two CustomFactors must be True close[1] < close[5] and close[1] > open[1]

PS. I didnt know I can share my Notebook here thats super cool :)

Loading notebook preview...
Notebook previews are currently unavailable.

The > and < comparisons define functions that result in booleans, so you should use a CustomFilter instead of a CustomFactor. I attached a modified version of your notebook that does this.

Loading notebook preview...
Notebook previews are currently unavailable.

Why I cannot use the CustomFactor and then Filter for Value == True in the Filter?

I wanted to add more conditions that include Volume and such into this CustomFactor as well.
So every time I have a Boolean returned I must use a CustomFilter otherwise it wont work?
Thanks for the help

Filters have a boolean datatype, so if you define something as a CustomFactor, it will return a float. You can add more conditions to a CustomFilter as well, but if the final result is a True/False output, you should keep it a CustomFilter. You should be able to treat the inputs and the computation the same between CustomFactors and CustomFilters in your work.

Is there function or easier way to filter for stocks that are down every single day for the last X days
right now I am doing something like this:

out[:] = close[5] < open[5] and close[4] < open[4] and close[3] < open[3] and close[2] < open[2] and close[1] < open[1]  

Can someone describe why 'and' would work there? I've always thought with > and < that those would need to be & between them. If that makes sense. Also I thought parens are always needed for those comparisons to avoid error.

Misc: I think this might be a good place to mention in passing a caution, that the following type of thing, while it doesn't produce an error, at certain places in pipeline would only process one part of it:

(close[4] > close[3] > close[2])

... would have to be separated out:

(close[4] > close[3] ) & (close[3] > close[2])

... or something to that effect. I discovered it the hard way with the gold king & his knights algo in what now seems like ages ago and just thought it worth mentioning.

I'm thinking you might use a 'for' loop. This factor with a loop that I found seems a bit much and just thinking out loud.

for i in range(self.window_length):  
    if close[i] > close[i+1]
class MACD(CustomFactor):  
    inputs = [USEquityPricing.close]  
    window_length = 60  
    def ema(self, data, window):      # Initial value for EMA is taken as trialing SMA  
        import numpy as np  
        c = 2.0 / (window + 1)  
        ema = np.mean(data[-(2*window)+1:-window+1], axis=0)  
        for value in data[-window+1:]:  
            ema = (c * value) + ((1 - c) * ema)  
        return ema  
    def compute(self, today, assets, out, close):  
        close = nanfill(close)  
        fema = self.ema(close, 12)  
        sema = self.ema(close, 26)  
        macd_line = fema - sema  
        macd = []  
        macd.insert(0, self.ema(close,12) - self.ema(close,26))  
        for i in range(1,15, 1):  
            macd.insert(0, self.ema(close[:-i],12) - self.ema(close[:-i],26))  
        signal = self.ema(macd,9)  
        out[:] = macd_line - signal  

I did not answer his question.