Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Question regarding pipeline and using for loops

Hello,

I am attempting to run the pipeline and return the highest price and lowest price for the period between 10 and 20 days ago. I was trying to nest the def compute function in a for loop to accomplish this, but it has led to a scope issue. Is there an easy fix for to this dilemma, or should I be coding it in an alternative way. I am really just getting started with coding and this is my first experience with python.

Thank You!!!

for x in range (10,20):
span_ b_high = 0
span_b_low = 0

def compute(self, today, assests, out, high, low):  
    prev_high = high[-x-1]  
    curr_high = high[-x]  
    if curr_bar > prev_bar:  
        span_b_high = curr_high  

    prev_low = low[-x-1]  
    curr_low = low[-x]  
    if curr_bar > prev_bar:  
        span_b_low = curr_high  

    out[:] = (span_b_high + span_b_low)/2  
5 responses

I believe I have finally solved the problem.

class Span_B(CustomFactor):

    #Pre-declare inputs and window_length  
    inputs = inputs = [USEquityPricing.high, USEquityPricing.low]  
    window_length = 104  

    # Compute factor value the span_b line  
    def compute(self, today, assests, out, high, low):  
        for i in range (26,35):  
            span_b_high = 0  
            span_b_low = 0  

            if high[-i-1].all()  > high[-i].all():  
               span_b_high = high[-i-1]  
            else:  
               span_b_high = high[-i]  

            if low[-i-1].all()  > low[-i].all():  
                span_b_low = low [-i-1]  
            else:  
                span_b_low = low [-i]  

            out[:] = (span_b_high + span_b_low)/2  

Hi Zach,

The compute() function can be tricky when you are calculating values over the entire window length like you are here. I think a good place to start is by reading this part of the help documentation. Specifically, this description may help: "On each simulation date, compute will be called with the current date, an array of sids, an output array, and an input array for each expression passed as inputs to the CustomFactor constructor."

Using a for loop in compute() is generally a bad idea as it can make your algo run really slowly, and likely has an equivalent function that can make your desired calculation on the entire matrix of values.

In this case, you might be interested in the numpy.nanmax() and numpy.nanmin() functions. There's an example in the link that I send you that uses these to get the maximum and minimum values over the window of your custom factor. Check it out and let me know if it helps or if you have any other questions.

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.

Jamie,

Numpy.nanmax() and numpy.nanmin will work much better than what I was attempting.

With the window_length parameter is there any way that I could call days from 10 to 20 days ago, or is this limited to only calling the last 20 days. In which case I would need to access the array and select days 10-20 and runing numpy.nanmin/max on that selection. Could this be done by removing the top 10 rows from the array? Or will this interfere with compute?

Thank you in advance,
Zach

Hi Zach,

Take a look at the response by Scott Sanderson in this post. His example shows how to get the mean of the close price, lagged by 10 days, which is very similar to what you're asking!

Let me know if this helps.

Jamie,

That was perfect!

Thank you so much,
Zach

class span_a(CustomFactor):

    #Pre-declare inputs and window_length  
    inputs = inputs = [USEquityPricing.high, USEquityPricing.low]  
    window_length = 52  


    def compute(self, today, assests, out, high, low):  
        from numpy import nanmin, nanmax  

        highest_high = nanmax(high[0: 26], axis = 0)  
        lowest_low = nanmin(high[0: 26], axis = 0)  

        out[:] = (highest_high + lowest_low)/2