Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
60 Day Best Performer (with History Function)

I would like to sort through a list of securities and find the one that has performed the best in the last 60 days. My intent is to then buy that best performing security after selling the previous best performing security. (I believe this strategy/method is called "paired switching.") The attached algo does this, but with the outdated batch_transform method. I was wondering if anyone knew how to use the history function achieve duplicate results (exact same functionality) and possibly clean up the code. Thanks and sorry if my English is not perfect.

6 responses

Sorry I forget to attach the backtest. It is now there for viewing.

This looks interesting. I'd like to see sample code for this as well.

Made some changes:

A) converted to 'history' instead of batch transform
B) You can now set how many stocks to buy (top 1, top 2, etc)
C) Cleaned a bunch of redundant code
D) Utilizing the pandas period object. pandas.Period(date,freq='D') is a 'Day'. There are also settings for minute, second, business day, month, year etc. These are better because they handle date addition/subtraction nicely as well as a bunch of other things.
E) Changed to using order_target_percent. This saves you calculating amounts/position sizes. eg order_target_percent(sid(23921),1.0) will order 100% of your portfolio of that stock. 0.0 will sell all, .5 will buy half your portfolio worth of it.
F) changed to get_open_orders to save remembering oids

NOTE: You are using calendar days for re balance period, but trading days for lookback period. This is confusing, but I have left unchanged to achieve same functionality as yours. Change the 'D' to a 'B' on 53 to make the both trading days.

Double check it is all working as intended .

Thanks a million Jeremy! Much appreciated! I am wondering, however, why the algos are performing differently. At the beginning of the original algo there is no trading but in your updated version there is. Also, towards the end, the updated version performs worse. Any ideas?

The way that the accumulating data method works is that it starts accumulating data from when the backtest starts. eg You don't have enough data in the lookback period until the simulation period has been running for the lookback period.

History get historical prices from before the the simulation starts.

As for differences in returns, I would have to investigate further, but I have found with a strategy like this even small changes in lookback period, rebalance period and starting date greatly effect returns. This means people end up 'cherry picking' the best of these numbers that works.

You can test yourself, changing the start date, lookback period and rebalance period and see how drastically it effects performance.

To help remove these problems you should really create in-sample out-of-sample testing.

eg create the algo off 2002-2006 data. Run it from 2006-2010 once the algo is formed. See if the first matches to the second and you can work out the reliability of the strategy.

Ah okay that makes sense. It's too bad that the strategy is so sensitive. Thanks for the advice I'll work on it.