Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
stock price 30 days ago?

I'm new to Quantopian. I've read thru the online API reference over and over, and I can't find some very basic stuff.

Example: How do I get the price of a stock 30 days ago? I haven't found how to access anything but the current price and some predefined averages.

7 responses

Hello Franklin,

This is a solution with a queue. I would like to see some others.

P.

This uses a batch transform.

P.

Hello Franklin (and Peter),

Perhaps not the most elegant solution, but you can create your own data accumulator by creating an empty context variable. Then, just append the data to it, as in the attached backtest. Here's the output, illustrating the accumulation of the price:

2008-01-04PRINT[179.93]  
2008-01-07PRINT[179.93, 177.58]  
2008-01-08PRINT[179.93, 177.58, 171.23]  
2008-01-09PRINT[179.93, 177.58, 171.23, 179.5]  
2008-01-10PRINT[179.93, 177.58, 171.23, 179.5, 178.02]  
2008-01-11PRINT[179.93, 177.58, 171.23, 179.5, 178.02, 172.4]  

Grant

Hello Grant,

As elegant as any other so far!

P.

I think using deque is the best way currently. If you supply the maxlen argument you don't have to handle removing events from the window, i.e.

# in initialize  
context.window = deque(maxlen=30)  
...
# append to window  
context.window.append(data)  
# access data from 30 days ago  
context.window[0]  

Having said that, we recently came up with a new design that will make this trivial on Quantopian. Stay tuned!

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,

I haven't tried deques yet. Would you expect a deque to be faster than the batch transform?

Grant

In this particular instance -- yes. deque does not need a continuous area of memory and in this case you just need to hold on to that last value.

batch_transform will be faster in the case where you do need a continuous array.

An easy way to remember this is: if you at any point you turn data into a np.array or np.matrix or pandas.DataFrame, use a batch_transform which is optimized for this case and gives you a panel directly (which you can slice to a dataframe easily and without copying memory), while a list or deque will have to be copied internally which is quite costly. If you don't need an array, deque (or list) will be faster.