Back to Community
How to get last week's data

Hi, guys,

I just started playing around 2 days ago and have a very very easy question: How do I get last week's data after the stock change is closed at 4 pm Friday?

Take the sample algo as an example:

Today is 2016-05-21, is there a way for me to get the best-performanced-stock/worst-performanced-stock from last week, which is from 2016-05-16 to 2016-05-20. From the sample code it only gives us the data from 2016-05-16 to 2016-05-29.

Can anyone help and post some samples?


6 responses

Anyone Help?

Hi Dili, what's the sample algo you're talking about?


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's an example of how to get a top and bottom stock from the past week using Pipeline. Obviously there are a lot of ways to evaluate which stock has the best or worst performance, but here we use the built-in Returns factor, looking back 5 days. Then we construct filters for the top and bottom stock according to that factor. At the beginning of each week we look at the pipeline output and filter down to lists containing just the top and bottom stock.

Clone Algorithm
Backtest from to with initial capital
Total Returns
Max Drawdown
Benchmark Returns
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
from quantopian.algorithm import attach_pipeline, pipeline_output
from quantopian.pipeline import Pipeline
from quantopian.pipeline.factors import Returns
def initialize(context):
    schedule_function(my_rebalance, date_rules.week_start(), time_rules.market_open())
    # Create our dynamic stock selector.
    attach_pipeline(my_pipeline(context), 'my_pipeline')
def my_pipeline(context):
    A function to create our dynamic stock selector (pipeline). Documentation on
    pipeline can be found here:
    pipe = Pipeline()
    returns = Returns(window_length = 5)
    top_returns =
    bottom_returns = returns.bottom(1)
    pipe.add(top_returns, 'Top Returns')
    pipe.add(bottom_returns, 'Bottom Returns')
    return pipe

def before_trading_start(context, data):
    context.output = pipeline_output('my_pipeline')
    context.top_returns_list = context.output[context.output['Top Returns']].index
    context.bottom_returns_list = context.output[context.output['Bottom Returns']].index

def my_rebalance(context, data):
    print("Top: %s" % context.top_returns_list)
    print("Bottom: %s" % context.bottom_returns_list)
There was a runtime error.

Hi, Nathan,

Thanks for the response!

The question I have is: I want dynamically select stocks that I want to rebalance by the close of market on each Friday (05/20). It could work on the back-test, but if I want to use it for me to analyze before the starting of current Monday (05/23), it looks like the historical data doesn't include the last Friday's data either because this kind of data is not added or because the pipeline is only executed by the beginning of each day and Monday(05/23) is currently not available yet.

Therefore, could you point me if there is a way to make the pipeline executed after the close of Friday so that I can test on Monday with the real data?

Thank you so much!

Hi Dili,

Unfortunately, pipelines are run only at the beginning of each trading day. But the only reason having the output right after close on Friday would be any different from having it right before open on Monday is that you want to look at it yourself. The algorithm itself shouldn't care what time during the weekend it has the output.

If you really need the data at the end of Friday, I would suggest that you run data.current or data.history at that time to get the data for that day, and then combine what you get for Friday with the pipeline output for the previous part of the week.

Thanks, man! That should work!