Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
How Can I Tell If It's The Last Period Of A Backtest?

I would like to print sime summary stats at the end of a backtest. Options:

(i) Print each time in handle-data? No. Not possible due to logging limits.

(ii) Is the last period available programmatically? No. Quantopian, please add context.portfolio.end_date

(iii) Call a function after handle_data completes? No.Quantopian, please add def deinitialize(context)

(iv) Define the length of the backtest in periods? Yes. Run a 5 year backtest for 1260 days. Messy.

Any better ideas, please?

P.

6 responses

Hello Peter,

It sounds like you are looking for an elegant approach, but if a hack will suffice, you could run the backtest once and count the number of periods (e.g. context.period_count). Perhaps this is your (iv) above. Then, you could add a statement like:

if context.period_count == last_period:  
    print summary  

You could even add a warning, if context.period_count exceeds last_period ("Warning: context.period_count > last_period"). As you say, kinda messy.

Have you considered using the security_end_date detailed on the help page? If you'll always backtest through the latest possible date, then this might work for you.

Alternatively, I wonder if the information you need might be available in zipline? As I learned for get_early_closes, undocumented functionality can be imported from zipline.

Grant

Hello Grant,

Thank you - I hadn't picked up on security_end_date as an option. I've just tried

    for stock in  data:  
        print stock.end_date  

in an algo. The output is

2008-01-02PRINT2013-09-09 05:00:00+00:00  

which is interesting since it is now 14:48 BST on the 7th September here in the UK!

I really need to look at zipline but I don't have any data of my own to use. As you say there will be features that can be imported - I saw someone using 'blotter' which looked useful.

P.

Hmm...the output you show above might be indicative of a bug. No time now, but I'll have a look when I get the chance. --Grant

I agree with the original post.
You have initialize() and handle_data(), please add last() or something like that to run at the end of a backtest (make the function name short for logging).

I want to log cash, portfolio value etc, a summary at the end of a backtest.

If you the reader would find something like last() or summary() useful, please chime in so they'll know it can be worth their time.

def handle_data(context, data):  
   [...]  
    # Rough workaround to print a summary at the end.  
    #    Must fill in the last date here manually.  
    last_date     = '2014-04-23'  
    exchange_time = pd.Timestamp(get_datetime()).tz_convert('US/Eastern')  
    if str(exchange_time.date()) == last_date:  
        stock_value = price * context.portfolio.positions[context.sec].amount  
        all_total   = context.books['cash'] + stock_value  
        # Overall value compared to input limit  
        x_value     = 'x' + str(all_total / context.initial_cash)  
        log.info(' _END_ ' + str(context.sec.symbol) + \  
           ' csh ' + str(int(context.books['cash'])) + \  
           ' ptflo ' + str(int(context.portfolio.portfolio_value)) + \  
           ' total ' + str(all_total) + '   ' + x_value  
        )  

i agree that some soft of "last" function would be helpful

Code in https://www.quantopian.com/posts/elapsed handles that and more.