Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
can lamda be used to look back?

Hi,
Lets say i wanted to build a system that will buy when a security is trading above an rsi(14) value of 70 for 5 consecutive days and sell 7 days later. How can i do that?

It looks like the handle_data method only processes data for the current period and doesn't allow any looking back at previous values. Is this correct?
if so, then is the only way to maintain a counter of the number of consecutive days the security is above an rsi value of 70? Or is there a way to use lamda expressions to look back and check if the last 5 values are > X?

6 responses

Hi Mahendra, you could keep a counter that you increment each time the rsi(14) value is > 70, and you reset to 0 when the rsi(14) value is <= 70. Then, once the counter hits 5 or some other threshold, you buy.

There is probably a way to use a batch transform to keep a window of the last N rsi(14) values. To read up on batch transforms, check out https://www.quantopian.com/help#ide-batch-transforms.

hope that helps!

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 Mahendra, I agree with Jean that a batch_transform could do this. However, a deque might be even simpler, e.g.:

from collections import deque

# init  
context.past_rsis = deque(max_len=5)

# in handle_data()  
context.past_rsis.append(rsi > 70)  
if numpy.all(context.past_rsis):  
    # do stuff  

I didn't try this so it might not work out of the box but it should give an idea.

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.

Thomas, one addition that is required to your code is to ensure that in addition to checking numpy.all, you also check that the algo has been running for at least five trading days, because otherwise it'll trigger after less than 5 days of rsi > 70 at the start of the backtest.

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.

Here is a mashup of the TA-Lib code example and everyone's ideas in this thread.

Thank you for your suggestions and advice. the current implementation does use a counter, but i would like to try the batch transform. Dennis, in your code, there is one thing i am struggling to understand. If you are appending to past_rsis, then doesn't that mean that line 45 will at most only yield true once. In other words, assuming there are initially 5 elements in past_rsis once initialize is called, then when handle_data is called a new element is appended, there will now be 6 elements, meaning line 45 will never return true on subsequent calls. Or is it that initialize is also called for each period, and thus resets past_rsis for each period? if that is true, it sounds like a counter might be more efficient in this case.

@Mahendra: That's the beauty of the maxlen argument passed to deque -- it causes the window length to remain constant. As you append new events, old ones will get dropped once you hit the maxlen.