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.

Clone Algorithm
18
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 52244f6a0b730706cb729325
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

This uses a batch transform.

P.

Clone Algorithm
4
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5224525a654e5a074353d9ec
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

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

Clone Algorithm
4
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 522473980b730706cb742311
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

Hello Grant,

As elegant as any other so far!

P.

Clone Algorithm
1
Loading...
Backtest from to with initial capital
Total Returns
--
Alpha
--
Beta
--
Sharpe
--
Sortino
--
Max Drawdown
--
Benchmark Returns
--
Volatility
--
Returns 1 Month 3 Month 6 Month 12 Month
Alpha 1 Month 3 Month 6 Month 12 Month
Beta 1 Month 3 Month 6 Month 12 Month
Sharpe 1 Month 3 Month 6 Month 12 Month
Sortino 1 Month 3 Month 6 Month 12 Month
Volatility 1 Month 3 Month 6 Month 12 Month
Max Drawdown 1 Month 3 Month 6 Month 12 Month
# Backtest ID: 5224dbfd722e8d06d25abe33
This backtest was created using an older version of the backtester. Please re-run this backtest to see results using the latest backtester. Learn more about the recent changes.
There was a runtime error.

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.